コード例 #1
0
 /**
  * Grabs all the latest tweets (up to 3200 because of API limits) and puts them into the database.
  *
  * @param string $type 'timeline'|'favorites'
  * @return string Returns a string with informational output.
  */
 public function archive($type = \Darathor\Amt\Archiver::TYPE_TIMELINE)
 {
     // This should use a maximum of 16 API calls if the user has 3200+ tweets.
     // api params
     $maxId = null;
     $sinceId = null;
     $userId = null;
     // not needed if using screen name
     $screenName = $this->username;
     $count = 200;
     $trimUser = null;
     $excludeReplies = false;
     $contributorDetails = true;
     $includeRts = true;
     $includeEntities = true;
     // loop variables
     $str = '';
     $page = 1;
     $gotResults = true;
     $apiCalls = 0;
     $tweetsFound = 0;
     $numAdded = 0;
     $numExceptions = 0;
     $maxExceptions = 5;
     // Don't get stuck in the loop if twitter is down.
     $maxPage = 2;
     // 20
     while ($gotResults && $page <= $maxPage) {
         $str .= 'max id: ' . ($maxId === null ? 'null' : $maxId) . PHP_EOL;
         try {
             if ($type === 'favorites') {
                 $tweetResults = $this->twitter->favoritesList($userId, $screenName, $count, $sinceId, $maxId, $includeEntities);
             } else {
                 $tweetResults = $this->twitter->statusesUserTimeline($userId, $screenName, $sinceId, $count, $maxId, $trimUser, $excludeReplies, $contributorDetails, $includeRts);
             }
             $apiCalls++;
             $numResults = count($tweetResults);
             $tweetsFound += $numResults;
             if ($numResults == 0) {
                 $str .= 'NO tweets on page ' . $page . ', exiting.' . PHP_EOL;
                 $gotResults = false;
             } else {
                 $newestTweet = $tweetResults[0];
                 $oldestTweet = end($tweetResults);
                 $str .= $numResults . ' tweets on page ' . $page . ' (oldest: ' . $oldestTweet['id'] . ', newest: ' . $newestTweet['id'] . ')' . PHP_EOL;
                 $page++;
                 // Add these tweets to the database.
                 $tweets = [];
                 foreach ($tweetResults as $t) {
                     $tweet = new Tweet();
                     $tweet->loadFromArray($t);
                     $tweets[] = $tweet;
                 }
                 $result = $this->model->addTweets($tweets);
                 if ($result === false) {
                     $str .= 'ERROR INSERTING TWEETS INTO DATABASE: ' . $this->model->getLastErrorMessage() . PHP_EOL;
                 } else {
                     if ($result == 0) {
                         $str .= 'Zero tweets added.' . PHP_EOL;
                     } else {
                         $str .= $result . ' tweets added.' . PHP_EOL;
                         $numAdded += $result;
                     }
                 }
                 // set max ID to the ID of the oldest tweet we've received, minus 1
                 // be mindful of 32 bit platforms
                 $maxId = $this->decrement64BitInteger($oldestTweet['id']);
             }
             // check if we've reached the rate limit
             $rate = $this->twitter->getLastRateLimitStatus();
             if (isset($rate['remaining']) && isset($rate['limit'])) {
                 $str .= $rate['remaining'] . '/' . $rate['limit'] . PHP_EOL;
                 if ($rate['remaining'] <= 0) {
                     $str .= 'API limit reached for this hour. Try again later.' . PHP_EOL;
                     $gotResults = false;
                 }
             } else {
                 $str .= 'Rate limit headers missing from response. Twitter may be having problems. Try again later.' . PHP_EOL;
                 $gotResults = false;
             }
         } catch (\Exception $e) {
             $str .= 'Exception: ' . $e->getMessage() . PHP_EOL;
             $numExceptions++;
             // break out to avoid infinite looping while twitter is down
             if ($numExceptions >= $maxExceptions) {
                 $str .= 'Too many connection errors. Twitter may be down. Try again later.' . PHP_EOL;
                 $gotResults = false;
             }
         }
     }
     $str .= $apiCalls . ' API calls, ' . $tweetsFound . ' tweets found, ' . $numAdded . ' tweets saved' . PHP_EOL;
     return $str;
 }
コード例 #2
0
 public function testLoadArray()
 {
     $tweetData = array('id' => 293780221621067776, 'user' => array('id' => 14061545), 'created_at' => '2013-01-22 13:00:37', 'text' => "Archive My Tweets has a new look, and can now import your official twitter archive. https://t.co/e8HDtbYa", 'source' => '<a href="http://twitterrific.com" rel="nofollow">Twitterrific for Mac</a>', 'truncated' => 0, 'favorited' => 0, 'in_reply_to_status_id' => 0, 'in_reply_to_user_id' => 0, 'in_reply_to_screen_name' => 0);
     $t = new Tweet();
     $t->loadFromArray($tweetData);
     foreach (array_keys($tweetData) as $key) {
         if ($key == 'user' || $key == 'text') {
             continue;
         }
         $this->assertEquals($tweetData[$key], $t->{$key});
     }
     $this->assertEquals($tweetData['user']['id'], $t->user_id);
     $this->assertEquals($tweetData['text'], $t->tweet);
 }