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.

74 lines
2.1 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->tweet_id = $data['id_str'];
  44. $tweet->player_id = $player ? $player->id : 0;
  45. $tweet->team_id = $player ? $player->team->id : 0;
  46. $tweet->text = $text;
  47. $tweet->photo = json_encode($photos, JSON_UNESCAPED_SLASHES);
  48. $tweet->mission_id = $mission_id;
  49. $tweet->tweet_date = date('Y-m-d H:i:s', strtotime($data['created_at']));
  50. if(isset($data['geo']))
  51. $tweet->geo = json_encode($data['geo']);
  52. if(isset($data['place']))
  53. $tweet->place = json_encode($data['place']);
  54. $tweet->save();
  55. if($tweet->mission_id && $tweet->team_id) {
  56. event(new NewTweetEvent($tweet));
  57. }
  58. return $data['id_str'];
  59. }
  60. }