Skip to content

Instantly share code, notes, and snippets.

@defel
Created December 20, 2010 04:26
Show Gist options
  • Save defel/748011 to your computer and use it in GitHub Desktop.
Save defel/748011 to your computer and use it in GitHub Desktop.
<?php
error_reporting(1);
function parseXML() {
$xmlfile = 'delicious.xml';
$xml = simplexml_load_file($xmlfile);
foreach($xml->post as $post) {
$tags = $post['tag'];
$tags = explode(' ',$tags);
foreach($tags as $tag) {
$tag = trim($tag);
if(!in_array($tag, $tags)) {
$tags[$tag]['name'] = $tag;
}
}
$posts[] = array(
'url' => (string)$post['href'],
'name' => (string)$post['description'],
'tags' => $tags,
'timestamp' => (string)$post['time'],
'description' => (string)$post['extended']
);
}
$posts = array_reverse($posts);
foreach($posts as $deliciouspost) {
echo $deliciouspost[name];
echo '<br>';
echo $deliciouspost[url];
echo '<br>';
echo $deliciouspost[description];
echo '<br>';
$tagstring = '';
foreach ($deliciouspost[tags] as $individualtag) {
$tagstring .= ','.$individualtag;
}
$tagstring = trim($tagstring,',');
echo $tagstring;
echo '<br>';
echo $deliciouspost[timestamp];
echo '<br>';
postToTumblr ($deliciouspost[url],$deliciouspost[name],$deliciouspost[description],$tagstring,$deliciouspost[timestamp]);
echo '<br><br>';
sleep(1);
}
}
function postToTumblr ($url,$name,$description,$tags,$timestamp) {
// Authorization info
$tumblr_email = 'foo@example.com';
$tumblr_password = 'meinpass';
$tumblr_group = 'myblog.tumblr.com';
// Data for new record
$post_type = 'link';
$post_generator = 'Delicious';
$post_url = $url;
$post_description = $name;
$post_tags = $tags;
$post_date = $timestamp;
$post_extended = $description;
// Prepare POST request
$request_data = http_build_query(
array(
'email' => $tumblr_email,
'password' => $tumblr_password,
'type' => $post_type,
'description' => $post_extended,
'generator' => $post_generator,
'date' => $post_date,
'url' => $post_url,
'tags' => $post_tags,
'name' => $post_description,
'group' => $tumblr_group
)
);
// Send the POST request (with cURL)
$c = curl_init('http://www.tumblr.com/api/write');
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $request_data);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($c);
$status = curl_getinfo($c, CURLINFO_HTTP_CODE);
curl_close($c);
// Check for success
if ($status == 201) {
echo "Success! The new post ID is $result.\n";
} else if ($status == 403) {
echo 'Bad email or password';
} else {
echo "Error: $result\n";
}
}
parseXML();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment