-
Notifications
You must be signed in to change notification settings - Fork 1
/
concert.py
160 lines (139 loc) · 3.93 KB
/
concert.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
156
157
158
159
160
import json
import datetime
import requests
import sys
from currency import getRate
from places import get_lat_lng_pair
baseURI = 'https://app.ticketmaster.com'
api_key = open('api_keys.txt').read().split("\n")[2]
def getEvents(artist):
URI = baseURI + "/discovery/v2/events?apikey=%s&keyword=%s&locale=*&size=200" % (api_key, artist)
response = requests.get(URI)
try:
events = json.loads(response.content)['_embedded']['events']
return events
except:
return []
def getCity(event):
try:
return event['_embedded']['venues'][0]['city']['name']
except:
return ""
def getVenueName(event):
try:
return event['_embedded']['venues'][0]['name']
except:
return ""
def getPC(event):
try:
return event['_embedded']['venues'][0]['postalCode']
except:
return ""
def getAddress(event):
try:
address = ''.join(event['_embedded']['venues'][0]['address'].values())
return address
except:
return ""
def getLongitude(event):
try:
return event['_embedded']['venues'][0]['location']['longitude']
except:
return ""
def getLatitude(event):
try:
return event['_embedded']['venues'][0]['location']['latitude']
except:
return ""
def getDate(event):
try:
return event['dates']['start']['localDate']
except:
return ""
def getTime(event):
try:
if 'localTime' not in event['dates']['start']:
return "00:00:00"
return event['dates']['start']['localTime']
except:
return ""
def getCurency(event):
try:
return event['priceRanges'][0]['currency']
except:
return ""
def getPrice(event):
if 'priceRanges' not in event:
return []
ps = event['priceRanges']
if ps != [] :
minPrice = ps[0]['min']
for p in ps:
if p['min'] < minPrice:
minPrice = p['min']
currency = getCurency(event)
if currency != 'EUR':
rate = getRate(currency)
return int(float(minPrice*rate)*100)
else:
return int(float(minPrice)*100)
else :
return ps
def concertSoldOut(event):
if(getPrice(event) == []):
return True
else:
return False
# no API call to get URL, do this the old fashioned way
def getURL(event):
try:
url = event['url']
return url
except:
return ""
def getName(event):
try:
return event['name']
except:
return ""
def getEventJson(event):
time = datetime.datetime.strptime(getDate(event)+" "+getTime(event), "%Y-%m-%d %H:%M:%S").timestamp()
result = {
"name": getName(event),
"start": time,
"url": getURL(event),
"price_pp": getPrice(event)
}
return result
def getVenueJson(event):
result = {
"name": getVenueName(event),
"address": getAddress(event),
"postcode": getPC(event),
"city": getCity(event),
"longitude": getLongitude(event),
"latitude": getLatitude(event)
}
if result["longitude"] == "" or result["latitude"] == "":
query_string = "%s %s %s %s" % (result["name"], result["address"], result["postcode"], result["city"])
lat_lng_pair = get_lat_lng_pair(query_string)
result["latitude"] = str(lat_lng_pair[0])
result["longitude"] = str(lat_lng_pair[1])
return result
def getKeywordEvents(keyword):
events = getEvents(keyword)
result = []
for event in events:
temp = {}
try:
temp['event'] = getEventJson(event)
temp['venue'] = getVenueJson(event)
except KeyError:
break
#### uncomment to skip things without price info
#if temp['event']["price_pp"] != []:
# result.append(temp)
result.append(temp)
return result
if __name__ == "__main__":
print(getKeywordEvents(sys.argv[1]))