-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathitunes_api.py
More file actions
66 lines (49 loc) · 1.54 KB
/
itunes_api.py
File metadata and controls
66 lines (49 loc) · 1.54 KB
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
"""
iTunes Search API:
https://www.apple.com/itunes/affiliates/resources/documentation/itunes-store-web-service-search-api.html
"""
import json
from operator import itemgetter
# PyPi
import requests
###
COUNTRY_CODE = 'fi'
###
def search_album(query, entity, limit):
main_array = []
base_url = 'https://itunes.apple.com/' + COUNTRY_CODE + '/search?term='
url = base_url + query + '&entity=' + entity + '&limit=' + str(limit)
try:
response = requests.get(url)
data_dict = response.json()
except Exception as e:
print(str(e))
# Quick and dirty iteration
for item in data_dict["results"]:
if (item["artistName"].lower()) == query.lower():
if item.get("artistName", None):
artist_name = item["artistName"]
if item.get("collectionName", None):
album_name = item["collectionName"]
if item.get("collectionPrice", None):
album_price = item["collectionPrice"]
else:
album_price = "n/a"
if item.get("artworkUrl60", None):
album_cover = item["artworkUrl60"]
if item.get("releaseDate", None):
release_date = item["releaseDate"]
sub_array = [artist_name,
album_name,
album_price,
album_cover,
release_date]
main_array.append(sub_array)
# Sort by releaseDate, latest first
main_array = sorted(main_array, key=itemgetter(4), reverse=True)
return main_array
def main():
data = search_album('Anthrax', 'album', 100)
print(data)
if __name__ == '__main__':
main()