Skip to content

Instantly share code, notes, and snippets.

@dscape
Created April 30, 2010 07:04
Show Gist options
  • Save dscape/384870 to your computer and use it in GitHub Desktop.
Save dscape/384870 to your computer and use it in GitHub Desktop.
Export your twitter favorites links (includes redirects, page titles and suggested tags) to delicious with #MarkLogic
xquery version "1.0-ml";
declare variable $twitter-username as xs:string external;
declare variable $delicious-username as xs:string external;
declare variable $delicious-password as xs:string external;
declare function local:get-favorite-page($username,$page) {
xdmp:http-get(
fn:concat(
"http://api.twitter.com/1/favorites/",$username,".xml?page=",$page))[2] };
(: https is available in yahoo apis - just change http for https. obvious: its slower:)
declare function local:store-in-delicious($url, $title, $options){
let $status-encoded := fn:concat("&description=", xdmp:url-encode($title))
let $url-encoded := xdmp:url-encode($url)
let $tags := fn:string-join( xdmp:http-get(
fn:concat( "http://api.del.icio.us/v1/posts/suggest?url=", $url),
$options)[2]//recommended/text(), "+" )
let $tags-encoded := if($tags) then fn:concat("&tags=", $tags) else ""
return xdmp:http-get(
fn:concat("http://api.del.icio.us/v1/posts/add?url=",
$url-encoded, $status-encoded, $tags-encoded,
"&replace=yes&shared=no"), $options) };
let $options :=
<options xmlns="xdmp:http">
<authentication method="basic">
<username>{$delicious-username}</username>
<password>{$delicious-password}</password>
</authentication>
</options>
let $first-page := local:get-favorite-page($twitter-username, 1)
let $page-count :=
xs:integer ( fn:round( ($first-page//favourites_count)[1] div 20 ) )
let $pages := ($first-page,
for $page in ( 2 to $page-count )
return local:get-favorite-page($twitter-username,$page))
let $rx-url := "(http://.*)"
let $urls := for $token in
fn:tokenize(fn:string-join($pages//text/text(), " "), " ")
return if(fn:matches($token,$rx-url)) then $token else ()
return for $url in $urls
let $headers := try { xdmp:http-get($url) } catch ($ex) {xdmp:log($ex)}
let $code := $headers//*:code[1]
let $local := ($headers//*:location[1]/text(), $url)[1]
let $title := try { (if($code=301)
then xdmp:tidy(xdmp:http-get($local)[2])//*:title[1]/text()
else xdmp:tidy($headers[2])//*:title[1]/text(), "from twitter")[1]
} catch( $ex ) { (xdmp:log($ex),"from twitter") }
return if($code=(200,301))
then local:store-in-delicious($local, fn:string-join($title, " "), $options)
else xdmp:log(fn:concat("Error processing ", $url))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment