Browse Source

update to support multiple photos

uses the video's poster frame as the photo if any of the multi-post images are videos
pull/49/head v1.3.1
Aaron Parecki 7 years ago
parent
commit
a37ed3bbae
No known key found for this signature in database GPG Key ID: 276C2817346D6056
6 changed files with 415 additions and 12 deletions
  1. +26
    -12
      lib/XRay/Formats/Instagram.php
  2. +33
    -0
      tests/InstagramTest.php
  3. +7
    -0
      tests/data/www.instagram.com/microformats_?__a=1
  4. +171
    -0
      tests/data/www.instagram.com/photos_and_video.html
  5. +7
    -0
      tests/data/www.instagram.com/pk_spam_?__a=1
  6. +171
    -0
      tests/data/www.instagram.com/two_photos.html

+ 26
- 12
lib/XRay/Formats/Instagram.php View File

@ -67,22 +67,36 @@ class Instagram extends Format {
];
}
$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'] = [];
@ -97,7 +111,7 @@ class Instagram extends Format {
}
}
// 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'] = [];

+ 33
- 0
tests/InstagramTest.php View File

@ -95,4 +95,37 @@ class InstagramTest extends PHPUnit_Framework_TestCase {
$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
- 0
tests/data/www.instagram.com/microformats_?__a=1 View File

@ -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
- 0
tests/data/www.instagram.com/photos_and_video.html
File diff suppressed because it is too large
View File


+ 7
- 0
tests/data/www.instagram.com/pk_spam_?__a=1
File diff suppressed because it is too large
View File


+ 171
- 0
tests/data/www.instagram.com/two_photos.html
File diff suppressed because it is too large
View File


Loading…
Cancel
Save