forked from amcquade/fresh_script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfresh.py
311 lines (266 loc) · 11 KB
/
fresh.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
import praw
import re
import sys, os, json, webbrowser, textwrap
import spotipy
from configparser import ConfigParser
import argparse
from constants import pun_dict
from constants import ft_set
from models import User
# convert a string to a bool
def str2bool(v):
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
def createUser():
# read config file
try:
if not os.path.isfile('.config.ini'):
client_id = input('Enter your Client ID: ').strip()
client_secret = input('Enter your Client Secret: ').strip()
username = input('Enter your Username: ').strip()
enteringPlaylists = True
playlists = []
while enteringPlaylists:
playlists.append(input('Enter your Playlist ID:').strip())
enteringPlaylists = str2bool(input('Would you like to enter another playlist ID? ').strip())
redirect = input('Enter your Redirect URI: ').strip()
config = ConfigParser()
config['spotify'] = {
'client_id': client_id,
'client_secret': client_secret,
'username': username,
'playlist_id': ','.join(playlists),
'redirect_uri': redirect
}
with open('.config.ini', 'w') as f:
config.write(f)
else:
# parse config
parser = ConfigParser()
parser.read('.config.ini')
# spotify info
username = parser.get('spotify', 'username')
playlists = parser.get('spotify', 'playlist_id') #returns a comma separated list of playlists
client_id = parser.get('spotify', 'client_id')
client_secret = parser.get('spotify', 'client_secret')
redirect = parser.get('spotify', 'redirect_uri')
'''
TODO
config['youtube'] = {}
config['soundcloud'] = {}
'''
except:
print('config failure')
return User(username, client_id, client_secret, redirect, playlists)
def filter_tags(title):
"""
Removes tags from post title and adds them to a set.
Any tags such as [FRESH], (feat. J-Bobby), etc. will be removed from the title
and placed in a set (without surrounding punctuation). Titles are also lower-cased
and any dashes/extra white space are removed.
Parameters
----------
title : str
The non-spotify Reddit post title to be filtered.
Returns
-------
filtered_title : str
The filtered post title.
tags : set
Container for any removed tags.
"""
tags = set()
filtered_title = []
# separate tags from title
# assumes there are no erroneous parentheses/brackets
# ex. [FRESH] Lil Pump - Nice 2 Yeet ya [prod. by D4NNY]
# there may be issues if song name contains parentheses
tag = []
last_pun = None
add_to_tag = False
for character in title:
character = character.lower()
# beginning of tag
if character == '[' or character == '(':
if add_to_tag:
tag.append(character)
else:
last_pun = character
add_to_tag = True
# end of tag
elif character == ']' or character == ')':
if add_to_tag:
if pun_dict[character] == last_pun:
# separate multi-word tags
for add_tag in ''.join(tag).split():
tags.add(add_tag)
tag.clear()
add_to_tag = False
else:
tag.append(character)
# remove dashes if they occur outside of tags
elif character != '-':
if add_to_tag:
tag.append(character)
else:
filtered_title.append(character)
# remove extra spaces from title
filtered_title = ''.join(filtered_title).split()
# remove feat from end of title (if not in parentheses/brackets, improves Spotify search results)
i = 0
for i in range(len(filtered_title)):
if filtered_title[i] in ft_set:
i -= 1
break
filtered_title = filtered_title[:i+1]
filtered_title = ' '.join(filtered_title)
return filtered_title, tags
def extract_track_url(search):
"""
Get the first Spotify track url from a given search.
Extended description of function.
Parameters
----------
search : dict
Contains information relating to Spotify API track search request.
Returns
-------
url : str
Spotify URL for the first track received from search query.
"""
if 'tracks' in search:
tracks = search['tracks']
if 'items' in tracks:
items = tracks['items']
# take the first url we can find
for item in items:
if 'external_urls' in item:
external_urls = item['external_urls']
if 'spotify' in external_urls:
url = external_urls['spotify']
return url
def main():
user = createUser()
argparser = argparse.ArgumentParser(formatter_class=lambda prog: argparse.HelpFormatter(prog,max_help_position=40))
argparser.add_argument("-s", "--sort", help="sort by hot, new, rising, random_rising, controversial or top", type=str,
choices=['hot', 'new', 'rising', 'random_rising', 'controversial', 'top'])
argparser.add_argument("-l", "--limit", help="how many posts to grab", type=int)
argparser.add_argument("-t", "--threshold", help="only post with score above threshold", type=int)
argparser.add_argument("-ia", "--include-albums", help="include tracks from albums", action="store_true")
argparser.add_argument("-v", "--verbose", help="output songs being added and other info", action="store_true")
argparser.add_argument("-f", "--fresh", help="only add tracks with the [FRESH] tag", action="store_true")
args = argparser.parse_args()
verbose = True if args.verbose else False
fresh = args.fresh
l = args.limit if args.limit else False
choice = args.sort if args.sort else None
threshold = args.threshold if args.threshold else None
includeAlbums = True if args.include_albums else False
# connect to reddit bot
reddit = praw.Reddit('bot1')
subreddit = reddit.subreddit('hiphopheads')
# create spotipy obj
spotifyObj = spotipy.Spotify(auth=user.token)
spotifyObj.trace = False
tracks = []
if verbose:
print('Welcome to the HipHopHeads Fresh Script')
if not choice:
inputPrompt = textwrap.dedent("""\
Enter your desired sorting method:
hot
new
rising
random_rising
controversial
top
""")
choice = input(inputPrompt)
if not l:
l = int(input('enter post limit: '))
if not fresh:
fresh_input = input('Would you like to only add tracks tagged as [FRESH]? (y/n)')
if fresh_input.lower().strip() == "y":
fresh = True
else:
fresh = False
if choice.lower() == 'hot':
sub_choice = subreddit.hot(limit=l)
elif choice.lower() == 'new':
sub_choice = subreddit.new(limit=l)
elif choice.lower() == 'rising':
sub_choice = subreddit.rising(limit=l)
elif choice.lower() == 'random_rising':
sub_choice = subreddit.random_rising(limit=l)
elif choice.lower() == 'controversial':
sub_choice = subreddit.controversial(limit=l)
elif choice.lower() == 'top':
sub_choice = subreddit.top(limit=l)
else:
print("Unsupported sorting method")
sys.exit()
for sub in sub_choice:
if sub.domain == "open.spotify.com":
# check if post is a track or album
isMatch = re.search('(track|album)', sub.url)
if isMatch != None:
if verbose:
print("Post: ", sub.title)
print("URL: ", sub.url)
print("Score: ", sub.score)
print("------------------------\n")
# Discard post below threshold if given
if threshold and sub.score < threshold:
continue
# If fresh flag given, discard post if not tagged [FRESH]
if fresh and "[FRESH]" not in sub.title:
continue
# handle possible query string in url
url = sub.url.split('?')
formattedUrl = url[0] if url != None else sub.url
# handle adding tracks from albums
if includeAlbums and isMatch.group(1) == 'album':
tracksInAlbum = spotifyObj.album_tracks(formattedUrl)
trackIds = [item['external_urls']['spotify'] for item in tracksInAlbum['items']]
tracks.extend(trackIds)
# handle adding tracks
elif isMatch.group(1) == 'track':
tracks.append(formattedUrl)
else:
# handle non-spotify posts
title, tags = filter_tags(sub.title)
if 'discussion' not in tags:
if 'album' in tags or 'impressions' in tags:
# there is a pull request for this feature at the moment
# so I will leave it out for now
search = spotifyObj.search(title, type='album')
else:
search = spotifyObj.search(title, type='track')
if search:
track_url = extract_track_url(search)
if track_url:
tracks.append(track_url)
# handle remove duplicates of tracks before adding new tracks
if tracks != []:
try:
for playlist in user.playlist.split(','):
# retrive information of the tracks in user's playlist
existing_tracks = spotifyObj.user_playlist_tracks(user.username, playlist)
spotifyObj.user_playlist_remove_all_occurrences_of_tracks(user.username, playlist, tracks)
results = spotifyObj.user_playlist_add_tracks(user.username, playlist, tracks)
if verbose:
print('New Tracks added to ', spotifyObj.user_playlist(user.username, playlist, 'name')['name'], ': ', abs(existing_tracks['total'] - spotifyObj.user_playlist_tracks(user.username, playlist)['total']))
print()
except:
if results == [] and verbose:
print("no new tracks have been added.")
else:
print("an error has occured removing or adding new tracks")
if verbose:
print(tracks)
if __name__ == '__main__':
main()