Outils pour utilisateurs

Outils du site


php:twitterarchive_json_to_rss

Script qui permet de convertir les fichiers JSON mensuels des archives Twitter en fichiers RSS (à faire traiter par Autoblog, par exemple, pour avoir un autoblog sauvegardant intégralement le compte Twitter).

exec.php
<?php
/*  unzip your downloaded "tweets.zip", place this script in "tweets/data/js/tweets/" and run !  */
 
$directory = "./";
$files = glob($directory . "*.js");
foreach($files as $file)
{
 
$twitter_data = json_decode(preg_replace('/^.+\n/', '', file_get_contents($file)), TRUE);
$screenName = $twitter_data[0]['user']['screen_name'];
 
	/*  RSS OUTPUT */
	// $rss is the HTML output string
	$rss = '<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">';
	$rss .= '<channel><title>Twitter / '.$screenName.'</title><link>http://twitter.com/'.$screenName.'</link><description>Twitter updates from '.$screenName.'.</description>';
 
	for ($i = 0; $i < 9999999; $i++) { /*  more than 3 tweets per second, don't tell me that's not large enough */
		if(isset($twitter_data[$i]['id_str']) and !isset($twitter_data[$i]['in_reply_to_screen_name'])) {  /*  check if tweet does exist, and is not a reply  */
		// Tweet Text
		$desc = htmlspecialchars($twitter_data[$i]['text']);
		$screenName = $twitter_data[$i]['user']['screen_name'];
		// Build link back
		$link = $twitter_data[$i]['id_str'];
		// Date tweet posted
		$date = $twitter_data[$i]['created_at'];
		$date = strtotime($date);
 
		// Build final output
			$rss .=	'
			<item>
			<title>'.$desc.'</title>
			<description>'.$desc.'</description>
			<pubDate>'.date(DATE_RSS, $date).'</pubDate>
			<guid>http://twitter.com/'.$screenName.'/statuses/'.$link.'</guid>
			<link>http://twitter.com/'.$screenName.'/statuses/'.$link.'</link>
			</item>';
 
 
		} }
 
	// Final touch
	$rss .= '
	</channel>
	</rss>';
 
	// write the stuff
	file_put_contents(str_replace('.js', '', $file).".rss", $rss);
	echo "\n$file: done";
 
}
 
echo "\n finished \n";
 
?>
php/twitterarchive_json_to_rss.txt · Dernière modification: 2013/04/11 12:51 par mitsu