You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.8 KiB

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Routing\Controller as BaseController;
  5. use Log;
  6. use App\Player, App\Team, App\Tweet, App\TransitCenter, App\TransitLine, App\Document, App\Mission;
  7. use App\Events\NewTweetEvent;
  8. class TwitterController extends BaseController
  9. {
  10. public function input(Request $request)
  11. {
  12. $data = $request->all();
  13. // Find the user who tweeted this
  14. $twitter_user_id = $data['user']['id_str'];
  15. $player = Player::where('twitter_user_id', $twitter_user_id)->first();
  16. if(isset($data['extended_tweet']['full_text']))
  17. $text = $data['extended_tweet']['full_text'];
  18. else
  19. $text = $data['text'];
  20. // Unshorten URLs
  21. if(isset($data['entities']['urls'])) {
  22. foreach($data['entities']['urls'] as $url) {
  23. $text = str_replace($url['url'], $url['expanded_url'], $text);
  24. }
  25. }
  26. // Remove media URLs from tweet text
  27. $photos = [];
  28. if(isset($data['extended_entities']['media'])) {
  29. foreach($data['extended_entities']['media'] as $media) {
  30. $text = str_replace($media['url'], '', $text);
  31. $photos[] = $media['media_url_https'];
  32. }
  33. $text = trim($text);
  34. }
  35. // Find the mission hashtag
  36. $mission_id = 0;
  37. foreach(Mission::all() as $mission) {
  38. if(strpos($text, $mission->hashtag) !== false) {
  39. $mission_id = $mission->id;
  40. }
  41. }
  42. $tweet = new Tweet;
  43. $tweet->player_id = $player ? $player->id : 0;
  44. $tweet->team_id = $player ? $player->team->id : 0;
  45. $tweet->text = $text;
  46. $tweet->photo = json_encode($photos);
  47. $tweet->mission_id = $mission_id;
  48. $tweet->tweet_date = date('Y-m-d H:i:s', strtotime($data['created_at']));
  49. $tweet->save();
  50. event(new NewTweetEvent($tweet));
  51. return $data['id_str'];
  52. }
  53. }