Skip to content

Instantly share code, notes, and snippets.

@timdp
Created May 30, 2012 13:47
Show Gist options
  • Save timdp/2836426 to your computer and use it in GitHub Desktop.
Save timdp/2836426 to your computer and use it in GitHub Desktop.
Tweets per day chart
<?php
// Plots a user's daily tweet count 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];
$stats_daily = 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);
$stats_daily[date('Y-m-d', $time)]++;
$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 "Generating chart ...", PHP_EOL;
$chart_data = array(array('Date', 'Tweets'));
$dates = array_keys($stats_daily);
sort($dates);
$date = $dates[0];
$max_date = $dates[count($dates) - 1];
while ($date <= $max_date) {
$chart_data[] = array($date, array_key_exists($date, $stats_daily)
? $stats_daily[$date] : 0);
list($y, $m, $d) = explode('-', $date);
$date = date('Y-m-d', mktime(0, 0, 0, $m, $d + 1, $y));
}
$chart_data = json_encode($chart_data);
$title = "Tweets per day: @$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($chart_data);
var options = {
title: '$title'
};
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;
echo "Creating $outfile ...", PHP_EOL;
$fh = fopen($outfile, 'w');
if ($fh === false) {
die("Failed to open $outfile");
}
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