Merge lp:~kai-mast/friends/fix-retweets into lp:friends

Proposed by Kai Mast
Status: Merged
Merged at revision: 247
Proposed branch: lp:~kai-mast/friends/fix-retweets
Merge into: lp:friends
Diff against target: 58 lines (+15/-8)
2 files modified
friends/protocols/twitter.py (+14/-7)
friends/tests/test_twitter.py (+1/-1)
To merge this branch: bzr merge lp:~kai-mast/friends/fix-retweets
Reviewer Review Type Date Requested Status
Robert Bruce Park Approve
Review via email: mp+198019@code.launchpad.net

Description of the change

This branch fixes the truncation of retweets. It also allows to properly support RTs in the future.

Be free to correct my coding style ;)

To post a comment you must log in.
lp:~kai-mast/friends/fix-retweets updated
262. By Kai Mast

Tried to removed whitespace in diff

Revision history for this message
Robert Bruce Park (robru) wrote :

So this mostly looks good, but there's a bad idiom you used here that you also did in your previous MP:

58 + if "retweeted_status" in tweet:
59 + shared_status = tweet.get('retweeted_status')

There's just no need for this kind of "look before you leap" logic in python. At worst, it's a race condition, and at best it's inefficient.

What you really want to do is something more like this:

message = tweet.get('retweeted_status', {}).get('text', '') or tweet.get('text', '')

If you do that, then you also reduce the number of calls to _resolve_tco() to 1, meaning you can inline it (meaning it doesn't have to be it's own method anymore).

Also you have "entities = tweet.get('entities', {})" in two different places, needlessly redundant.

review: Needs Fixing
Revision history for this message
Kai Mast (kai-mast) wrote :

But how would I add "RT"-part in this case? (see line 61 and 80)

Revision history for this message
Robert Bruce Park (robru) wrote :

Still better to just have "shared_status = tweet.get('retweeted_status', {}).get('text', '')" and then you can say "if shared_status: message = 'RT ' + message" or something. Testing a dict before pulling a value from a dict is really bad form in python.

Revision history for this message
Kai Mast (kai-mast) wrote :

Okay. I will do that.

Do you want me to inline the other stuff again? I really like small functions; they make the code more readable in my opinion.

Revision history for this message
Robert Bruce Park (robru) wrote :

Not sure right now... I like small functions too but I dislike functions
that only get called once :)

lp:~kai-mast/friends/fix-retweets updated
263. By Kai Mast

Simplified code as suggested

264. By Kai Mast

Diff cleanups

Revision history for this message
Kai Mast (kai-mast) wrote :

Okay the diff really is a lot smaller now.

I added an if-clause to fetching the media url. I think otherwise me may run into problems when the picture is not the last entity.

Revision history for this message
Kai Mast (kai-mast) wrote :

we*

Revision history for this message
Robert Bruce Park (robru) wrote :

Ok, this is looking good now just from reading it. I need a bit more time to review it fully, hopefully later today.

Revision history for this message
Robert Bruce Park (robru) wrote :

Kai, thanks for this branch. I merged it with some changes, you should review them here:

http://bazaar.launchpad.net/~super-friends/friends/trunk/revision/247

Note how I managed to achieve the same logic without having to do any dictionary membership tests.

review: Approve
Revision history for this message
Kai Mast (kai-mast) wrote :

Okay that really looks nicer. Thanks for merging.

Just wondering, how often is the friends package for trusty rebuilt? Would be nice to benefit from those changes.

Revision history for this message
Robert Bruce Park (robru) wrote :

It gets rebuilt every 4 hours, except that it doesn't, because of reasons.

So basically you have to ask for it. Right now there's an informal 'freeze' on releases because we're trying to get our automated phone-image testing system at 100% pass rate and we don't want to release *anything* that might inadvertently cause regressions. Over the last couple weeks we've gotten it from something like 80% to 98%, so it should be done soon. Hopefully we might be able to do a friends release this week, barring any catastrophic failures.

Revision history for this message
Robert Bruce Park (robru) wrote :

Kai, if you really need to test a newly built package, you can enable ppa:ubuntu-unity/daily-build and install friends from there (but be sure to disable the ppa before upgrading your system, most of the other stuff in that PPA will hose your system).

Revision history for this message
Kai Mast (kai-mast) wrote :

Okay cool. I just took the friends package from the PPA so nothing else should break.

As far as I see friends is not running in an upstart usersession yet. Is that also something I could tackle?

Revision history for this message
Robert Bruce Park (robru) wrote :

You're welcome to try! I haven't been keeping up with that stuff much myself.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'friends/protocols/twitter.py'
2--- friends/protocols/twitter.py 2013-12-05 19:59:31 +0000
3+++ friends/protocols/twitter.py 2013-12-09 19:56:55 +0000
4@@ -117,20 +117,22 @@
5 permalink = self._tweet_permalink.format(
6 user_id=screen_name,
7 tweet_id=tweet_id)
8-
9- message = tweet.get('text', '')
10+
11+ entities = tweet.get('retweeted_status', {}).get('entities', '') or tweet.get('entities', {})
12+ message = tweet.get('retweeted_status', {}).get('text', '') or tweet.get('text', '')
13 picture_url = ''
14-
15- # Resolve t.co links.
16- #TODO support more than one media file
17- entities = tweet.get('entities', {})
18+
19+ #Resolve t.co
20+ #TODO support more than one url and/or media file
21 for url in (entities.get('urls', []) + entities.get('media', [])):
22 begin, end = url.get('indices', (None, None))
23
24 expanded_url = url.get('expanded_url', '')
25 display_url = url.get('display_url', '')
26 other_url = url.get('url', '')
27- picture_url = url.get('media_url', '')
28+
29+ if 'media_url' in url:
30+ picture_url = url.get('media_url')
31
32 # Friends has no notion of display URLs, so this is handled at the protocol level
33 if None not in (begin, end):
34@@ -143,6 +145,11 @@
35 '</a>',
36 message[end:]])
37
38+ #Friends has no native support for retweets so we just encode it in the message
39+ if "retweeted_status" in tweet:
40+ other_user = tweet.get('retweeted_status').get('user', {})
41+ message = "RT @" + other_user.get('screen_name', '') + ": " + message
42+
43 self._publish(
44 message_id=tweet_id,
45 message=message,
46
47=== modified file 'friends/tests/test_twitter.py'
48--- friends/tests/test_twitter.py 2013-12-05 12:26:21 +0000
49+++ friends/tests/test_twitter.py 2013-12-09 19:56:55 +0000
50@@ -470,7 +470,7 @@
51 'messages', 'Robert Bruce', '836242932', 'therealrobru', True,
52 '2013-04-16T17:58:26Z', 'RT @tarek_ziade: Just found a "Notification '
53 'of Inspection" card in the bottom of my bag. looks like they were '
54- 'curious about those raspbe ...',
55+ 'curious about those raspberry-pi :O',
56 'https://si0.twimg.com/profile_images/2631306428/'
57 '2a509db8a05b4310394b832d34a137a4.png',
58 'https://twitter.com/therealrobru/status/324220250889543682',

Subscribers

People subscribed via source and target branches

to all changes: