16
16
Enter the API Key below
17
17
API Key""" ))
18
18
19
+ EXCLUDED_WORDS = ['read' , 'favorites' , 'book' ,
20
+ 'own' , 'series' , 'novel' , 'kindle' , 'shelf'
21
+ 'library' , 'buy' , 'abandoned' ,
22
+ 'audible' , 'audio' , 'finish' , 'wish' ]
23
+
19
24
20
25
def _extract_authors (authors ):
21
26
if isinstance (authors ['author' ], OrderedDict ):
@@ -39,14 +44,36 @@ def _extract_language(alpha_3):
39
44
return pycountry .languages .get (alpha_3 = alpha_3 ).name
40
45
41
46
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
+
42
67
def _process_book (books ):
43
68
keys_wanted = ['id' , 'title' , 'isbn' , 'isbn13' , 'description' ,
44
69
'language_code' , 'publication_year' , 'publisher' ,
45
- 'image_url' , 'url' , 'authors' , 'average_rating' , 'work' ]
70
+ 'image_url' , 'url' , 'authors' , 'average_rating' ,
71
+ 'work' , 'popular_shelves' ]
46
72
book = {k : v for k , v in books if k in keys_wanted }
47
73
book ['authors' ] = _extract_authors (book ['authors' ])
48
74
book ['ratings_count' ] = int (book ['work' ]['ratings_count' ]['#text' ])
49
75
book ['language' ] = _extract_language (book ['language_code' ])
76
+ book ['shelves' ] = _extract_shelves (book ['popular_shelves' ]['shelf' ], 10 )
50
77
return book
51
78
52
79
@@ -64,7 +91,7 @@ def search(self, path):
64
91
book = read_metadata (path )
65
92
isbn = ''
66
93
try :
67
- isbn = book ['Identifiers' ].split (':' )[1 ]
94
+ isbn = book ['Identifiers' ].split (':' )[1 ]. split ( ',' )[ 0 ]
68
95
except KeyError :
69
96
pass
70
97
0 commit comments