def handle(self, filename, **options): for isbn in open(filename): isbn = isbn.strip() edition = bookloader.add_by_isbn(isbn) if edition: self.stdout.write("loaded %s as %s" % (isbn, edition)) else: self.stdout.write("failed to load book for %s" % isbn)
def handle(self, filename, username, **options): user = User.objects.get(username=username) wishlist = user.wishlist for isbn in open(filename): isbn = isbn.strip() edition = bookloader.add_by_isbn(isbn) if edition: bookloader.add_related(isbn) user.wishlist.add_work(edition.work, source="user") print "loaded %s as %s for %s" % (isbn, edition, user) else: print "failed to load book for %s" % isbn
def add_all_isbns(isbns, work, language=None, title=None): first_edition = None for isbn in isbns: edition = bookloader.add_by_isbn(isbn, work, language=language, title=title) if edition: first_edition = first_edition if first_edition else edition if work and (edition.work_id != work.id): if work.doab and edition.work.doab and work.doab != edition.work.doab: if work.created < edition.work.created: work = merge_works(work, edition.work) else: work = merge_works(edition.work, work) else: work = edition.work return work, first_edition
def load_librarything_into_wishlist(user, lt_username, max_books=None): """ Load a specified LibraryThing shelf (by default: all the books from the LibraryThing account associated with user) """ from regluit.core import bookloader from regluit.core import tasks from itertools import islice logger.info("Entering into load_librarything_into_wishlist") lt = LibraryThing(lt_username) for (i, book) in enumerate( islice(lt.parse_user_catalog(view_style=5), max_books)): isbn = book["isbn"] # grab the first one logger.info("%d %s %s", i, book["title"]["title"], isbn) try: if not isbn: continue edition = bookloader.add_by_isbn(isbn) if not edition: continue # add the librarything ids to the db since we know them now identifier = models.Identifier.get_or_add(type='thng', value=book['book_id'], edition=edition, work=edition.work) identifier = models.Identifier.get_or_add(type='ltwk', value=book['work_id'], work=edition.work) if book['lc_call_number']: identifier = models.Identifier.get_or_add( type='lccn', value=book['lc_call_number'], edition=edition, work=edition.work) user.wishlist.add_work(edition.work, 'librarything', notify=True) if edition.new: tasks.populate_edition.delay(edition.isbn_13) logger.info("Work with isbn %s added to wishlist.", isbn) except Exception as e: logger.info("error adding ISBN %s: %s", isbn, e)
def load_goodreads_shelf_into_wishlist(user, shelf_name='all', goodreads_user_id=None, max_books=None, expected_number_of_books=None): """ Load a specified Goodreads shelf (by default: all the books from the Goodreads account associated with user) """ logger.info('Entering load_goodreads_shelf_into_wishlist. user: %s, shelf_name: %s, goodreads_user_id: %s, max_books: %s, expected_number_of_books: %s', user, shelf_name, goodreads_user_id, max_books, expected_number_of_books) gc = GoodreadsClient(key=settings.GOODREADS_API_KEY, secret=settings.GOODREADS_API_SECRET, user=user) if goodreads_user_id is None: if user.profile.goodreads_user_id is not None: goodreads_user_id = user.profile.goodreads_user_id else: raise Exception("No Goodreads user_id is associated with user.") logger.info('computed goodreads_user_id: %s ', goodreads_user_id) for (i, review) in enumerate(islice(gc.review_list(goodreads_user_id,shelf=shelf_name),max_books)): isbn = review["book"]["isbn10"] if review["book"]["isbn10"] is not None else review["book"]["isbn13"] logger.info("%d %s %s %s ", i, review["book"]["title"], isbn, review["book"]["small_image_url"]) try: edition = bookloader.add_by_isbn(isbn) if not edition: continue # save the goodreads id since we know it at this point # we need to extract it from the link since review['id'] # is the id for a users review, not the book link = review['book']['link'] match = re.search('/show/(\d+)', link) if match: identifier= models.Identifier.get_or_add(type = 'gdrd', value = match.group(1), edition = edition, work = edition.work) user.wishlist.add_work(edition.work, 'goodreads', notify=True) logger.info("Work with isbn %s added to wishlist.", isbn) else: logger.error("unable to extract goodreads id from %s", link) if edition.new: regluit.core.tasks.populate_edition.delay(edition.isbn_13) except Exception, e: logger.info ("Exception adding ISBN %s: %s", isbn, e)
def load_penguin_moby_dick(): seed_isbn = '9780142000083' ed = bookloader.add_by_isbn(seed_isbn) if ed.new: ed = tasks.populate_edition.delay(ed.isbn_13)
def editions_for_lt(books): """return the Editions that correspond to the list of LibraryThing books""" editions = [bookloader.add_by_isbn(b["isbn"]) for b in books] return editions
def get_edition_for_id(id_type, id_value, user=None): ''' the identifier is assumed to not be in database ''' identifiers = {id_type: id_value} if id_type == 'http': # check for urls implying other identifiers identifiers.update(ids_from_urls(id_value)) for new_id_type in identifiers.keys(): idents = models.Identifier.objects.filter( type=new_id_type, value=identifiers[new_id_type], ) if idents: ident = idents[0] return ident.edition if ident.edition else ident.work.preferred_edition #need to make a new edition if identifiers.has_key('goog'): edition = add_by_googlebooks_id(identifiers['goog']) if edition: return edition if identifiers.has_key('isbn'): edition = add_by_isbn(identifiers['isbn']) if edition: return edition if identifiers.has_key('oclc'): edition = add_by_oclc(identifiers['oclc']) if edition: return edition if identifiers.has_key('glue'): try: work = models.safe_get_work(identifiers['glue']) return work.preferred_edition except: pass if identifiers.has_key('http'): edition = add_by_webpage(identifiers['http'], user=user) return edition # return a dummy edition and identifier title = '!!! missing title !!!' work = models.Work.objects.create(title=title) edition = models.Edition.objects.create(title='!!! missing title !!!', work=work) for key in identifiers.keys(): if key == 'glue': id_value = work.id if key not in ('http', 'goog', 'oclc', 'isbn'): if key in WORK_IDENTIFIERS: edid = str(edition.id) models.Identifier.objects.create(type='edid', value=edid, work=work, edition=edition) models.Identifier.objects.create(type=key, value=id_value, work=work, edition=None) else: models.Identifier.objects.create(type=key, value=id_value, work=work, edition=edition) return edition