-
Notifications
You must be signed in to change notification settings - Fork 0
/
songkick.py
70 lines (55 loc) · 1.94 KB
/
songkick.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
'''
all songkick functions
'''
import requests
import config
from util import get_date
API_KEY = config.SONGKICK_API_KEY
# TO DO
# Try location with IP address
def get_events(location, start, end):
'''
wrapper function
'''
return get_artists(get_location_id(location), location, start, end)
def get_location_id(location):
'''
get location ID given a location
'''
request = 'https://api.songkick.com/api/3.0/search/locations.json?query={}&apikey={}'.format(
location, API_KEY)
response = requests.get(request)
for place in response.json()['resultsPage']['results']['location']:
if str(place['city']['displayName']).lower() == location.lower():
return place['metroArea']['id']
return None
def get_artists(location_id, location, start, end):
'''
get events given a location ID
'''
artists = set()
latest_date = start
end_date = end
page_number = 1
while latest_date < end_date:
response = get_response(location_id, page_number)
for event in response['resultsPage']['results']['event']:
if location.lower() in str(event['location']['city']).lower():
if event['start']['datetime']:
date = get_date(event['start']['datetime'][:10])
if latest_date < date:
latest_date = get_date(event['start']['datetime'][:10])
if event['performance']:
artists.add(
event['performance'][0]['artist']['displayName'])
page_number += 1
return list(artists)
def get_response(location_id, page_number):
'''
get response from songkick api
'''
parameters = {"page": page_number}
request = 'https://api.songkick.com/api/3.0/metro_areas/{}/calendar.json?apikey={}'.format(
location_id, API_KEY)
response = requests.get(request, params=parameters)
return response.json()