Skip to content

Commit

Permalink
More tags and links! ⛓️⛓️⛓️⛓️⛓️
Browse files Browse the repository at this point in the history
  • Loading branch information
znedw committed Oct 20, 2020
1 parent faeb971 commit d20c399
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
28 changes: 24 additions & 4 deletions pythonbits/bb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -1012,15 +1015,32 @@ 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})
Expand All @@ -1031,7 +1051,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']),
Expand Down
31 changes: 29 additions & 2 deletions pythonbits/goodreads.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -39,14 +44,36 @@ 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


Expand All @@ -64,7 +91,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

Expand Down

0 comments on commit d20c399

Please sign in to comment.