Skip to content

Commit

Permalink
More tags
Browse files Browse the repository at this point in the history
  • Loading branch information
znedw committed Oct 20, 2020
1 parent c63ac6a commit 91aedde
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 12 deletions.
14 changes: 10 additions & 4 deletions pythonbits/bb.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from . import musicbrainz as mb
from . import imagehosting
from . import goodreads
from .googlebooks import find_cover
from .googlebooks import find_cover, find_categories
from .ffmpeg import FFMpeg
from . import templating as bb
from .submission import (Submission, form_field, finalize, cat_map,
Expand Down Expand Up @@ -934,8 +934,10 @@ def _render_form_description(self):


class BookSubmission(BbSubmission):
default_fields = BbSubmission.default_fields + ("isbn", "format", "title", "author", "publisher", "language", "description", "year", "cover",
"title")
default_fields = BbSubmission.default_fields + ("isbn", "format", "title",
"author", "publisher",
"language", "description",
"year", "cover", "title")

_cat_id = 'book'
_form_type = 'E-Books'
Expand Down Expand Up @@ -1003,8 +1005,10 @@ def _render_summary(self):

@form_field('tags')
def _render_tags(self):
categories = find_categories(self['book']['isbn'])
authors = self['book']['authors']
return uniq(list(format_tag(a['name']) for a in authors))
return uniq(list(format_tag(a['name']) for a in authors) +
list(format_tag(a) for a in categories))

def _render_section_information(self):
def gr_author_link(gra):
Expand Down Expand Up @@ -1046,6 +1050,8 @@ def _render_description(self):
@finalize
@form_field('image')
def _render_cover(self):
# Goodreads usually won't give you a cover image as they don't have the
# the right to distribute them
if "nophoto" in self['book']['image_url']:
return find_cover(self['book']['isbn'])
else:
Expand Down
5 changes: 3 additions & 2 deletions pythonbits/calibre.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ def get_version():
return ebook_meta.communicate()[0].decode('utf8')
except OSError:
raise EbookMetaException(
"Could not find %s, please ensure it is installed (via Calibre)." % COMMAND)
"Could not find {}, please ensure it is installed (via Calibre)."
.format(COMMAND))


def read_metadata(path):
Expand All @@ -34,6 +35,6 @@ def read_metadata(path):
try:
key, value = row.split(': ')
result[key.strip(' .')] = value.strip()
except:
except KeyError:
pass
return result
11 changes: 7 additions & 4 deletions pythonbits/goodreads.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from .config import config
from .logging import log
from .calibre import read_metadata
from typing import OrderedDict
from collections import OrderedDict

config.register(
'Goodreads', 'api_key',
Expand Down Expand Up @@ -76,13 +76,16 @@ def search(self, path):
print('{}: {} by {} ({})'
.format(i, book['best_book']['title'],
book['best_book']['author']['name'],
book['original_publication_year'].get('#text', '')))
book['original_publication_year']
.get('#text', '')))

while True:
choice = input('Select number or enter an alternate'
' search term (or an ISBN with isbn: prefix):'
' search term'
' (or an ISBN with isbn: prefix):'
' [0-{}, 0 default] '
.format(len(book_results['results']['work']) - 1))
.format(
len(book_results['results']['work']) - 1))
try:
choice = int(choice)
except ValueError:
Expand Down
17 changes: 15 additions & 2 deletions pythonbits/googlebooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,21 @@ def find_cover(isbn):
log.debug('Fetching alt cover art from {}'.format(resp.url))
if resp.status_code == 200:
content = json.loads(resp.content)
return content['items'][0]
['volumeInfo']['imageLinks']['thumbnail'] or ''
return (content['items'][0]['volumeInfo']
['imageLinks']['thumbnail'] or '')
else:
log.warn('Couldn\'t find cover art for ISBN {}'.format(isbn))
return ''


def find_categories(isbn):
path = 'volumes?q=isbn:{}'.format(isbn)
resp = requests.get(API_URL+path)
log.debug('Fetching categories from {}'.format(resp.url))
if resp.status_code == 200:
content = json.loads(resp.content)
return (content['items'][0]['volumeInfo']
['categories'] or '')
else:
log.warn('Couldn\'t find categories for ISBN {}'.format(isbn))
return ''

0 comments on commit 91aedde

Please sign in to comment.