-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.py
63 lines (52 loc) · 2.42 KB
/
index.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
from flask import Flask, render_template
import requests
import praw
import spotipy
import spotipy.util as util
import time
import datetime
app = Flask(__name__)
spotify_username = 'SPOTIFY_USERNAME'
spotify_playlist_id = 'SPOTIFY_PLAYLIST_ID'
scope = 'playlist-modify-public'
spotify_client_secret= 'SPOTIFY_CLIENT_SECRET'
spotify_client_id= 'SPOTIFY_CLIENT_ID'
spotify_redirect_uri= 'SPOTIFY_REDIRECT_URI'
reddit_client_id='REDDIT_CLIENT_ID'
reddit_client_secret='REDDIT_CLIENT_SECRET'
reddit_user_agent='YOUR_NAME'
@app.route('/')
def index():
r = praw.Reddit(user_agent=reddit_user_agent, client_id=reddit_client_id,client_secret=reddit_client_secret)
sp = spotipy.Spotify()
songs_to_add = []
token = util.prompt_for_user_token(spotify_username, scope, client_id=spotify_client_id,
client_secret=spotify_client_secret, redirect_uri=spotify_redirect_uri)
# dates that will be searched for new music on reddit
time_to = time.mktime(datetime.datetime.now().timetuple())
time_from = time.mktime((datetime.datetime.now() - datetime.timedelta(days=3)).timetuple())
for submission in r.subreddit('indieheads').submissions(time_from, time_to):
if '[FRESH]' in submission.title:
query = submission.title.replace('[FRESH]', '')
# search for the track on spotify and add to the playlist
results = sp.search(q=query, limit=1)['tracks']['items']
for n in results:
song_id = n['id']
# if the song is not already in the playlist and
# 100 songs have not already been found (max for spotipy)
# and the song is not already in the list of songs to add
if (len(songs_to_add) < 100) and (song_id not in songs_to_add):
songs_to_add.insert(len(songs_to_add), song_id)
if token:
sp = spotipy.Spotify(auth=token)
sp.trace = False
# add the tracks to the playlist on spotify
if len(songs_to_add) > 0:
print songs_to_add
sp.user_playlist_remove_all_occurrences_of_tracks(spotify_username, spotify_playlist_id, songs_to_add)
sp.user_playlist_add_tracks(spotify_username, spotify_playlist_id, songs_to_add)
else:
print("Can't get token for", spotify_username)
return render_template('index.html', title='Home', results=songs_to_add)
if __name__ == '__main__':
app.run()