-
Notifications
You must be signed in to change notification settings - Fork 6
/
__init__.py
368 lines (322 loc) · 17.9 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#!/usr/bin/python
# Made with <3 by Fran González (@spaceisstrange)
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a
# copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
'''
This is a simple module made in my free time. It's not perfect, but it works well.
I'm accepting any pull request to improve it, just follow the code style.
For examples and documentation, take a look at the README.md
'''
from typing import Any, Dict, List, Union
import pycountry
import requests
from urllib.parse import urlencode
from itunespy import artist
from itunespy import music_artist
from itunespy import music_album
from itunespy import movie_artist
from itunespy import ebook_artist
from itunespy import track
from itunespy import result_item
# --------
# Searches
# --------
def search(
term: str,
country: str = 'US',
media: str = 'all',
entity: str = None,
attribute: str = None,
limit: int = 50) -> List[result_item.ResultItem]:
"""
Returns the result of the search of the specified term in an array of result_item(s)
:param term: String. The URL-encoded text string you want to search for. Example: Steven Wilson.
The method will take care of spaces so you don't have to.
:param country: String. The two-letter country code for the store you want to search.
For a full list of the codes: http://en.wikipedia.org/wiki/%20ISO_3166-1_alpha-2
:param media: String. The media type you want to search for. Example: music
:param entity: String. The type of results you want returned, relative to the specified media type. Example: musicArtist.
Full list: musicArtist, musicTrack, album, musicVideo, mix, song
:param attribute: String. The attribute you want to search for in the stores, relative to the specified media type.
:param limit: Integer. The number of search results you want the iTunes Store to return.
:return: An array of result_item(s)
"""
search_url = _url_search_builder(term, country, media, entity, attribute, limit)
r = requests.get(search_url)
try:
json = r.json()['results']
result_count = r.json()['resultCount']
except:
raise ConnectionError(general_no_connection)
if result_count == 0:
raise LookupError(search_error + str(term))
return _get_result_list(json, country)
# --------
# Lookups
# --------
def lookup(
id: Union[str, int] = None,
artist_amg_id: Union[str, int] = None,
upc: Union[str, int] = None,
country: str = 'US',
media: str = 'all',
entity: str = None,
attribute: str = None,
limit: int = 50) -> List[result_item.ResultItem]:
"""
Returns the result of the lookup of the specified id, artist_amg_id or upc in an array of result_item(s)
:param id: String. iTunes ID of the artist, album, track, ebook or software
:param artist_amg_id: String. All Music Guide ID of the artist
:param upc: String. UPCs/EANs
:param country: String. The two-letter country code for the store you want to search.
For a full list of the codes: http://en.wikipedia.org/wiki/%20ISO_3166-1_alpha-2
:param media: String. The media type you want to search for. Example: music
:param entity: String. The type of results you want returned, relative to the specified media type. Example: musicArtist.
Full list: musicArtist, musicTrack, album, musicVideo, mix, song
:param attribute: String. The attribute you want to search for in the stores, relative to the specified media type.
:param limit: Integer. The number of search results you want the iTunes Store to return.
:return: An array of result_item(s)
"""
# If none of the basic lookup arguments are provided, raise a ValueError
if id is None and artist_amg_id is None and upc is None:
raise ValueError(lookup_no_ids)
lookup_url = _url_lookup_builder(id, artist_amg_id, upc, country, media, entity, attribute, limit)
r = requests.get(lookup_url)
try:
json = r.json()['results']
result_count = r.json()['resultCount']
except KeyError:
raise ConnectionError(general_no_connection)
if result_count == 0:
raise LookupError(lookup_error)
return _get_result_list(json, country)
# --------
# Specific searches and lookups
# --------
# Music
def search_artist(term: str, country: str = 'US', attribute: str = None, limit: int = 50) -> List[music_artist.MusicArtist]:
return search(term, country, 'music', entities['musicArtist'], attribute, limit) # type: ignore
def search_album(term: str, country: str = 'US', attribute: str = None, limit: int = 50) -> List[music_album.MusicAlbum]:
return search(term, country, 'music', entities['album'], attribute, limit) # type: ignore
def search_track(term: str, country: str = 'US', attribute: str = None, limit: int = 50) -> List[track.Track]:
return search(term, country, 'music', entities['song'], attribute, limit) # type: ignore
def lookup_artist(id: Union[str, int] = None, artist_amg_id: Union[str, int] = None, upc: Union[str, int] = None, country: str = 'US', attribute: str = None, limit: int = 50) -> List[result_item.ResultItem]:
return lookup(id, artist_amg_id, upc, country, 'music', entities['musicArtist'], attribute, limit)
def lookup_album(id: Union[str, int] = None, artist_amg_id: Union[str, int] = None, upc: Union[str, int] = None, country: str = 'US', attribute: str = None, limit: int = 50) -> List[result_item.ResultItem]:
return lookup(id, artist_amg_id, upc, country, 'music', entities['album'], attribute, limit)
def lookup_track(id: Union[str, int] = None, artist_amg_id: Union[str, int] = None, upc: Union[str, int] = None, country: str = 'US', attribute: str = None, limit: int = 50) -> List[result_item.ResultItem]:
return lookup(id, artist_amg_id, upc, country, 'music', entities['song'], attribute, limit)
# Movies
def search_director(term: str, country: str = 'US', attribute: str = None, limit: int = 50) -> List[artist.Artist]:
# Unfortunately 'movieArtist' is not a valid entity so this method may return other artist types as well.
# Example: Search term 'Michael'
return search(term, country, 'movie', entities['movieArtist'], attribute, limit) # type: ignore
def search_movie(term: str, country: str = 'US', attribute: str = None, limit: int = 50) -> List[track.Track]:
return search(term, country, 'movie', entities['movie'], attribute, limit) # type: ignore
def lookup_directory(id: Union[str, int], country: str = 'US', attribute: str = None, limit: int = 50) -> List[result_item.ResultItem]:
return lookup(id, None, None, country, 'movie', entities['movieArtist'], attribute, limit)
def lookup_movie(id: Union[str, int], country: str = 'US', attribute: str = None, limit: int = 50) -> List[result_item.ResultItem]:
return lookup(id, None, None, country, 'movie', entities['movie'], attribute, limit)
# Books
def search_book(term: str, country: str = 'US', attribute: str = None, limit: int = 50) -> List[result_item.ResultItem]:
return search(term, country, 'ebook', entities['ebook'], attribute, limit)
def search_book_author(term: str, country: str = 'US', attribute: str = None, limit: int = 50) -> List[artist.Artist]:
# Unfortunately 'ebookAuthor' is not a valid entity so this method may return other artist types as well.
# Example: Search term 'test'
return search(term, country, 'ebook', entities['ebookAuthor'], attribute, limit) # type: ignore
def lookup_book(id: Union[str, int] = None, artist_amg_id: Union[str, int] = None, upc: Union[str, int] = None, country: str = 'US', attribute: str = None, limit: int = 50) -> List[result_item.ResultItem]:
return lookup(id, artist_amg_id, upc, country, 'ebook', entities['ebook'], attribute, limit)
def lookup_book_author(id: Union[str, int] = None, artist_amg_id: Union[str, int] = None, upc: Union[str, int] = None, country: str = 'US', attribute: str = None, limit: int = 50) -> List[result_item.ResultItem]:
return lookup(id, artist_amg_id, upc, country, 'ebook', entities['ebookAuthor'], attribute, limit)
# TV Shows
def search_tv_episodes(term: str, country: str = 'US', attribute: str = None, limit: int = 50) -> List[result_item.ResultItem]:
return search(term, country, 'tvShow', entities['tvEpisode'], attribute, limit)
def search_tv_season(term: str, country: str = 'US', attribute: str = None, limit: int = 50) -> List[result_item.ResultItem]:
return search(term, country, 'tvShow', entities['tvSeason'], attribute, limit)
def lookup_tv_episodes(id: Union[str, int] = None, artist_amg_id: Union[str, int] = None, upc: Union[str, int] = None, country: str = 'US', attribute: str = None, limit: int = 50) -> List[result_item.ResultItem]:
return lookup(id, artist_amg_id, upc, country, 'tvShow', entities['tvEpisode'], attribute, limit)
def lookup_tv_season(id: Union[str, int] = None, artist_amg_id: Union[str, int] = None, upc: Union[str, int] = None, country: str = 'US', attribute: str = None, limit: int = 50) -> List[result_item.ResultItem]:
return lookup(id, artist_amg_id, upc, country, 'tvShow', entities['tvSeason'], attribute, limit)
# Software
def search_software(term: str, country: str = 'US', attribute: str = None, limit: int = 50) -> List[result_item.ResultItem]:
return search(term, country, 'software', entities['software'], attribute, limit)
def search_ipad_software(term: str, country: str = 'US', attribute: str = None, limit: int = 50) -> List[result_item.ResultItem]:
return search(term, country, 'software', entities['iPadSoftware'], attribute, limit)
def search_mac_software(term: str, country: str = 'US', attribute: str = None, limit: int = 50) -> List[result_item.ResultItem]:
return search(term, country, 'software', entities['macSoftware'], attribute, limit)
# --------
# Parameters
# --------
base_search_url = 'https://itunes.apple.com/search'
base_lookup_url = 'https://itunes.apple.com/lookup'
entities = {
'movieArtist': 'movieArtist',
'movie': 'movie',
'podcastAuthor': 'podcastAuthor',
'podcast': 'podcast',
'musicArtist': 'musicArtist',
'musicTrack': 'musicTrack',
'album': 'album',
'musicVideo': 'musicVideo',
'mix': 'mix',
'song': 'song',
'audiobookAuthor': 'audiobookAuthor',
'audiobook': 'audiobook',
'shortFilmArtist': 'shortFilmArtist',
'shortFilm': 'shortFilm',
'tvEpisode': 'tvEpisode',
'tvSeason': 'tvSeason',
'software': 'software',
'iPadSoftware': 'iPadSoftware',
'macSoftware': 'macSoftware',
'ebook': 'ebook',
'ebookAuthor': 'ebookAuthor',
'all': 'all',
'allArtist': 'allArtist',
'allTrack': 'allTrack'
}
general_no_connection = 'Cannot fetch JSON data'
search_error = 'No results found with the keyword '
artist_search_error = 'No artists found with the keyword '
album_search_error = 'No albums found with the keyword '
track_search_error = 'No tracks found with the keyword '
lookup_error = 'No results with the given parameters'
lookup_no_ids = 'No id, amg id or upc arguments provided'
# --------
# Private
# --------
def _get_result_list(json: List[Dict[str, Any]], country: str) -> List[result_item.ResultItem]:
"""
Analyzes the provided JSON data and returns an array of result_item(s) based on its content
:param json: Raw JSON data to analyze
:return: An array of result_item(s) from the provided JSON data
"""
result_list: List[result_item.ResultItem] = []
for item in json:
if 'country' not in item:
item['country'] = pycountry.countries.get(alpha_2=country).alpha_3
if 'wrapperType' in item:
# Music
if item['wrapperType'] == 'artist' and item['artistType'] == 'Artist':
music_artist_result = music_artist.MusicArtist(item)
result_list.append(music_artist_result)
elif item['wrapperType'] == 'collection' and item['collectionType'] == 'Album':
music_album_result = music_album.MusicAlbum(item)
result_list.append(music_album_result)
elif item['wrapperType'] == 'track' and item['kind'] == 'song':
music_track_result = track.Track(item)
result_list.append(music_track_result)
elif item['wrapperType'] == 'track' and item['kind'] == 'music-video':
music_video_result = track.Track(item)
result_list.append(music_video_result)
# Movies
elif item['wrapperType'] == 'artist' and item['artistType'] == 'Movie Artist':
movie_artist_result = movie_artist.MovieArtist(item)
result_list.append(movie_artist_result)
elif item['wrapperType'] == 'track' and item['kind'] == 'feature-movie':
movie_result = track.Track(item)
result_list.append(movie_result)
# Ebook Author
elif item['wrapperType'] == 'artist' and item['artistType'] == 'Author':
ebook_artist_result = ebook_artist.EbookArtist(item)
result_list.append(ebook_artist_result)
# Tv Shows
elif item['wrapperType'] == 'collection' and item['collectionType'] == 'TV Season':
tv_season_result = result_item.ResultItem(item)
result_list.append(tv_season_result)
elif item['wrapperType'] == 'track' and item['kind'] == 'tv-episode':
tv_episode_result = track.Track(item)
result_list.append(tv_episode_result)
# Software
elif item['wrapperType'] == 'software' and item['kind'] == 'software':
software_result = result_item.ResultItem(item)
result_list.append(software_result)
elif item['wrapperType'] == 'software' and item['kind'] == 'mac-software':
mac_software_result = result_item.ResultItem(item)
result_list.append(mac_software_result)
elif 'kind' in item and item['kind'] == 'ebook':
ebook_result = result_item.ResultItem(item)
result_list.append(ebook_result)
else:
unknown_result = result_item.ResultItem(item)
result_list.append(unknown_result)
return result_list
def _url_search_builder(
term: str,
country: str = 'US',
media: str = 'all',
entity: str = None,
attribute: str = None,
limit: int = 50) -> str:
"""
Builds the URL to perform the search based on the provided data
:param term: String. The URL-encoded text string you want to search for. Example: Steven Wilson.
The method will take care of spaces so you don't have to.
:param country: String. The two-letter country code for the store you want to search.
For a full list of the codes: http://en.wikipedia.org/wiki/%20ISO_3166-1_alpha-2
:param media: String. The media type you want to search for. Example: music
:param entity: String. The type of results you want returned, relative to the specified media type. Example: musicArtist.
Full list: musicArtist, musicTrack, album, musicVideo, mix, song
:param attribute: String. The attribute you want to search for in the stores, relative to the specified media type.
:param limit: Integer. The number of search results you want the iTunes Store to return.
:return: The built URL as a string
"""
query = {
"term": term,
"country": country,
"media": media,
"limit": limit
}
if entity is not None:
query["entity"] = entity
if attribute is not None:
query["attribute"] = attribute
return base_search_url + "?" + urlencode(query)
def _url_lookup_builder(id: Union[str, int] = None,
artist_amg_id: Union[str, int] = None,
upc: Union[str, int] = None,
country: str = 'US',
media: str = 'music',
entity: str = None,
attribute: str = None,
limit: int = 50) -> str:
"""
Builds the URL to perform the lookup based on the provided data
:param id: String. iTunes ID of the artist, album, track, ebook or software
:param artist_amg_id: String. All Music Guide ID of the artist
:param upc: String. UPCs/EANs
:param country: String. The two-letter country code for the store you want to search.
For a full list of the codes: http://en.wikipedia.org/wiki/%20ISO_3166-1_alpha-2
:param media: String. The media type you want to search for. Example: music
:param entity: String. The type of results you want returned, relative to the specified media type. Example: musicArtist.
Full list: musicArtist, musicTrack, album, musicVideo, mix, song
:param attribute: String. The attribute you want to search for in the stores, relative to the specified media type.
:param limit: Integer. The number of search results you want the iTunes Store to return.
:return: The built URL as a string
"""
query = {
"country": country,
"media": media,
"limit": limit
}
has_one_argument = False
if id is not None:
query["id"] = id
if artist_amg_id is not None:
query["amgArtistId"] = artist_amg_id
if upc is not None:
query["upc"] = upc
if entity is not None:
query["entity"] = entity
if attribute is not None:
query["attribute"] = attribute
return base_lookup_url + "?" + urlencode(query)