forked from grasses/NetEase-Wireless-MusicBox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.py
executable file
·352 lines (312 loc) · 12.4 KB
/
index.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/usr/local/Cellar/python
# -*- coding: utf-8 -*-
# @Author: homeway
# @Link: http://homeway.me
# @Version: 15.03.17
import tornado.auth
import tornado.escape
import tornado.ioloop
import tornado.options
import tornado.httpserver
import tornado.web
import os.path
import json
import time
import random
# upload
import shutil
import os
#url
import urlparse
import logging
#import Image
#import exifread
#import tempfile
# cookie_secret
import base64
import uuid
import hashlib
# mongodb
# import pymongo
# session
#from mongosion import Session
#import session
from tornado.escape import json_encode
from tornado.options import define, options
#some global information like session
import play
import api
# 先初始化播放器
global player
# mongodb数据库
option = {
'host':'localhost',
'port': 27017,
'db':'MusicBox',
}
player = play.Play(option)
define("port", default=80, help="run on the given port", type=int)
define("db", default="MusicBox", help="database name", type=str)
define("db_host", default='localhost', help="database host", type=str)
define("db_port", default=27017, help="database port", type=int)
define("db_user", default='root', help="database user", type=str)
define("db_pwd", default='xiaocao', help="database password", type=str)
NetEase = api.NetEase()
login = NetEase.login('username', 'password')
title = u"点歌台"
class Application(tornado.web.Application):
'''setting || main || router'''
def __init__(self):
handlers = [
#for html
(r"/", MainHandler),
(r"/song.html", GetSongHandler),
(r"/album.html", GetAlbumHandler),
(r"/play-list.html", GetPlayListHandler),
#for ajax
(r"/ajaxSearch", AjaxSearchHandler),
(r"/ajaxGetSong", AjaxGetSongHandler),
(r"/ajaxAlbum", AjaxGetAlbumHandler),
(r"/ajaxLogin", AjaxLoginHandler),
(r"/ajaxOrderMusic", AjaxOrderMusicHandler),
(r"/ajaxPlayMusic", AjaxPlayMusicHandler),
(r"/ajaxPauseMusic", AjaxPauseMusicHandler),
(r"/ajaxPlayNextMusic", AjaxPlayNextMusicHandler),
(r"/ajaxNewAlbums", AjaxNewAlbumsHandler),
(r"/ajaxHotSong", AjaxHotSongHandler),
(r"/ajaxSetVolume", AjaxSetVolumeHandler),
]
settings = dict(
cookie_secret=base64.b64encode(uuid.uuid4().bytes + uuid.uuid4().bytes),
base_path=os.path.join(os.path.dirname(__file__), ""),
template_path=os.path.join(os.path.dirname(__file__), "templates"),
static_path=os.path.join(os.path.dirname(__file__), "static"),
xsrf_cookies=False,
debug=True,
)
tornado.web.Application.__init__(self, handlers, **settings)
################################## html ############################################
class MainHandler(tornado.web.RequestHandler):
def initialize(self):
'''database init'''
self.sid = self.get_secure_cookie("sid")
#self.data = session.get(self.sid)
#self.set_secure_cookie("sid",self.data['_id'])
def get(self):
self.set_header("Accept-Charset", "utf-8")
NetEase.refresh()
new_albums = NetEase.artists('4292')
self.render("index.html", title=title, base_url=base_url, data=new_albums[0])
class GetPlayListHandler(tornado.web.RequestHandler):
def initialize(self):
pass
def get(self):
self.set_header("Accept-Charset", "utf-8")
res = {}
res['current'] = player.current_playing_music()
# print res['current']
res['list'] = player.get_play_list(1, 500)
res['volume'] = player.getVolume()
res['status'] = player.status()
# self.write( tornado.escape.json_encode(res) )
self.render("play-list.html", title=title, data=res)
class GetSongHandler(tornado.web.RequestHandler):
def initialize(self):
pass
def get(self):
self.set_header("Accept-Charset", "utf-8")
NetEase.refresh()
req = { 'sid':self.get_argument("sid") }
res = NetEase.song_detail( req['sid'] )
#self.write( tornado.escape.json_encode(res) )
self.render("song.html", title=title, data=res)
class GetAlbumHandler(tornado.web.RequestHandler):
def initialize(self):
pass
def get(self):
self.set_header("Accept-Charset", "utf-8")
NetEase.refresh()
req = { 'aid':self.get_argument("aid") }
res = NetEase.album( req['aid'] )
#self.write( tornado.escape.json_encode(res) )
self.render("album.html", title=title, data=res)
################################## ajax ############################################
#搜索信息
class AjaxSearchHandler(tornado.web.RequestHandler):
def initialize(self):
'''database init'''
self.sid = self.get_secure_cookie("sid")
#self.data = session.get(self.sid)
#self.set_secure_cookie("sid",self.data['_id'])
def get(self):
self.write( tornado.escape.json_encode( {'result': False, 'info': '拒绝GET请求!!' } ) )
def post(self):
self.set_header("Accept-Charset", "utf-8")
NetEase.refresh()
req = { 'key':self.get_argument("key") }
res = NetEase.search( req['key'] )
self.write( tornado.escape.json_encode(res['result']) )
# 点播音乐
class AjaxOrderMusicHandler(tornado.web.RequestHandler):
def initialize(self):
'''database init'''
self.sid = self.get_secure_cookie("sid")
def get(self):
self.write( tornado.escape.json_encode( {'result': False, 'info': '拒绝GET请求!!' } ) )
def post(self):
self.set_header("Accept-Charset", "utf-8")
req = {
'sid':self.get_argument("sid"),
'url':self.get_argument("url"),
'name':self.get_argument("name"),
'duration':self.get_argument("duration"),
'artists':self.get_argument("artists"),
'album_pic':self.get_argument("album_pic"),
'album_name':self.get_argument("album_name"),
}
ok = player.order_music( req )
self.write( tornado.escape.json_encode( {'result': ok['success'], 'info': ok['error'] } ) )
# 播放音乐
class AjaxPlayMusicHandler(tornado.web.RequestHandler):
def initialize(self):
'''database init'''
self.sid = self.get_secure_cookie("sid")
#self.data = session.get(self.sid)
#self.set_secure_cookie("sid",self.data['_id'])
def get(self):
self.write( tornado.escape.json_encode( {'result': False, 'info': '拒绝GET请求!!' } ) )
def post(self):
self.set_header("Accept-Charset", "utf-8")
req = { 'sid':self.get_argument("sid"), 'url':self.get_argument("url"), }
if req['sid'] == '':
m = player.get_play_list_next()
if m:
req['sid'] = m['sid']
player.play_music( req['sid'] )
self.write( tornado.escape.json_encode( {'result': True, 'info': 'play now...!!' } ) )
# 播放下一首音乐
class AjaxPlayNextMusicHandler(tornado.web.RequestHandler):
def initialize(self):
'''database init'''
self.sid = self.get_secure_cookie("sid")
def get(self):
self.write( tornado.escape.json_encode( {'result': False, 'info': '拒绝GET请求!!' } ) )
def post(self):
self.set_header("Accept-Charset", "utf-8")
res = {'result': True, 'info': ''}
player.next_song( 5 )
self.write( tornado.escape.json_encode( res ) )
# 暂停播放音乐
class AjaxPauseMusicHandler(tornado.web.RequestHandler):
def initialize(self):
'''database init'''
self.sid = self.get_secure_cookie("sid")
def get(self):
self.write( tornado.escape.json_encode( {'result': False, 'info': '拒绝GET请求!!' } ) )
def post(self):
self.set_header("Accept-Charset", "utf-8")
res = {'result': True, 'info': ''}
if player.play_status == play.PLAYING:
player.need_to_pause = True
print u'stop music'
self.write( tornado.escape.json_encode( res ) )
# 登录网易云
class AjaxLoginHandler(tornado.web.RequestHandler):
def initialize(self):
'''database init'''
self.sid = self.get_secure_cookie("sid")
#self.data = session.get(self.sid)
#self.set_secure_cookie("sid",self.data['_id'])
def post(self):
self.write( tornado.escape.json_encode( {'result': False, 'info': '拒绝GET请求!!' } ) )
def get(self):
self.set_header("Accept-Charset", "utf-8")
NetEase.refresh()
req = { 'user':self.get_argument("user"), 'pass':self.get_argument("pass") }
res = NetEase.login( req['user'], req['pass'] )
self.write( tornado.escape.json_encode(res['profile']) )
# 获取音乐信息
class AjaxGetSongHandler(tornado.web.RequestHandler):
def initialize(self):
'''database init'''
self.sid = self.get_secure_cookie("sid")
#self.data = session.get(self.sid)
#self.set_secure_cookie("sid",self.data['_id'])
def post(self):
self.write( tornado.escape.json_encode( {'result': False, 'info': '拒绝GET请求!!' } ) )
def get(self):
self.set_header("Accept-Charset", "utf-8")
NetEase.refresh()
req = { 'sid':self.get_argument("sid") }
res = NetEase.song_detail( req['sid'] )
self.write( tornado.escape.json_encode(res) )
class AjaxGetAlbumHandler(tornado.web.RequestHandler):
def initialize(self):
'''database init'''
self.sid = self.get_secure_cookie("sid")
#self.data = session.get(self.sid)
#self.set_secure_cookie("sid",self.data['_id'])
def post(self):
self.write( tornado.escape.json_encode( {'result': False, 'info': '拒绝GET请求!!' } ) )
def get(self):
self.set_header("Accept-Charset", "utf-8")
NetEase.refresh()
req = { 'aid':self.get_argument("aid") }
res = NetEase.album( req['aid'] )
self.write( tornado.escape.json_encode(res) )
# 调节音量
class AjaxSetVolumeHandler(tornado.web.RequestHandler):
def initialize(self):
'''database init'''
self.sid = self.get_secure_cookie("sid")
def get(self):
self.write( tornado.escape.json_encode( {'result': False, 'info': '拒绝GET请求!!' } ) )
def post(self):
self.set_header("Accept-Charset", "utf-8")
req = { 'value': self.get_argument("value") }
res = player.setVolume( req['value'] )
self.write( tornado.escape.json_encode( {'result': True, 'info': 'success!!' } ) )
# 获取最新的专辑
class AjaxNewAlbumsHandler(tornado.web.RequestHandler):
def initialize(self):
'''database init'''
self.sid = self.get_secure_cookie("sid")
#self.data = session.get(self.sid)
#self.set_secure_cookie("sid",self.data['_id'])
def get(self):
self.write( tornado.escape.json_encode( {'result': False, 'info': '拒绝GET请求!!' } ) )
def post(self):
self.set_header("Accept-Charset", "utf-8")
NetEase.refresh()
req = { 'offset': self.get_argument("offset"), 'limit': self.get_argument("limit") }
res = NetEase.new_albums( req['offset'],req['limit'] )
self.write( tornado.escape.json_encode(res) )
# 获取最热歌单
class AjaxHotSongHandler(tornado.web.RequestHandler):
def initialize(self):
'''database init'''
self.sid = self.get_secure_cookie("sid")
#self.data = session.get(self.sid)
#self.set_secure_cookie("sid",self.data['_id'])
def get(self):
self.write( tornado.escape.json_encode( {'result': False, 'info': '拒绝GET请求!!' } ) )
def post(self):
self.set_header("Accept-Charset", "utf-8")
NetEase.refresh()
req = { 'offset': self.get_argument("offset"), 'limit': self.get_argument("limit") }
res = NetEase.top_songlist( req['offset'],req['limit'] )
self.write( tornado.escape.json_encode(res) )
def base_url(path):
return "http://127.0.0.1/"+path
def main():
tornado.options.parse_command_line()
global player
option['db'] = options['db']
option['host'] = options['db_host']
option['port'] = options['db_port']
player = play.Play(option)
http_server = tornado.httpserver.HTTPServer(Application())
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()