/** * 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(); } } } }
public function postNewTweet(Request $request) { $tweet = new Tweet(); $tweet->x = $request->x; $tweet->y = $request->y; $tweet->score_x = 1; $tweet->score_y = 1; $tweet->user()->associate(User::find(1)); $tweet->save(); if ($request->first) { $oldTweet = UpcomingTweet::orderBy('id', 'asc')->first(); $oldTweet->delete(); $newTweet = new UpcomingTweet(); $newTweet->id = 1; $newTweet->end = date('Y-m-d H:i:s', strtotime('+ 1 hour')); $newTweet->tweet()->associate($tweet); $newTweet->save(); } else { $newTweet = new UpcomingTweet(); $newTweet->end = date('Y-m-d H:i:s', strtotime('+ 1 year')); $newTweet->tweet()->associate($tweet); $newTweet->save(); } return redirect()->route('admin'); }