|
|
- <?php
- namespace App\Http\Controllers;
-
- use Illuminate\Http\Request;
- use Illuminate\Routing\Controller as BaseController;
- use Log;
- use App\Player, App\Team, App\Tweet, App\TransitCenter, App\TransitLine, App\Document, App\Mission;
- use App\Events\NewTweetEvent;
- use Twitter;
-
- class ImportController extends BaseController
- {
- public function __construct() {
- $this->middleware('auth');
- }
-
- public function index(Request $request) {
- return view('import', []);
- }
-
- public function preview(Request $request) {
- $url = $request->input('url');
- if(preg_match('/twitter\.com\/.+\/status(?:es)?\/(\d+)/', $url, $match)) {
- $tweetID = $match[1];
- } else {
- return response()->json(['error'=>'Invalid URL']);
- }
-
- try {
- $data = Twitter::getTweet($tweetID);
- } catch(\Exception $e) {
- return response()->json(['error'=>'Could not fetch tweet']);
- }
-
- $player = Player::where('twitter_user_id', $data->user->id_str)->first();
-
- if(!$player) {
- return response()->json(['error'=>'Couldn\'t find the player for this tweet. Check that they are added to a team.']);
- }
-
- $tweet = self::buildTweetFromTwitterObject($player, $data);
- $event = new NewTweetEvent($tweet);
-
- unset($event->socket);
-
- return response()->json($event);
- }
-
- public function save(Request $request) {
- $tweetID = $request->input('tweet_id');
-
- try {
- $data = Twitter::getTweet($tweetID);
- } catch(\Exception $e) {
- return response()->json(['error'=>'Could not fetch tweet']);
- }
-
- $player = Player::where('twitter_user_id', $data->user->id_str)->first();
-
- if(!$player) {
- return response()->json(['error'=>'Couldn\'t find the player for this tweet. Check that they are added to a team.']);
- }
-
- $tweet = self::buildTweetFromTwitterObject($player, $data);
- $tweet->save();
-
- if($tweet->mission_id && $tweet->team_id) {
- event(new NewTweetEvent($tweet));
- }
-
- return response()->json(['result'=>'ok']);
- }
-
- private static function buildTweetFromTwitterObject($player, $data) {
-
- if(isset($data->extended_tweet->full_text))
- $text = $data->extended_tweet->full_text;
- else
- $text = $data->text;
-
- // Unshorten URLs
- if(isset($data->entities->urls)) {
- foreach($data->entities->urls as $url) {
- $text = str_replace($url->url, $url->expanded_url, $text);
- }
- }
-
- // Remove media URLs from tweet text
- $photos = [];
- if(isset($data->extended_entities->media)) {
- foreach($data->extended_entities->media as $media) {
- $text = str_replace($media->url, '', $text);
- $photos[] = $media->media_url_https;
- }
- $text = trim($text);
- }
-
- $mission_id = 0;
- foreach(Mission::all() as $mission) {
- if(strpos($text, $mission->hashtag) !== false) {
- $mission_id = $mission->id;
- }
- }
-
- $tweet = new Tweet;
- $tweet->tweet_id = $data->id_str;
- $tweet->player_id = $player ? $player->id : 0;
- $tweet->team_id = $player ? $player->team->id : 0;
- $tweet->text = $text;
- $tweet->photo = json_encode($photos, JSON_UNESCAPED_SLASHES);
- $tweet->mission_id = $mission_id;
- $tweet->tweet_date = date('Y-m-d H:i:s', strtotime($data->created_at));
- return $tweet;
- }
-
- }
|