def proceed(self, product): # Fetch metadata from Google service = GoogleBookService(utils.configuration.googlebook_key) data = service.fetch_data(product.book.isbn13) if data is not None: # Parse the metadata parser = GoogleBookDataParser() if parser.parse(data): # Convert to book book = Book() book.title = parser.title book.isbn10 = parser.isbn10 book.isbn13 = parser.isbn13 book.year = parser.publishedYear book.description = parser.description book.nb_pages = parser.pageCount book.language = parser.language def convert_name_to_author(name): author = Author() author.name = name return author book.authors = map(convert_name_to_author, parser.authors) if book is not None and book.isbn10 == product.book.isbn10 and book.isbn13 == product.book.isbn13: # Store the book product.book = book # Save data from Googlebook product.metadata_path = "{0}/{1}.json".format(utils.configuration.tmp_path, product.book.isbn13) with open(product.metadata_path, 'w') as outfile: json.dump(data, outfile) # Download the thumbnail product.thumbnail_path = "{0}/{1}.picture".format(utils.configuration.tmp_path, product.book.isbn13) download = Download(parser.thumbnail_url, product.thumbnail_path) downloader = Downloader(download) downloader.run() if download.status: utils.logger.info("Match found in Google Book database [{0}]".format(book.title)) if self.next: return self.next.proceed(product) product.status = Status.Done return product utils.logger.info("This book is unknow from Google Book database") product.status = Status.Error return product
def create_book(name, **kwargs): owner_id = get_current_user_id() if not owner_id: return False description = kwargs.get('description') genres_ids = kwargs.get('genres_ids') or [] cover_image_url = kwargs.get('cover_image_url') page_count = kwargs.get('page_count') author_name = kwargs.get('author_name') year = kwargs.get('year') book = Book(name=name, owner_id=owner_id) book.description = description book.cover_image_url = cover_image_url book.page_count = page_count book.author_name = author_name book.year = year genres = [] for id in genres_ids: g = get_genre_by_id(id) if g: genres.append(g) book.genres = genres session.add(book) session.commit() return book