From 70f157692615568f6f8e0e6295bb496eab03e7a9 Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Sat, 14 Jul 2018 11:09:18 -0700 Subject: [PATCH] support twitter animated gifs --- lib/XRay/Formats/Twitter.php | 8 +- tests/TwitterTest.php | 12 + .../data/api.twitter.com/tweet-with-gif.json | 206 ++++++++++++++++++ 3 files changed, 225 insertions(+), 1 deletion(-) create mode 100644 tests/data/api.twitter.com/tweet-with-gif.json diff --git a/lib/XRay/Formats/Twitter.php b/lib/XRay/Formats/Twitter.php index f4e0dc8..8d5e27f 100644 --- a/lib/XRay/Formats/Twitter.php +++ b/lib/XRay/Formats/Twitter.php @@ -183,10 +183,16 @@ class Twitter extends Format { $entry['photo'][] = $media->media_url_https; - } elseif($media->type == 'video') { + } elseif($media->type == 'video' || $media->type == 'animated_gif') { + if(!array_key_exists('photo', $entry)) + $entry['photo'] = []; + if(!array_key_exists('video', $entry)) $entry['video'] = []; + // Include the thumbnail + $entry['photo'][] = $media->media_url_https; + // Find the highest bitrate video that is mp4 $videos = $media->video_info->variants; $videos = array_filter($videos, function($v) { diff --git a/tests/TwitterTest.php b/tests/TwitterTest.php index 3f904a9..509aa63 100644 --- a/tests/TwitterTest.php +++ b/tests/TwitterTest.php @@ -117,9 +117,21 @@ class TwitterTest extends PHPUnit_Framework_TestCase { $this->assertEquals('entry', $data['data']['type']); $this->assertEquals('Tweet with a video', $data['data']['content']['text']); + $this->assertEquals('https://pbs.twimg.com/ext_tw_video_thumb/818913089248595970/pr/img/qVoEjF03Y41SKpNt.jpg', $data['data']['photo'][0]); $this->assertEquals('https://video.twimg.com/ext_tw_video/818913089248595970/pr/vid/1280x720/qP-sDx-Q0Hs-ckVv.mp4', $data['data']['video'][0]); } + public function testTweetWithGif() { + list($url, $json) = $this->loadTweet('tweet-with-gif'); + + $data = $this->parse(['url' => $url, 'body' => $json]); + + $this->assertEquals('entry', $data['data']['type']); + $this->assertEquals('Look! A distraction 🐁', $data['data']['content']['text']); + $this->assertEquals('https://video.twimg.com/tweet_video/DiFOUuYV4AAUsgL.mp4', $data['data']['video'][0]); + $this->assertEquals('https://pbs.twimg.com/tweet_video_thumb/DiFOUuYV4AAUsgL.jpg', $data['data']['photo'][0]); + } + public function testTweetWithLocation() { list($url, $json) = $this->loadTweet('818912506496229376'); diff --git a/tests/data/api.twitter.com/tweet-with-gif.json b/tests/data/api.twitter.com/tweet-with-gif.json new file mode 100644 index 0000000..3c6af35 --- /dev/null +++ b/tests/data/api.twitter.com/tweet-with-gif.json @@ -0,0 +1,206 @@ +{ + "created_at": "Sat Jul 14 17:11:30 +0000 2018", + "id": 1018181204384731136, + "id_str": "1018181204384731136", + "full_text": "@SwiftOnSecurity Look! A distraction 🐁 https://t.co/MRXk3g3Gvc", + "truncated": false, + "display_text_range": [ + 17, + 38 + ], + "entities": { + "hashtags": [ + + ], + "symbols": [ + + ], + "user_mentions": [ + { + "screen_name": "SwiftOnSecurity", + "name": "SwiftOnSecurity", + "id": 2436389418, + "id_str": "2436389418", + "indices": [ + 0, + 16 + ] + } + ], + "urls": [ + + ], + "media": [ + { + "id": 1018181108716920832, + "id_str": "1018181108716920832", + "indices": [ + 39, + 62 + ], + "media_url": "http://pbs.twimg.com/tweet_video_thumb/DiFOUuYV4AAUsgL.jpg", + "media_url_https": "https://pbs.twimg.com/tweet_video_thumb/DiFOUuYV4AAUsgL.jpg", + "url": "https://t.co/MRXk3g3Gvc", + "display_url": "pic.twitter.com/MRXk3g3Gvc", + "expanded_url": "https://twitter.com/JenMsft/status/1018181204384731136/photo/1", + "type": "photo", + "sizes": { + "small": { + "w": 500, + "h": 210, + "resize": "fit" + }, + "thumb": { + "w": 150, + "h": 150, + "resize": "crop" + }, + "large": { + "w": 500, + "h": 210, + "resize": "fit" + }, + "medium": { + "w": 500, + "h": 210, + "resize": "fit" + } + } + } + ] + }, + "extended_entities": { + "media": [ + { + "id": 1018181108716920832, + "id_str": "1018181108716920832", + "indices": [ + 39, + 62 + ], + "media_url": "http://pbs.twimg.com/tweet_video_thumb/DiFOUuYV4AAUsgL.jpg", + "media_url_https": "https://pbs.twimg.com/tweet_video_thumb/DiFOUuYV4AAUsgL.jpg", + "url": "https://t.co/MRXk3g3Gvc", + "display_url": "pic.twitter.com/MRXk3g3Gvc", + "expanded_url": "https://twitter.com/JenMsft/status/1018181204384731136/photo/1", + "type": "animated_gif", + "sizes": { + "small": { + "w": 500, + "h": 210, + "resize": "fit" + }, + "thumb": { + "w": 150, + "h": 150, + "resize": "crop" + }, + "large": { + "w": 500, + "h": 210, + "resize": "fit" + }, + "medium": { + "w": 500, + "h": 210, + "resize": "fit" + } + }, + "video_info": { + "aspect_ratio": [ + 50, + 21 + ], + "variants": [ + { + "bitrate": 0, + "content_type": "video/mp4", + "url": "https://video.twimg.com/tweet_video/DiFOUuYV4AAUsgL.mp4" + } + ] + } + } + ] + }, + "source": "Twitter Web Client", + "in_reply_to_status_id": 1018178408398966784, + "in_reply_to_status_id_str": "1018178408398966784", + "in_reply_to_user_id": 2436389418, + "in_reply_to_user_id_str": "2436389418", + "in_reply_to_screen_name": "SwiftOnSecurity", + "user": { + "id": 3309105596, + "id_str": "3309105596", + "name": "Jen Gentleman 🌺", + "screen_name": "JenMsft", + "location": "Seattle, WA", + "description": "Software Engineer, Community Manager & #WindowsInsider on Shell team @ Microsoft. I work w/ Windows feedback 4 Start, Action center, taskbar, Windows Ink + more", + "url": "https://t.co/ACVIR7xvcT", + "entities": { + "url": { + "urls": [ + { + "url": "https://t.co/ACVIR7xvcT", + "expanded_url": "https://www.instagram.com/jenmsft/", + "display_url": "instagram.com/jenmsft/", + "indices": [ + 0, + 23 + ] + } + ] + }, + "description": { + "urls": [ + + ] + } + }, + "protected": false, + "followers_count": 18038, + "friends_count": 224, + "listed_count": 352, + "created_at": "Fri Aug 07 22:21:33 +0000 2015", + "favourites_count": 130904, + "utc_offset": null, + "time_zone": null, + "geo_enabled": false, + "verified": false, + "statuses_count": 33996, + "lang": "en", + "contributors_enabled": false, + "is_translator": false, + "is_translation_enabled": false, + "profile_background_color": "1A1B1F", + "profile_background_image_url": "http://abs.twimg.com/images/themes/theme9/bg.gif", + "profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme9/bg.gif", + "profile_background_tile": false, + "profile_image_url": "http://pbs.twimg.com/profile_images/987773638614048768/_Z7nZCoF_normal.jpg", + "profile_image_url_https": "https://pbs.twimg.com/profile_images/987773638614048768/_Z7nZCoF_normal.jpg", + "profile_banner_url": "https://pbs.twimg.com/profile_banners/3309105596/1524338784", + "profile_link_color": "CB26D1", + "profile_sidebar_border_color": "000000", + "profile_sidebar_fill_color": "000000", + "profile_text_color": "000000", + "profile_use_background_image": true, + "has_extended_profile": true, + "default_profile": false, + "default_profile_image": false, + "following": false, + "follow_request_sent": false, + "notifications": false, + "translator_type": "none" + }, + "geo": null, + "coordinates": null, + "place": null, + "contributors": null, + "is_quote_status": false, + "retweet_count": 25, + "favorite_count": 90, + "favorited": false, + "retweeted": false, + "possibly_sensitive": false, + "possibly_sensitive_appealable": false, + "lang": "en" + }