Skip to content

Commit

Permalink
twitter: support reading alt text as well as writing it
Browse files Browse the repository at this point in the history
fixes #183
  • Loading branch information
snarfed committed Feb 2, 2020
1 parent d06dca0 commit b2b1bf7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ Non-breaking changes:
* Instagram:
* Scraping: fetch 50 likes instead of 24. ([snarfed/bridgy#898](https://github.com/snarfed/bridgy/issues/898))
* Scraping bug fix for `get_actor()` with `user_id`.
* Twitter:
* Add [image alt text](https://blog.twitter.com/developer/en_us/a/2016/alt-text-support-for-twitter-cards-and-the-rest-api.html) support to `get_activites()` etc ([#183](https://github.com/snarfed/granary/issues/183)).
* RSS:
* Add `itunes:image`, `itunes:author`, and `itunes:category`.
* Strip HTML from `title` element ([#177](https://github.com/snarfed/granary/issues/177)). [Background.](https://validator.w3.org/feed/docs/warning/ContainsHTML.html)
Expand Down
7 changes: 5 additions & 2 deletions granary/tests/test_twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ def tag_uri(name):
'display_url': 'http://pic.twitter.com/1',
'indices': [83, 102],
'type': 'photo',
}, {
'ext_alt_text': 'the alt text',
}, {
# duplicated in extended_entities; we should de-dupe
'id': 'picture3',
'media_url': 'http://p.twimg.com/picture3',
Expand Down Expand Up @@ -246,6 +247,7 @@ def tag_uri(name):
}, {
'objectType': 'image',
'image': {'url': 'https://p.twimg.com/picture1'},
'displayName': 'the alt text',
}],
}
ACTIVITY = { # ActivityStreams
Expand Down Expand Up @@ -650,7 +652,8 @@ def tag_uri(name):
</p>
<p>
<a class="link" href="https://twitter.com/snarfed_org/status/100">
<img class="u-photo" src="https://p.twimg.com/picture1" alt="" />
<img class="u-photo" src="https://p.twimg.com/picture1" alt="the alt text" />
<span class="name">the alt text</span>
</a>
</p>
<p> <span class="p-location h-card">
Expand Down
20 changes: 12 additions & 8 deletions granary/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,30 @@

from . import source

# common to all API calls that fetch tweets
API_TWEET_PARAMS = '&include_entities=true&tweet_mode=extended&include_ext_alt_text=true'

API_BASE = 'https://api.twitter.com/1.1/'
API_BLOCK_IDS = 'blocks/ids.json?count=5000&stringify_ids=true&cursor=%s'
API_BLOCKS = 'blocks/list.json?skip_status=true&count=5000&cursor=%s'
API_CURRENT_USER = 'account/verify_credentials.json'
API_DELETE_TWEET = 'statuses/destroy.json'
API_DELETE_FAVORITE = 'favorites/destroy.json'
API_FAVORITES = 'favorites/list.json?screen_name=%s&include_entities=true&tweet_mode=extended'
API_LIST_TIMELINE = 'lists/statuses.json?include_entities=true&tweet_mode=extended&count=%(count)d&slug=%(slug)s&owner_screen_name=%(owner_screen_name)s'
API_LOOKUP = 'statuses/lookup.json?id=%s&include_entities=true&tweet_mode=extended'
API_FAVORITES = 'favorites/list.json?screen_name=%s'
API_LIST_TIMELINE = 'lists/statuses.json?count=%(count)d&slug=%(slug)s&owner_screen_name=%(owner_screen_name)s' + API_TWEET_PARAMS
API_LOOKUP = 'statuses/lookup.json?id=%s' + API_TWEET_PARAMS
API_POST_FAVORITE = 'favorites/create.json'
API_POST_MEDIA = 'statuses/update_with_media.json'
API_POST_RETWEET = 'statuses/retweet/%s.json'
API_POST_TWEET = 'statuses/update.json'
API_RETWEETS = 'statuses/retweets.json?id=%s&tweet_mode=extended'
API_SEARCH = 'search/tweets.json?q=%(q)s&include_entities=true&tweet_mode=extended&result_type=recent&count=%(count)d'
API_STATUS = 'statuses/show.json?id=%s&include_entities=true&tweet_mode=extended'
API_TIMELINE = 'statuses/home_timeline.json?include_entities=true&tweet_mode=extended&count=%d'
API_RETWEETS = 'statuses/retweets.json?id=%s' + API_TWEET_PARAMS
API_SEARCH = 'search/tweets.json?q=%(q)s&result_type=recent&count=%(count)d' + API_TWEET_PARAMS
API_STATUS = 'statuses/show.json?id=%s' + API_TWEET_PARAMS
API_TIMELINE = 'statuses/home_timeline.json?count=%d' + API_TWEET_PARAMS
API_UPLOAD_MEDIA = 'https://upload.twitter.com/1.1/media/upload.json'
API_MEDIA_METADATA = 'https://upload.twitter.com/1.1/media/metadata/create.json'
API_USER = 'users/show.json?screen_name=%s'
API_USER_TIMELINE = 'statuses/user_timeline.json?include_entities=true&tweet_mode=extended&count=%(count)d&screen_name=%(screen_name)s'
API_USER_TIMELINE = 'statuses/user_timeline.json?count=%(count)d&screen_name=%(screen_name)s' + API_TWEET_PARAMS
HTML_FAVORITES = 'https://twitter.com/i/activity/favorited_popup?id=%s'

TWEET_URL_RE = re.compile(r'https://twitter\.com/[^/?]+/status(es)?/[^/?]+$')
Expand Down Expand Up @@ -1205,6 +1208,7 @@ def tweet_to_object(self, tweet):
'objectType': types.get(m.get('type')),
'image': {'url': m.get('media_url_https') or m.get('media_url')},
'stream': {'url': self._video_url(m)},
'displayName': m.get('ext_alt_text'),
} for m in media]

first = obj['attachments'][0]
Expand Down

0 comments on commit b2b1bf7

Please sign in to comment.