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.

76 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;
  7. class TwitterController extends BaseController
  8. {
  9. public function input(Request $request)
  10. {
  11. $data = $request->all();
  12. // Find the user who tweeted this
  13. $twitter_user_id = $data['user']['id_str'];
  14. $player = Player::where('twitter_user_id', $twitter_user_id)->first();
  15. if(isset($data['extended_tweet']['full_text']))
  16. $text = $data['extended_tweet']['full_text'];
  17. else
  18. $text = $data['text'];
  19. // Unshorten URLs
  20. if(isset($data['entities']['urls'])) {
  21. foreach($data['entities']['urls'] as $url) {
  22. $text = str_replace($url['url'], $url['expanded_url'], $text);
  23. }
  24. }
  25. // Remove media URLs from tweet text
  26. $photos = [];
  27. if(isset($data['extended_entities']['media'])) {
  28. foreach($data['extended_entities']['media'] as $media) {
  29. $text = str_replace($media['url'], '', $text);
  30. $photos[] = $media['media_url_https'];
  31. }
  32. $text = trim($text);
  33. }
  34. // Find the mission hashtag
  35. if(strpos($text, '#transitspy') !== false)
  36. $mission = 1;
  37. elseif(strpos($text, '#intercept') !== false)
  38. $mission = 2;
  39. elseif(strpos($text, '#airlair') !== false)
  40. $mission = 3;
  41. elseif(strpos($text, '#transittea') !== false)
  42. $mission = 4;
  43. elseif(strpos($text, '#sing') !== false)
  44. $mission = 5;
  45. elseif(strpos($text, '#passport') !== false)
  46. $mission = 6;
  47. elseif(strpos($text, '#document') !== false)
  48. $mission = 7;
  49. else
  50. $mission = 0;
  51. $tweet = new Tweet;
  52. $tweet->player_id = $player ? $player->id : 0;
  53. $tweet->team_id = $player ? $player->team->id : 0;
  54. $tweet->text = $text;
  55. $tweet->photo = json_encode($photos);
  56. $tweet->mission = $mission;
  57. $tweet->tweet_date = date('Y-m-d H:i:s', strtotime($data['created_at']));
  58. $tweet->save();
  59. // TODO: Broadcast this to the web interface
  60. return $data['id_str'];
  61. }
  62. }