Tumblr tag list

In two of my tumblr blogs, Type Nesting and Looking For Glove there is a list of all the tags used in that blog. Tumblr offers a way to generate such list via its api, but the api is very slow and there is always a need for more then one call.

Below is a PHP script that makes crates a js file that has all the tags in a sorted array that looks like this

var tagsArray=new Array("BLACK","BLUE","CAPITAL A","CAPITAL B");

I have a cron that runs it every couple of hours on my server.


$myblog="lookingforglove"; //     modify this line     http://myblog.tumblr.com
$filename=$myblog.".js";
$stack = array();
$done=false;
$postsCounter=0;
$chunk = 50;  
while (!$done)
{	
	$url="http://".$myblog.".tumblr.com/api/read/json?callback=json&num=".$chunk."&start=".$postsCounter;
	$ch = curl_init($url);
	curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	$buffer = curl_exec($ch);
	curl_close($ch);
	if (empty($buffer))
		{
    	$done=false;
		}
	else
		{
		$buffer= substr($buffer, 5,-3);
		$json_o=json_decode($buffer,true); 
		$jpost= $json_o['posts'];
		
		foreach  ($jpost as $jpostItem)
			{
			$jtags=$jpostItem['tags'];
			foreach  ($jtags as $jtagsItem)
				{
				array_push($stack,strtoupper($jtagsItem));	
				}
			}
		}
	$postsCounter=$postsCounter+sizeof($jpost);
	if (sizeof($jpost)< $chunk)   ///no more posts
		{ $done=true;}
}
$stack=array_unique($stack);
asort($stack);
$longString="var tagsArray=new Array(\"";
$longString.=implode("\",\"", $stack);
$longString.="\");";

$fp = fopen($filename, 'w');
fwrite($fp, $longString);
fclose($fp);


The javascript used in the theme where I want to have the tags.

Leave a Reply