-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleMPDVoteWebServerBottle.py
134 lines (112 loc) · 4.87 KB
/
SimpleMPDVoteWebServerBottle.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
from BallotServer import BallotServer
import json
import sys
if sys.version_info < (3, 0, 0):
import urllib
else:
import urllib.parse
from bottle import Bottle, run, get, request, response, static_file, redirect, abort
HTTP_OK = 200
HTTP_BAD_REQUEST = 400
HTTP_FORBIDDEN = 403
HTTP_NOT_FOUND = 404
class SimpleMPDVoteWebServer():
def __init__(self):
self.voting_disabled = False
self.bs = BallotServer('localhost', 6600)
self.app = Bottle()
self.setup_routes()
def setup_routes(self):
app = self.app
@app.get('/browse')
@app.get('/browse/')
@app.get('/browse/<path:path>')
def browsePath(path = '/'):
if self.voting_disabled:
abort(HTTP_NOT_FOUND, "Sorry voting is currently disabled!")
response.content_type = 'application/json'
if sys.version_info < (3, 0, 0):
path=urllib.unquote(path).decode('utf8')
else:
path=urllib.parse.unquote(path, encoding='utf-8')
listing = self.bs.getLsInfo(path)
return json.dumps(listing)
""" Add a file to the playlist """
@app.get('/queue/<path:path>')
def queueFile(path = None):
if self.voting_disabled:
abort(HTTP_NOT_FOUND, "Sorry voting is currently disabled!")
if not path:
abort(HTTP_NOT_FOUND, "The file you tried to queue does not exist!")
if sys.version_info < (3, 0, 0):
song_path=urllib.unquote(path).decode('utf8')
else:
song_path=urllib.parse.unquote(path, encoding='utf-8')
(songQueued, playlistPosition) = self.bs.queueSong(song_path)
if not songQueued and playlistPosition == -1:
abort(HTTP_NOT_FOUND, "The file you tried to queue does not exist!")
return { "songQueued": songQueued, "playlistPosition": playlistPosition }
""" Search in the database """
@app.get('/search/<path>')
def searchFile(path = None):
if self.voting_disabled:
abort(HTTP_NOT_FOUND, "Sorry voting is currently disabled!")
if not path:
abort(HTTP_OK, "[{{}}}]")
if sys.version_info < (3, 0, 0):
query=urllib.unquote(path).decode('utf8')
else:
query=urllib.parse.unquote(path, encoding='utf-8')
return json.dumps(self.bs.searchFile(query))
@app.get('/library.json')
def playlist():
if self.voting_disabled:
abort(HTTP_NOT_FOUND, "Sorry voting is currently disabled!")
response.content_type = 'application/json'
return self.bs.getLibraryAsJson()
@app.get('/playlist.json')
def playlist():
if self.voting_disabled:
abort(HTTP_NOT_FOUND, "Sorry voting is currently disabled!")
response.content_type = 'application/json'
return self.bs.getPlaylistAsJson()
@app.route('/vote/deactivate')
def deactivate():
if request.remote_addr != '127.0.0.1' and request.remote_addr != '::1':
abort(HTTP_FORBIDDEN, "Sorry, you're not allowed to deactivate the voting service!")
self.voting_disabled = True
print ("Voting disabled".format())
@app.route('/vote/activate')
def deactivate():
if request.remote_addr != '127.0.0.1' and request.remote_addr != '::1':
abort(HTTP_FORBIDDEN, "Sorry, you're not allowed to activate the voting service!")
self.voting_disabled = False
print ("Voting enabled".format())
@app.route('/vote/status')
def vote_status():
return str(not self.voting_disabled)
"""Process a vote with given id """
@app.get('/vote/<mpdId:int>')
def vote(mpdId = -1):
if self.voting_disabled:
abort(HTTP_NOT_FOUND, "Sorry voting is currently disabled!")
(songIdExists, newPosition) = self.bs.voteForMpdId(mpdId)
if songIdExists:
return {"newPosition": newPosition}
else:
abort(HTTP_NOT_FOUND, "The song with id {0} does not exist".format(mpdId))
@app.route('/<filename:path>')
def webapp(filename='index.html'):
if self.voting_disabled:
abort(HTTP_NOT_FOUND, "Sorry voting is currently disabled!")
return static_file(filename, root='ionic/mpdVoteClient/www/')
@app.route('/')
def redirect_to_webapp():
if self.voting_disabled:
abort(HTTP_NOT_FOUND, "Sorry voting is currently disabled!")
redirect('/index.html')
def run(self, _host='', _port=''):
run(self.app, host=_host, port=_port)
if __name__ == '__main__':
s = SimpleMPDVoteWebServer()
s.run('', 8080)