Created
May 30, 2012 14:10
-
-
Save timdp/2836576 to your computer and use it in GitHub Desktop.
Tweets rhythm chart
This file contains hidden or 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 | |
// Plots a user's tweet rhythm using Google Chart Tools | |
// Limited to about 3200 tweets by the Twitter API | |
// Hacked together by @tmdpw on 2012-05-30 | |
if (count($argv) != 3) { | |
die("Usage: php $argv[0] USERNAME FILENAME"); | |
} | |
$user = $argv[1]; | |
$outfile = $argv[2]; | |
$scale = 60; | |
$stat = array(); | |
$total_tweets = 0; | |
echo "Fetching tweets ..."; | |
$url = 'http://api.twitter.com/1/statuses/user_timeline.json?' | |
. http_build_query( | |
array('screen_name' => $user, 'include_rts' => 1, 'count' => 200)); | |
$json_str = get_url($url); | |
$data = json_decode($json_str); | |
while (count($data)) { | |
foreach ($data as $tweet) { | |
$time = strtotime($tweet->created_at); | |
$stat[floor($time / $scale)]++; | |
$total_tweets++; | |
} | |
echo " $total_tweets ..."; | |
$next = minus_one($data[count($data) - 1]->id); | |
$json_str = get_url("$url&max_id=$next"); | |
$data = json_decode($json_str); | |
} | |
echo " Done.", PHP_EOL; | |
echo "Writing chart to $outfile ...", PHP_EOL; | |
$fh = fopen($outfile, 'w'); | |
if ($fh === false) { | |
die("Failed to open $outfile"); | |
} | |
$title = "Tweet rhythm: @$user"; | |
$html = <<<HERE | |
<html> | |
<head> | |
<title>$title</title> | |
<script type="text/javascript" src="https://www.google.com/jsapi"></script> | |
<script type="text/javascript"> | |
google.load("visualization", "1", {packages:["corechart"]}); | |
google.setOnLoadCallback(function() { | |
var data = google.visualization.arrayToDataTable( | |
HERE; | |
fwrite($fh, $html); | |
$chart_data = array(array('Time', 'Tweets')); | |
$times = array_keys($stat); | |
sort($times, SORT_NUMERIC); | |
$time = $times[0]; | |
$max_time = $times[count($times) - 1]; | |
$total = 0; | |
while ($time <= $max_time) { | |
if (array_key_exists($time, $stat)) { | |
$total += $stat[$time]; | |
} | |
$chart_data[] = array(date('Y-m-d H:i:s', $time * $scale), $total); | |
$time += $scale; | |
} | |
fwrite($fh, json_encode($chart_data)); | |
$html = <<<HERE | |
); | |
var options = { | |
title: "$title", | |
legend: { | |
position: "none" | |
} | |
}; | |
var chart = new google.visualization.LineChart( | |
document.getElementById('chart_div')); | |
chart.draw(data, options); | |
}); | |
</script> | |
</head> | |
<body> | |
<div id="chart_div" style="width: 900px; height: 500px;"></div> | |
</body> | |
</html> | |
HERE; | |
fwrite($fh, $html); | |
fclose($fh); | |
echo "Done.", PHP_EOL; | |
function get_url($url) { | |
if (extension_loaded('curl')) { | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
$data = curl_exec($ch); | |
$result = curl_getinfo($ch, CURLINFO_HTTP_CODE) == 200 | |
? $data : false; | |
curl_close($ch); | |
} else { | |
$result = file_get_contents($url); | |
} | |
if ($result === false) { | |
die("Failed to load $url"); | |
} | |
return $result; | |
} | |
function minus_one($num) { | |
$num = number_format($num, 0, '', ''); | |
$pos = strlen($num) - 1; | |
while ($pos >= 0 && $num[$pos] == '0') { | |
$pos--; | |
} | |
return $pos < 0 | |
? -1 | |
: substr($num, 0, $pos) | |
. ($num[$pos] - 1) | |
. str_repeat('9', strlen($num) - $pos - 1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment