· 6 years ago · Mar 11, 2020, 08:00 PM
1Authors: Madeleine Macaulay and Turi M.
2Date: 03.10.2020
3email:
4
5
6import requests, json
7
8API_KEY = '8db124574adbe37c5dfb650db59438b8'
9USER_AGENT = 'MusicProject205'
10
11def lastfm_get(payload):
12 # define headers and URL
13 headers = {'user-agent': USER_AGENT}
14 url = 'http://ws.audioscrobbler.com/2.0/'
15
16 # Add API key and format to the payload
17 payload['api_key'] = API_KEY
18 payload['format'] = 'json'
19
20 response = requests.get(url, headers=headers, params=payload)
21 return response
22
23r = lastfm_get({
24 'method': 'chart.gettopartists'
25})
26
27def jprint(obj):
28 # create a formatted string of the Python JSON object
29 text = json.dumps(obj, sort_keys=True, indent=4)
30 print(text)
31
32jprint(r.json()['artists']['@attr'])