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.

121 lines
3.4 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. use Twitter;
  9. class ImportController extends BaseController
  10. {
  11. public function __construct() {
  12. $this->middleware('auth');
  13. }
  14. public function index(Request $request) {
  15. return view('import', []);
  16. }
  17. public function preview(Request $request) {
  18. $url = $request->input('url');
  19. if(preg_match('/twitter\.com\/.+\/status(?:es)?\/(\d+)/', $url, $match)) {
  20. $tweetID = $match[1];
  21. } else {
  22. return response()->json(['error'=>'Invalid URL']);
  23. }
  24. try {
  25. $data = Twitter::getTweet($tweetID);
  26. } catch(\Exception $e) {
  27. return response()->json(['error'=>'Could not fetch tweet']);
  28. }
  29. $player = Player::where('twitter_user_id', $data->user->id_str)->first();
  30. if(!$player) {
  31. return response()->json(['error'=>'Couldn\'t find the player for this tweet. Check that they are added to a team.']);
  32. }
  33. $tweet = self::buildTweetFromTwitterObject($player, $data);
  34. $event = new NewTweetEvent($tweet);
  35. unset($event->socket);
  36. return response()->json($event);
  37. }
  38. public function save(Request $request) {
  39. $tweetID = $request->input('tweet_id');
  40. try {
  41. $data = Twitter::getTweet($tweetID);
  42. } catch(\Exception $e) {
  43. return response()->json(['error'=>'Could not fetch tweet']);
  44. }
  45. $player = Player::where('twitter_user_id', $data->user->id_str)->first();
  46. if(!$player) {
  47. return response()->json(['error'=>'Couldn\'t find the player for this tweet. Check that they are added to a team.']);
  48. }
  49. $tweet = self::buildTweetFromTwitterObject($player, $data);
  50. $tweet->save();
  51. if($tweet->mission_id && $tweet->team_id) {
  52. event(new NewTweetEvent($tweet));
  53. }
  54. return response()->json(['result'=>'ok']);
  55. }
  56. private static function buildTweetFromTwitterObject($player, $data) {
  57. if(isset($data->extended_tweet->full_text))
  58. $text = $data->extended_tweet->full_text;
  59. else
  60. $text = $data->text;
  61. // Unshorten URLs
  62. if(isset($data->entities->urls)) {
  63. foreach($data->entities->urls as $url) {
  64. $text = str_replace($url->url, $url->expanded_url, $text);
  65. }
  66. }
  67. // Remove media URLs from tweet text
  68. $photos = [];
  69. if(isset($data->extended_entities->media)) {
  70. foreach($data->extended_entities->media as $media) {
  71. $text = str_replace($media->url, '', $text);
  72. $photos[] = $media->media_url_https;
  73. }
  74. $text = trim($text);
  75. }
  76. $mission_id = 0;
  77. foreach(Mission::all() as $mission) {
  78. if(strpos($text, $mission->hashtag) !== false) {
  79. $mission_id = $mission->id;
  80. }
  81. }
  82. $tweet = new Tweet;
  83. $tweet->tweet_id = $data->id_str;
  84. $tweet->player_id = $player ? $player->id : 0;
  85. $tweet->team_id = $player ? $player->team->id : 0;
  86. $tweet->text = $text;
  87. $tweet->photo = json_encode($photos, JSON_UNESCAPED_SLASHES);
  88. $tweet->mission_id = $mission_id;
  89. $tweet->tweet_date = date('Y-m-d H:i:s', strtotime($data->created_at));
  90. $tweet->geo = json_encode($data->geo);
  91. $tweet->place = json_encode($data->place);
  92. $tweet->author_username = $data->user->screen_name;
  93. $tweet->author_photo = $data->user->profile_image_url_https;
  94. $tweet->json = json_encode($data);
  95. return $tweet;
  96. }
  97. }