-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtacklebox.py
More file actions
206 lines (170 loc) · 6.89 KB
/
Copy pathtacklebox.py
File metadata and controls
206 lines (170 loc) · 6.89 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#Tacklebox
#A nerdy diversion using the phish.net API
import json
import re
import http.client
import argparse
from bs4 import BeautifulSoup
import random
##func to get setlists
def getShow(show_type):
conn = http.client.HTTPSConnection("api.phish.net")
payload = "{}"
conn.request("GET", show_type, payload)
res = conn.getresponse()
data = res.read()
json_string = data.decode("utf-8")
parsed_json = json.loads(json_string)
return parsed_json
##since we're not a webpage we have to do some cleanup on the setlist string
def displaySetlist(sl_text):
sl_text = re.sub('Set 1: ', '\nSet 1: ',sl_text)
sl_text = re.sub('Set 2: ', '\nSet 2: ',sl_text)
sl_text = re.sub('Set 3: ', '\nSet 3: ',sl_text)
sl_text = re.sub('Encore: ', '\nEncore: ',sl_text)
sl_text = re.sub('Encore 2: ', '\nEncore 2: ',sl_text)
sl_text = re.sub('Encore 3: ', '\nEncore 3: ',sl_text)
encore = sl_text[sl_text.find('Encore: '):]
if encore.find('[1] ') == -1:
notes = ''
else:
notes = encore[encore.find('[1] '):]
sl_text = sl_text[0:sl_text.find('Encore: ')]
if encore.find('[1] ') != -1 :
encore = encore[0:encore.find('[1] ')]
sl_text = sl_text + encore + '\n' + notes
return sl_text
##Make a way to clean up the notes in case some of them are a little squirrley
def displayNotes(sl_notes):
sl_notes = re.sub('\nvia phish.net','via phish.net',sl_notes) #some setlists have the newline, so remove those that do
sl_notes = re.sub('via phish.net','\nvia phish.net',sl_notes)
return sl_notes
##Check JEMP for if Phish is playing
def jemp():
jemp_conn = http.client.HTTPSConnection("public.radio.co")
station = 'stations/sd71de59b3/status'
payload = "{}"
jemp_conn.request("GET", station, payload)
res = jemp_conn.getresponse()
data = res.read()
json_string = data.decode("utf-8")
now_playing = json.loads(json_string)
print("JEMP is currently playing: " + now_playing["current_track"]["title"])
song = now_playing["current_track"]["title"]
artist = song[0:7]
showdate = song[song.find("(")+1:song.find(")")]
if artist == 'Phish -':
print("Phish is playing!")
if int(showdate[-2:]) < 80:
year = str(int(showdate[-2:]) + 2000)
else:
year = str(int(showdate[-2:]) + 1900)
month = showdate[:-3]
month = "000" + month[:month.find("-")]
month = month[-2:]
day = showdate[:-3]
day = "000" + day[day.find("-")+1:]
day = day[-2:]
show = year + "-" + month + "-" + day
else:
print("Phish is not playing :(")
show = "1900-01-01"
return(show)
def jamcharts(songname):
show_type = '/v3/jamcharts/all?apikey=' + apikey
setlist = getShow(show_type)
songs = setlist["response"]["data"]
songid = -1
for x in range(0,len(songs)-1):
if songs[x]["song"] == songname:
songid = songs[x]["songid"]
if songid != -1:
show_type = '/v3/jamcharts/get?apikey=' + apikey + '&songid=' + str(songid)
jamchart = getShow(show_type)
jamchart = jamchart["response"]["data"]["entries"]
print("There are " + str(len(jamchart)) + " Jamchart entries for " + songname + ".")
x = random.randrange(0,len(jamchart)-1)
var = '/v3/setlists/get?apikey=' + apikey + '&showdate=' + jamchart[x]["showdate"]
else:
print("I'm sorry " + songname + " doesn't have any Jamchart listings. I'll display a random show")
var = '/v3/setlist/random?apikey=' + apikey
return var
def randYear(yr):
show_type = '/v3/shows/query?apikey=' + apikey + '&order=ASC&year=' + yr + '&artistid=1'
setlist = getShow(show_type)
if int(setlist["response"]["count"]) > 0:
i = int(setlist["response"]["count"])
i = random.randrange(0,i-1)
var = '/v3/setlists/get?apikey=' + apikey + '&showdate=' +setlist["response"]["data"][i]["showdate"]
else:
print("I'm sorry Phish didn't play in " + yr + ". I'll display a random show")
var = '/v3/setlist/random?apikey=' + apikey
return var
##---Begin Main Script------------------------------------------------------------------------------------------------
###Get API Key
fo = open("api.txt","r")
apikey = fo.read()
apikey = re.sub(' ', '',apikey)
apikey = re.sub('\n','',apikey)
fo.close()
callhome = True
#########################################
###set up arguments
parser = argparse.ArgumentParser(description='Welcome to Tacklebox! Your one stop shop for Phish setlists from the command line.')
group = parser.add_mutually_exclusive_group(required=False)
group.add_argument("--date", default="1900-01-01", help="Format = YYYY-MM-DD. Bad requests mean random show.")
group.add_argument("--previous", action="store_true", help="Brings back the last show pulled.")
group.add_argument("--latest", action="store_true", help="Brings back the latest complete show.")
group.add_argument("--jemp",action="store_true", help="Checks JEMP for live Phish. Random if not.")
group.add_argument("--tiph",action="store_true", help="Brings back Today in Phish History.")
group.add_argument("--progress",action="store_true", help="Brings back In Progress show. Or latest.")
group.add_argument("--song", default="Empty", type=str, help="Make sure to put songs in double quotes.")
group.add_argument("--year", default="1900", type=str, help="Four digit year. Random show if Phish didn't play.")
args = parser.parse_args()
#########################################
if args.previous == True:
callhome = False
print("Displaying the last show you retrieved.")
if args.date == '1900-01-01':
show_type = '/v3/setlist/random?apikey=' + apikey
else:
show_type = '/v3/setlists/get?apikey=' + apikey + '&showdate=' + args.date
if args.latest == True:
show_type = '/v3/setlists/latest?apikey=' + apikey
if args.tiph == True:
show_type = '/v3/setlists/tiph?apikey=' + apikey
if args.progress == True:
show_type = '/v3/setlists/progress?apikey=' + apikey
if args.jemp == True:
showdate = jemp()
if showdate == '1900-01-01':
print("Since Phish isn't playing here's the last show you got:")
callhome = False
else:
show_type = '/v3/setlists/get?apikey=' + apikey + '&showdate=' + showdate
if args.song != "Empty":
show_type = jamcharts(args.song)
if args.year != "1900":
show_type = randYear(args.year)
if callhome == True:
setlist = getShow(show_type)
else:
fo = open("setlist.json","r")
setlist_base = fo.read()
setlist = json.loads(setlist_base)
fo.close
if setlist["response"]["count"] != 0:
sl_text = BeautifulSoup(setlist["response"]["data"][0]["setlistdata"],"html.parser").text
sl_notes = BeautifulSoup(setlist["response"]["data"][0]["setlistnotes"],"html.parser").text
print("Show Date: " + setlist["response"]["data"][0]["showdate"])
print("Venue: " + BeautifulSoup(setlist["response"]["data"][0]["venue"],"html.parser").text)
print("Location: " + BeautifulSoup(setlist["response"]["data"][0]["location"],"html.parser").text)
print("\nSetlist: " + displaySetlist(sl_text))
print("\nNotes: " + displayNotes(sl_notes))
print("\nPhish.in link: http://phish.in/" + setlist["response"]["data"][0]["showdate"])
else:
print("There was no show on that date, try again.")
if callhome == True:
f = open("setlist.json","w+")
json.dump(setlist, f)
f.close()