def fulltext_search(q, page=1, limit=100, js=False): offset = (page - 1) * limit ia_results = fulltext_search_api({ 'q': q, 'from': offset, 'size': limit, 'olonly': 'true' }) if 'error' not in ia_results and ia_results['hits']: hits = ia_results['hits'].get('hits', []) ocaids = [hit['fields'].get('identifier', [''])[0] for hit in hits] availability = get_availability_of_ocaids(ocaids) if 'error' in availability: return [] editions = web.ctx.site.get_many([ '/books/%s' % availability[ocaid].get('openlibrary_edition') for ocaid in availability if availability[ocaid].get('openlibrary_edition') ]) for ed in editions: if ed.ocaid in ocaids: idx = ocaids.index(ed.ocaid) ia_results['hits']['hits'][idx]['edition'] = ( format_book_data(ed) if js else ed) ia_results['hits']['hits'][idx]['availability'] = availability[ ed.ocaid] return ia_results
def get_sorted_editions(self): """ Get this work's editions sorted by publication year :rtype: list[Edition] """ use_solr_data = self._solr_data and \ self._solr_data.get('edition_key') and \ len(self._solr_data.get('edition_key')) == self.edition_count if use_solr_data: edition_keys = [ "/books/" + olid for olid in self._solr_data.get('edition_key') ] else: db_query = { "type": "/type/edition", "works": self.key, "limit": 10000 } edition_keys = web.ctx.site.things(db_query) editions = web.ctx.site.get_many(edition_keys) editions.sort(key=lambda ed: ed.get_publish_year(), reverse=True) availability = lending.get_availability_of_ocaids( [ed.ocaid for ed in editions if ed.ocaid]) for ed in editions: ed.availability = availability.get(ed.ocaid) or {"status": "error"} return editions
def get_sorted_editions(self): """Return a list of works sorted by publish date""" w = self._solr_data editions = w.get('edition_key') if w else [] if editions: # solr is stale if len(editions) != self.edition_count: q = { "type": "/type/edition", "works": self.key, "limit": 10000 } editions = [k[len("/books/"):] for k in web.ctx.site.things(q)] books = web.ctx.site.get_many( ["/books/" + olid for olid in editions]) availability = lending.get_availability_of_ocaids( [book.ocaid for book in books if book.ocaid]) for book in books: book.availability = availability.get(book.ocaid) or { "status": "error" } return books[::-1] else: return []
def get_book_availability(self, id_type, ids): return ( lending.get_availability_of_works(ids) if id_type == "openlibrary_work" else lending.get_availability_of_editions(ids) if id_type == "openlibrary_edition" else lending.get_availability_of_ocaids(ids) if id_type == "identifier" else [] )
def get_book_availability(self, id_type, ids): return ( lending.get_availability_of_works(ids) if id_type == "openlibrary_work" else lending.get_availability_of_editions(ids) if id_type == "openlibrary_edition" else lending.get_availability_of_ocaids(ids) if id_type == "identifier" else [] )
def get_sorted_editions(self, ebooks_only=False, limit=None, keys=None): """ Get this work's editions sorted by publication year :param bool ebooks_only: :param int limit: :param list[str] keys: ensure keys included in fetched editions :rtype: list[Edition] """ db_query = {"type": "/type/edition", "works": self.key} db_query['limit'] = limit or 10000 edition_keys = [] if ebooks_only: if self._solr_data: from openlibrary.book_providers import get_book_providers # Always use solr data whether it's up to date or not # to determine which providers this book has # We only make additional queries when a # trusted book provider identifier is present for provider in get_book_providers(self._solr_data): query = {**db_query, **provider.editions_query} edition_keys += web.ctx.site.things(query) else: db_query["ocaid~"] = "*" if not edition_keys: solr_is_up_to_date = (self._solr_data and self._solr_data.get('edition_key') and len(self._solr_data.get('edition_key')) == self.edition_count) if solr_is_up_to_date: edition_keys += [ "/books/" + olid for olid in self._solr_data.get('edition_key') ] else: # given librarians are probably doing this, show all editions edition_keys += web.ctx.site.things(db_query) edition_keys.extend(keys or []) editions = web.ctx.site.get_many(list(set(edition_keys))) editions.sort(key=lambda ed: ed.get_publish_year() or -sys.maxsize, reverse=True) # 2022-03 Once we know the availability-type of editions (e.g. open) # via editions-search, we can sidestep get_availability to only # check availability for borrowable editions ocaids = [ed.ocaid for ed in editions if ed.ocaid] availability = lending.get_availability_of_ocaids( ocaids) if ocaids else {} for ed in editions: ed.availability = availability.get(ed.ocaid) or {"status": "error"} return editions
def get_sorted_editions(self, ebooks_only=False, limit=10000): """ Get this work's editions sorted by publication year :param bool ebooks_only: :rtype: list[Edition] """ db_query = {"type": "/type/edition", "works": self.key, "limit": limit} if ebooks_only: edition_keys = [] if self._solr_data: from openlibrary.book_providers import get_book_providers # Always use solr data whether it's up to date or not # to determine which providers this book has # We only make additional queries when a # trusted book provider identifier is present for provider in get_book_providers(self._solr_data): query = {**db_query, **provider.editions_query} edition_keys += web.ctx.site.things(query) else: db_query["ocaid~"] = "*" else: solr_is_up_to_date = (self._solr_data and self._solr_data.get('edition_key') and len(self._solr_data.get('edition_key')) == self.edition_count) if solr_is_up_to_date: edition_keys = [ "/books/" + olid for olid in self._solr_data.get('edition_key') ] else: # given librarians are probably doing this, show all editions edition_keys = web.ctx.site.things(db_query) editions = web.ctx.site.get_many(edition_keys) editions.sort(key=lambda ed: ed.get_publish_year() or -sys.maxsize, reverse=True) availability = lending.get_availability_of_ocaids( [ed.ocaid for ed in editions if ed.ocaid]) for ed in editions: ed.availability = availability.get(ed.ocaid) or {"status": "error"} return editions
def get_results(q, offset=0, limit=100): ia_results = inside_search_select({ 'q': escape_q(q), 'from': offset, 'size': limit, 'olonly': 'true' }) if 'error' not in ia_results and ia_results['hits']: hits = ia_results['hits'].get('hits', []) ocaids = [hit['fields'].get('identifier', [''])[0] for hit in hits] availability = get_availability_of_ocaids(ocaids) if 'error' in availability: return [] editions = web.ctx.site.get_many([ '/books/%s' % availability[ocaid].get('openlibrary_edition') for ocaid in availability if availability[ocaid].get('openlibrary_edition')]) for ed in editions: idx = ocaids.index(ed.ocaid) ia_results['hits']['hits'][idx]['edition'] = ed ia_results['hits']['hits'][idx]['availability'] = availability[ed.ocaid] return ia_results
def fulltext_search(q, page=1, limit=100): offset = (page - 1) * limit ia_results = fulltext_search_api({ 'q': q, 'from': offset, 'size': limit, 'olonly': 'true' }) if 'error' not in ia_results and ia_results['hits']: hits = ia_results['hits'].get('hits', []) ocaids = [hit['fields'].get('identifier', [''])[0] for hit in hits] availability = get_availability_of_ocaids(ocaids) if 'error' in availability: return [] editions = web.ctx.site.get_many([ '/books/%s' % availability[ocaid].get('openlibrary_edition') for ocaid in availability if availability[ocaid].get('openlibrary_edition')]) for ed in editions: idx = ocaids.index(ed.ocaid) ia_results['hits']['hits'][idx]['edition'] = ed ia_results['hits']['hits'][idx]['availability'] = availability[ed.ocaid] return ia_results
def get_sorted_editions(self): """Return a list of works sorted by publish date""" w = self._solr_data editions = w.get('edition_key') if w else [] # solr is stale if len(editions) != self.edition_count: q = {"type": "/type/edition", "works": self.key, "limit": 10000} editions = [k[len("/books/"):] for k in web.ctx.site.things(q)] if editions: books = web.ctx.site.get_many(["/books/" + olid for olid in editions]) availability = lending.get_availability_of_ocaids([ book.ocaid for book in books if book.ocaid ]) for book in books: book.availability = availability.get(book.ocaid) or {"status": "error"} return books[::-1] else: return []