From 4a4bc73f5e3cc03b7bc57c37f09d64718d9c59b8 Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Wed, 1 Feb 2017 08:06:24 -0800 Subject: [PATCH] don't include the RT'd photo or video in the main entry they are part of the reposted object instead. closes #27 --- lib/Formats/Twitter.php | 72 ++-- tests/TwitterTest.php | 14 + .../api.twitter.com/820039442773798912.json | 393 ++++++++++++++++++ 3 files changed, 445 insertions(+), 34 deletions(-) create mode 100644 tests/data/api.twitter.com/820039442773798912.json diff --git a/lib/Formats/Twitter.php b/lib/Formats/Twitter.php index fb6a364..2c49900 100644 --- a/lib/Formats/Twitter.php +++ b/lib/Formats/Twitter.php @@ -93,45 +93,49 @@ class Twitter { } } - // Photos and Videos - if(property_exists($tweet, 'extended_entities') && property_exists($tweet->extended_entities, 'media')) { - foreach($tweet->extended_entities->media as $media) { - if($media->type == 'photo') { - if(!array_key_exists('photo', $entry)) - $entry['photo'] = []; - - $entry['photo'][] = $media->media_url_https; - - } elseif($media->type == 'video') { - if(!array_key_exists('video', $entry)) - $entry['video'] = []; - - // Find the highest bitrate video that is mp4 - $videos = $media->video_info->variants; - $videos = array_filter($videos, function($v) { - return property_exists($v, 'bitrate') && $v->content_type == 'video/mp4'; - }); - if(count($videos)) { - usort($videos, function($a,$b) { - return $a->bitrate < $b->bitrate; + // Don't include the RT'd photo or video in the main object. + // They get included in the reposted object instead. + if(!property_exists($tweet, 'retweeted_status')) { + // Photos and Videos + if(property_exists($tweet, 'extended_entities') && property_exists($tweet->extended_entities, 'media')) { + foreach($tweet->extended_entities->media as $media) { + if($media->type == 'photo') { + if(!array_key_exists('photo', $entry)) + $entry['photo'] = []; + + $entry['photo'][] = $media->media_url_https; + + } elseif($media->type == 'video') { + if(!array_key_exists('video', $entry)) + $entry['video'] = []; + + // Find the highest bitrate video that is mp4 + $videos = $media->video_info->variants; + $videos = array_filter($videos, function($v) { + return property_exists($v, 'bitrate') && $v->content_type == 'video/mp4'; }); - $entry['video'][] = $videos[0]->url; + if(count($videos)) { + usort($videos, function($a,$b) { + return $a->bitrate < $b->bitrate; + }); + $entry['video'][] = $videos[0]->url; + } } } } - } - // Place - if(property_exists($tweet, 'place') && $tweet->place) { - $place = $tweet->place; - if($place->place_type == 'city') { - $entry['location'] = $place->url; - $refs[$place->url] = [ - 'type' => 'adr', - 'name' => $place->full_name, - 'locality' => $place->name, - 'country-name' => $place->country, - ]; + // Place + if(property_exists($tweet, 'place') && $tweet->place) { + $place = $tweet->place; + if($place->place_type == 'city') { + $entry['location'] = $place->url; + $refs[$place->url] = [ + 'type' => 'adr', + 'name' => $place->full_name, + 'locality' => $place->name, + 'country-name' => $place->country, + ]; + } } } diff --git a/tests/TwitterTest.php b/tests/TwitterTest.php index 91decbf..a766788 100644 --- a/tests/TwitterTest.php +++ b/tests/TwitterTest.php @@ -148,6 +148,20 @@ class TwitterTest extends PHPUnit_Framework_TestCase { $this->assertEquals('Yeah that\'s me http://xkcd.com/1782/', $tweet['content']['text']); } + public function testRetweetWithPhoto() { + list($url, $json) = $this->loadTweet('820039442773798912'); + + $data = $this->parse(['url' => $url, 'json' => $json]); + + $this->assertEquals('entry', $data['data']['type']); + $this->assertArrayNotHasKey('content', $data['data']); + $this->assertArrayNotHasKey('photo', $data['data']); + $repostOf = 'https://twitter.com/phlaimeaux/status/819943954724556800'; + $this->assertEquals($repostOf, $data['data']['repost-of']); + $tweet = $data['refs'][$repostOf]; + $this->assertEquals('this headline is such a rollercoaster', $tweet['content']['text']); + } + public function testQuotedTweet() { list($url, $json) = $this->loadTweet('818913488609251331'); diff --git a/tests/data/api.twitter.com/820039442773798912.json b/tests/data/api.twitter.com/820039442773798912.json new file mode 100644 index 0000000..6b749b1 --- /dev/null +++ b/tests/data/api.twitter.com/820039442773798912.json @@ -0,0 +1,393 @@ +{ + "created_at": "Fri Jan 13 22:46:53 +0000 2017", + "id": 820039442773798912, + "id_str": "820039442773798912", + "full_text": "RT @phlaimeaux: this headline is such a rollercoaster https://t.co/YX4liS3uK8", + "truncated": false, + "display_text_range": [ + 0, + 77 + ], + "entities": { + "hashtags": [ + + ], + "symbols": [ + + ], + "user_mentions": [ + { + "screen_name": "phlaimeaux", + "name": "David O'Doherty", + "id": 22560570, + "id_str": "22560570", + "indices": [ + 3, + 14 + ] + } + ], + "urls": [ + + ], + "media": [ + { + "id": 819943761371418624, + "id_str": "819943761371418624", + "indices": [ + 54, + 77 + ], + "media_url": "http://pbs.twimg.com/media/C2EGgbhXUAAC54v.jpg", + "media_url_https": "https://pbs.twimg.com/media/C2EGgbhXUAAC54v.jpg", + "url": "https://t.co/YX4liS3uK8", + "display_url": "pic.twitter.com/YX4liS3uK8", + "expanded_url": "https://twitter.com/phlaimeaux/status/819943954724556800/photo/1", + "type": "photo", + "sizes": { + "large": { + "w": 622, + "h": 158, + "resize": "fit" + }, + "thumb": { + "w": 150, + "h": 150, + "resize": "crop" + }, + "small": { + "w": 622, + "h": 158, + "resize": "fit" + }, + "medium": { + "w": 622, + "h": 158, + "resize": "fit" + } + }, + "source_status_id": 819943954724556800, + "source_status_id_str": "819943954724556800", + "source_user_id": 22560570, + "source_user_id_str": "22560570" + } + ] + }, + "extended_entities": { + "media": [ + { + "id": 819943761371418624, + "id_str": "819943761371418624", + "indices": [ + 54, + 77 + ], + "media_url": "http://pbs.twimg.com/media/C2EGgbhXUAAC54v.jpg", + "media_url_https": "https://pbs.twimg.com/media/C2EGgbhXUAAC54v.jpg", + "url": "https://t.co/YX4liS3uK8", + "display_url": "pic.twitter.com/YX4liS3uK8", + "expanded_url": "https://twitter.com/phlaimeaux/status/819943954724556800/photo/1", + "type": "photo", + "sizes": { + "large": { + "w": 622, + "h": 158, + "resize": "fit" + }, + "thumb": { + "w": 150, + "h": 150, + "resize": "crop" + }, + "small": { + "w": 622, + "h": 158, + "resize": "fit" + }, + "medium": { + "w": 622, + "h": 158, + "resize": "fit" + } + }, + "source_status_id": 819943954724556800, + "source_status_id_str": "819943954724556800", + "source_user_id": 22560570, + "source_user_id_str": "22560570" + } + ] + }, + "source": "Twitter Web Client", + "in_reply_to_status_id": null, + "in_reply_to_status_id_str": null, + "in_reply_to_user_id": null, + "in_reply_to_user_id_str": null, + "in_reply_to_screen_name": null, + "user": { + "id": 208111887, + "id_str": "208111887", + "name": "Auke Hulst", + "screen_name": "aukehulst", + "location": "Amsterdam", + "description": "Schrijver van het Ruige Land | Muzikant bij o.a. @de_meisjes | Wijsneus @nrc | En ik herinner me Titus Broederland (**** VK / NRC / De Standaard / DvhN / AD).", + "url": "http://t.co/51ffnkz9u9", + "entities": { + "url": { + "urls": [ + { + "url": "http://t.co/51ffnkz9u9", + "expanded_url": "http://www.aukehulst.nl", + "display_url": "aukehulst.nl", + "indices": [ + 0, + 22 + ] + } + ] + }, + "description": { + "urls": [ + + ] + } + }, + "protected": false, + "followers_count": 2243, + "friends_count": 457, + "listed_count": 48, + "created_at": "Tue Oct 26 17:12:32 +0000 2010", + "favourites_count": 782, + "utc_offset": null, + "time_zone": null, + "geo_enabled": false, + "verified": false, + "statuses_count": 17419, + "lang": "en", + "contributors_enabled": false, + "is_translator": false, + "is_translation_enabled": false, + "profile_background_color": "FFF04D", + "profile_background_image_url": "http://pbs.twimg.com/profile_background_images/513237743950786560/aZFYSiNy.jpeg", + "profile_background_image_url_https": "https://pbs.twimg.com/profile_background_images/513237743950786560/aZFYSiNy.jpeg", + "profile_background_tile": true, + "profile_image_url": "http://pbs.twimg.com/profile_images/771698996658180096/a_RhSq5T_normal.jpg", + "profile_image_url_https": "https://pbs.twimg.com/profile_images/771698996658180096/a_RhSq5T_normal.jpg", + "profile_banner_url": "https://pbs.twimg.com/profile_banners/208111887/1440355860", + "profile_link_color": "0099CC", + "profile_sidebar_border_color": "FFFFFF", + "profile_sidebar_fill_color": "F6FFD1", + "profile_text_color": "333333", + "profile_use_background_image": true, + "has_extended_profile": false, + "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, + "retweeted_status": { + "created_at": "Fri Jan 13 16:27:27 +0000 2017", + "id": 819943954724556800, + "id_str": "819943954724556800", + "full_text": "this headline is such a rollercoaster https://t.co/YX4liS3uK8", + "truncated": false, + "display_text_range": [ + 0, + 37 + ], + "entities": { + "hashtags": [ + + ], + "symbols": [ + + ], + "user_mentions": [ + + ], + "urls": [ + + ], + "media": [ + { + "id": 819943761371418624, + "id_str": "819943761371418624", + "indices": [ + 38, + 61 + ], + "media_url": "http://pbs.twimg.com/media/C2EGgbhXUAAC54v.jpg", + "media_url_https": "https://pbs.twimg.com/media/C2EGgbhXUAAC54v.jpg", + "url": "https://t.co/YX4liS3uK8", + "display_url": "pic.twitter.com/YX4liS3uK8", + "expanded_url": "https://twitter.com/phlaimeaux/status/819943954724556800/photo/1", + "type": "photo", + "sizes": { + "large": { + "w": 622, + "h": 158, + "resize": "fit" + }, + "thumb": { + "w": 150, + "h": 150, + "resize": "crop" + }, + "small": { + "w": 622, + "h": 158, + "resize": "fit" + }, + "medium": { + "w": 622, + "h": 158, + "resize": "fit" + } + } + } + ] + }, + "extended_entities": { + "media": [ + { + "id": 819943761371418624, + "id_str": "819943761371418624", + "indices": [ + 38, + 61 + ], + "media_url": "http://pbs.twimg.com/media/C2EGgbhXUAAC54v.jpg", + "media_url_https": "https://pbs.twimg.com/media/C2EGgbhXUAAC54v.jpg", + "url": "https://t.co/YX4liS3uK8", + "display_url": "pic.twitter.com/YX4liS3uK8", + "expanded_url": "https://twitter.com/phlaimeaux/status/819943954724556800/photo/1", + "type": "photo", + "sizes": { + "large": { + "w": 622, + "h": 158, + "resize": "fit" + }, + "thumb": { + "w": 150, + "h": 150, + "resize": "crop" + }, + "small": { + "w": 622, + "h": 158, + "resize": "fit" + }, + "medium": { + "w": 622, + "h": 158, + "resize": "fit" + } + } + } + ] + }, + "source": "Twitter Web Client", + "in_reply_to_status_id": null, + "in_reply_to_status_id_str": null, + "in_reply_to_user_id": null, + "in_reply_to_user_id_str": null, + "in_reply_to_screen_name": null, + "user": { + "id": 22560570, + "id_str": "22560570", + "name": "David O'Doherty", + "screen_name": "phlaimeaux", + "location": "", + "description": "my recorded comedy works are available at https://t.co/sBJTcq49up", + "url": "http://t.co/uYxxjGy2Qc", + "entities": { + "url": { + "urls": [ + { + "url": "http://t.co/uYxxjGy2Qc", + "expanded_url": "http://www.davidodoherty.com", + "display_url": "davidodoherty.com", + "indices": [ + 0, + 22 + ] + } + ] + }, + "description": { + "urls": [ + { + "url": "https://t.co/sBJTcq49up", + "expanded_url": "http://davidodoherty.bandcamp.com", + "display_url": "davidodoherty.bandcamp.com", + "indices": [ + 42, + 65 + ] + } + ] + } + }, + "protected": false, + "followers_count": 142131, + "friends_count": 666, + "listed_count": 991, + "created_at": "Tue Mar 03 00:43:57 +0000 2009", + "favourites_count": 3091, + "utc_offset": 0, + "time_zone": "Dublin", + "geo_enabled": false, + "verified": true, + "statuses_count": 20323, + "lang": "en", + "contributors_enabled": false, + "is_translator": false, + "is_translation_enabled": false, + "profile_background_color": "C0DEED", + "profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png", + "profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme1/bg.png", + "profile_background_tile": false, + "profile_image_url": "http://pbs.twimg.com/profile_images/378800000664498083/f698d88b299fb50e3a97aabcd962c217_normal.jpeg", + "profile_image_url_https": "https://pbs.twimg.com/profile_images/378800000664498083/f698d88b299fb50e3a97aabcd962c217_normal.jpeg", + "profile_banner_url": "https://pbs.twimg.com/profile_banners/22560570/1469218172", + "profile_link_color": "1DA1F2", + "profile_sidebar_border_color": "C0DEED", + "profile_sidebar_fill_color": "DDEEF6", + "profile_text_color": "333333", + "profile_use_background_image": true, + "has_extended_profile": true, + "default_profile": true, + "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": 232, + "favorite_count": 518, + "favorited": false, + "retweeted": false, + "possibly_sensitive": false, + "possibly_sensitive_appealable": false, + "lang": "en" + }, + "is_quote_status": false, + "retweet_count": 232, + "favorite_count": 0, + "favorited": false, + "retweeted": false, + "possibly_sensitive": false, + "possibly_sensitive_appealable": false, + "lang": "en" +} \ No newline at end of file