forked from andrewmilas10/HackMIT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
141 lines (117 loc) · 4.33 KB
/
app.py
File metadata and controls
141 lines (117 loc) · 4.33 KB
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
from distutils.log import debug
from flask import Flask, request, url_for, redirect
from flask_socketio import SocketIO, emit
from flask_socketio import join_room, leave_room
from room import Room
from flask_cors import CORS
import random
import uuid
import threading
import time
app = Flask(__name__, static_folder='./frontend/build', static_url_path='/')
socketio = SocketIO(app)
CORS(app)
import sys
import spotipy
from spotipy.oauth2 import SpotifyOAuth
scope = "user-read-currently-playing user-read-playback-state user-modify-playback-state"
client_id='f038c9e7ef86446fa418a6dbc29fe429'
client_secret='2b6c56181a484c0ca0464c811778574a'
redirect_uri='http://127.0.0.1:5000/callback'
prod_redirect_uri = "https://party-session.herokuapp.com/callback"
prod_url = "https://party-session.herokuapp.com/"
CACHE = '.spotipyoauthcache'
access_token = ""
rooms = {}
current_oauths = []
def looper_thread():
# print("doing stuff", file=sys.stdout)
while True:
for r in rooms:
# print("NEW LINES\n")
rooms[r].someFunction()
socketio.emit("update_state", rooms[r].sendDanAll(), to=r)
socketio.sleep(1)
# x.start()
# socketio.start_background_task(target = looper_thread)
@app.route('/')
def buildstatic():
return app.send_static_file('index.html')
@app.errorhandler(404)
def not_found(e):
return app.send_static_file('index.html')
print('test', file=sys.stdout)
@app.route('/login', methods=['GET', 'POST'])
def verify():
# sp_oauth = SpotifyOAuth(client_id, client_secret,redirect_uri,scope=scope, open_browser=True)
sp_oauth = SpotifyOAuth(client_id, client_secret,prod_redirect_uri,scope=scope, open_browser=True)
current_oauths.append(sp_oauth)
auth_url = sp_oauth.get_authorize_url()
print("redirect", auth_url, file=sys.stdout)
return redirect(auth_url)
@app.route('/callback', methods=['GET', 'POST'])
def index():
if len(current_oauths) == 0:
sp_oauth = SpotifyOAuth(client_id, client_secret,prod_redirect_uri,scope=scope, open_browser=True)
else:
sp_oauth = current_oauths[-1]
token_info = sp_oauth.get_cached_token()
if token_info:
access_token = token_info['access_token']
else:
url = request.url
code = sp_oauth.parse_response_code(url)
if code:
token_info = sp_oauth.get_access_token(code)
access_token = token_info['access_token']
if sp_oauth.is_token_expired(token_info):
token_info = sp_oauth.refresh_access_token(token_info['refresh_token'])
access_token = token_info['access_token']
if access_token:
sp = spotipy.Spotify(access_token)
room_id = uuid.uuid1()
rooms[str(room_id)] = Room(sp_oauth)
# return redirect('http://localhost:3000/' + str(room_id))
return redirect(prod_url + str(room_id))
return 'test'
@app.route('/search', methods=['GET', 'POST'])
def search():
params = request.get_json()['params']
room_id, query = params['room_id'], params['query']
return rooms[room_id].getSong(query)
@app.route('/upvote', methods=['GET', 'POST'])
def upvote():
params = request.get_json()['params']
room_id, songid = params['room_id'], params['song']['id']
rooms[room_id].addVote(songid, 1)
socketio.emit("update_state", rooms[room_id].sendDanAll(), to=room_id)
return ''
@app.route('/downvote', methods=['GET', 'POST'])
def downvote():
params = request.get_json()['params']
room_id, songid = params['room_id'], params['song']['id']
rooms[room_id].removeVote(songid, -1)
socketio.emit("update_state", rooms[room_id].sendDanAll(), to=room_id)
return ''
@app.route('/queue', methods=['GET', 'POST'])
def queue():
params = request.get_json()['params']
room_id, song = params['room_id'], params['song']
rooms[room_id].addtoQueue(song)
socketio.emit("update_state", rooms[room_id].sendDanAll(), to=room_id)
return ''
@socketio.on('join')
def on_join(data):
# username = data['username']
room = data['room']
join_room(room)
# send(username + ' has entered the room.', to=room)
@socketio.on('leave')
def on_leave(data):
# username = data['username']
room = data['room']
leave_room(room)
# send(username + ' has left the room.', to=room)
if __name__ == '__main__':
# socketio.start_background_task(target = looper_thread)
socketio.run(app)