Created
December 20, 2010 04:26
-
-
Save defel/748011 to your computer and use it in GitHub Desktop.
modification of http://www.mikeindustries.com/blog/archive/2009/01/how-to-really-import-your-delicious-links-into-tumblr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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