Skip to content

Commit

Permalink
update to support multiple photos
Browse files Browse the repository at this point in the history
uses the video's poster frame as the photo if any of the multi-post images are videos
  • Loading branch information
aaronpk committed Sep 22, 2017
1 parent 07d1df2 commit a37ed3b
Show file tree
Hide file tree
Showing 6 changed files with 415 additions and 12 deletions.
38 changes: 26 additions & 12 deletions lib/XRay/Formats/Instagram.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,36 @@ public static function parse($http, $html, $url) {
];
}

$refs = [];

// Include the photo/video media URLs
// (Always return arrays)
if(array_key_exists('display_src', $photoData))
$entry['photo'] = [$photoData['display_src']];
elseif(array_key_exists('display_url', $photoData))
$entry['photo'] = [$photoData['display_url']];

if(array_key_exists('is_video', $photoData) && $photoData['is_video']) {
$entry['video'] = [$photoData['video_url']];
}
// (Always return arrays, even for single images)
if(array_key_exists('edge_sidecar_to_children', $photoData)) {
// Multi-post
// For now, we will only pull photos from multi-posts, and skip videos.

$entry['photo'] = [];
foreach($photoData['edge_sidecar_to_children']['edges'] as $edge) {
$entry['photo'][] = $edge['node']['display_url'];
// Don't need to pull person-tags from here because the main parent object already has them.
}

$refs = [];
} else {
// Single photo or video

if(array_key_exists('display_src', $photoData))
$entry['photo'] = [$photoData['display_src']];
elseif(array_key_exists('display_url', $photoData))
$entry['photo'] = [$photoData['display_url']];

if(array_key_exists('is_video', $photoData) && $photoData['is_video']) {
$entry['video'] = [$photoData['video_url']];
}
}

// Find person tags and fetch user profiles

// old json
// old instagram json
if(isset($photoData['usertags']['nodes'])) {
if(!isset($entry['category'])) $entry['category'] = [];

Expand All @@ -97,7 +111,7 @@ public static function parse($http, $html, $url) {
}
}

// new json as of approximately 2017-04-19
// new instagram json as of approximately 2017-04-19
if(isset($photoData['edge_media_to_tagged_user']['edges'])) {
if(!isset($entry['category'])) $entry['category'] = [];

Expand Down
33 changes: 33 additions & 0 deletions tests/InstagramTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,37 @@ public function testInstagramPhotoWithVenue() {
$this->assertEquals('2016-12-10T21:48:56-08:00', $data['data']['published']);
}

public function testTwoPhotos() {
// Original URL: https://www.instagram.com/p/BZWmUB_DVtp/
$url = 'http://www.instagram.com/two_photos.html';
$response = $this->parse(['url' => $url]);

$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body, true);

$this->assertEquals(2, count($data['data']['photo']));
$this->assertEquals('https://instagram.fsea1-1.fna.fbcdn.net/t51.2885-15/e35/21827424_134752690591737_8093088291252862976_n.jpg', $data['data']['photo'][0]);
$this->assertEquals('https://instagram.fsea1-1.fna.fbcdn.net/t51.2885-15/e35/21909774_347707439021016_5237540582556958720_n.jpg', $data['data']['photo'][1]);
$this->assertArrayNotHasKey('video', $data['data']);
$this->assertEquals(2, count($data['data']['category']));
}

public function testMixPhotosAndVideos() {
// Original URL: https://www.instagram.com/p/BZWmpecjBwN/
$url = 'http://www.instagram.com/photos_and_video.html';
$response = $this->parse(['url' => $url]);

$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body, true);

$this->assertEquals(3, count($data['data']['photo']));
$this->assertEquals('https://instagram.fsea1-1.fna.fbcdn.net/t51.2885-15/e35/21878922_686481254874005_8468823712617988096_n.jpg', $data['data']['photo'][0]);
$this->assertEquals('https://instagram.fsea1-1.fna.fbcdn.net/t51.2885-15/e15/21910026_1507234999368159_6974261907783942144_n.jpg', $data['data']['photo'][1]);
$this->assertEquals('https://instagram.fsea1-1.fna.fbcdn.net/t51.2885-15/e35/21878800_273567963151023_7672178549897297920_n.jpg', $data['data']['photo'][2]);
$this->assertArrayNotHasKey('video', $data['data']);
$this->assertEquals(2, count($data['data']['category']));
}

}
7 changes: 7 additions & 0 deletions tests/data/www.instagram.com/microformats_?__a=1
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
HTTP/1.1 200 OK
Server: Apache
Date: Wed, 09 Dec 2015 03:29:14 GMT
Content-Type: application/json
Connection: keep-alive

{"user": {"biography": "http://twitter.com/microformats", "blocked_by_viewer": false, "country_block": false, "external_url": "http://microformats.org/", "external_url_linkshimmed": "http://l.instagram.com/?u=http%3A%2F%2Fmicroformats.org%2F\u0026e=ATN_CrtZkKLdaFphortpLsVRly6f_2zeEV78c4j2uxUlPFrxFrE0no5f4Z5PjcpLJsZ0IQ", "followed_by": {"count": 303}, "followed_by_viewer": false, "follows": {"count": 66}, "follows_viewer": true, "full_name": null, "has_blocked_viewer": false, "has_requested_viewer": false, "id": "8911340", "is_private": false, "is_verified": false, "profile_pic_url": "https://instagram.fsea1-1.fna.fbcdn.net/t51.2885-19/11849954_706937769411730_315174835_a.jpg", "profile_pic_url_hd": "https://instagram.fsea1-1.fna.fbcdn.net/t51.2885-19/11849954_706937769411730_315174835_a.jpg", "requested_by_viewer": false, "username": "microformats", "connected_fb_page": null, "media": {"nodes": [{"__typename": "GraphImage", "id": "207279526", "comments_disabled": false, "dimensions": {"height": 612, "width": 612}, "gating_info": null, "media_preview": null, "owner": {"id": "8911340"}, "thumbnail_src": "https://instagram.fsea1-1.fna.fbcdn.net/t51.2885-15/e15/11184669_651043525029552_626633635_n.jpg", "thumbnail_resources": [{"src": "https://instagram.fsea1-1.fna.fbcdn.net/t51.2885-15/s150x150/e15/11184669_651043525029552_626633635_n.jpg", "config_width": 150, "config_height": 150}, {"src": "https://instagram.fsea1-1.fna.fbcdn.net/t51.2885-15/s240x240/e15/11184669_651043525029552_626633635_n.jpg", "config_width": 240, "config_height": 240}, {"src": "https://instagram.fsea1-1.fna.fbcdn.net/t51.2885-15/s320x320/e15/11184669_651043525029552_626633635_n.jpg", "config_width": 320, "config_height": 320}, {"src": "https://instagram.fsea1-1.fna.fbcdn.net/t51.2885-15/s480x480/e15/11184669_651043525029552_626633635_n.jpg", "config_width": 480, "config_height": 480}], "is_video": false, "code": "MWtWm", "date": 1315334603, "display_src": "https://instagram.fsea1-1.fna.fbcdn.net/t51.2885-15/e15/11184669_651043525029552_626633635_n.jpg", "caption": "Brighton #microformats meetup", "comments": {"count": 4}, "likes": {"count": 17}}], "count": 1, "page_info": {"has_next_page": false, "end_cursor": "AQDxmbcFVBEKns9y1r3onb4coy-unYo21exQcDSdubHFRIrPL3MhxQpRmCFFazPVzKE"}}, "saved_media": {"nodes": [], "count": 0, "page_info": {"has_next_page": false, "end_cursor": null}}}, "logging_page_id": "profilePage_8911340"}
171 changes: 171 additions & 0 deletions tests/data/www.instagram.com/photos_and_video.html

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions tests/data/www.instagram.com/pk_spam_?__a=1

Large diffs are not rendered by default.

Loading

0 comments on commit a37ed3b

Please sign in to comment.