-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
64 lines (47 loc) · 1.91 KB
/
main.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
from flask import Flask, request, redirect, url_for, render_template
import spotipy
from spotipy.oauth2 import SpotifyOAuth
from dotenv import load_dotenv
import os
import spotifySetup
load_dotenv()
client_id = os.getenv('ID')
client_secret = os.getenv('SECRET')
redirect_uri = os.getenv('REDIRECT')
username = os.getenv('USERNAME')
scope = 'user-modify-playback-state user-read-playback-state'
ngrok_authtoken = os.getenv('NGROK')
app = Flask(__name__)
app.secret_key = os.getenv('SECRET_KEY')
sp_oauth = SpotifyOAuth(scope=scope, client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri,
username=username)
token_info = sp_oauth.get_cached_token()
if not token_info:
spotifySetup.authenticate(scope, client_id, client_secret, redirect_uri, username)
sp = spotipy.Spotify(auth=token_info['access_token'])
@app.route('/', methods=['GET', 'POST'])
def index():
playback_info = sp.current_playback()
if playback_info is None:
current_track = {'artists': [{'name': 'No Song Playing'}]}
else:
current_track = playback_info['item']
if request.method == 'POST':
song_name = request.form.get('song_name')
if not song_name:
return "Error: No search query provided", 400
results = sp.search(q=song_name, limit=5, type='track')
return render_template('results.html', results=results['tracks']['items'], current_track=current_track)
return render_template('index.html', current_track=current_track)
@app.route('/play/<song_uri>')
def play(song_uri):
with open('log.txt', 'r') as file:
played_songs = file.readlines()
if song_uri+'\n' in played_songs:
return "Error: Song has already been played", 400
with open('log.txt', 'a') as file:
file.write(f"{song_uri}\n")
sp.add_to_queue(song_uri)
return redirect(url_for('index'))
if __name__ == '__main__':
app.run(debug=True)