diff --git a/pythonbits/bb.py b/pythonbits/bb.py index 17f0d81..607c3c9 100644 --- a/pythonbits/bb.py +++ b/pythonbits/bb.py @@ -942,6 +942,9 @@ def _desc(self): s = self['summary'] return re.sub('<[^<]+?>', '', s['description']) + def _render_scene(self): + return False + @form_field('book_retail', 'checkbox') def _render_retail(self): return bool( @@ -1012,15 +1015,29 @@ def _render_form_title(self): def _render_tags(self): categories = find_categories(self['summary']['isbn']) authors = self['summary']['authors'] - return ",".join(uniq(list(format_tag(a['name']) for a in authors) + - list(format_tag(a) for a in categories))) + shelves = self['summary']['shelves'] + + tags = uniq(list(format_tag(a['name']) for a in authors) + + list(format_tag(c) for c in categories) + + list(format_tag(s['name']) for s in shelves)) + # Maximum tags length is 200 characters + + def tags_string(tags): + return ",".join(format_tag(tag) for tag in tags) + while len(tags_string(tags)) > 200: + del tags[-1] + return tags_string(tags) def _render_section_information(self): def gr_author_link(gra): return bb.link(gra['name'], gra['link']) book = self['summary'] - links = [("Goodreads", book['url'])] + isbn = book['isbn'] + links = [('Goodreads', book['url']), + ('Amazon', 'http://amzn.com/{}'.format(isbn)), + ('LibraryThing', 'http://www.librarything.com/isbn/{}/'.format(isbn)), + ('Google Books', 'http://books.google.com/books?vid=ISBN{}'.format(isbn))] return dedent("""\ [b]Title[/b]: {title} ({links}) @@ -1031,7 +1048,7 @@ def gr_author_link(gra): [b]Author(s)[/b]: {authors}""").format( links=", ".join(bb.link(*l) for l in links), title=book['title'], - isbn=book['isbn'], + isbn=isbn, publisher=book['publisher'], publication_year=book['publication_year'], rating=bb.format_rating(float(book['average_rating']), diff --git a/pythonbits/goodreads.py b/pythonbits/goodreads.py index 506f91e..2446c93 100755 --- a/pythonbits/goodreads.py +++ b/pythonbits/goodreads.py @@ -16,6 +16,11 @@ Enter the API Key below API Key""")) +EXCLUDED_WORDS = ['read', 'favorites', 'book', + 'own', 'series', 'novels', 'kindle', 'shelf' + 'library', 'buy', 'abandoned', + 'audible', 'audio', 'novel', 'finish', 'wish'] + def _extract_authors(authors): if isinstance(authors['author'], OrderedDict): @@ -39,14 +44,34 @@ def _extract_language(alpha_3): return pycountry.languages.get(alpha_3=alpha_3).name +def _extract_shelves(shelves, take): + # source for tags e.g. sci-fi + return [_extract_shelf(shelf) + for shelf in filter(_exclude_well_known, + sorted(shelves, key=_shelf_sort_key, reverse=True)[:take])] + + +def _exclude_well_known(s): + return not any(w in s['@name'] for w in EXCLUDED_WORDS) + + +def _shelf_sort_key(s): + return int(s['@count']) + + +def _extract_shelf(shelf): + return {'name': shelf['@name'], 'count': shelf['@count']} + + def _process_book(books): keys_wanted = ['id', 'title', 'isbn', 'isbn13', 'description', 'language_code', 'publication_year', 'publisher', - 'image_url', 'url', 'authors', 'average_rating', 'work'] + 'image_url', 'url', 'authors', 'average_rating', 'work', 'popular_shelves'] book = {k: v for k, v in books if k in keys_wanted} book['authors'] = _extract_authors(book['authors']) book['ratings_count'] = int(book['work']['ratings_count']['#text']) book['language'] = _extract_language(book['language_code']) + book['shelves'] = _extract_shelves(book['popular_shelves']['shelf'], 10) return book @@ -64,7 +89,7 @@ def search(self, path): book = read_metadata(path) isbn = '' try: - isbn = book['Identifiers'].split(':')[1] + isbn = book['Identifiers'].split(':')[1].split(',')[0] except KeyError: pass