Ejemplo n.º 1
0
    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 addRecord(data):
    """
    The incoming 'data' paramater should be a tuple of two dictionaries in the following 
    format:
    
    ("author":{"first_name":"John", "last_name":"Doe"},
     "book":{"title":"Some book", "isbn":"1234567890", 
             "publisher":"Packt"}
    )
    """
    # Create an instance of the Book and Person classes and populate them with the values
    # collected from the 'Add' dialog and passed in as the tuple of dictionaries.
    book = Book()
    book.title = data['book']['title']
    book.isbn = data['book']['isbn']
    book.publisher = data['book']['publisher']

    author = Person()
    author.first_name = data['author']['first_name']
    author.last_name = data['author']['last_name']
    book.person = author

    # Create a session, connect to the database, commit and close the connection.
    session = connectToDatabase()
    session.add(book)
    session.commit()
    session.close
Ejemplo n.º 3
0
def add_record(session, data):
    book = Book()
    book.title = data["book"]["title"]
    book.isbn = data["book"]["isbn"]
    book.publisher = data["book"]["publisher"]
    author = Person()
    author.first_name = data["author"]["first_name"]
    author.last_name = data["author"]["last_name"]
    book.person = author

    # try:
    session.add(book)
    session.commit()
Ejemplo n.º 4
0
    def test_save_update_changes_to_db(self):

        bk = Book(author='CCC', title='DDD', read=True)
        bk.save()

        # Change some attributes and save
        bk.author = 'EEE'
        bk.title = 'FFF'
        bk.read = False

        bk.save()

        # Check DB has same data as bk Book object
        self.assertEqual(bk, bookstore.get_book_by_id(bk.id))
        self.assertTrue(bk, bookstore.exact_match(bk))
def add_record(session, data):
    """
    Data should be a dictionary of two dictionaries in the following format:

    {"author":{"first_name":"John", "last_name":"Doe"},
     "book":{"title":"Some book", "isbn":"1234567890",
             "publisher":"Packt"}
    }
    """
    book = Book()
    book.title = data["book"]["title"]
    book.isbn = data["book"]["isbn"]
    book.publisher = data["book"]["publisher"]
    author = Person()
    author.first_name = data["author"]["first_name"]
    author.last_name = data["author"]["last_name"]
    book.person = author

    session.add(book)
    session.commit()
Ejemplo n.º 6
0
def addRecord(data):
    """
    Data should be a tuple of two dictionaries in the following format:
 
    ("author":{"first_name":"John", "last_name":"Doe"},
     "book":{"title":"Some book", "isbn":"1234567890", 
             "publisher":"Packt"}
    )
    """
    book = Book()
    book.title = data["book"]["title"]
    book.config_value = data["book"]["config_value"]
    book.comment = data["book"]["comment"]
    book.is_list = data["book"]["is_list"]

    # connect to session and commit data to database
    session = connectToDatabase()
    session.add(book)
    session.commit()
    session.close()
Ejemplo n.º 7
0
def _load_book_from_worldcat_details_page(html_content, details_page_url, oclc_id):
    """Given the HTML content of the details page, create a new Book object with all the necessary details."""

    current_book = Book()
    pq_page = pq(html_content)

    current_book.book_id = oclc_id

    current_book.title = _load_title_from_worldcat_details_page(pq_page)  # TITLE (e.g. 'Lean in : women, work, and the will to lead')
    current_book.publisher = _load_publisher_from_worldcat_details_page(pq_page)  # PUBLISHER (e.g. 'New York : Alfred A. Knopf, 2013.')
    current_book.worldcaturl = details_page_url
    current_book.page_count = _load_page_count_from_worldcat_details_page(pq_page)
    current_book.summary = _load_summary_from_worldcat_details_page(pq_page)
    current_book.coverurl = _load_cover_url_from_worldcat_details_page(pq_page)

    # TODO - check if author_name is unique before insertion
    current_book.authors = _load_authors_from_worldcat_details_page(html_content)

    isbn10_list, isbn13_list = _load_isbns_from_worldcat_details_page(html_content)
    current_book.isbn10s = isbn10_list
    current_book.isbn13s = isbn13_list

    return current_book
Ejemplo n.º 8
0
def addRecord(data):
    """
    Data should be a tuple of two dictionaries in the following format:
    
    ("author":{"first_name":"John", "last_name":"Doe"},
     "book":{"title":"Some book", "isbn":"1234567890", 
             "publisher":"Packt"}
    )
    """
    book = Book()
    book.title = data["book"]["title"]
    book.isbn = data["book"]["isbn"]
    book.publisher = data["book"]["publisher"]
    author = Person()
    author.first_name = data["author"]["first_name"]
    author.last_name = data["author"]["last_name"]
    book.person = author
    
    # connect to session and commit data to database
    session = connectToDatabase()
    session.add(book)
    session.commit()
    session.close()