Skip to content

Commit

Permalink
implement title and displayName for twitter
Browse files Browse the repository at this point in the history
  • Loading branch information
snarfed committed Feb 3, 2014
1 parent 3d0bdfb commit bf932cc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
7 changes: 4 additions & 3 deletions source.py
Expand Up @@ -243,8 +243,7 @@ def postprocess_object(self, obj):
content = obj.get('content')
if content and not obj.get('displayName'):
if obj.get('verb') in ('like', 'repost'):
name = obj.get('author', {}).get('displayName') or 'Unknown'
obj['displayName'] = '%s %s' % (name, content)
obj['displayName'] = '%s %s' % (self.actor_name(obj.get('author')), content)
else:
obj['displayName'] = util.ellipsize(content)

Expand Down Expand Up @@ -284,7 +283,9 @@ def original_post_discovery(activity):
@staticmethod
def actor_name(actor):
"""Returns the given actor's name if available, otherwise Unknown."""
return actor.get('displayName', 'Unknown') if actor else 'Unknown'
if actor:
return actor.get('displayName') or actor.get('username') or 'Unknown'
return 'Unknown'

@staticmethod
def is_public(obj):
Expand Down
11 changes: 7 additions & 4 deletions twitter.py
Expand Up @@ -409,7 +409,7 @@ def tweet_to_object(self, tweet):
obj['location']['url'] = ('https://maps.google.com/maps?q=%s,%s' %
tuple(coords))

return util.trim_nulls(obj)
return self.postprocess_object(obj)

def user_to_actor(self, user):
"""Converts a tweet to an activity.
Expand Down Expand Up @@ -458,12 +458,15 @@ def retweet_to_object(self, retweet):
'verb': 'share',
'object': {'url': self.status_url(orig.get('user', {}).get('screen_name'),
orig.get('id_str'))},
# postprocess_object() populates displayName based on content, but we
# want to override it to omit the link.
'displayName': '%s retweeted this.' % self.actor_name(share.get('author')),
'content': content,
})
if 'tags' in share:
# the existing tags apply to the original tweet's text, which we replaced
del share['tags']
return share
return self.postprocess_object(share)

def streaming_event_to_object(self, event):
"""Converts a Streaming API event to an object.
Expand All @@ -484,7 +487,7 @@ def streaming_event_to_object(self, event):
tweet_id = tweet.get('id_str')
id = self.tag_uri('%s_favorited_by_%s' % (tweet_id, source.get('id_str')))
url = self.status_url(event.get('target').get('screen_name'), tweet_id)
return {
return self.postprocess_object({
'id': id,
'url': url,
'objectType': 'activity',
Expand All @@ -493,7 +496,7 @@ def streaming_event_to_object(self, event):
'author': self.user_to_actor(source),
'content': 'favorited this.',
'published': self.rfc2822_to_iso8601(event.get('created_at')),
}
})

@staticmethod
def rfc2822_to_iso8601(time_str):
Expand Down
11 changes: 9 additions & 2 deletions twitter_test.py
Expand Up @@ -103,6 +103,7 @@ def tag_uri(name):
OBJECT = { # ActivityStreams
'objectType': 'note',
'author': ACTOR,
'displayName': '@twitter meets @seepicturely at #tcdisrupt <3 first instagr.am/p/MuW67 [picture]',
'content': '@twitter meets @seepicturely at #tcdisrupt <3 first instagr.am/p/MuW67 [picture]',
'id': tag_uri('100'),
'published': '2012-02-22T20:26:41',
Expand Down Expand Up @@ -171,7 +172,7 @@ def tag_uri(name):
'url': 'http://twitter.com/snarfed_org/status/100',
'actor': ACTOR,
'object': OBJECT,
'title': 'Ryan Barrett: @twitter meets @seepicturely at #tcdisrupt <3 first instagr.am/p/MuW67 [picture]',
'title': '@twitter meets @seepicturely at #tcdisrupt <3 first instagr.am/p/MuW67 [picture]',
'generator': {'displayName': 'Choqok', 'url': 'http://choqok.gnufolks.org/'},
'context': {
'inReplyTo' : [{
Expand Down Expand Up @@ -221,6 +222,7 @@ def tag_uri(name):
'username': 'alice',
'url': 'http://twitter.com/alice',
},
'displayName': 'reply 200',
'content': 'reply 200',
'url': 'http://twitter.com/alice/status/200',
}, {
Expand All @@ -231,6 +233,7 @@ def tag_uri(name):
'username': 'bob',
'url': 'http://twitter.com/bob',
},
'displayName': 'reply 300',
'content': 'reply 300',
'url': 'http://twitter.com/bob/status/300',
}, {
Expand All @@ -241,6 +244,7 @@ def tag_uri(name):
'username': 'snarfed_org',
'url': 'http://twitter.com/snarfed_org',
},
'displayName': 'reply 400',
'content': 'reply 400',
'url': 'http://twitter.com/snarfed_org/status/400',
}, {
Expand All @@ -251,6 +255,7 @@ def tag_uri(name):
'username': 'alice',
'url': 'http://twitter.com/alice',
},
'displayName': 'reply 500',
'content': 'reply 500',
'url': 'http://twitter.com/alice/status/500',
}],
Expand Down Expand Up @@ -310,6 +315,7 @@ def tag_uri(name):
'url': 'http://twitter.com/alizz',
'image': {'url': 'http://alice/picture'},
},
'displayName': 'Alice retweeted this.',
'content': '<a href="http://twitter.com/alizz/status/123">retweeted this.</a>',
'published': '2013-02-24T20:26:41',
}, {
Expand All @@ -325,6 +331,7 @@ def tag_uri(name):
'url': 'http://twitter.com/bobbb',
'image': {'url': 'http://bob/picture'},
},
'displayName': 'Bob retweeted this.',
'content': '<a href="http://twitter.com/bobbb/status/456">retweeted this.</a>',
'published': '2013-02-26T20:26:41',
}]
Expand Down Expand Up @@ -353,6 +360,7 @@ def tag_uri(name):
'username': 'eve',
'url': 'http://twitter.com/eve',
},
'displayName': 'eve favorited this.',
'content': 'favorited this.',
'published': '2013-12-27T17:25:55',
}
Expand Down Expand Up @@ -501,7 +509,6 @@ def test_get_activities_start_index_count(self):
tweet2['user']['name'] = 'foo'
activity2 = copy.deepcopy(ACTIVITY)
activity2['actor']['displayName'] = 'foo'
activity2['title'] = activity2['title'].replace('Ryan Barrett: ', 'foo: ')

self.expect_urlopen(
'https://api.twitter.com/1.1/statuses/home_timeline.json?'
Expand Down

0 comments on commit bf932cc

Please sign in to comment.