Permalink
Browse files

initial commit, code is functional, not polished, and you run into ra…

…te limits before you can do anything interesting
  • Loading branch information...
0 parents commit 447ca850edf2f076be33d011e5a94988521a8a09 @alexkingorg committed Jan 15, 2011
Showing with 1,310 additions and 0 deletions.
  1. +6 −0 README
  2. +35 −0 get-report.php
  3. +112 −0 get-tweets.php
  4. +29 −0 get-users.php
  5. +878 −0 lib/OAuth.php
  6. +250 −0 lib/twitteroauth.php
6 README
@@ -0,0 +1,6 @@
+Some little scripts to look at tweet frequency of my Twitter friends.
+
+## TODO
+
+- have the script run and archive data by day better
+- ability to pick up where left off after being rate limited
@@ -0,0 +1,35 @@
+<?php
+
+define('DB_USERNAME', 'username');
+define('DB_PASSWORD', 'password');
+define('DB_NAME', 'tweet_frequency');
+define('DB_TABLE', 'tweets_005');
+
+define('LIMIT_DAYS', '30');
+
+set_time_limit(0);
+
+header('Content-type: text/plain');
+
+$db_connect = mysql_connect('localhost', DB_USERNAME, DB_PASSWORD);
+mysql_select_db(DB_NAME, $db_connect);
+
+$counts = $limits = array();
+
+$day_from = '2011-01-02';
+$day_range = floor(LIMIT_DAYS / 4);
+
+for ($i = 1; $i <= 4; $i++) {
+// last $day_range, starting with today
+ $from = date('Y-m-d 00:00:00', strtotime('-'.($day_range * $i).' days', strtotime($day_from)));
+ $to = date('Y-m-d 00:00:00', strtotime('-'.($day_range * ($i - 1)).' days', strtotime($day_from)));
+ $result = mysql_query("
+ SELECT COUNT(id) as tweet_count
+ FROM `".DB_TABLE."`
+ WHERE `day` > '$from'
+ AND `day` <= '$to'
+ ");
+ while ($data = mysql_fetch_object($result)) {
+ echo $from.' - '.$to.': '.$data->tweet_count.PHP_EOL;
+ }
+}
@@ -0,0 +1,112 @@
+<?php
+
+define('DB_USERNAME', 'username');
+define('DB_PASSWORD', 'password');
+define('DB_NAME', 'tweet_frequency');
+define('DB_TABLE', 'tweets_006');
+
+define('LIMIT_DAYS', '30');
+
+set_time_limit(0);
+
+header('Content-type: text/plain');
+
+$db_connect = mysql_connect('localhost', DB_USERNAME, DB_PASSWORD);
+mysql_select_db(DB_NAME, $db_connect);
+
+mysql_query("
+CREATE TABLE IF NOT EXISTS `".DB_TABLE."` (
+ `id` int(5) NOT NULL auto_increment,
+ `timestamp` datetime NOT NULL,
+ `day` datetime NOT NULL,
+ `retweet` int(1) DEFAULT 0,
+ `reply` int(1) DEFAULT 0,
+ `tweet_id` varchar(255) NOT NULL,
+ `screen_name` varchar(255) NOT NULL,
+ PRIMARY KEY (`id`)
+) ENGINE=MyISAM DEFAULT CHARSET=utf8;
+") or die(mysql_error());
+
+mysql_query("
+ TRUNCATE TABLE `".DB_TABLE."`
+") or die(mysql_error());
+
+// pull users from cookie
+
+if (!empty($_COOKIE['tf_tw_users'])) {
+ $tw_users = json_decode(stripslashes($_COOKIE['tf_tw_users']), true);
+}
+if (!is_array($tw_users) || !count($tw_users)) {
+ die('Error finding users, try Step 1.');
+}
+
+$requests = 0;
+$request_limit = 100;
+$user_limit = 10;
+$user_count = 0;
+
+foreach ($tw_users as $tw_user) {
+ $user_count++;
+ $forced = false;
+ for ($i = 1; $i <= $user_limit; $i++) { // pages
+ $args = array(
+ 'screen_name' => $tw_user,
+ 'count' => '200',
+ 'page' => $i,
+ );
+ $url = 'http://api.twitter.com/1/statuses/user_timeline.json?'.http_build_query($args);
+ sleep(1);
+ if ($data = file_get_contents($url)) {
+ if ($tweets = json_decode($data)) {
+ if (!is_array($tweets) || !count($tweets)) {
+ echo $tw_user.' failed to return tweets'.PHP_EOL;
+ $i = $user_limit;
+ $forced = true;
+ break;
+ }
+ else {
+ $insert = array();
+ foreach ($tweets as $tweet) {
+// limit by date
+ if (strtotime($tweet->created_at) < strtotime('-'.LIMIT_DAYS.' days')) {
+ echo $tw_user.' maxed out'.PHP_EOL;
+ $i = $user_limit;
+ $forced = true;
+ break;
+ }
+ $data = array();
+ $data['timestamp'] = mysql_real_escape_string(date('Y-m-d H:i:s', strtotime($tweet->created_at)));
+ $data['date'] = mysql_real_escape_string(date('Y-m-d 00:00:00', strtotime($tweet->created_at)));
+ $data['retweet'] = (int) !empty($tweet->retweeted_status);
+ $data['reply'] = (int) !empty($tweet->in_reply_to_screen_name);
+ $data['tweet_id'] = mysql_real_escape_string($tweet->id);
+ $data['screen_name'] = mysql_real_escape_string($tweet->user->screen_name);
+ $insert[] = '("'.implode('", "', $data).'")';
+ }
+ mysql_query("
+ INSERT INTO `".DB_TABLE."` (
+ `timestamp`,
+ `day`,
+ `retweet`,
+ `reply`,
+ `tweet_id`,
+ `screen_name`
+ )
+ VALUES ".implode(', '.PHP_EOL, $insert)."
+ ");
+ echo ' inserted '.count($insert).' tweets'.PHP_EOL;
+ }
+ }
+ }
+ if ($i == $user_limit && !$forced) {
+ echo $tw_user.' hit limit'.PHP_EOL;
+ }
+ $requests++;
+ flush();
+ }
+ if ($requests > $request_limit) {
+ break;
+ }
+}
+
+echo PHP_EOL.PHP_EOL.'---'.PHP_EOL.PHP_EOL.'Fetched '.$user_count.' users.'.PHP_EOL.'Last fetched: '.$tw_user;
@@ -0,0 +1,29 @@
+<?php
+
+set_time_limit(0);
+
+include('config.php');
+
+// get all users
+
+$cursor = '-1';
+$tw_users = array();
+$i = 0;
+while ($cursor != '0') {
+ $url = 'http://api.twitter.com/1/statuses/friends/'.$username.'.json?cursor='.$cursor;
+ if ($data = file_get_contents($url)) {
+ if ($data = json_decode($data)) {
+ foreach ($data->users as $tw_user) {
+ if (isset($tw_user->screen_name)) {
+ $tw_users[] = $tw_user->screen_name;
+ }
+ }
+ $cursor = $data->next_cursor_str;
+ }
+ }
+}
+sort($tw_users);
+
+setcookie('tf_tw_users', json_encode($tw_users), strtotime('+1 week'));
+
+die(count($tw_users).' users found. On to step 2.');
Oops, something went wrong.

0 comments on commit 447ca85

Please sign in to comment.