<?php
set_time_limit(120);
$username = isset($_REQUEST['username']) && !empty($_REQUEST['username']) ? str_replace(".tumblr.com", "", strtolower($_REQUEST['username'])) : '';
if(empty($username))
{
?>
<html>
<head>
<title>Tumblr's Post Scraper v 1.0 :D</title>
<style>
body
{
font-family: Verdana, Tahoma, Arial, Helvetica, sans-serif;
}
.sep
{
color: #CCCCCC;
}
</style>
</head>
<body>
Original Tumblr blog name [<i><b>xyz</b></i><font color="#aaa">.tumblr.com,</font> not your email address or custom domain]:<br/><br/>
<form method="POST" action="">
<input type="text" id="username" name="username" size="40"/>
<input type="submit" value=" Export "/>
<br/><br/>
</form>
<br/><br/>
<script type="text/javascript">document.getElementById('username').focus();</script>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-5335675-1");
pageTracker._trackPageview();
</script>
</body>
</html>
<?php
die();
}
$type = $_REQUEST["type"];
$i = 0;
$posts = array();
$feed = '';
$allTags = array();
do
{
$url = 'http://'.$username.'.tumblr.com/api/read?start='. $i . '&num=50';
$file = file_get_contents($url);
$feed = new SimpleXMLElement($file);
$posts = array_merge($posts, $feed->xpath('posts//post'));
$i = (int)$feed->posts->attributes()->start + 50;
}while($i <= (int)$feed->posts["total"]);
function formatForWP($str)
{
global $type;
switch($type)
{
case "wordpress.com":
$str = formatVideoForWP(formatImageForWP($str));
}
return $str;
}
function formatImageForWP($str)
{
if(preg_match_all('/(<p>)?\s*(<img[^>]*\/?>)\s*(<\/p>)?/', $str, $matches))
{
for($i=0;$i<sizeof($matches[0]);$i++)
{
$str = str_replace($matches[0][$i], str_replace('/>',' alt=""/>', $matches[2][$i]), $str);
}
}
return $str;
}
function formatVideoForWP($str)
{
if(preg_match_all('/<object[\s\S]*src="([\S\s]*?)&[\s\S]*"[\s\S]*<\/object>/', $str, $matches))
{
for($i=0;$i<sizeof($matches);$i++)
{
if((strpos($matches[1][$i], 'youtube.com') !== false))
{
$str = str_replace($matches[0][$i], '[youtube='.$matches[1][$i].']', $str);
}
}
}
return $str;
}
function removeWeirdChars($str)
{
return trim(preg_replace('{(-)\1+}','$1',preg_replace('/[^a-zA-Z0-9-]/', '', str_replace(' ','-',strtolower(strip_tags($str))))),'-');
}
function getTags($post)
{
if($post->attributes()->type)
{
echo "<category><![CDATA[" . $post->attributes()->type . "]]></category>\n";
echo "\t\t<category domain=\"category\" nicename=\"" . $post->attributes()->type . "\"><![CDATA[" . $post->attributes()->type . "]]></category>\n";
}
else
{
echo "<category><![CDATA[Uncategorized]]></category>\n";
echo "\t\t<category domain=\"category\" nicename=\"uncategorized\"><![CDATA[Uncategorized]]></category>\n";
}
if($post->tag)
{
foreach($post->tag as $tag)
{
echo "\t\t<category domain=\"tag\"><![CDATA[$tag]]></category>\n";
echo "\t\t<category domain=\"tag\" nicename=\"" . removeWeirdChars($tag) . "\"><![CDATA[$tag]]></category>\n";
addTag((String)$tag);
}
}
}
function addTag($tag)
{
global $allTags;
if(!in_array($tag, $allTags))
$allTags[] = $tag;
}
function getAllTags()
{
global $allTags;
foreach($allTags as $tag)
{
echo "\t<wp:tag><wp:tag_slug>". removeWeirdChars($tag) . "</wp:tag_slug><wp:tag_name><![CDATA[$tag]]></wp:tag_name></wp:tag>\n";
}
}
header('content-type: text/xml');
header("content-disposition: attachment; filename=tumblr_$username.xml");
?>
<?php
ob_start();
foreach($posts as $post)
{
?>
<?php
switch($post->attributes()->type)
{
case "regular":
?>
<?php echo $post->attributes()->url ?>
<?php
break;
case "photo":
?>
<?php echo $post->attributes()->url ?>
<?php
break;
case "quote":
?>
<?php echo $post->attributes()->url ?>
<?php
break;
case "link":
?>
<?php echo $post->attributes()->url ?>
<?php
break;
case "conversation":
?>
<?php echo $post->attributes()->url ?>
<?php
break;
case "video":
?>
<?php echo $post->attributes()->url ?>
<?php
break;
case "audio":
?>
<?php echo $post->attributes()->url ?>
<?php
break;
}
?>
<?php
}
$out = ob_get_contents();
ob_end_clean();
getAllTags();
echo $out;
?>