-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkexp.py
149 lines (117 loc) · 5.62 KB
/
kexp.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
import datetime
import requests
import spotipy
import spotipy.util as sputil
PLAY_URL = 'https://legacy-api.kexp.org/play/?limit=200&end_time={}&ordering=-airdate&channel=1'
SHOW_URL = 'https://legacy-api.kexp.org/show/?showid='
def make_url(time) -> str:
"""create the URL to get all the plays for a single date"""
return PLAY_URL.format(time)
def plays_for_date(time):
"""fetch all the plays for a single date"""
url = make_url(time)
response = requests.get(url)
return response.json()['results']
def scrape_date(time):
plays = plays_for_date(time)
# `plays` has a lot of repeated info, so we gzip the output files, which
# gets them about 10x smaller. this probably isn't entirely necessary, but
# I was running this on an ec2 instance without a lot of disk space.
# with gzip.open('{}/{}.txt.gz'.format(output_dir, date.isoformat()), 'w') as f:
# for play in plays:
# f.write(bytes(json.dumps(play) + "\n", 'utf-8'))
return plays
def no_john_or_cheryl(cur_show):
no_john = True;
show_response = requests.get(SHOW_URL + str(cur_show))
shows = show_response.json()['results']
if (len(shows) < 1):
print("Play with less than 1 show")
return False
if (len(shows) > 1):
print("Play with multiple shows")
return False
cur_show_name = shows[0]["program"]["name"]
hosts = shows[0]['hosts']
print("show = " + shows[0]["program"]["name"])
if "Street Sounds" in cur_show_name:
return True
for host in hosts:
print("host = " + host['name'])
is_john = host['name'] == "John Richards" or host['name'] == "Cheryl Waters"
no_john = no_john and not is_john
return no_john
if __name__ == "__main__":
token = sputil.prompt_for_user_token("", client_id='',
client_secret='',
redirect_uri='http://www.kexp.org', scope='playlist-modify-public')
sp = spotipy.Spotify(auth=token)
plays = []
month_string = '2020-10-'
playlists = {}
visited_plays = []
for i in range(10, 18):
date = month_string + str(i)
print("reading data for {}".format(date))
# 21 is UTC for 2 PM in Seattle now (it'll be 22 later when Seattle is 8 behind and not 7)
plays = scrape_date(date + "T" + "22:15:00" + "Z")
cur_show = 0
cur_show_name = None
no_john = True
last_is_not_john = False
is_monday = datetime.datetime.strptime(plays[-1]['airdate'], "%Y-%m-%dT%H:%M:%SZ").weekday()
while (plays and (is_monday or not last_is_not_john)):
last_play = plays[-1]
is_monday = 0 == datetime.datetime.strptime(last_play['airdate'], "%Y-%m-%dT%H:%M:%SZ").weekday()
last_is_not_john = no_john_or_cheryl(last_play['showid'])
if (not last_is_not_john) or is_monday:
plays.extend(scrape_date(last_play['airdate']))
else:
print("verified that last track isn't John's or Cheryl's")
prev_url = None
for play in plays:
if (play['playid'] in visited_plays):
print("skipping play number:" + str(play['playid']))
continue
visited_plays.append(play['playid'])
if (play['showid'] != cur_show):
prev_show = cur_show
cur_show = play['showid']
no_john = no_john_or_cheryl(cur_show)
if no_john:
print("Show without John or Cheryl: " + str(cur_show))
else:
show_response = requests.get(SHOW_URL + str(cur_show))
shows = show_response.json()['results']
cur_show_name = shows[0]["program"]["name"] + ' ' + date
if not cur_show_name in playlists:
playlists[cur_show_name] = []
hosts = shows[0]['hosts']
print("Show with " + hosts[0]['name'] + " " + str(cur_show))
if prev_show == 0:
assert (no_john)
print("verified that first track isn't John's or Cheryl's")
if (not no_john and play['artist'] and play['track']):
query_string = 'artist:' + play['artist']['name'] + ' track:' + play['track']['name']
results = sp.search(q=query_string, limit=1)
if len(results['tracks']['items']) > 0:
track = results['tracks']['items'][0]
if prev_url == track['uri']:
print("just saw " + track['name'] + ' ' + track['uri'])
else:
print(' ', track['name'] + ' ' + track['uri'])
playlists[cur_show_name].append(track['uri'])
prev_url = track['uri']
else:
print('FAILED TO FIND TRACK: ' + query_string)
assert (no_john)
print("verified that last track isn't John's or Cheryl's")
for kexp_playlist in playlists:
if (len(playlists[kexp_playlist]) > 0):
playlist = sp.user_playlist_create('dzolotusky', 'KEXP - ' + kexp_playlist, True)
playlists[kexp_playlist].reverse()
sp.user_playlist_add_tracks(playlist_id=playlist['id'], user='dzolotusky', tracks=playlists[kexp_playlist])
print('Created ' + playlist['id'] + ' with ' + str(len(playlists[kexp_playlist])) + ' tracks')
print("Found " + str(len(playlists[kexp_playlist])) + " tracks for " + kexp_playlist)
else:
print('Did not create playlist, no tracks found to add')