Skip to content

Commit 40d22b1

Browse files
committed
More tags and links! ⛓️⛓️⛓️⛓️⛓️
1 parent faeb971 commit 40d22b1

File tree

2 files changed

+53
-6
lines changed

2 files changed

+53
-6
lines changed

pythonbits/bb.py

Lines changed: 24 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,32 @@ 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/{}'
1039+
.format(isbn)),
1040+
('LibraryThing', 'http://www.librarything.com/isbn/{}/'
1041+
.format(isbn)),
1042+
('Google Books', 'http://books.google.com/books?vid=ISBN{}'
1043+
.format(isbn))]
10241044

10251045
return dedent("""\
10261046
[b]Title[/b]: {title} ({links})
@@ -1031,7 +1051,7 @@ def gr_author_link(gra):
10311051
[b]Author(s)[/b]: {authors}""").format(
10321052
links=", ".join(bb.link(*l) for l in links),
10331053
title=book['title'],
1034-
isbn=book['isbn'],
1054+
isbn=isbn,
10351055
publisher=book['publisher'],
10361056
publication_year=book['publication_year'],
10371057
rating=bb.format_rating(float(book['average_rating']),

pythonbits/goodreads.py

Lines changed: 29 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', 'novel', 'kindle', 'shelf'
21+
'library', 'buy', 'abandoned',
22+
'audible', 'audio', 'finish', 'wish']
23+
1924

2025
def _extract_authors(authors):
2126
if isinstance(authors['author'], OrderedDict):
@@ -39,14 +44,36 @@ 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,
52+
reverse=True)[:take])]
53+
54+
55+
def _exclude_well_known(s):
56+
return not any(w in s['@name'] for w in EXCLUDED_WORDS)
57+
58+
59+
def _shelf_sort_key(s):
60+
return int(s['@count'])
61+
62+
63+
def _extract_shelf(shelf):
64+
return {'name': shelf['@name'], 'count': shelf['@count']}
65+
66+
4267
def _process_book(books):
4368
keys_wanted = ['id', 'title', 'isbn', 'isbn13', 'description',
4469
'language_code', 'publication_year', 'publisher',
45-
'image_url', 'url', 'authors', 'average_rating', 'work']
70+
'image_url', 'url', 'authors', 'average_rating',
71+
'work', 'popular_shelves']
4672
book = {k: v for k, v in books if k in keys_wanted}
4773
book['authors'] = _extract_authors(book['authors'])
4874
book['ratings_count'] = int(book['work']['ratings_count']['#text'])
4975
book['language'] = _extract_language(book['language_code'])
76+
book['shelves'] = _extract_shelves(book['popular_shelves']['shelf'], 10)
5077
return book
5178

5279

@@ -64,7 +91,7 @@ def search(self, path):
6491
book = read_metadata(path)
6592
isbn = ''
6693
try:
67-
isbn = book['Identifiers'].split(':')[1]
94+
isbn = book['Identifiers'].split(':')[1].split(',')[0]
6895
except KeyError:
6996
pass
7097

0 commit comments

Comments
 (0)