Skip to content

Commit

Permalink
twitter create: handle video tweet URLs as well as photo URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
snarfed committed Mar 18, 2017
1 parent d74ce20 commit 16bb9d5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
19 changes: 18 additions & 1 deletion granary/test/test_twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1901,8 +1901,9 @@ def test_create_reply(self):
# replies with leading @-mentions, should be removed
('@you foo', 'http://twitter.com/you/status/100', 'foo', 'foo'),
('@YoU foo', 'http://twitter.com/you/status/100', 'foo', 'foo'),
# photo URL. tests Twitter.base_object()
# photo and video URLs. tests Twitter.base_object()
('foo', 'http://twitter.com/you/status/100/photo/1', 'foo', 'foo'),
('foo', 'http://twitter.com/you/status/100/video/1', 'foo', 'foo'),
# mobile.twitter.com URL. the mobile should be stripped from embed.
('foo', 'http://mobile.twitter.com/you/status/100', 'foo', 'foo'),
)
Expand Down Expand Up @@ -1994,6 +1995,22 @@ def test_create_favorite(self):
preview = self.twitter.preview_create(LIKES_FROM_HTML[0])
self.assertIn("""\
<span class="verb">favorite</span>
<a href="https://twitter.com/snarfed_org/status/100">this tweet</a>:""",
preview.description)

def test_create_favorite_of_video_url(self):
like = copy.deepcopy(LIKES_FROM_HTML[0])
like['object']['url'] = 'https://twitter.com/snarfed_org/status/100/video/1'

self.expect_urlopen(twitter.API_POST_FAVORITE, TWEET, params={'id': 100})
self.mox.ReplayAll()
self.assert_equals({'url': 'https://twitter.com/snarfed_org/status/100',
'type': 'like'},
self.twitter.create(like).content)

preview = self.twitter.preview_create(like)
self.assertIn("""\
<span class="verb">favorite</span>
<a href="https://twitter.com/snarfed_org/status/100">this tweet</a>:""",
preview.description)

Expand Down
5 changes: 3 additions & 2 deletions granary/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -990,8 +990,9 @@ def request():
def base_object(self, obj):
"""Returns the 'base' silo object that an object operates on.
Includes special handling for Twitter photo URLs, e.g.
Includes special handling for Twitter photo and video URLs, e.g.
https://twitter.com/nelson/status/447465082327298048/photo/1
https://twitter.com/nelson/status/447465082327298048/video/1
Args:
obj: ActivityStreams object
Expand All @@ -1006,7 +1007,7 @@ def base_object(self, obj):
try:
parsed = urlparse.urlparse(url)
parts = parsed.path.split('/')
if len(parts) >= 3 and parts[-2] == 'photo':
if len(parts) >= 3 and parts[-2] in ('photo', 'video'):
base_obj['id'] = parts[-3]
parsed = list(parsed)
parsed[2] = '/'.join(parts[:-2])
Expand Down

0 comments on commit 16bb9d5

Please sign in to comment.