Skip to content

Instantly share code, notes, and snippets.

@hubgit
Created March 29, 2009 17:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hubgit/87468 to your computer and use it in GitHub Desktop.
Save hubgit/87468 to your computer and use it in GitHub Desktop.
Post starred items from Google Reader to Delicious
<?php
//$first_run = TRUE; // fetch all shared items from Google Reader on the first run
$id = 'YOUR_GOOGLE_ID_NUMBER';
$delicious = array(
'user' =>'YOUR_DELICIOUS_USERNAME',
'password' => 'YOUR_DELICIOUS_PASSWORD',
);
/* edit above here */
define('DELICIOUS_API', sprintf('https://%s:%s@api.del.icio.us/v1', rawurlencode($delicious['user']), rawurlencode($delicious['password'])));
$posted_file = 'posted.txt';
if (file_exists($posted_file))
$posted = json_decode(file_get_contents($posted_file));
if (!is_object($posted))
$posted = new stdClass();
$path = sprintf('user/%s/state/com.google/broadcast', $id); // too long for %d
$url = 'http://www.google.com/reader/public/atom/' . rawurlencode($path);
if ($first_run)
$url .= '?n=10000'; // for first import, fetch all items
$feed = simplexml_load_file($url);
$feed->registerXPathNamespace('atom', 'http://www.w3.org/2005/Atom');
foreach ($feed->xpath('atom:entry') as $entry){
$entry->registerXPathNamespace('atom', 'http://www.w3.org/2005/Atom');
$data = $entry->children('http://www.w3.org/2005/Atom');
$item = array(
'id' => (string) $data->id,
'title' => (string) $data->title,
'date' => strtotime((string) $data->published),
'link' => (string) current($entry->xpath('atom:link/@href')),
);
if ($posted->{$item['link']})
continue;
print $item['link'] . "\n";
$result = post_bookmark($item);
print $result . "\n";
switch ($result){
case 'done':
case 'item already exists':
break;
default:
exit('stopping (error posting bookmark)');
break;
}
$posted->{$item['link']} = 1;
file_put_contents($posted_file, json_encode($posted));
}
function post_bookmark($item){
if (!$item['link'])
return FALSE;
$link = resolve_url($item['link']);
print $link . "\n";
$params = array(
'url' => $link,
'description' => $item['title'],
'tags' => 'gshared',
'dt' => date('Y-m-d\Th:m:s\Z', $item['date']),
'replace' => 'no',
);
$url = DELICIOUS_API . '/posts/add?' . http_build_query($params);
$xml = simplexml_load_file($url);
sleep(1);
if (!is_object($xml))
return FALSE;
return (string) $xml['code'];
}
function resolve_url($url){
$ch = curl_init($url);
curl_setopt_array($ch, array(
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_NOBODY => TRUE, // uses HEAD
));
curl_exec($ch);
return curl_getinfo($ch, CURLINFO_HTTP_CODE) == 200 ? curl_getinfo($ch, CURLINFO_EFFECTIVE_URL) : $url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment