-
Notifications
You must be signed in to change notification settings - Fork 1
/
pyspotify-web.py
119 lines (102 loc) · 3.49 KB
/
pyspotify-web.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
import web
import logging
import logging.config
import threading
import json
import datetime
import thread
from web import form
from jukebox import Jukebox
from threading import Condition
from spotify import Link
logging.config.fileConfig("logging.conf")
web.jukebox = None
jukeboxCondition = Condition()
render = web.template.render('templates/', base='wrapper')
urls = (
'/', 'index',
'/login', 'login',
'/playlists', 'playlists',
'/listtracks/(.*)', 'listtracks',
'/play/(.*)/(.*)', 'play',
'/search', 'search',
'/logout', 'logout',
)
app = web.application(urls, globals())
def redirectWhenNotLoggedIn():
if not web.jukebox and not web.ctx.path == '/login' and not web.ctx.path == 'favicon.ico':
print "Redirecting back to login"
raise web.seeother('/login')
app.add_processor(web.loadhook(redirectWhenNotLoggedIn))
LoginForm = form.Form(
form.Textbox("username", form.notnull),
form.Password('password', form.notnull),
form.Button('Login'))
class login:
def GET(self):
loginForm = LoginForm()
return render.loginTemplate(loginForm)
def POST(self):
loginForm = LoginForm()
if not loginForm.validates():
return self.onBadLogin(loginForm)
else:
try:
web.jukebox = Jukebox(jukeboxCondition, loginForm.d.username, loginForm.d.password, True)
thread.start_new_thread(web.jukebox.connect, ())
jukeboxCondition.acquire()
jukeboxCondition.wait()
jukeboxCondition.release()
raise web.seeother('/')
except Exception as e:
logging.error(e)
raise e
def onBadLogin(self, loginForm):
return render.loginTemplate(loginForm);
class index:
def GET(self):
raise web.seeother('/playlists')
class status:
def GET(self):
web.header('Content-Type', 'application/json')
return json.dumps(GlobalsManager.getPlayer().getCurrentSongAsDict())
class playlists:
def GET(self):
for playlist in web.jukebox.ctr:
while(not playlist.is_loaded()):
pass
return render.playlists(web.jukebox.ctr)
class listtracks:
def GET(self, playlistId):
return render.listtracks(playlistId, web.jukebox.ctr[int(playlistId)])
class play:
def GET(self, playlistId, trackId):
web.jukebox.load(int(playlistId), int(trackId))
web.jukebox.play()
return render.nowPlaying(web.jukebox.ctr[int(playlistId)][int(trackId)])
class search:
def POST(self):
self.results = None
def search_finished(results, userdata):
resultsRendered = []
for a in results.tracks():
resultsRendered.append(dict(link=Link.from_track(a, 0), name=a.name()))
self.results = resultsRendered
jukeboxCondition.acquire()
jukeboxCondition.notify()
jukeboxCondition.release()
input = web.webapi.input('searchString')
searchString = input['searchString']
web.jukebox.search(searchString, search_finished)
jukeboxCondition.acquire()
jukeboxCondition.wait()
jukeboxCondition.release()
return render.searchResults(self.results)
class logout:
def GET(self):
web.jukebox.disconnect()
web.jukebox = None
raise web.seeother('/login')
if __name__=="__main__":
web.internalerror = web.debugerror
app.run()