Merge lp:~facundo/ubuntu-rest-scopes/fix-openlibrary-nodocs into lp:ubuntu-rest-scopes

Proposed by Facundo Batista
Status: Merged
Approved by: Facundo Batista
Approved revision: 518
Merged at revision: 519
Proposed branch: lp:~facundo/ubuntu-rest-scopes/fix-openlibrary-nodocs
Merge into: lp:ubuntu-rest-scopes
Diff against target: 91 lines (+23/-7)
3 files modified
src/scopes/openlibrary.py (+9/-7)
src/scopes/tests/fixtures/openlibrary-search-nodocs.json (+6/-0)
src/scopes/tests/test_openlibrary.py (+8/-0)
To merge this branch: bzr merge lp:~facundo/ubuntu-rest-scopes/fix-openlibrary-nodocs
Reviewer Review Type Date Requested Status
Bret Barker (community) Approve
Review via email: mp+293416@code.launchpad.net

Commit message

Support no-docs case in the OpenLibrary search.

Description of the change

Support no-docs case in the OpenLibrary search.

To post a comment you must log in.
Revision history for this message
Bret Barker (noise) wrote :

+1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'src/scopes/openlibrary.py'
--- src/scopes/openlibrary.py 2015-03-03 20:38:27 +0000
+++ src/scopes/openlibrary.py 2016-04-29 16:02:40 +0000
@@ -109,11 +109,9 @@
109def _get_book_cover(d):109def _get_book_cover(d):
110 """Get book cover."""110 """Get book cover."""
111 if d.get('cover_i'):111 if d.get('cover_i'):
112 cover = 'http://covers.openlibrary.org/b/id/' \112 cover = 'http://covers.openlibrary.org/b/id/' + str(d['cover_i']) + '-M.jpg'
113 + str(d['cover_i'])+'-M.jpg'
114 elif d.get('cover_id'):113 elif d.get('cover_id'):
115 cover = 'http://covers.openlibrary.org/b/id/' \114 cover = 'http://covers.openlibrary.org/b/id/' + str(d['cover_id']) + '-M.jpg'
116 + str(d['cover_id'])+'-M.jpg'
117 else:115 else:
118 cover = None116 cover = None
119 return cover117 return cover
@@ -190,6 +188,8 @@
190 subject = choice(SURFACING_SUBJECTS)188 subject = choice(SURFACING_SUBJECTS)
191 resp = self._hit_backend('subject', subject=subject, limit=limit)189 resp = self._hit_backend('subject', subject=subject, limit=limit)
192 docs = resp['docs']190 docs = resp['docs']
191 if not docs:
192 return
193 cat_books = BOOKS_CATEGORY.copy()193 cat_books = BOOKS_CATEGORY.copy()
194 cat_books['title'] = translator(subject, locale)194 cat_books['title'] = translator(subject, locale)
195 yield dict(category=cat_books)195 yield dict(category=cat_books)
@@ -206,13 +206,15 @@
206 featured_author = None206 featured_author = None
207 resp = self._hit_backend('search', query=query, limit=limit)207 resp = self._hit_backend('search', query=query, limit=limit)
208 docs = resp['docs']208 docs = resp['docs']
209 if not docs:
210 return
209 yield dict(category=localized_category(BOOKS_SEARCH_CATEGORY,211 yield dict(category=localized_category(BOOKS_SEARCH_CATEGORY,
210 locale))212 locale))
211 parsed_books = []213 parsed_books = []
212 for d in docs:214 for d in docs:
213 cover = _get_book_cover(d)215 cover = _get_book_cover(d)
214 author = _get_book_author(d)216 author = _get_book_author(d)
215 if author+d['title'] in parsed_books or not cover:217 if author + d['title'] in parsed_books or not cover:
216 continue218 continue
217 first_sentence = d.get('first_sentence', [None])[0]219 first_sentence = d.get('first_sentence', [None])[0]
218 first_published = d.get('first_publish_year')220 first_published = d.get('first_publish_year')
@@ -245,7 +247,7 @@
245 'editions': editions,247 'editions': editions,
246 'has_fulltext': d['has_fulltext'],248 'has_fulltext': d['has_fulltext'],
247 })249 })
248 parsed_books.append(author+d['title'])250 parsed_books.append(author + d['title'])
249251
250 def preview(self, **kwargs):252 def preview(self, **kwargs):
251 """Do the preview."""253 """Do the preview."""
@@ -267,7 +269,7 @@
267 desc = _get_description(data)269 desc = _get_description(data)
268 if not desc:270 if not desc:
269 if result['first_sentence']:271 if result['first_sentence']:
270 desc = '\"'+result['first_sentence']+'\"'272 desc = '"' + result['first_sentence'] + '"'
271 else:273 else:
272 desc = None274 desc = None
273275
274276
=== added file 'src/scopes/tests/fixtures/openlibrary-search-nodocs.json'
--- src/scopes/tests/fixtures/openlibrary-search-nodocs.json 1970-01-01 00:00:00 +0000
+++ src/scopes/tests/fixtures/openlibrary-search-nodocs.json 2016-04-29 16:02:40 +0000
@@ -0,0 +1,6 @@
1{
2 "start": 0,
3 "num_found": 22630,
4 "numFound": 22630,
5 "docs": []
6}
07
=== modified file 'src/scopes/tests/test_openlibrary.py'
--- src/scopes/tests/test_openlibrary.py 2015-03-03 20:38:27 +0000
+++ src/scopes/tests/test_openlibrary.py 2016-04-29 16:02:40 +0000
@@ -121,6 +121,14 @@
121 self.maxDiff = None121 self.maxDiff = None
122 self.assertEqual(response[2], should_res)122 self.assertEqual(response[2], should_res)
123123
124 def test_search_no_authors(self):
125 app = openlibrary.App(CONFIG)
126 with patch.object(app, '_hit_backend') as mock:
127 mock.return_value = json.loads(get_fixture(
128 'openlibrary-search-nodocs.json'))
129 response = list(app.search(query='alice', platform='desktop', limit=5, locale='C'))
130 self.assertEqual(response, [])
131
124132
125class PreviewTestCase(TestCase):133class PreviewTestCase(TestCase):
126 """Tests for the preview function."""134 """Tests for the preview function."""

Subscribers

People subscribed via source and target branches