1616 Enter the API Key below
1717 API Key""" ))
1818
19+ EXCLUDED_WORDS = ['read' , 'favorites' , 'book' ,
20+ 'own' , 'series' , 'novels' , 'kindle' , 'shelf'
21+ 'library' , 'buy' , 'abandoned' ,
22+ 'audible' , 'audio' , 'novel' , 'finish' , 'wish' ]
23+
1924
2025def _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+
4267def _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