Embedding your tumblr somewhere else
EDIT: Disregard all of this since the ruddy API has moved to OAuth for everything.
Seize the day! be a gangster. Put your tumblr in a page using the magic of PHP. I never use Javascript exclusively to deliver content because some people don’t have javascript going, or don’t want it. And it’s just unfair to ask them to process your junk. The following code downloads your Tumblr API XML and makes an array of posts that you can print out in some normal PHP. This code is not even, JV, not even thirds, not even practice sqaud, not even rec. It’s thinking about joining an intramural team. It is weak. Call this an 0.0.0.0.0.0.0.1 release.
Ok first we need to grab the XML. We are going to create a new DOMDocument with it. The DOMDocument::load function returns false if it was unable to load the requested XML. This is important, as Tumblr will often throw 503s.
$doc = new DOMDocument();
while(@!$doc->load("http://perkee.tumblr.com/api/read?$query"))
{
sleep(1); //seriously this is a design for only the criminally insane. Do not use it.
}
So we wait a second between each try. you can use usleep() to wait for less time.
Then we want to make an array of posts. No biggie, but this code is large and in charge. Since I resent the wackiness of PHP, I did not break this up into enough modular functions. My b. I told you this sucks.
$posts = array();
date_default_timezone_set('America/New_York');
foreach($doc->getElementsByTagName('post') as $post)
{
$p = array(
"id" => $post->getAttribute("id"),
"type" => $post->getAttribute("type"),
"date" => date('l j F Y',$post->getAttribute("unix-timestamp")),
"tags" => array(),
);
foreach($post->getElementsByTagName('tag') as $tag)
{
$p['tags'][] = $tag->nodeValue;
}
switch($p['type'])
{
case "regular":
$p['title'] = $post->getElementsByTagName('regular-title')->item(0)->nodeValue;
$p['body'] = $post->getElementsByTagName('regular-body')->item(0)->nodeValue."\n";
break;
case "video":
$p['body'] = "<div class=\"video\">\n ".
$post->getElementsByTagName('video-player')->item(0)->nodeValue.
"\n</div><div class=\"caption\">\n ".
$post->getElementsByTagName('video-caption')->item(0)->nodeValue.
"\n</div>\n";
break;
case "audio":
$p['title'] = $post->getElementsByTagName('id3-artist')->item(0)->nodeValue.
' - <span class="song">'.
$post->getElementsByTagName('id3-title')->item(0)->nodeValue.
"</span>";
$p['body'] = "<div class=\"audio\">\n ".
$post->getElementsByTagName('audio-player')->item(0)->nodeValue.
'</div>'.
"\n<div class=\"caption\">\n ".
$post->getElementsByTagName('audio-caption')->item(0)->nodeValue.
"\n</div>\n";
break;
case "quote":
$p['body'] = "<div class=\"quote\">\n".
" <span class=\"quote\">\n".
// " <big class=\"quote\"><a href=\"./posts/".$p['id']."\">“</a></big>\n".
" ".$post->getElementsByTagName('quote-text')->item(0)->nodeValue."\n".
" </span>\n".
" <div class=\"source\">— ".
$post->getElementsByTagName('quote-source')->item(0)->nodeValue.
"</div>\n</div>\n";
break;
case "photo":
$p['body'] = "<img src=\"".
$post->getElementsByTagName('photo-url')->item(0)->nodeValue.
"\" />\n<p>\n".
$post->getElementsByTagName('photo-caption')->item(0)->nodeValue.
"\n</p>";
break;
case "conversation":
$p['title'] = $post->getElementsByTagName('conversation-title')->item(0)->nodeValue;
$convo = $post->getElementsByTagName('conversation')->item(0)->getElementsByTagName('line');
$p['body'] = "<ul>\n ";
$odd = true;
foreach($convo as $line)
{
$p['body'] .= "<li class=\"".($odd ? "odd" : "even")."\">\n <span class=\"label\">".
$line->getAttribute('label') . "</span>\n ".
$line->nodeValue . "\n</li>\n";
$odd = !$odd;
}
case "link":
$p['title'] = "<a href=\"".$post->getElementsByTagName('link-url')->item(0)->nodeValue."\">".
$post->getElementsByTagName('link-text')->item(0)->nodeValue."</a>";
$p['body'] = $post->getElementsByTagName('link-description')->item(0)->nodeValue."\n";
break;
}
if(count($p['tags']) > 0)
{
$p['body'] .= "<ul class=\"tags\">\n";
foreach($p['tags'] as $tag)
{
$p['body'] .= " <li><a href=\"/blog/tags/$tag\">$tag</a></li>\n";
}
$p['body'] .= "</ul>";
}
$posts[] = $p;
}
?>
Ok, so you’re probably a little weirded out. Well, the XML stores most of the post meta-info in attributes of the <code> tag. So those are dealt with first. They are really easy; getAttribute() does what it says on the tin.
Building the titles and bodies of posts is super arbitrary. I chose the most descriptive things I could, which is why I used the ID3 tags for MP3 posts. If you posted a song without them, then they won’t show up. In fact, everything will go to hell if you leave titles off of most things. It’ll just look terrible; please don’t. It’s easier to read with titles anyways. So that switch builds the bodies and titles based upon the post type. Conversation doesn’t work and I don’t know why. ‘Scuse me while I kiss the sky.
Ok tags are last. They are super easy. Not much to say there. Style them nice of course; they’re worth it. But why, you ask, did I make the links the ones I did? I want permalinks to stay on my page, not on Tumblr. Ditto next page and tags. I’ll throw in search too. So for that part, I have this on top of everything.
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$postsPerPage = 10;
$query = "?num=$postsPerPage";
if(isset($_GET['posts']))
{
$query .= '&id='.$_GET['posts'];
}
elseif(isset($_GET['page']))
{
$query .= '&start='.(($_GET['page'] - 1) * $postsPerPage);
}
elseif(isset($_GET['tags']))
{
$query .= '&tagged='.$_GET['tags'];
}
To handle rewrite the pretty URLs into PHP GET queries, I have this little gem in my ./htaccess file in the blog directory. I should mention that all of the PHP goes into /blog/index.php on my site.
RewriteEngine On RewriteBase /blog RewriteRule ^([^/]+)/([^/]+)$ ?$1=$2
Ok more to come later. I’m writing from a Macbook Pro in a Starbuck’s and if I keep going for too long I’ll have to kill myself. I put most of the finishing touches on this on the AMtrak up to Boston today. Shit was boss.
(Source: perk.ee)
