|
18 | 18 | from openlibrary.core.follows import PubSub |
19 | 19 | from openlibrary.core.lending import ( |
20 | 20 | add_availability, |
21 | | - get_availability, |
22 | 21 | get_loans_of_user, |
23 | 22 | ) |
24 | 23 | from openlibrary.core.models import LoggedBooksData, User |
25 | 24 | from openlibrary.core.observations import Observations, convert_observation_ids |
26 | 25 | from openlibrary.i18n import gettext as _ |
| 26 | +from openlibrary.plugins.upstream.utils import safeget |
27 | 27 | from openlibrary.utils import extract_numeric_id_from_olid |
28 | 28 | from openlibrary.utils.dateutil import current_year |
29 | 29 |
|
@@ -77,65 +77,14 @@ def render_template(self, mb): |
77 | 77 | currently_reading = mb.readlog.get_works(key='currently-reading', **params) |
78 | 78 | already_read = mb.readlog.get_works(key='already-read', **params) |
79 | 79 |
|
80 | | - def add_availability_with_edition_preference(docs): |
81 | | - """Check edition-level availability when available, fallback to work-level.""" |
82 | | - filtered_docs = [d for d in docs if d.get('title')] |
83 | | - |
84 | | - docs_with_editions = [] |
85 | | - editions_to_check = [] |
86 | | - docs_without_editions = [] |
87 | | - |
88 | | - for doc in filtered_docs: |
89 | | - # editions can be a list [edition] or dict {'docs': [edition]} |
90 | | - editions = doc.get('editions') |
91 | | - if editions: |
92 | | - if isinstance(editions, list) and editions: |
93 | | - edition = editions[0] |
94 | | - elif isinstance(editions, dict) and editions.get('docs'): |
95 | | - edition = editions['docs'][0] |
96 | | - else: |
97 | | - edition = None |
98 | | - |
99 | | - if edition: |
100 | | - docs_with_editions.append((doc, edition)) |
101 | | - editions_to_check.append(edition) |
102 | | - else: |
103 | | - docs_without_editions.append(doc) |
104 | | - else: |
105 | | - docs_without_editions.append(doc) |
106 | | - |
107 | | - if editions_to_check: |
108 | | - edition_olids = [ |
109 | | - edition['key'].split('/')[-1] |
110 | | - for doc, edition in docs_with_editions |
111 | | - if edition.get('key') |
112 | | - ] |
113 | | - if edition_olids: |
114 | | - edition_availabilities = get_availability( |
115 | | - 'openlibrary_edition', edition_olids |
116 | | - ) |
117 | | - for doc, edition in docs_with_editions: |
118 | | - if edition.get('key'): |
119 | | - edition_olid = edition['key'].split('/')[-1] |
120 | | - if edition_olid in edition_availabilities: |
121 | | - doc['availability'] = edition_availabilities[ |
122 | | - edition_olid |
123 | | - ] |
124 | | - |
125 | | - if docs_without_editions: |
126 | | - add_availability(docs_without_editions) |
127 | | - |
128 | | - return filtered_docs |
129 | | - |
130 | | - # Ideally, do all 3 lookups in one add_availability call |
131 | | - want_to_read.docs = add_availability_with_edition_preference( |
132 | | - want_to_read.docs |
| 80 | + want_to_read.docs = add_availability( |
| 81 | + [safeget(lambda: d['editions']['docs'][0], d) for d in want_to_read.docs if d.get('title')] |
133 | 82 | )[:5] |
134 | | - currently_reading.docs = add_availability_with_edition_preference( |
135 | | - currently_reading.docs |
| 83 | + currently_reading.docs = add_availability( |
| 84 | + [safeget(lambda: d['editions']['docs'][0], d) for d in currently_reading.docs if d.get('title')] |
136 | 85 | )[:5] |
137 | | - already_read.docs = add_availability_with_edition_preference( |
138 | | - already_read.docs |
| 86 | + already_read.docs = add_availability( |
| 87 | + [safeget(lambda: d['editions']['docs'][0], d) for d in already_read.docs if d.get('title')] |
139 | 88 | )[:5] |
140 | 89 |
|
141 | 90 | docs = { |
|
0 commit comments