-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.py
248 lines (205 loc) · 9.16 KB
/
server.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/usr/bin/python
# -*- coding: utf8 -*-
from bottle import route, run, template, request, static_file, post, get, redirect, error, response
import subprocess
import sys
import time
import urllib
import urllib2
import bottle
from urlparse import urlparse
from Constants import websites, actionDesc
from Helper import newFifo, newDir
from database import db_getHistory, db_delete
from player import Controller
from config import get_cfg
from webparser import Video
bottle.debug = True
import logging
import logging.handlers
handler = logging.handlers.RotatingFileHandler(get_cfg('logStorage'), maxBytes=1000000, backupCount=2)
formatter = logging.Formatter(fmt='%(asctime)s %(threadName)s %(module)s:%(lineno)d %(levelname)s: %(message)s')
handler.setFormatter(formatter)
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.addHandler(handler)
def exceptionLogger(type, value, tb):
logging.exception("Uncaught exception: %s", value)
sys.__excepthook__(type, value, tb)
sys.excephook = exceptionLogger
controller = None
current_website = None
def _render(wait=False, redirect_to=None):
if (wait):
while not (controller.currentPlayer and controller.currentPlayer.video and controller.currentPlayer.isAlive()):
time.sleep(0.1)
if redirect_to:
redirect(redirect_to)
if controller and controller.currentPlayer and controller.currentPlayer.video and controller.currentPlayer.isAlive():
return template("index", websites=websites, currentVideo=controller.currentPlayer.video, actionDesc=actionDesc,
history=db_getHistory())
else:
return template("index", websites=websites, currentVideo=None, actionDesc=actionDesc,
history=db_getHistory())
@route('/')
def index():
return _render()
@route('/play')
def history_play():
global controller
if controller:
controller.stopAll()
controller = Controller(None)
controller.parseHistory(request.query.id, request.query.start)
_render(wait=True, redirect_to='/')
@route('/static/<filename:path>')
def static(filename):
return static_file(filename, root='static')
@post('/control/<action>')
def control(action):
feedback = ""
if controller and controller.currentPlayer.isAlive():
feedback = "OK"
if action == "stop":
controller.stopAll()
elif action == "pause":
controller.currentPlayer.toggle_pause()
elif action == "volup":
controller.currentPlayer.volup()
elif action == "voldown":
controller.currentPlayer.voldown()
else:
feedback = "Not implemented action: " + action
else:
feedback = "Sorry but I can't find any player running."
return feedback
@get('/history')
def history():
videos = db_getHistory()
responseString = ""
for video in videos:
responseString += """
<li>
<a href="/play?id=%s&start=-1" class="ui-link-inherit" data-ajax="false">
<h3>%s(%s)</h3>
<p>总共%s(上次播放到%s)</p>
</a>
<a href="#" onclick="deleteHistory('%s');return false" class="split-button-custom ui-btn ui-btn-up-b ui-shadow ui-btn-corner-all ui-btn-icon-notext" data-role="button" data-icon="delete" data-iconpos="notext" data-corners="true" data-shadow="true" data-iconshadow="true" data-wrapperels="span" data-theme="b" title=""><span class="ui-btn-inner"><span class="ui-btn-text"></span><span class="ui-icon ui-icon-delete ui-icon-shadow"> </span></span></a>
<a href="/play?id=%s&start=0" class="split-button-custom ui-btn ui-btn-up-b ui-shadow ui-btn-corner-all ui-btn-icon-notext" data-role="button" data-icon="refresh" data-iconpos="notext" data-ajax="false" data-corners="true" data-shadow="true" data-iconshadow="true" data-wrapperels="span" data-theme="b" title=""><span class="ui-btn-inner"><span class="ui-btn-text"></span><span class="ui-icon ui-icon-refresh ui-icon-shadow"> </span></span></a> <a href="#" style="display: none;">Dummy</a>
</li>
""" % (video.dbid, video.title, websites[video.site]['title'],
video.durationToStr(), Video.formatDuration(video.progress), video.dbid, video.dbid)
return responseString
@post('/delete/<id>')
def delete(id):
db_delete(id)
@post('/clear')
def clear():
db_delete(-1)
@get('/favicon.ico')
def favicon():
return static_file('favicon.ico', '.')
@get('/progress')
def get_progress():
if controller and controller.currentPlayer and controller.currentPlayer.isAlive():
currentVideo = controller.currentPlayer.video
currentVideo.progress = controller.currentPlayer.video.progress
return {"title": currentVideo.title, "progress": str(currentVideo.progress), "duration": currentVideo.durationToStr()}
else:
return {"title": "N/A", "progress": "0", "duration": "N/A"}
@post('/goto/<where>')
def goto(where):
controller.goto(where)
return "OK"
@route('/forward')
def forward():
global current_website, controller
format, dbid = None, None
if 'site' in request.query:
current_website = request.query.site
if 'format' in request.query:
format = int(request.query.format)
if 'dbid' in request.query:
dbid = request.query.dbid
url = request.query.url
# if it's the same url with the nextPlayer then just stop the current player.
if controller and controller.nextPlayer and urllib2.unquote(url) == urllib2.unquote(controller.nextPlayer.video.url):
controller.stopCurrentPlayer()
_render(wait=True, redirect_to='/')
logging.info("Forwarding to %s", url)
if current_website and url.startswith('/'):
url = websites[current_website]['url'] + url
logging.debug("forward to url: %s", url)
if current_website == 'youtube' and 'search_query' in request.query:
url = "%s?%s" % ('http://www.youtube.com/results', request.query_string)
logging.debug("The url for youtube search is: %s", url)
if current_website == 'youku' and 'searchdomain' in request.query:
url = "http://www.soku.com/search_video/q_" + urllib.quote_plus(request.query.q.encode('utf8'))
logging.debug("The url for youku search is: %s", url)
if current_website in ('qq', 'qqmp4') and 'ms_key' in request.query:
url = "http://v.qq.com/search.html"
logging.debug("The url for youku search is: %s", url)
if current_website == 'wangyi' and 'vs' in request.query and not url:
url = "http://so.open.163.com/movie/search/searchprogram/ot0/%s/1.html?vs=%s&pltype=2" \
% (request.query.vs, request.query.vs)
logging.debug("The url for wangyi search is: %s", url)
if current_website == 'sohu' and 'wd' in request.query and not url:
url = "http://so.tv.sohu.com/mts?box=1&wd=%s" % (request.query.wd)
logging.debug("The url for sohu search is: %s", url)
newController = Controller(current_website)
if dbid:
res = newController.parseHistory(dbid, -1, format)
else:
res = newController.parse(url, format)
if res:
return res
else:
if controller:
controller.stopAll()
controller = newController
_render(wait=True, redirect_to='/')
@get('/shutdown')
def shutdown():
subprocess.call(['sync'])
subprocess.Popen('sleep 2; poweroff', shell=True)
@get('/restart')
def restart():
subprocess.call(['sync'])
subprocess.Popen('sleep 2; reboot', shell=True)
@error(404)
def error404(error):
global current_website
logging.debug("404 on url: %s", request.url)
if request.url == "http://192.168.1.100/jsonrpc":
return
p_results = urlparse(request.url)
to_replace = p_results.scheme + "://" + p_results.netloc
if request.url.endswith('.css'):
redirectTo = request.url.replace(to_replace, websites[current_website]['url'])
else:
redirectTo = "%s?site=%s&url=%s" % ('/forward', current_website,
urllib2.quote(request.url.replace(to_replace,
websites[current_website]['url'])))
if current_website == 'youku' and request.url.startswith("/search_video"):
redirectTo = "%s?site=%s&url=%s" % ('/forward', current_website,
urllib2.quote(request.url.replace(to_replace,
"http://www.soku.com")))
logging.debug("Redirect to %s", redirectTo)
response.set_header('location', redirectTo)
response.status = 303
@error(500)
def error500(error):
return ("Exception: %s\nDetails: %s" % (error.exception, error.traceback)).replace('\n', '<br>')
newFifo('/tmp/cmd')
newFifo('/tmp/all.ts')
newDir('/tmp/ffmpeg_part')
newDir('/tmp/download_part')
for i in range(1000):
newFifo('/tmp/download_part/%s' % i)
try:
if sys.platform == 'darwin':
run(host='0.0.0.0', port=7777, reloader=True)
else:
run(host='0.0.0.0', port=80, quiet=True)
except:
logging.exception("Exception catched")