Example #1
0
 /**
  * Imports tweets from the JSON files in a downloaded Twitter Archive
  *
  * @param string $directory The directory to look for Twitter .js files.
  * @param Model $model The persistence model.
  * @return string Returns a string with informational output.
  * @author awhalen
  */
 public function importJSON($directory, $model)
 {
     $str = 'Importing from Twitter Archive JS Files...' . "\n";
     if (!is_dir($directory)) {
         return $str . 'Could not import from official Twitter archive. Not a valid directory: ' . $directory . "\n";
     }
     $jsFiles = glob($directory . "/*.js");
     if (count($jsFiles)) {
         $numAdded = 0;
         // find all JS files and grab the tweets from each one
         foreach ($jsFiles as $filename) {
             $tweets = $this->getTweetsInJsonFile($filename);
             if ($tweets != false) {
                 $numFoundTweets = count($tweets);
                 $plural = $numFoundTweets == 1 ? '' : 's';
                 $str .= basename($filename) . ': found ' . $numFoundTweets . ' tweet' . $plural . "\n";
                 // add
                 $result = $model->addTweets($tweets);
                 if ($result === false) {
                     $str .= 'ERROR INSERTING INTO DATABASE: ' . $model->getLastErrorMessage() . "\n";
                 } else {
                     if ($result == 0) {
                         $str .= 'No new tweets found.' . "\n";
                     } else {
                         $numAdded += $result;
                         $str .= 'Added new tweets: ' . $result . "\n";
                     }
                 }
             } else {
                 $str .= $filename . ': No tweets found' . "\n";
             }
         }
         $str .= 'JS import done. Added tweets: ' . $numAdded . "\n";
     } else {
         $str .= 'No Twitter Archive JS files found.' . "\n";
     }
     return $str;
 }