-
Notifications
You must be signed in to change notification settings - Fork 1
/
groupme_to_spotify.py
387 lines (323 loc) · 13.1 KB
/
groupme_to_spotify.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
'''
Author: Chris Isacs
https://github.com/shen3443/groupme-to-spotify
'''
from groupy.client import Client
import spotipy
from spotipy.oauth2 import SpotifyOAuth
import json
from alive_progress import alive_bar
class GroupmeSpotifyPlaylistUpdate:
'''
Parses through Groupme Groupchat messages and finds links to Spotify
songs, then adds those songs to a Spotify playlist
'''
def __init__(self, input_dict):
'''
input dict : dictionary built with DataFromUser class with inputs
['gm_token',
'group_name',
'log_file',
'sp_client_id',
'sp_client_secret',
'sp_redirect',
'sp_username',
'sp_user_id',
'sp_playlist_id',
'playlist_name']
to act as dictionary keys
'''
# Assign paramaters to class attributes
self.__dict__.update(input_dict)
self.sp_scope = "playlist-modify-public"
# Groupme Client
gp_client = Client.from_token(self.gm_token)
# Groupme Groupchat
groupchat = self.find_groupchat(gp_client)
# Song URLs From Messages
song_urls = self.get_tracks_from_groupchat(
groupchat,
self.find_last_checked_message(self.log_file)
)
# If there are new songs in the groupchat:
if song_urls:
# Login to Spotify
sp_cred = SpotifyOAuth(
client_id=self.sp_client_id,
client_secret=self.sp_client_secret,
redirect_uri=self.sp_redirect,
username=self.sp_username,
scope=self.sp_scope
)
self.sp = spotipy.Spotify(client_credentials_manager=sp_cred)
# Add songs to Spotify playlist
self.add_songs_to_playlist(song_urls)
def find_groupchat(self, gp_client):
'''
Finds the correct groupchat by name and returns a groupy
group object. Will raise an exception if the group cannot
be found
'''
print(f"\nFinding {self.group_name} Group in GroupMe...")
found = False
# Iterate through groups the user is a meber of
for group in gp_client.groups.list_all(omit='memberships'):
# Find the right group by name
if group.name == self.group_name:
# Save that group
groupchat = group
found = True
# Raise an exception if there is no group with that name
if not found:
raise Exception('Group name not recognized')
print("Done")
# Return the group
return groupchat
def find_last_checked_message(self, log_file):
'''
Checks the log file to find the last message that has already
been checked (the last time the script was run) and returns
the message ID
If this is the first time running the script & no log file exists,
returns False
If the file is not found because input was incorrect, user is prompted
to try again with new input
'''
print("Finding new messages...")
# Try opening the file and reading it
try:
with open(log_file, 'r') as log:
last_checked_message = log.read()
print("\nLast checked message ID: ", last_checked_message)
# If the file doesn't exist (ie. first time running the program,
# or filename miss-entered) exeption will be triggered
except FileNotFoundError:
print("Log file not found...")
log_file = input(
"Re-enter log file name, or press enter"
"to parse all messages in group"
)
# If user passes input, recursively call method with new input
if log_file:
return self.find_last_checked_message(log_file)
# If no input is passed, return False to use all messages
return False
# Return the ID of the last checked message
return last_checked_message
def update_log(self, message_id):
'''
Overwrites the log file with the message ID of the most
recent message the script has parsed
'''
print("Updating log...")
# Open the log file in overwrite and write the new message ID
with open(self.log_file, 'w') as log:
log.write(message_id)
print("Done")
@staticmethod
def clean_url(text):
'''
Takes a string that may contain a url link and returns the
link if it exists.
'''
# Iterate through the characters in the text
for i in range(len(text)):
# Find the beginning of the https link
if text[i:i+5] == "https":
# Trim excess characters from the front
url = text[i:]
# Split the text by ' ', trimming anything added after the link
url_tokens = url.split()
# Return the URL
return url_tokens[0]
def get_tracks_from_groupchat(self, groupchat, last_checked_message):
'''
Parses through messages in the group chat to find URLs for
spotify songs
Returns a list of song URLs
'''
# Create a list to store song URLs
song_urls = []
# If a last checked message was identified
if last_checked_message:
messages = list(groupchat.messages.list_after(
last_checked_message).autopage())
with alive_bar(
len(messages), title='Scanning messages...\n'
) as bar:
for message in messages:
# Store the ID of the most recently checked message
last_message_id = message.id
if message.text:
if 'https://open.spotify.com/track/' in message.text:
# Isolate the spotify link
track_url = self.clean_url(message.text)
song_urls.append(track_url)
bar()
# If no last checked message was identified
else:
messages = list(groupchat.messages.list_all().autopage())
with alive_bar(
len(messages), title='Scanning messages...\n'
) as bar:
for message in messages:
# Store the ID of the most recently checked message
last_message_id = message.id
if message.text:
if 'https://open.spotify.com/track/' in message.text:
# Isolate the Spotify link
track_url = self.clean_url(message.text)
song_urls.append(track_url)
bar()
print(
f'Found {len(song_urls)} new songs to add to {self.playlist_name}'
)
# Update the log file with the ID of the most recent message in the
# group that has been checked
self.update_log(last_message_id)
return song_urls
def add_songs_to_playlist(self, song_urls):
'''
Takes a list of spotify song URLs and adds them to a
spotify playlist
'''
lsu = len(song_urls)
with alive_bar(
lsu,
title=(f'Adding {lsu} new songs to {self.playlist_name}...\n')
) as bar:
# Spotify API only accepts 100 songs at a time
while song_urls:
temp = song_urls[0:100]
song_urls = song_urls[100:]
self.sp.user_playlist_add_tracks(
self.sp_user_id,
self.sp_playlist_id,
temp
)
bar(incr=len(temp))
print("Done\n")
class DataFromUser:
def __init__(self, required_inputs):
self.required_inputs = required_inputs
def get_user_data(self):
'''
Asks user if they would like to use stored data. Either opens file
and returns the data, returns a call of get_inputs() to get new data,
or returns itself if the user input is invalid
'''
# Explain to the user what they are being asked
print(
"If this is not your first time running this program,"
" you may have a save file with all of the required data..."
)
print(
"If this is your first time, answer 'n' "
"to the following question..."
)
answer = input(
"Would you like to use stored data on login, group"
" and playlist info? (y/n): "
)
if answer == 'y':
# Get the file name
data_file = input(
"Enter the name of the .txt file you would like to use: "
)
try:
# Open the file, load and return the data
with open(data_file, 'r') as fp:
return json.loads(fp.read())
# If the file doesn't exist, call get_user_data() again
# for new input
except FileNotFoundError:
print("File not found...")
return self.get_user_data()
elif answer == 'n':
# Get and return new user inputs
return self.get_inputs()
else:
# Invalid inputs trigger a recursive call on the function
# for new input
print("Answer not recognized, please answer (y/n)")
return self.get_user_data()
def get_inputs(self):
'''
Takes a list of required information from user, gets user input for
each item in the list, then returns a dict where items from the list
are keys and the corresponding user inputs are values
'''
# Get inputs, store to dict
data = {i: input('Enter %s: ' % i) for i in self.required_inputs}
# Give user option to save the data
self.give_save_option(data)
# Return the data
return data
def give_save_option(self, data):
'''
Gives the user the option to save the data for future use.
Lets user input a filename, then writes the dictionary to
that file (using json.dumps()).
Returns the files name if a file is written, False if not
Recursively returns a call on itself if input is invalid
'''
yn = input("Would you like to save inputs for future use? (y/n): ")
if yn == 'y':
valid = False
# While loop repeats until a valid file name is given
while not valid:
print(
"WARNING: program will overwrite an existing file if you "
"give a file name already in use in this folder"
)
filename = input(
"What would you like to name the save file"
" (must end in .txt): "
)
# Check that file name is valid
if filename[-4:] == '.txt':
print("Saving to %s..." % (filename))
# Write data to file
with open(filename, 'w') as fp:
fp.write(json.dumps(data))
# Break out of while loop
print("Done")
valid = True
else:
# Inform user filename was invalid and cycle back
# through while loop
print("File name must end in .txt")
print("Trying again...")
# Return the file name
return filename
elif yn == 'n':
# User does not want to save, return False
return False
else:
# Input is invalid, return recursive call to let user try again
print("Answer not recognized, please answer (y/n)")
return self.give_save_option(data)
def main():
required_inputs = [
'gm_token',
'group_name',
'log_file',
'sp_client_id',
'sp_client_secret',
'sp_redirect',
'sp_username',
'sp_user_id',
'sp_playlist_id',
'playlist_name'
]
print(
'\n***Please see README file for explanation of required inputs'
' & instructions on how to find them***\n'
)
print(
'https://github.com/shen3443/groupme-to-spotify/blob/master/README.md'
)
print()
GroupmeSpotifyPlaylistUpdate(DataFromUser(required_inputs).get_user_data())
if __name__ == "__main__":
main()