Browse Source

Include alt text from Instagram posts

This adds a new property, "meta", which includes alt text and has room to include additional properties later.

closes #85
pull/87/head
Aaron Parecki 5 years ago
parent
commit
156fd62678
No known key found for this signature in database GPG Key ID: 276C2817346D6056
5 changed files with 682 additions and 0 deletions
  1. +17
    -0
      lib/XRay/Formats/Instagram.php
  2. +31
    -0
      tests/InstagramTest.php
  3. +316
    -0
      tests/data/www.instagram.com/p_BsdjKytBZyx_
  4. +316
    -0
      tests/data/www.instagram.com/p_BsdlOmLh_IX_
  5. +2
    -0
      tests/download-instagram-data.sh

+ 17
- 0
lib/XRay/Formats/Instagram.php View File

@ -128,17 +128,24 @@ class Instagram extends Format {
}
$refs = [];
$meta = [];
// Include the photo/video media URLs
// (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.
// https://github.com/aaronpk/XRay/issues/84
$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.
if(isset($edge['node']['accessibility_caption'])) {
$meta[$edge['node']['display_url']] = [
'alt' => $edge['node']['accessibility_caption']
];
}
}
} else {
@ -149,6 +156,12 @@ class Instagram extends Format {
elseif(array_key_exists('display_url', $photoData))
$entry['photo'] = [$photoData['display_url']];
if(isset($photoData['accessibility_caption']) && $photoData['accessibility_caption']) {
$meta[$entry['photo'][0]] = [
'alt' => $photoData['accessibility_caption']
];
}
if(isset($photoData['is_video']) && $photoData['is_video'] && isset($photoData['video_url'])) {
$entry['video'] = [$photoData['video_url']];
}
@ -200,6 +213,10 @@ class Instagram extends Format {
$entry['refs'] = $refs;
}
if(count($meta)) {
$entry['meta'] = $meta;
}
$entry['post-type'] = \p3k\XRay\PostType::discover($entry);
return [

+ 31
- 0
tests/InstagramTest.php View File

@ -247,4 +247,35 @@ class InstagramTest extends PHPUnit_Framework_TestCase {
$this->assertEquals('https://www.instagram.com/p/BGC8l_ZCMKb/', $data['data']['items'][11]['url']);
}
public function testInstagramPhotoWithAltText() {
$url = 'https://www.instagram.com/p/BsdjKytBZyx/';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body, true);
$this->assertEquals(200, $data['code']);
$this->assertEquals('instagram', $data['source-format']);
$this->assertEquals('Pink text on a white background that says "Photo with alt text"', $data['data']['meta']['https://instagram.fsjc1-3.fna.fbcdn.net/vp/a7e61adf3d84f07863ffdb99f0fdcc86/5CD9B7F3/t51.2885-15/e35/47692478_2276538359047529_8318084305806697090_n.jpg?_nc_ht=instagram.fsjc1-3.fna.fbcdn.net']['alt']);
}
public function testInstagramMultiPhotoWithAltText() {
$url = 'https://www.instagram.com/p/BsdlOmLh_IX/';
$response = $this->parse(['url' => $url]);
$body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body, true);
$this->assertEquals(200, $data['code']);
$this->assertEquals('instagram', $data['source-format']);
$this->assertEquals('A large pink "1" in a circle with a small green "2" behind it', $data['data']['meta']['https://instagram.fsjc1-3.fna.fbcdn.net/vp/90bf019b7396d7bc2b1ee02170902a2e/5CCC9B87/t51.2885-15/e35/47692921_321791688431421_3314633848293773579_n.jpg?_nc_ht=instagram.fsjc1-3.fna.fbcdn.net']['alt']);
$this->assertEquals('A large green "2" in a circle with a small pink "1" behind it', $data['data']['meta']['https://instagram.fsjc1-3.fna.fbcdn.net/vp/a6c93d8fcd5ad0e3b60f2ac0695eb34e/5CC3898E/t51.2885-15/e35/49663055_349750985612151_2949260446582336214_n.jpg?_nc_ht=instagram.fsjc1-3.fna.fbcdn.net']['alt']);
}
}

+ 316
- 0
tests/data/www.instagram.com/p_BsdjKytBZyx_
File diff suppressed because it is too large
View File


+ 316
- 0
tests/data/www.instagram.com/p_BsdlOmLh_IX_
File diff suppressed because it is too large
View File


+ 2
- 0
tests/download-instagram-data.sh View File

@ -11,6 +11,8 @@ urls=(
'https://www.instagram.com/p/BZWmpecjBwN/'
'https://www.instagram.com/explore/locations/109284789535230/'
'https://www.instagram.com/explore/locations/359000003/'
'https://www.instagram.com/p/BsdjKytBZyx/'
'https://www.instagram.com/p/BsdlOmLh_IX/'
)
for url in ${urls[@]}; do

Loading…
Cancel
Save