Skip to content

Commit 0476bc7

Browse files
committed
More tags and links!
1 parent faeb971 commit 0476bc7

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

pythonbits/bb.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,9 @@ def _desc(self):
942942
s = self['summary']
943943
return re.sub('<[^<]+?>', '', s['description'])
944944

945+
def _render_scene(self):
946+
return False
947+
945948
@form_field('book_retail', 'checkbox')
946949
def _render_retail(self):
947950
return bool(
@@ -1012,15 +1015,29 @@ def _render_form_title(self):
10121015
def _render_tags(self):
10131016
categories = find_categories(self['summary']['isbn'])
10141017
authors = self['summary']['authors']
1015-
return ",".join(uniq(list(format_tag(a['name']) for a in authors) +
1016-
list(format_tag(a) for a in categories)))
1018+
shelves = self['summary']['shelves']
1019+
1020+
tags = uniq(list(format_tag(a['name']) for a in authors) +
1021+
list(format_tag(c) for c in categories) +
1022+
list(format_tag(s['name']) for s in shelves))
1023+
# Maximum tags length is 200 characters
1024+
1025+
def tags_string(tags):
1026+
return ",".join(format_tag(tag) for tag in tags)
1027+
while len(tags_string(tags)) > 200:
1028+
del tags[-1]
1029+
return tags_string(tags)
10171030

10181031
def _render_section_information(self):
10191032
def gr_author_link(gra):
10201033
return bb.link(gra['name'], gra['link'])
10211034

10221035
book = self['summary']
1023-
links = [("Goodreads", book['url'])]
1036+
isbn = book['isbn']
1037+
links = [('Goodreads', book['url']),
1038+
('Amazon', 'http://amzn.com/{}'.format(isbn)),
1039+
('LibraryThing', 'http://www.librarything.com/isbn/{}/'.format(isbn)),
1040+
('Google Books', 'http://books.google.com/books?vid=ISBN{}'.format(isbn))]
10241041

10251042
return dedent("""\
10261043
[b]Title[/b]: {title} ({links})
@@ -1031,7 +1048,7 @@ def gr_author_link(gra):
10311048
[b]Author(s)[/b]: {authors}""").format(
10321049
links=", ".join(bb.link(*l) for l in links),
10331050
title=book['title'],
1034-
isbn=book['isbn'],
1051+
isbn=isbn,
10351052
publisher=book['publisher'],
10361053
publication_year=book['publication_year'],
10371054
rating=bb.format_rating(float(book['average_rating']),

pythonbits/goodreads.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
Enter the API Key below
1717
API Key"""))
1818

19+
EXCLUDED_WORDS = ['read', 'favorites', 'book',
20+
'own', 'series', 'novels', 'kindle', 'shelf'
21+
'library', 'buy', 'abandoned',
22+
'audible', 'audio', 'novel', 'finish', 'wish']
23+
1924

2025
def _extract_authors(authors):
2126
if isinstance(authors['author'], OrderedDict):
@@ -39,14 +44,34 @@ def _extract_language(alpha_3):
3944
return pycountry.languages.get(alpha_3=alpha_3).name
4045

4146

47+
def _extract_shelves(shelves, take):
48+
# source for tags e.g. sci-fi
49+
return [_extract_shelf(shelf)
50+
for shelf in filter(_exclude_well_known,
51+
sorted(shelves, key=_shelf_sort_key, reverse=True)[:take])]
52+
53+
54+
def _exclude_well_known(s):
55+
return not any(w in s['@name'] for w in EXCLUDED_WORDS)
56+
57+
58+
def _shelf_sort_key(s):
59+
return int(s['@count'])
60+
61+
62+
def _extract_shelf(shelf):
63+
return {'name': shelf['@name'], 'count': shelf['@count']}
64+
65+
4266
def _process_book(books):
4367
keys_wanted = ['id', 'title', 'isbn', 'isbn13', 'description',
4468
'language_code', 'publication_year', 'publisher',
45-
'image_url', 'url', 'authors', 'average_rating', 'work']
69+
'image_url', 'url', 'authors', 'average_rating', 'work', 'popular_shelves']
4670
book = {k: v for k, v in books if k in keys_wanted}
4771
book['authors'] = _extract_authors(book['authors'])
4872
book['ratings_count'] = int(book['work']['ratings_count']['#text'])
4973
book['language'] = _extract_language(book['language_code'])
74+
book['shelves'] = _extract_shelves(book['popular_shelves']['shelf'], 10)
5075
return book
5176

5277

@@ -64,7 +89,7 @@ def search(self, path):
6489
book = read_metadata(path)
6590
isbn = ''
6691
try:
67-
isbn = book['Identifiers'].split(':')[1]
92+
isbn = book['Identifiers'].split(':')[1].split(',')[0]
6893
except KeyError:
6994
pass
7095

0 commit comments

Comments
 (0)