Skip to content

Commit

Permalink
instagram: handle user-provided alt text
Browse files Browse the repository at this point in the history
  • Loading branch information
snarfed committed Jan 10, 2019
1 parent 28bef75 commit 69d8ec1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
13 changes: 13 additions & 0 deletions granary/instagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@
_last_rate_limited = None # datetime
_last_rate_limited_exc = None # requests.HTTPError

AUTO_ALT_TEXT_PREFIXES = (
'No photo description available.',
'Image may contain: ',
)


class Instagram(source.Source):
"""Instagram source class. See file docstring and Source class for details."""
Expand Down Expand Up @@ -609,6 +614,14 @@ def media_to_object(self, media):
self._mention_tags_from_content(content)
}

# alt text
# https://instagram-press.com/blog/2018/11/28/creating-a-more-accessible-instagram/
alt = media.get('accessibility_caption')
if alt and not any(alt.startswith(prefix) for prefix in AUTO_ALT_TEXT_PREFIXES):
for att in object['attachments']:
for img in att.get('image', []):
img['displayName'] = alt

for version in ('standard_resolution', 'low_resolution', 'thumbnail'):
image = media.get('images', {}).get(version)
if image:
Expand Down
15 changes: 15 additions & 0 deletions granary/tests/test_instagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,7 @@ def tag_uri(name):
'id': '1455954810972087680',
'dimensions': {'height': 1080, 'width': 1080},
'display_url': 'https://instagram.fsnc1-2.fna.fbcdn.net/t51.2885-15/s1080x1080/e35/16906679_776417269184045_871950675452362752_n.jpg',
'accessibility_caption': 'this is my alt text',
'is_video': False,
'edge_media_to_tagged_user': {'edges': []},
},
Expand Down Expand Up @@ -953,6 +954,7 @@ def tag_uri(name):
'url': 'https://www.instagram.com/p/ABC123/',
'image': [{
'url': 'https://instagram.fsnc1-2.fna.fbcdn.net/t51.2885-15/s1080x1080/e35/16906679_776417269184045_871950675452362752_n.jpg',
'displayName': 'this is my alt text',
'width': 1080,
'height': 1080,
}],
Expand Down Expand Up @@ -1623,6 +1625,19 @@ def test_html_to_activities_multi_photo(self):
self.assert_equals([HTML_MULTI_PHOTO_ACTIVITY], activities)
self.assertIsNone(viewer)

def test_html_to_activities_multi_photo_omit_auto_alt_text(self):
multi = copy.deepcopy(HTML_MULTI_PHOTO_PAGE)
multi['entry_data']['PostPage'][0]['graphql']['shortcode_media']\
['edge_sidecar_to_children']['edges'][1]['node']['accessibility_caption'] = \
instagram.AUTO_ALT_TEXT_PREFIXES[1] + 'foo bar'
html = HTML_HEADER + json.dumps(multi) + HTML_FOOTER

expected = copy.deepcopy(HTML_MULTI_PHOTO_ACTIVITY)
del expected['object']['attachments'][1]['image'][0]['displayName']

activities, _ = self.instagram.html_to_activities(html)
self.assert_equals([expected], activities)

def test_html_to_activities_missing_profile_picture_external_url(self):
data = copy.deepcopy(HTML_FEED)
data['config']['viewer']['profile_pic_url'] = None
Expand Down

0 comments on commit 69d8ec1

Please sign in to comment.