-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpy.py
178 lines (152 loc) · 4.16 KB
/
webpy.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# coding: utf-8
import web
import createSearchTree
import os
import mpdPlayer
import thread
import votedb
from web.contrib.template import render_cheetah
from Cheetah.Template import Template
import settings
import adminControls
render = render_cheetah('templates/')
urls = (
'/', 'index',
'/images/(.*)', 'images',
'/liked/(.*)', 'liked',
'/hated/(.*)', 'hated',
'/admin/(.*)', 'admin',
'/notification/', 'notification',
'/voted/', 'voted',
'/tracks/', 'tracks',
'/artists/', 'artists',
'/login', 'login',
'/reset', 'reset',
'/search/', 'search',
'/history/', 'history'
)
app = web.application(urls, globals())
class login:
def GET(self):
return render.login()
def POST(self):
passwd = web.input().passwd
try:
if passwd == settings.pw :
return render.admin()
else:
return render.login_error()
except:
return render.login_error()
class admin:
def POST(self, code):
if code == "resVotes" :
adminControls.resetAllVotes()
elif code == "resLastPlay":
adminControls.resetLastPlayed()
else:
pass
raise web.seeother('/')
# html page for votes
class liked:
def POST(self, id):
userip = web.ctx.ip
# user hat fuer einen song gestimmt
success , time= createSearchTree.handleVote(id, True, userip )
if success == 0:
# vote in db eingetragen
raise web.seeother('/voted/')
elif success == 1:
# user hat bereits fuer diesen song gestimmt
raise web.seeother('/notification/')
elif success == 2:
# song ist noch gesperrt
nameSpace = {'blockover': time, 'minutes': settings.repeatTime }
t= Template(file="templates/songblocked.html", searchList=[nameSpace])
return t
class hated:
def POST(self, id):
userip = web.ctx.ip
# user hat gegen einen song gestimmt
success , time = createSearchTree.handleVote(id, False, userip )
if success == 0:
# vote in db eingetragen
raise web.seeother('/voted/')
elif success == 1:
# user hat bereits fuer diesen song gestimmt
raise web.seeother('/notification/')
elif success == 2:
# song ist noch gesperrt
nameSpace = {'blockover': time, 'minutes': settings.repeatTime }
t= Template(file="templates/songblocked.html", searchList=[nameSpace])
return t
# user notification (vote was succesfull)
class voted:
def GET(self):
return render.voted()
# user notification (you already voted for this song)
class notification:
def GET(self):
return render.notification()
# show all tracks of an album
class tracks:
def GET(self):
urlInput = web.input(artist = '', album='')
artist = urlInput.artist
album = urlInput.album
if artist == "" or album == "":
requestHtml = createSearchTree.buildArtists()
return requestHtml
else:
tracksHtml = createSearchTree.buildTracks(album, artist)
return tracksHtml
# history page
class history:
def GET(self):
historyHtml = createSearchTree.buildHistory()
return historyHtml
# index page
class index:
def GET(self):
indexHtml = createSearchTree.buildIndex()
return indexHtml
# all artists page
class artists:
def GET(self):
urlInput = web.input(artist = '')
artist = urlInput.artist
if artist == "":
requestHtml = createSearchTree.buildArtists()
return requestHtml
else:
requestHtml = createSearchTree.buildAlben(artist)
return requestHtml
# search artist page
class search:
def GET(self):
searchHtml = createSearchTree.buildSearchpage()
return searchHtml
# handling images in website (albumart,..)
class images:
def GET(self,name):
ext = name.split(".")[-1] # Gather extension
# umlaute in cover pathnames
name = name.encode('utf-8', 'replace')
cType = {
"png":"images/png",
"jpg":"image/jpeg",
"gif":"image/gif",
"ico":"image/x-icon" }
if name in os.listdir('images'): # Security
web.header("Content-Type", cType[ext]) # Set the Header
web.header("Cache-Control", "public")
web.header("Expires", "Sun, 17 Jan 2038 19:14:07 GMT")
return open('images/%s'%name,"rb").read() # Notice 'rb' for reading images
else:
raise web.notfound()
if __name__ == "__main__":
# create votes per user count db
votedb.setupDB()
#make independant thread for musicplayer
thread.start_new_thread(mpdPlayer.manager, ())
app.run()