Skip to content

Commit

Permalink
twitter bug fix: handle names with emoji correctly in favorites_html_…
Browse files Browse the repository at this point in the history
…to_likes()

for snarfed/bridgy#697
  • Loading branch information
snarfed committed Sep 8, 2016
1 parent 222e8f3 commit fb3644b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ Changelog
* Twitter:
* Bug fix: ensure like.author.displayName is a plain unicode string so that it
can be pickled normally, e.g. by App Engine's memcache.
* Bug fix: handle names with emoji correctly in favorites_html_to_likes().
* Atom:
* Render full original quoted tweet in retweets of quote tweets.

Expand Down
49 changes: 37 additions & 12 deletions granary/test/test_twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ def tag_uri(name):
},
'published': '2013-12-27T17:25:55+00:00',
}
FAVORITES_HTML = """ # Twitter, from /i/activity/favorited_popup?id=...
FAVORITES_HTML = u""" # Twitter, from /i/activity/favorited_popup?id=...
<ol class="activity-popup-users">
<li class="js-stream-item stream-item stream-item
" data-item-id="353" id="stream-item-user-353" data-item-type="user">
Expand All @@ -418,14 +418,25 @@ def tag_uri(name):
<li class="js-stream-item stream-item stream-item">
<!-- snipped <div class="account"... -->
<div class="content">
<div class="stream-item-header">
<a class="account-group js-user-profile-link" href="/ge" >
<span class="username js-action-profile-name">@jo</span></a>
</div>
<div class="content">
<div class="stream-item-header">
<a class="account-group js-user-profile-link" href="/ge" >
<span class="username js-action-profile-name">@jo</span></a>
</div>
</div>
</li>
</li>
<li class="js-stream-item stream-item stream-item">
<div class="content">
<div class="stream-item-header">
<a class="account-group js-user-profile-link" href="/c_foo" >
<img class="avatar js-action-profile-avatar " src="https://pbs.twimg.com/profile_images/123/abc_normal.jpg" alt="" data-user-id="23238890"/>
<strong class="fullname js-action-profile-name">Charles <span class="Emoji Emoji--forLinks" style="background-image:url('https://abs.twimg.com/emoji/v2/72x72/000.png')" title="Hot beverage" aria-label="Emoji: Hot beverage">&nbsp;</span><span class="visuallyhidden" aria-hidden="true">☕</span> Foo</strong>
<span class="username js-action-profile-name">@c_foo</span>
</a>
</div>
</div>
</li>
</ol>
"""
LIKES_FROM_HTML = [{ # ActivityStreams
Expand All @@ -442,8 +453,8 @@ def tag_uri(name):
'displayName': 'George',
'url': 'https://twitter.com/ge',
'image': {'url': 'https://twimg/353'},
},
}, {
},
}, {
'url': 'https://twitter.com/snarfed_org/status/100',
'objectType': 'activity',
'verb': 'like',
Expand All @@ -454,9 +465,23 @@ def tag_uri(name):
'username': 'jo',
'displayName': 'jo',
'url': 'https://twitter.com/jo',
},
}
]
},
}, {
'id': tag_uri('100_favorited_by_23238890'),
'url': 'https://twitter.com/snarfed_org/status/100#favorited-by-23238890',
'objectType': 'activity',
'verb': 'like',
'object': {'url': 'https://twitter.com/snarfed_org/status/100'},
'author': {
'objectType': 'person',
'id': tag_uri('c_foo'),
'numeric_id': '23238890',
'username': 'c_foo',
'displayName': u'Charles ☕ Foo',
'url': 'https://twitter.com/c_foo',
'image': {'url': u'https://pbs.twimg.com/profile_images/123/abc.jpg'},
},
}]
OBJECT_WITH_LIKES = copy.deepcopy(OBJECT)
OBJECT_WITH_LIKES['tags'] += LIKES_FROM_HTML
ACTIVITY_WITH_LIKES = copy.deepcopy(ACTIVITY)
Expand Down
7 changes: 5 additions & 2 deletions granary/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1288,13 +1288,16 @@ def favorites_html_to_likes(self, tweet, html):
username = username[1:]

img = user.find(class_='js-action-profile-avatar') or {}
fullname = user.find(class_='fullname') or {}
author = {
'id_str': img.get('data-user-id'),
'screen_name': username,
'name': unicode(fullname.string) if fullname else None,
'profile_image_url': img.get('src'),
}

fullname = user.find(class_='fullname')
if fullname:
author['name'] = fullname.get_text(' ', strip=True)

likes.append(self._make_like(tweet, author))

return likes
Expand Down

0 comments on commit fb3644b

Please sign in to comment.