예제 #1
0
 /**
  * Cronjob endpoint, merges user hashtags and twitter api trends
  * and stores the result to redis.
  * If any change occurs this service will notify the daemon via
  * DeamonController passing a new array of trends to track.
  *
  * @return Array of trends
  */
 public function daemonServiceTrends()
 {
     // TODO laravel connection
     $redis = new Redis();
     $redis->connect('127.0.0.1', 6379);
     // Api Woeid trends
     $trends = [];
     foreach ($this->getPlaces() as $city => $woeid) {
         $trends = array_merge($trends, $this->requestTrendsByLocation($woeid));
     }
     // Save woeid_hashtags
     $redis->set('woeid_hashtags', json_encode($trends));
     // User trends
     $hashtags = Hashtag::all();
     foreach ($hashtags as $hashtag) {
         array_push($trends, $hashtag->name);
     }
     // Discard duplicates
     array_unique($trends);
     $redisTrends = json_decode($redis->get('daemon_hashtags'));
     // If any change, return
     if ($trends == $redisTrends) {
         return response()->json(null)->header('Content-Type', 'application/json');
     }
     // Store new hashtags to database
     $redis->set('daemon_hashtags', json_encode(array($trends)));
     $notify = new DaemonController();
     $notify->updateTrends($trends);
     return response()->json($trends)->header('Content-Type', 'application/json');
 }