Skip to content

Commit 1d8468e

Browse files
committed
More tags
1 parent c63ac6a commit 1d8468e

File tree

4 files changed

+35
-12
lines changed

4 files changed

+35
-12
lines changed

pythonbits/bb.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from . import musicbrainz as mb
2626
from . import imagehosting
2727
from . import goodreads
28-
from .googlebooks import find_cover
28+
from .googlebooks import find_cover, find_categories
2929
from .ffmpeg import FFMpeg
3030
from . import templating as bb
3131
from .submission import (Submission, form_field, finalize, cat_map,
@@ -934,8 +934,10 @@ def _render_form_description(self):
934934

935935

936936
class BookSubmission(BbSubmission):
937-
default_fields = BbSubmission.default_fields + ("isbn", "format", "title", "author", "publisher", "language", "description", "year", "cover",
938-
"title")
937+
default_fields = BbSubmission.default_fields + ("isbn", "format", "title",
938+
"author", "publisher",
939+
"language", "description",
940+
"year", "cover", "title")
939941

940942
_cat_id = 'book'
941943
_form_type = 'E-Books'
@@ -1003,8 +1005,10 @@ def _render_summary(self):
10031005

10041006
@form_field('tags')
10051007
def _render_tags(self):
1008+
categories = find_categories(self['book']['isbn'])
10061009
authors = self['book']['authors']
1007-
return uniq(list(format_tag(a['name']) for a in authors))
1010+
return uniq(list(format_tag(a['name']) for a in authors) +
1011+
list(format_tag(a) for a in categories))
10081012

10091013
def _render_section_information(self):
10101014
def gr_author_link(gra):
@@ -1046,6 +1050,8 @@ def _render_description(self):
10461050
@finalize
10471051
@form_field('image')
10481052
def _render_cover(self):
1053+
# Goodreads usually won't give you a cover image as they don't have the
1054+
# the right to distribute them
10491055
if "nophoto" in self['book']['image_url']:
10501056
return find_cover(self['book']['isbn'])
10511057
else:

pythonbits/calibre.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ def get_version():
1818
return ebook_meta.communicate()[0].decode('utf8')
1919
except OSError:
2020
raise EbookMetaException(
21-
"Could not find %s, please ensure it is installed (via Calibre)." % COMMAND)
21+
"Could not find {}, please ensure it is installed (via Calibre)."
22+
.format(COMMAND))
2223

2324

2425
def read_metadata(path):
@@ -34,6 +35,6 @@ def read_metadata(path):
3435
try:
3536
key, value = row.split(': ')
3637
result[key.strip(' .')] = value.strip()
37-
except:
38+
except KeyError:
3839
pass
3940
return result

pythonbits/goodreads.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,16 @@ def search(self, path):
7676
print('{}: {} by {} ({})'
7777
.format(i, book['best_book']['title'],
7878
book['best_book']['author']['name'],
79-
book['original_publication_year'].get('#text', '')))
79+
book['original_publication_year']
80+
.get('#text', '')))
8081

8182
while True:
8283
choice = input('Select number or enter an alternate'
83-
' search term (or an ISBN with isbn: prefix):'
84-
' [0-{}, 0 default] '
85-
.format(len(book_results['results']['work']) - 1))
84+
' search term'
85+
' (or an ISBN with isbn: prefix):'
86+
' [0-{}, 0 default]'
87+
.format(
88+
len(book_results['results']['work']) - 1))
8689
try:
8790
choice = int(choice)
8891
except ValueError:

pythonbits/googlebooks.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,21 @@ def find_cover(isbn):
1313
log.debug('Fetching alt cover art from {}'.format(resp.url))
1414
if resp.status_code == 200:
1515
content = json.loads(resp.content)
16-
return content['items'][0]
17-
['volumeInfo']['imageLinks']['thumbnail'] or ''
16+
return (content['items'][0]['volumeInfo']
17+
['imageLinks']['thumbnail'] or '')
1818
else:
1919
log.warn('Couldn\'t find cover art for ISBN {}'.format(isbn))
2020
return ''
21+
22+
23+
def find_categories(isbn):
24+
path = 'volumes?q=isbn:{}'.format(isbn)
25+
resp = requests.get(API_URL+path)
26+
log.debug('Fetching categories from {}'.format(resp.url))
27+
if resp.status_code == 200:
28+
content = json.loads(resp.content)
29+
return (content['items'][0]['volumeInfo']
30+
['categories'] or '')
31+
else:
32+
log.warn('Couldn\'t find categories for ISBN {}'.format(isbn))
33+
return ''

0 commit comments

Comments
 (0)