|
8 | 8 | # Setup logger |
9 | 9 | log = Logging() |
10 | 10 |
|
11 | | -asin_regex = '(?=.\\d)[A-Z\\d]{10}' |
12 | | -region_regex = '(?<=\[)[A-Za-z]{2}(?=\])' |
| 11 | +asin_regex = re.compile(r'(?=.\d)[A-Z\d]{10}') |
| 12 | +region_regex = re.compile(r'(?<=\[)[A-Za-z]{2}(?=\])') |
13 | 13 |
|
14 | 14 |
|
15 | 15 | class SearchTool: |
@@ -41,18 +41,26 @@ def build_url(self, query): |
41 | 41 |
|
42 | 42 | def check_for_asin(self): |
43 | 43 | """ |
44 | | - Checks filename and search query for ASIN to quick match. |
| 44 | + Checks filename (for books) and/or search query for ASIN to quick match. |
45 | 45 | """ |
46 | | - filename_search_asin = self.search_asin(self.media.filename) |
| 46 | + # Check filename for ASIN if content type is books |
| 47 | + if self.content_type == 'books': |
| 48 | + # Provide a plain filename for ASIN search |
| 49 | + filename_unquoted = urllib.unquote( |
| 50 | + self.media.filename).decode('utf8') |
| 51 | + filename_search_asin = self.search_asin(filename_unquoted) |
| 52 | + |
| 53 | + if filename_search_asin: |
| 54 | + log.info('ASIN found in filename') |
| 55 | + self.check_for_region(filename_unquoted) |
| 56 | + return filename_search_asin.group(0) + '_' + self.region_override |
| 57 | + |
| 58 | + # Check search query for ASIN |
47 | 59 | # Default to album and use artist if no album |
48 | 60 | manual_asin = self.media.album if self.media.album else self.media.artist |
49 | 61 | manual_search_asin = self.search_asin(manual_asin) |
50 | 62 |
|
51 | | - if filename_search_asin: |
52 | | - log.info('ASIN found in filename') |
53 | | - self.check_for_region(self.media.filename) |
54 | | - return filename_search_asin.group(0) + '_' + self.region_override |
55 | | - elif manual_search_asin: |
| 63 | + if manual_search_asin: |
56 | 64 | log.info('ASIN found in manual search') |
57 | 65 | self.check_for_region(manual_asin) |
58 | 66 | return manual_search_asin.group(0) + '_' + self.region_override |
@@ -137,14 +145,14 @@ def search_asin(self, input): |
137 | 145 | Searches for ASIN in a string. |
138 | 146 | """ |
139 | 147 | if input: |
140 | | - return re.search(asin_regex, urllib.unquote(input).decode('utf8')) |
| 148 | + return re.search(asin_regex, input) |
141 | 149 |
|
142 | 150 | def search_region(self, input): |
143 | 151 | """ |
144 | 152 | Searches for region in a string. |
145 | 153 | """ |
146 | 154 | if input: |
147 | | - return re.search(region_regex, urllib.unquote(input).decode('utf8')) |
| 155 | + return re.search(region_regex, input) |
148 | 156 |
|
149 | 157 | def validate_author_name(self): |
150 | 158 | """ |
|
0 commit comments