-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathitunesapi.py
121 lines (92 loc) · 3.73 KB
/
itunesapi.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
import urllib.request
import urllib.parse
import json
__all__ = [
"iTunesFindAlbum",
"iTunesFindSong",
"iTunesGetTracks",
"findAlbumArt"]
__version__ = "1.8"
def __getArt(search, entity, country):
#url = 'http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/wa/wsSearch?term=%s&country=%s&entity=%s' % (urllib.parse.quote(search), urllib.parse.quote(country), urllib.parse.quote(entity))
url = 'https://itunes.apple.com/search?term=%s&country=%s&entity=%s' % (
urllib.parse.quote(search), urllib.parse.quote(country), urllib.parse.quote(entity))
with urllib.request.urlopen(url) as r:
data = json.loads(
r.read().decode(
r.info().get_param('charset') or 'utf-8'))
return data
def __getTracks(collectionId, country):
#url = 'http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/wa/wsLookup?id=%s&entity=song' % (str(collectionId))
url = 'https://itunes.apple.com/lookup?id=%s&entity=song&country=%s' % (
str(collectionId), str(country))
with urllib.request.urlopen(url) as r:
data = json.loads(
r.read().decode(
r.info().get_param('charset') or 'utf-8'))
return data
def iTunesFindAlbum(search, dimensions=(600, 600, 'bb'), country="us"):
data = __getArt(search, "album", country)
results = []
for item in data['results']:
result = {
"collectionId": item['collectionId'],
"artistId": item['artistId'],
"artist": item['artistName'],
"name": item['collectionName'],
"genre": item['primaryGenreName'],
"date": item['releaseDate'] if 'releaseDate' in item else None,
"totalTracks": item['trackCount'],
"publisher": item['copyright'] if 'copyright' in item else None,
"image": item['artworkUrl100'].replace(
"100x100bb.jpg",
"%dx%d%s.jpg" %
dimensions)}
results.append(result)
return results
def iTunesFindSong(search, dimensions=(600, 600, 'bb'), country="us"):
data = __getArt(search, "song", country)
results = []
for item in data['results']:
if item['wrapperType'] != 'track':
continue
result = {
"trackId": item['trackId'],
"collectionId": item['collectionId'],
"name": item['trackName'],
"artist": item['artistName'],
"album": item['collectionName'],
"albumArtist": item['collectionArtistName'] if 'collectionArtistName' in item else None,
"genre": item['primaryGenreName'],
"date": item['releaseDate'] if 'releaseDate' in item else None,
"track": item['trackNumber'],
"totalTracks": item['trackCount'],
"image": item['artworkUrl100'].replace(
"100x100bb.jpg",
"%dx%d%s.jpg" %
dimensions)}
results.append(result)
return results
def iTunesGetTracks(collectionId, country="us"):
data = __getTracks(collectionId, country=country)
results = []
for item in data['results']:
if item['wrapperType'] == 'track':
result = {
"name": item["trackName"],
"track": item["trackNumber"],
"artist": item["artistName"],
"trackId": item["trackId"],
"disc" : item["discNumber"],
"totalDiscs" : item["discCount"],
}
results.append(result)
results.sort(key=lambda v: v["track"])
return results
#
# Below for testing only
#
if __name__ == '__main__':
from pprint import pprint
pprint(findAlbumArt("Damian Marley - Stony Hill"))
pprint(iTunesGetTracks(1462355433, country="es"))