This repository was archived by the owner on Nov 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsongkick.py
155 lines (113 loc) · 4.97 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
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
"""Functions for interacting with the Songkick API"""
import os
import requests
import arrow
SONGKICK_API_URL = "http://api.songkick.com/api/3.0"
def find_songkick_locations(search_term):
"""Return list of Songkick metro areas matching search term
Makes a GET request to Songkick API for location data using the term.
"""
# Make GET request to Songkick API for location
payload = {
'query': search_term,
'apikey': os.getenv('SONGKICK_KEY'),
}
loc_response = requests.get(SONGKICK_API_URL + "/search/locations.json",
payload)
# Create empty list of metro areas
metros = []
# If request is successful, get results from the response
if loc_response.ok:
# Add the locations to our list
metros = create_location_list(loc_response.json())
# Return list of metro areas for each location in results
return metros
def create_location_list(location_json):
"""Takes SK loc search results JSON and returns list of metro area dicts
Iterates through data in JSON to return list of metro area information.
Returns max of 5 areas and avoids repeated locations (duplicate metro ids)
"""
metro_list = []
results = location_json['resultsPage']['results']
# If the results are not empty, create empty set of metro ids
if results:
metro_id_set = set([])
# Iterate over each location in results
for loc in results['location']:
metro = loc['metroArea']
# Return a max of 5 matching areas
if len(metro_list) == 5:
return metro_list
# If metro area has not already been added to list of metros
elif metro['id'] not in metro_id_set:
# Add metro area to list
metro_list.append(metro)
metro_id_set.add(metro['id'])
return metro_list
def find_songkick_concerts(search_dict, location="sk:26330"):
"""Takes Spotify artist info and returns a list of concert dictionaries
Makes requests to the Songkick API for upcoming events in Songkick location
for the provided artist name
"""
songkick_key = os.getenv('SONGKICK_KEY')
# Create empty recommendation list
concert_recs_list = []
# Make GET request to songkick API for this location & artist
payload = {
'apikey': songkick_key,
'artist_name': search_dict['artist'],
'location': location,
}
event_response = requests.get(SONGKICK_API_URL + "/events.json", payload)
# If request is successful
if event_response.ok:
# Add the concerts to our list
concert_recs_list = create_concert_list(event_response.json(), search_dict)
# If request unsuccessful, print(error)
else: # pragma: no cover
artist = search_dict['artist']
print("Failed: {}".format(artist))
return concert_recs_list
def create_concert_list(event_json, search_dict):
"""Takes Songkick event search results JSON and returns list of concert dictionaries
Also takes a dictionary of the searched artist's information
"""
event_list = []
# Get list of events from response
events = event_json['resultsPage']['results'].get('event')
# If event list not empty
if events:
# Iterate over event list
for event in events:
# Create dictionary of concert's information
concert = {
'display_name': event['displayName'],
'songkick_id': event['id'],
'songkick_url': event['uri'],
'artist': search_dict['artist'],
'spotify_id': search_dict['spotify_id'],
'image_url': search_dict['image_url'],
'venue_name': event['venue']['displayName'],
'venue_lat': event['venue']['lat'],
'venue_lng': event['venue']['lng'],
'city': event['location']['city'],
'source': search_dict['source'],
}
# Set concert dict's start & end date & time
if event.get('start'):
if event['start']['datetime']:
concert['start_datetime'] = arrow.get(event['start']['datetime']).isoformat()
# Only use date if there's no time
else:
concert['start_datetime'] = arrow.get(event['start']['date']).isoformat()
concert['start_date'] = arrow.get(event['start']['date']).isoformat()
if event.get('end'):
if event['end']['datetime']:
concert['end_datetime'] = arrow.get(event['end']['datetime']).isoformat()
# Only use date if there's no time
else:
concert['end_datetime'] = arrow.get(event['end']['date']).isoformat()
concert['end_date'] = arrow.get(event['end']['date']).isoformat()
# Add concert to recommendation list
event_list.append(concert)
return event_list