Merge lp:~robru/friends/no-avatar-downsample into lp:friends

Proposed by Robert Bruce Park
Status: Merged
Merged at revision: 150
Proposed branch: lp:~robru/friends/no-avatar-downsample
Merge into: lp:friends
Diff against target: 187 lines (+38/-16)
6 files modified
friends/protocols/facebook.py (+1/-1)
friends/tests/test_avatars.py (+5/-1)
friends/tests/test_facebook.py (+8/-4)
friends/tests/test_flickr.py (+3/-2)
friends/tests/test_twitter.py (+13/-5)
friends/utils/avatar.py (+8/-3)
To merge this branch: bzr merge lp:~robru/friends/no-avatar-downsample
Reviewer Review Type Date Requested Status
Ken VanDine Needs Fixing
MichaƂ Sawicz Pending
Review via email: mp+150647@code.launchpad.net

Description of the change

Remove avatar downsampling code; always cache the largest possible image.

To post a comment you must log in.
152. By Robert Bruce Park

Keep both original sizes and 100px scaled sizes.

This stores the original size at the traditional URI, and then appends
'.100px' to the filename for the 100px-scaled copy. With test coverage.

153. By Robert Bruce Park

Make test check both images.

Revision history for this message
Ken VanDine (ken-vandine) wrote :

We should also bump up the size of the original, for example with facebook we currently request type=large, which returns a 200x200 avatar. You can specify width and height requested and facebook will fetch the image as large as possible up to the requested size. For example:

This returns a 200x200 avatar:
https://graph.facebook.com/shaverm/picture?type=large

This returns a 256x256 avatar:
https://graph.facebook.com/shaverm/picture?width=840&height=840

For reference:
https://developers.facebook.com/docs/reference/api/using-pictures/

review: Needs Fixing
154. By Robert Bruce Park

Force a larger Facebook avatar.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'friends/protocols/facebook.py'
2--- friends/protocols/facebook.py 2013-02-26 18:44:05 +0000
3+++ friends/protocols/facebook.py 2013-02-27 19:06:21 +0000
4@@ -101,7 +101,7 @@
5 args['sender_id'] = sender_id = from_record.get('id', '')
6 args['url'] = PERMALINK.format(id=sender_id)
7 args['icon_uri'] = Avatar.get_image(
8- API_BASE.format(id=sender_id) + '/picture?type=large')
9+ API_BASE.format(id=sender_id) + '/picture?width=840&height=840')
10 args['sender_nick'] = from_record.get('name', '')
11 args['from_me'] = (sender_id == self._account.user_id)
12
13
14=== modified file 'friends/tests/test_avatars.py'
15--- friends/tests/test_avatars.py 2013-02-05 01:11:35 +0000
16+++ friends/tests/test_avatars.py 2013-02-27 19:06:21 +0000
17@@ -84,7 +84,8 @@
18 self.assertEqual(os.listdir(cache_dir),
19 # hashlib.sha1('http://example.com'
20 # .encode('utf-8')).hexdigest()
21- ['89dce6a446a69d6b9bdc01ac75251e4c322bcdff'])
22+ ['89dce6a446a69d6b9bdc01ac75251e4c322bcdff',
23+ '89dce6a446a69d6b9bdc01ac75251e4c322bcdff.100px'])
24
25 @mock.patch('friends.utils.http.Soup.Message',
26 FakeSoupMessage('friends.tests.data', 'ubuntu.png'))
27@@ -118,6 +119,9 @@
28 path = Avatar.get_image('http://example.com')
29 # The image must have been downloaded at least once.
30 pixbuf = GdkPixbuf.Pixbuf.new_from_file(path)
31+ self.assertEqual(pixbuf.get_height(), 285)
32+ self.assertEqual(pixbuf.get_width(), 285)
33+ pixbuf = GdkPixbuf.Pixbuf.new_from_file(path + '.100px')
34 self.assertEqual(pixbuf.get_height(), 100)
35 self.assertEqual(pixbuf.get_width(), 100)
36 # Confirm that the resulting cache image is actually a PNG.
37
38=== modified file 'friends/tests/test_facebook.py'
39--- friends/tests/test_facebook.py 2013-02-26 19:13:31 +0000
40+++ friends/tests/test_facebook.py 2013-02-27 19:06:21 +0000
41@@ -119,7 +119,8 @@
42 False,
43 '2012-09-26T17:16:00Z',
44 'OK Don...10) Headlong Flight',
45- '',
46+ GLib.get_user_cache_dir() +
47+ '/friends/avatars/b688c8def0455d4a3853d5fcdfaf0708645cfd3e',
48 'https://www.facebook.com/809',
49 0.0,
50 False,
51@@ -138,7 +139,8 @@
52 False,
53 '2012-09-26T17:34:00Z',
54 'Rush takes off to the Great White North',
55- '',
56+ GLib.get_user_cache_dir() +
57+ '/friends/avatars/7d1a70e6998f4a38954e93ca03d689463f71d63b',
58 'https://www.facebook.com/117402931676347',
59 16.0,
60 False,
61@@ -157,7 +159,8 @@
62 False,
63 '2012-09-26T17:49:06Z',
64 'http://www2.gibson.com/Alex-Lifeson-0225-2011.aspx',
65- '',
66+ GLib.get_user_cache_dir() +
67+ '/friends/avatars/7d1a70e6998f4a38954e93ca03d689463f71d63b',
68 'https://www.facebook.com/117402931676347',
69 27.0,
70 False,
71@@ -281,7 +284,8 @@
72 timestamp='2012-11-06T13:49:08Z',
73 sender_id=None,
74 from_me=True,
75- icon_uri='',
76+ icon_uri=GLib.get_user_cache_dir() +
77+ '/friends/avatars/d49d72a384d50adf7c736ba27ca55bfa9fa5782d',
78 message='This is Ubuntu!',
79 message_id='234125',
80 sender=None)
81
82=== modified file 'friends/tests/test_flickr.py'
83--- friends/tests/test_flickr.py 2013-02-19 17:00:41 +0000
84+++ friends/tests/test_flickr.py 2013-02-27 19:06:21 +0000
85@@ -22,7 +22,7 @@
86
87 import unittest
88
89-from gi.repository import Dee
90+from gi.repository import GLib, Dee
91
92 from friends.errors import AuthorizationError, FriendsError
93 from friends.protocols.flickr import Flickr
94@@ -218,7 +218,8 @@
95 False,
96 '',
97 '',
98- '',
99+ GLib.get_user_cache_dir() +
100+ '/friends/avatars/b913501d6face9d13f3006b731a711b596d23099',
101 'http://www.flickr.com/people/789',
102 0.0,
103 False,
104
105=== modified file 'friends/tests/test_twitter.py'
106--- friends/tests/test_twitter.py 2013-02-05 01:11:35 +0000
107+++ friends/tests/test_twitter.py 2013-02-27 19:06:21 +0000
108@@ -23,7 +23,7 @@
109
110 import unittest
111
112-from gi.repository import Dee
113+from gi.repository import GLib, Dee
114 from urllib.error import HTTPError
115
116 from friends.protocols.twitter import RateLimiter, Twitter
117@@ -130,7 +130,9 @@
118 expected = [
119 [[['twitter', '1234', '240558470661799936']],
120 'messages', 'OAuth Dancer', '119476949', 'oauth_dancer', False,
121- '2012-08-28T21:16:23Z', 'just another test', '',
122+ '2012-08-28T21:16:23Z', 'just another test',
123+ GLib.get_user_cache_dir() +
124+ '/friends/avatars/ded4ba3c00583ee511f399d0b2537731ca14c39d',
125 'https://twitter.com/oauth_dancer/status/240558470661799936',
126 0.0, False, '', '', '', '', '', '',
127 ],
128@@ -138,14 +140,18 @@
129 'messages', 'Raffi Krikorian', '8285392', 'raffi', False,
130 '2012-08-28T21:08:15Z', 'lecturing at the "analyzing big data ' +
131 'with twitter" class at @cal with @othman http://t.co/bfj7zkDJ',
132- '', 'https://twitter.com/raffi/status/240556426106372096',
133+ GLib.get_user_cache_dir() +
134+ '/friends/avatars/0219effc03a3049a622476e6e001a4014f33dc31',
135+ 'https://twitter.com/raffi/status/240556426106372096',
136 0.0, False, '', '', '', '', '', '',
137 ],
138 [[['twitter', '1234', '240539141056638977']],
139 'messages', 'Taylor Singletary', '819797', 'episod', False,
140 '2012-08-28T19:59:34Z',
141 'You\'d be right more often if you thought you were wrong.',
142- '', 'https://twitter.com/episod/status/240539141056638977',
143+ GLib.get_user_cache_dir() +
144+ '/friends/avatars/0c829cb2934ad76489be21ee5e103735d9b7b034',
145+ 'https://twitter.com/episod/status/240539141056638977',
146 0.0, False, '', '', '', '', '', '',
147 ],
148 ]
149@@ -176,7 +182,9 @@
150 expected_row = [
151 [['twitter', '1234', '240558470661799936']],
152 'messages', 'OAuth Dancer', '119476949', 'oauth_dancer', True,
153- '2012-08-28T21:16:23Z', 'just another test', '',
154+ '2012-08-28T21:16:23Z', 'just another test',
155+ GLib.get_user_cache_dir() +
156+ '/friends/avatars/ded4ba3c00583ee511f399d0b2537731ca14c39d',
157 'https://twitter.com/oauth_dancer/status/240558470661799936',
158 0.0, False, '', '', '', '', '', '',
159 ]
160
161=== modified file 'friends/utils/avatar.py'
162--- friends/utils/avatar.py 2013-02-20 13:08:27 +0000
163+++ friends/utils/avatar.py 2013-02-27 19:06:21 +0000
164@@ -69,15 +69,20 @@
165 if size == 0:
166 log.debug('Getting: {}'.format(url))
167 image_data = Downloader(url).get_bytes()
168+
169+ # Save original size at canonical URI
170+ with open(local_path, 'wb') as fd:
171+ fd.write(image_data)
172+
173+ # Append '.100px' to filename and scale image there.
174 input_stream = Gio.MemoryInputStream.new_from_data(
175 image_data, None)
176 try:
177 pixbuf = GdkPixbuf.Pixbuf.new_from_stream_at_scale(
178 input_stream, 100, 100, True, None)
179- pixbuf.savev(local_path, 'png', [], [])
180+ pixbuf.savev(local_path + '.100px', 'png', [], [])
181 except GLib.GError:
182- log.error('Failed to save image: {}'.format(url))
183- return ''
184+ log.error('Failed to scale image: {}'.format(url))
185 return local_path
186
187 @staticmethod

Subscribers

People subscribed via source and target branches