Skip to content

Instantly share code, notes, and snippets.

@edwinschaap
Created March 13, 2011 11:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edwinschaap/868025 to your computer and use it in GitHub Desktop.
Save edwinschaap/868025 to your computer and use it in GitHub Desktop.
module namespace tweetmap = "tweetmap";
declare function tweetmap:fetch-tweets($keyword as xs:string) as node()* {
let $baseuri := "http://search.twitter.com/search.atom?geocode=52.1583,4.49306,200km&rpp=100&q="
let $uri := concat($baseuri, $keyword)
return doc($uri)//*:feed//*:entry
};
declare function tweetmap:get-geo($placename as xs:string) as xs:string {
let $uri := replace("http://where.yahooapis.com/v1/places.q("XXX");count=1?format=xml&appid=oWI.U034EMJO6CnsA4vFEvJ5oYEKsoD0qLIRfcoEKOLLTqv9bh6jBHZn9GhdRA--","XXX",$placename),
$centroid := exactly-one(doc($uri)//*:centroid)
return concat($centroid//*:latitude, ",", $centroid//*:longitude)
};
declare function tweetmap:safe-geo($placename as xs:string) as xs:string { (: protect Yahoo against strange locations :)
let $safename := translate($placename,";:=+&amp;{}[]<>!@#$%^*=+\/|'&quot;"," ()()()")
return if (contains($placename,":")) then substring-after($placename,":") else tweetmap:get-geo($safename)
};
declare function tweetmap:order-tweets($keyword as xs:string) as node()* {
let $tweets := tweetmap:fetch-tweets($keyword)
let $retweets := for $tweet in $tweets
where starts-with($tweet//*:title , "RT")
return $tweet
let $topics := distinct-values(for $tweet in $retweets return substring($tweet//*:title, 1,40))
let $groups := for $topic in $topics
let $group := for $tweet in $retweets
where starts-with($tweet//*:title, $topic)
return $tweet
order by count($group)
return $group
let $newtweets := for $group in $groups
let $tweet := exactly-one($group)
(:$group/@rtcount=count($group):)
(:order by count(group) descending:)
return $tweet
return ($newtweets, $tweets except $retweets) (: note the use of set operator "except" :)
};
declare function tweetmap:get-tweets($keyword as xs:string) as node() {
<result keyword="{$keyword}">{
let $tweets := tweetmap:order-tweets($keyword)[position() <=9] (: TODO: restrict to the first 9 :)
for $tweet in $tweets
let
$location := exactly-one($tweet//*:location),
$geo := tweetmap:safe-geo($location),
$uri := $tweet//*:author//*:uri,
$txt := $tweet//*:title/text(),
$nr := (for $item at $pos in $tweets where $item//*:location=$tweet//*:location return $pos)[position() =1]
return <tweet nr="{$nr}" geo="{$geo}" user="{$uri}" cnt="">{$txt}</tweet>
}</result>
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment