function debug($str) { if (DEBUG) { //echo '<p style="font-family: monospace;">'.$str.'</p>'; twitlog("DEBUG: {$str}"); } }
/** * Send an authenticated request to Twitter for the timeline of authenticating users friends. * Returns the last 20 updates by default * @param boolean|integer $id Specifies the ID or screen name of the user for whom to return the friends_timeline. (set to false if you want to use authenticated user). * @param boolean|integer $since Narrows the returned results to just those statuses created after the specified date. * @param integer $count Specifies the number of statuses to retrieve. May not be greater than 200. * @param integer $page The page number to retrieve. * @return string */ function friendsTimeline($id = false, $since = false, $count = false, $page = 0) { $qs = ''; if ($since !== false) { if (is_numeric($since)) { $qs .= ($qs != '' ? '&' : '?') . 'since_id=' . urlencode($since); } else { $qs .= ($qs != '' ? '&' : '?') . 'since=' . urlencode($since); } } if ($count !== false) { $qs .= ($qs != '' ? '&' : '?') . 'count=' . urlencode($count); } if ($page) { $qs .= ($qs != '' ? '&' : '?') . 'page=' . urlencode($page); } if ($id === false) { $request = 'http://twitter.com/statuses/friends_timeline.' . $this->type . $qs; } else { $request = 'http://twitter.com/statuses/friends_timeline/' . urlencode($id) . '.' . $this->type . $qs; } twitlog("REQUEST: {$request}", 3, LOG_FILE); $xml = $this->process($request); //twitlog("XML: $xml",3,LOG_FILE); return $this->objectify($xml); }
function refresh_via_search_api($since = false) { $pid = fopen(UPDATE_PID_FILE, 'w'); fwrite($pid, getmypid()); fclose($pid); $this->twitter->type = 'xml'; $friends = $this->twitter->friends(); $base_qs = "rpp=100&"; if (isset($since)) { $base_qs .= "since_id=" . $since . "&"; } $base_qs .= "q="; $i = 0; $queries = array(); // This attempts to batch calls to the Twitter API. foreach ($friends as $friend) { $next = urlencode(HASHTAG . " from:" . $friend->screen_name); $next_length = strlen($queries[$j]) + strlen('+OR+' . $next); if (strlen($queries[$j]) == 0) { // simply initialize the query, this is for the first time through $queries[$j] = $base_qs . $next; } else { if ($next_length > QUERY_LIMIT) { // initialize a new query and advance counters, if query limit exceeded $i = 0; // reset friend counter $j++; // advance query index $queries[$j] = $base_qs . $next; } else { $queries[$j] .= '+OR+' . $next; } } } $this->twitter->type = 'atom'; $tweets = array(); foreach ($queries as $q) { twitlog("Sending query {$q}", 3, LOG_FILE); $tmp = $this->twitter->search($q); debug("query (" . strlen($q) . "): {$q}"); foreach ($tmp as $t) { $tweet = Tweet::fromSimpleXML($t); $tweets[] = $tweet; $tweet->insert(); } } debug("# of tweets: " . count($tweets)); touch(LAST_UPDATE_FILE); unlink(UPDATE_PID_FILE); return $tweets; }