public function findOrCreateByText($tweet)
 {
     $text = $tweet->text;
     if (!count(Tweet::where('text', $text)->get())) {
         return Tweet::create(['text' => $text, 'link' => $this->tweetHasLink($text), 'retweet_count' => $tweet->retweet_count, 'time' => $this->setTime($tweet->created_at), 'hour' => $this->findHour($tweet->created_at), 'favorite_count' => $tweet->favorite_count, 'hashtag_count' => count($tweet->entities->hashtags), 'retweet' => $this->tweetIsRT($text)]);
     } else {
         return Tweet::where('text', $text)->get();
     }
 }
 /**
  * Display a listing of the resource.
  *
  * @return \Illuminate\Http\Response
  */
 public function index(Request $request)
 {
     $search = '';
     if ($search = $request->input('search')) {
         $tweets = Tweet::where('title', "LIKE", "%" . $search . "%")->orWhere('body', "LIKE", "%" . $search . "%")->paginate(10);
     } else {
         $tweets = Tweet::simplePaginate(5);
         //Tweet::all()
     }
     $users = User::all();
     $tags = ['foo', 'bar'];
     $latests = Tweet::take(5)->get();
     return view('home', compact('tweets', 'search', 'users', 'tags', 'latests'));
 }
Exemple #3
0
 public function search()
 {
     $search = Input::get('search');
     $country_id = Input::get('country_id');
     $users = null;
     $tweets = null;
     if ($search != "") {
         $users = User::where('username', 'like', '%' . $search . '%')->where('country_id', $country_id)->get();
         $tweets = Tweet::where('tweet', 'like', '%' . $search . '%')->where('country_id', $country_id)->get();
     } else {
         $users = User::where('country_id', $country_id)->get();
         $tweets = Tweet::where('country_id', $country_id)->get();
     }
     return view('search_results', compact('users', 'tweets'));
 }
 public static function searchLocationTweets($locName, $locLat, $locLng)
 {
     $location = Location::where('latitude', '=', round($locLat, 7))->where('longitude', '=', round($locLng, 7))->first();
     $tweets = null;
     if ($location == null) {
         $location = new Location();
         $location->name = $locName;
         $location->latitude = round($locLat, 7);
         $location->longitude = round($locLng, 7);
         $locRet = $location->save();
         //save search history
         $search = new Search();
         $search->location_id = $location->id;
         $search->user = $_COOKIE['user'];
         $search->save();
         $tweets = self::getTweets($locName, round($locLat, 7), round($locLng, 7));
         //save tweets
         foreach ($tweets as $tweet) {
             $tweet->search_id = $search->id;
             $tweet->save();
         }
     } else {
         $search = Search::where('location_id', '=', $location->id)->orderBy('created_at', 'desc')->first();
         $searchTime = strtotime($search->created_at);
         //save new search
         $newSearch = new Search();
         $newSearch->location_id = $location->id;
         $newSearch->user = $_COOKIE['user'];
         $newSearch->save();
         //if search is older than 1 hour, tweet again
         if ($searchTime <= strtotime('-' . Config::get('app.cache_duration') . ' minutes')) {
             $tweets = self::getTweets($locName, round($locLat, 7), round($locLng, 7));
             //save tweets
             foreach ($tweets as $tweet) {
                 $tweet->search_id = $search->id;
                 $tweet->save();
             }
         } else {
             //get last search with tweets
             $search = DB::table('searches')->where('location_id', '=', $location->id)->whereExists(function ($query) {
                 $query->select(DB::raw(1))->from('tweets')->whereRaw('tweets.search_id = searches.id');
             })->orderBy('created_at', 'desc')->first();
             $tweets = Tweet::where('search_id', '=', $search->id)->get();
         }
     }
     return $tweets;
 }
Exemple #5
0
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     \Log::info('Fetch tweets');
     $client = new Client('https://api.twitter.com/1.1/');
     $auth = new Oauth(['consumer_key' => env('TWITTER_CONSUMER_KEY'), 'consumer_secret' => env('TWITTER_CONSUMER_SECRET'), 'token' => env('TWITTER_ACCESS_TOKEN'), 'token_secret' => env('TWITTER_ACCESS_TOKEN_SECRET')]);
     $client->addSubscriber($auth);
     //$response = $client->get('search/tweets.json?q=%23peukpoll&since=' . date('Y-m-d'))->send();
     $response = $client->get('search/tweets.json?q=%23peukpoll')->send();
     $data = $response->json()['statuses'];
     $tweets = array_fetch($response->json()['statuses'], 'text');
     foreach ($tweets as $tweet) {
         $values[] = explode(' ', $tweet);
     }
     for ($i = 0; $i < count($values); $i++) {
         $user = User::where('username', $data[$i]['user']['screen_name'])->first();
         if ($user == null) {
             $user = new User();
             $user->username = $data[$i]['user']['screen_name'];
             $user->save();
         }
         $tweet = Tweet::where('tweet', $data[$i]['id'])->first();
         if ($tweet == null) {
             $tweet = new Tweet();
             $tweet->x = $values[$i][0];
             $tweet->y = $values[$i][2];
             $tweet->score_x = 1;
             $tweet->score_y = 1;
             $tweet->tweet = $data[$i]['id'];
             $tweet->user()->associate($user);
             $tweet->save();
             $numberOfUpcomingTweets = UpcomingTweet::all()->count();
             if ($numberOfUpcomingTweets > 0) {
                 $upcomingTweet = new UpcomingTweet();
                 $upcomingTweet->end = date('Y-m-d H:i:s', strtotime('+ 1 year'));
                 $upcomingTweet->tweet()->associate($tweet);
                 $upcomingTweet->save();
             } else {
                 $upcomingTweet = new UpcomingTweet();
                 $upcomingTweet->end = date('Y-m-d H:i:s', strtotime('+ 1 hour'));
                 $upcomingTweet->tweet()->associate($tweet);
                 $upcomingTweet->save();
             }
         }
     }
 }
Exemple #6
0
 public function repost_number()
 {
     return Tweet::where('tweet_id', $this->id)->count();
 }