/**
  * This command processes the Data pulled from twitter
  */
 public function actionProcessData()
 {
     $CurrentDate = new \DateTime('NOW');
     $crawlerDataCollection = CrawlerData::findAll(['parsed_at' => null]);
     $json_data = [];
     $regex = [];
     foreach ($crawlerDataCollection as $crawlerData) {
         $Contest = Contest::findOne($crawlerData->contest_id);
         $contestRegex = CrawlerProfile::findOne(['id' => $Contest->crawler_profile_id]);
         if ($Contest->custom_regex_entry == null) {
             $regex["entry"] = $contestRegex->regex_entry;
         } else {
             $regex["entry"] = $Contest->custom_regex_entry;
         }
         if ($Contest->custom_regex_rating == null) {
             $regex["rating"] = $contestRegex->regex_rating;
         } else {
             $regex["rating"] = $Contest->custom_regex_rating;
         }
         $jsonData = Json::decode($crawlerData->data, true);
         foreach ($jsonData["statuses"] as $id => $tweet) {
             // Get tweet creation date
             $TweetDate = DateTime::createFromFormat("D M d H:i:s T Y", $tweet["created_at"]);
             // check if Tweet creation date is within restrictions!
             if ($TweetDate->format('Y-m-d') >= $Contest->parse_from && $TweetDate->format('Y-m-d') <= $Contest->parse_to) {
                 $Tweet = $this->CreateTweetEntry($tweet, $Contest->id, $regex);
                 if ($Tweet != null) {
                     $Contest->last_parsed_tweet_id = $Tweet->id;
                 }
             }
         }
         if (array_key_exists("next_results", $jsonData["search_metadata"])) {
             $Contest->next_result_query = $jsonData["search_metadata"]["next_results"];
         } else {
             // Set new refresh URL only if we have processed new tweets
             if (sizeof($jsonData["statuses"]) > 0) {
                 $Contest->next_result_query = $jsonData["search_metadata"]["refresh_url"];
             }
         }
         if ($Contest->save()) {
             $this->stdout("Last Tweet ID for Contest " . $Contest->name . " saved!\n", Console::FG_GREEN);
             // Update Entries with new Data
             $this->UpdateContestEntries($Contest);
         } else {
             $this->stdout("Error Saving Tweet !\n", Console::BOLD);
             $this->stdout(print_r($Contest->errors) . "\n", Console::BOLD);
         }
         $parsed_at = new DateTime('NOW');
         $crawlerData->parsed_at = $parsed_at->format('Y-m-d H:i:s');
         //save parsed_at time
         $crawlerData->save();
     }
 }
 /**
  * Finds the Contest model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param string $id
  * @return Contest the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Contest::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }