This repository has been archived by the owner on Sep 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.py
730 lines (640 loc) · 27.9 KB
/
api.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
# -*- coding: utf-8 -*-
import sys
import os
import re
import base64
import binascii
import md5
if sys.path[1].find('neteaseDebug') == -1:
sys.path[1:1] = [os.path.abspath("e:\\neteaseDebug")]
import simplejson as json
try:
from Crypto.Cipher import AES
except:
import aes as AES
import random
import copy
import StringIO
import httplib
import urllib
import socket
try:
import appuifw2 as appuifw
import appuifw as appuifw_
import e32
import audio
except:
pass
import time
from util import *
infoPopup = appuifw_.InfoPopup()
progress = 0
writeprogress = 0
def downProg(n,s,t):
global progress
pc = n*s*100/t
if pc > 100:
pc=100
if pc != progress:
infoPopup.show(cn('↓ ') + u'%s %%' % pc,(0,0), 1000,0, appuifw_.EHCenterVTop)
progress = pc
e32.ao_yield()
def writeProg(n,s,t):
global writeprogress
pc = n*s*100/t
if pc > 100:pc=100
if pc != writeprogress:
infoPopup.show(cn('○ ') + u'%s %%' % pc,(0,0), 1000,0, appuifw_.EHCenterVTop)
writeprogress = pc
e32.ao_yield()
def cookie2Dict(str1):return dict([(x.replace(' ','').split('=') + [''])[0:2] for x in re.split(',|;', str1)])
def dict2Cookie(dict1):return ';'.join([a + '=' + b for a,b in dict1.items()])
def aesEncrypt(text, secKey):
pad = 16 - len(text) % 16
text = text + pad * chr(pad)
# encryptor = AES.new(secKey, 2, '0102030405060708')
# ciphertext = encryptor.encrypt(text)
# ciphertext = base64.b64encode(ciphertext)
# return ciphertext.decode()
encryptor = AES.new(secKey, 2, '0102030405060708')
ciphertext = encryptor.encrypt(text)
ciphertext = base64.encodestring(ciphertext).replace('\n','')
return ciphertext.decode()
def modpow(base, exponent, mod):
ans = 1
index = 0
while(1 << index <= exponent):
if(exponent & (1 << index)):
ans = (ans * base) % mod
index += 1
base = (base * base) % mod
return ans
def rsaEncrypt(text, pubKey, modulus):
textRvs=""
for x in text:
textRvs = x + textRvs
text = textRvs.encode()
rs = modpow(long(binascii.hexlify(text), 16), long(pubKey, 16), long(modulus, 16))
return ('%x' % rs).zfill(256)
def createSecretKey(size):
return ''.join( [ hex(random.randint(1,255))[2:] for x in range(16) ] )[0:16]
def getParams(paramsData):
modulus = '00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7'
nonce = '0CoJUm6Qyw8W8jud'
pubKey = '010001'
text = json.dumps(paramsData)
secKey = createSecretKey(16)
#secKey = "7874369994448f96"
encText = aesEncrypt(aesEncrypt(text, nonce), secKey)
encSecKey = rsaEncrypt(secKey, pubKey, modulus)
data = {
'params': encText,
'encSecKey': encSecKey
}
return data
#代码来自http://github.com/yanunon/NeteaseCloudMusic
#根据音乐的dfsId在本地算出音乐的url
def encDfsId(dfsId):
magic = str('3go8&$8*3*3h0k(2)2')
song_id = str(dfsId)
song_id_ = ''
magic_len = len(magic)
for i, sid in enumerate(song_id):
song_id_ += chr(ord(sid) ^ ord(magic[i % magic_len]))
m_ = md5.new()
m_.update(song_id_)
m = m_.digest()
result = m.encode('base64')[:-1]
result = result.replace('/', '_')
result = result.replace('+', '-')
return result
def encMp3Url(dfsId):
encdfsid = encDfsId(dfsId)
mp3Url = "http://p%s.music.126.net/%s/%s.mp3" % (random.randint(1,4), encdfsid, dfsId)
return mp3Url
def getUrlBySongDict(song, br, mode1=False):
bpsKeyList = ['b', 'l', 'm', 'h']
bpsKey = 0
if br > 96000:bpsKey = 1
if br > 128000:bpsKey = 2
if br > 160000:bpsKey = 3
url = ''
if br <= 128000:
url = song.get('mp3Url').replace('http://m', 'http://p')
if not(url):
if not(mode1):
def getDfsIdWrapped(bpsKey1):
dfsDict = song.get('%sMusic' % bpsKeyList[bpsKey1])
dfsId = 0
if dfsDict:
dfsId = dfsDict.get('dfsId')
return dfsId
dfsId = getDfsIdWrapped(bpsKey)
if dfsId == 0:
dfsId = getDfsIdWrapped(bpsKey-1)
if dfsId == 0:
dfsId = getDfsIdWrapped(bpsKey-2)
if dfsId == 0:
dfsId = getDfsIdWrapped(bpsKey-3)
if dfsId != 0:
url = encMp3Url(dfsId)
logs('Use Old Api: url = ' + url)
else:
url = 'http://p2.music.126.net/rsoHYJSo3I4Dfw10iaKUrg==/5751545324986254.mp3?mark=Failed'
return url
class NEApi:
def __init__(self, username, password, phone = False, relogin = False, mode1=True):
self.uid=0
self.cookie = INIT_COOKIE
self.updateCookie(self.loadCookie())
self.netFailedCallback = lambda:None
self.loginSucceed = False
self.httpResponseCallback = downProg
self.useNewApi = mode1
if not(self.checkLogin()) or relogin:
logs('ReLogin')
loginCount = 0
while True:
if loginCount >= 5:
raise Exception, 'Login Failed. Unknown Error2'
break
loginCount +=1
try:
self.login(username, password, phone)
except Exception, e:
printException(Exception, e, cn('在登录时出现了一些问题。用户名或密码错误吗?'))
break
if self.checkLogin():
break
def updateCookie(self, str1):
self.cookie = INIT_COOKIE + str1
reFind = re.findall(r'(?<=NETEASE\_WDA\_UID=).*(?=#\|)',str1)
if reFind:
self.uid=reFind[0]
else:
self.uid=0
def rawHttpReq(self, url, method='GET', params='', query='', header=None):
#发HTTP请求,并返回cookie内容和正文
if header == None:
header = {'Cookie': self.cookie, 'Referer': 'http://music.163.com/', 'Content-Type':"application/x-www-form-urlencoded"}
urlhost = url.split('/')[2]
httpLogs(cn('<Request URI> ') + cn(url))
httpLogs(cn('<Request Body> ') + cn(params))
try:
conn1 = httplib.HTTPConnection(urlhost, 80, 3)
conn1.request(method, url, params, header)
resp1=conn1.getresponse()
totalSize = getIOLength(resp1)
responseData = StringIO.StringIO()
callbackWhenRead(resp1, responseData, self.httpResponseCallback, 512, totalSize)
responseData.seek(0)
result = (resp1.getheader('set-cookie'), responseData.read())
httpLogs(cn('<Response Cookie> \n') + cn(result[0]))
httpLogs(cn('<Response Body> \n Length: ') + unicode(len(result[1])))
conn1.close()
return result
except Exception, e:
printException(Exception,e)
logs_('SomeProblemsWithHttpRequest')
self.netFailedCallback()
def encHttpReq(self, url, params={'':''}):
#针对网易云网页版新API发加密的POST
httpLogs(cn('<Encryped Data> ') + cn(urllib.urlencode(params)))
encParams = getParams(params)
return self.rawHttpReq(url, "POST", urllib.urlencode(encParams))
def loadCookie(self):
#从默认位置载入COOKIE
if not os.path.exists('e:\\netease\\data\\cookie.txt'):
file0 = open('e:\\netease\\data\\cookie.txt','wb')
file0.write('')
file0.close()
file1=open('e:\\netease\\data\\cookie.txt','r')
readCookie = file1.read()
file1.close()
return readCookie
def writeCookie(self):
file1=open('e:\\netease\\data\\cookie.txt','wb')
file1.write(self.cookie)
file1.close()
return None
def checkLogin(self):
#通过检查个人推荐页的结果是否为200来判定是否登录成功,<并且通过UID检测判断是否同一账号>
try:
if json.loads(self.encHttpReq(HOST+'/weapi/v1/discovery/recommend/songs?csrf_token=', {'':''})[1]).get('code') == 200:
self.loginSucceed = True
return True
else:
return False
except Exception, e:
printException(Exception, e, cn('在检测登录时出现了一些问题。'))
return False
def login(self, username, password, phone = False):
loginSuc = False
m = md5.new()
m.update(password.encode())
pwEnc=m.hexdigest()
loginDict = {
'username': username,
'password': pwEnc,
'rememberLogin': 'true'
}
loginUrl = HOST + '/weapi/login/'
if phone :
loginDict = {
'phone': username,
'password': pwEnc,
'rememberLogin': 'true'
}
loginUrl = loginUrl + '/cellphone'
loginCount = 0
while loginSuc == False:
if loginCount >= 5:
raise Exception, 'Login Failed. Unknown Error'
break
loginCount += 1
(loginCookie,loginReads) = self.encHttpReq(loginUrl, loginDict)
loginReads = json.loads(loginReads)
if str(loginReads['code'])=='415':
captValid = False
captCount = 0
while captValid == False :
if captCount >= 5:
raise Exception, 'Login Failed. IP Frequent and Many Wrong Captcha'
break
download(HOST + '/captcha?id=' + loginReads['captchaId'], 'E:\\netease\\data\\captcha.png')
inputCapt = appuifw.query(cn('请输入E:\\netease\\data\\captcha.png中的验证码:'),
'text')
captReads = self.encHttpReq(HOST + '/weapi/image/captcha/verify/hf', {'id': loginReads['captchaId'] , 'captcha': inputCapt})[1]
captReads = json.loads(captReads)
if str(captReads['result'])=='True':captValid = True
captCount += 1
elif str(loginReads['code'])=='200':
loginSuc = True
else:
raise Exception, 'Login Failed. Wrong Username or Password?'
self.updateCookie(INIT_COOKIE + loginCookie)
self.writeCookie()
return None
def dailySign(self):
try:
signUrl = HOST + '/weapi/point/dailyTask'
signResult = json.loads(self.encHttpReq(signUrl, {'type': 0 })[1])
return signResult
except Exception, e:
printException(Exception, e, cn('SomeErrorsWithdailySign'))
def getUserPLs(self, uid = 0, offset=0, limit=1000):
try:
if not uid:
uid = self.uid
uplUrl = HOST + '/api/user/playlist/?offset=%s&limit=%s&uid=%s' % (offset, limit, uid)
return json.loads(self.rawHttpReq(uplUrl)[1])
except Exception, e:
printException(Exception, e, cn('SomeErrorsWithgetUserPLs'))
def getRecomPL(self):
try:
recUrl = HOST + '/weapi/v1/discovery/recommend/songs?csrf_token='
recomResult = self.encHttpReq(recUrl, {'': ''})[1]
recomResult_ = json.loads(recomResult)['recommend']
return recomResult_
except Exception, e:
printException(Exception, e, cn('SomeErrorsWithgetRecomPL'))
# def getFML(self):
# recUrl = HOST + '/api/radio/get'
# return json.loads(self.rawHttpReq(recUrl)[1])
def getFML(self, limit = 3):
try:
recReq = {'limit': limit}
# recReq = {}
recUrl = HOST + '/api/radio/get'
return json.loads(self.encHttpReq(recUrl, recReq)[1])
except Exception, e:
printException(Exception, e, cn('SomeErrorsWithgetFML'))
def fmLike(self, songId, like=True, time=0, alg='itembased'):
try:
likeUrl = HOST + '/api/radio/like?alg=%s&trackId=%s&like=%s&time=%s' % (alg, songId, like, time)
likeResult = json.loads(self.rawHttpReq(likeUrl)[1])
if likeResult['code'] == 200:
return likeResult
else:return -1
except Exception, e:
printException(Exception, e, cn('SomeErrorsWithfmLike'))
def fmTrash(self, songId, time=0, alg='RT'):
try:
trashUrl = HOST + '/api/radio/trash?alg=%s&songId=%s&time=%s' % (alg, songId, time)
trashResult = json.loads(self.rawHttpReq(trashUrl)[1])
if trashResult['code'] == 200:
return trashResult
else:return -1
except Exception, e:
printException(Exception, e, cn('SomeErrorsWithfmTrash'))
def getPlaylist(self, plId, useDownload = True, forceRefresh = True):
global progress,writeprogress
try:
plPath = u'e:\\netease\\cache\\playlist\\playlist_%s.txt'%plId
plUrl = HOST + '/api/playlist/detail?id=%s' % (plId)
if not useDownload:return json.loads(self.rawHttpReq(plUrl)[1])['result']
if forceRefresh or not(os.path.exists(plPath)):
download(plUrl, plPath, downProg, lambda:None, writeProg)
infoPopup.hide()
file1 = open(plPath, 'r')
content = json.loads(file1.read())['result']
file1.close()
return content
except Exception, e:
printException(Exception, e, cn('SomeErrorsWithgetPlaylist'))
def like(self, songId, like=True, time=25, alg='itembased'):
try:
likeUrl = HOST + '/api/radio/like?alg=%s&trackId=%s&like=%s&time=%s' % (alg, songId, ['false','true'][like], time)
return json.loads(self.rawHttpReq(likeUrl)[1])
except Exception, e:
printException(Exception, e, cn('SomeErrorsWithlike'))
def favorPlaylist(self, playlistId):
try:
favorUrl = HOST + "/weapi/playlist/subscribe?csrf_token="
csrf = cookie2Dict(self.cookie)['__csrf']
logs('csrf : ' + csrf)
favorUrl += csrf
favorReq = {'id':playlistId, 'csrf_token':csrf}
# favorReq = {'id':playlistId}
return json.loads(self.encHttpReq(favorUrl, favorReq)[1])['code']
except Exception, e:
printException(Exception, e, cn('SomeErrorsWithfavorPlaylist'))
'''
获取歌曲ID列表中每首歌曲的Url(raw仅获取原始json, 未处理)
两种模式:
1. 用新API enhance/player/url 获取歌曲url信息(长网址)
缺点:没法获取到版权/下架歌曲信息 需要发加密post
2. 用旧API /api/song/detail 提取歌曲url信息(h,m,..)
若没有url信息,则反查歌曲专辑,会得到歌曲的dfsId,用加密算法得到url (短网址)
借鉴cunyu loadfield的代码思路
缺点:将来可能会失效 并且服务器需要从m换成p
!!(16.8.31)现在对于高音质来说很大几率无效!!
!!!
目前 选择第二条
这个函数返回的是一个数组 其上有各mp3的url
'''
def getSongsUrl_mode1(self, songIds=[], br = 96000):
songsDetailRes = json.loads(self.rawgetSongsUrl(songIds, br))['data']
songsDetailRes_ = copy.copy(songsDetailRes)
for x in songsDetailRes:
i = songIds.index(int(x['id']))
#待写
songsDetailRes_[i] = x
return songsDetailRes_
def getSongsUrl(self, songDict, br=96000):
#songDict实际上是Dict构成的List
mode1 = self.useNewApi
try:
urlList = ['http://p2.music.126.net/rsoHYJSo3I4Dfw10iaKUrg==/5751545324986254.mp3?mark=Failed' for i in range(len(songDict))]
listToUseOldApi = []
if mode1:
logs('useNewApi' )
newApiResult = self.getSongsUrl_mode1([x['id'] for x in songDict], br)
for i, j in enumerate(newApiResult):
# logs('%s s newapiurl = %s'%(i,j.get('url')))
urlList[i] = j.get('url')
if not(j.get('url')):
#下架
logs('Song %s Undercart!' % i)
listToUseOldApi.append(i)
else:
listToUseOldApi = range(len(songDict))
logs('listToUseOldApi: ' + str(listToUseOldApi))
if listToUseOldApi != []:
for i,j in enumerate(listToUseOldApi):
if False and songDict[j].get('mp3Url') and songDict[j].get('lMusic'):
#can be searched
#old api dead. no hope for getting url by mp3url or dfsid
url = getUrlBySongDict(songDict[j], br)
else:
logs('%s cannot be searched! use album'%(j))
url = self.getUrlByAlbum(songDict[j]['id'], songDict[j]['album']['id'], br)
if url != '': urlList[j] = url.replace(u'http://m', u'http://p')
# logs('%s s oldapiurl = %s'%(j,urlList[j]))
return urlList
except Exception, e:
printException(Exception, e, cn('SomeErrorsWithgetSongsUrl'))
def getUrlByAlbum(self, songId, albumId, br):
albumDict = json.loads(self.rawgetAlbum(albumId))
url = ''
if albumDict.get('code') != 200: return url
for song in albumDict['album']['songs']:
if song['id'] == songId:
url = getUrlBySongDict(song, br)
return url
#可以获取详细信息(获取歌曲ID列表中每首歌曲的详细信息,有可能出现查不到Url的情况)
def getSongsDetail(self, songIds=[]):
try:
songsDetailRes = json.loads(self.rawgetSongsDetail(songIds))['songs']
songsDetailRes_ = copy.copy(songsDetailRes)
for x in songsDetailRes:
try:
i = songIds.index(x['id'])
except:
pass
try:
i = songIds.index(str(x['id']))
except:
pass
songsDetailRes_[i] = x
return songsDetailRes_
except Exception, e:
printException(Exception, e, cn('SomeErrorsWithgetSongsDetail'))
# def getSongsDetail_(self, songIds=[]):
# return [self.getSongDetail_(x) for x in songIds]
def getSongDetail_(self, songId):
songsDetailUrl = HOST + '/api/song/detail?ids=[%s]'%songId
(urlCookie, urlReads) = self.rawHttpReq(songsDetailUrl)
return json.loads(urlReads)['songs'][0]
def getSongUrl(self, songId, br = 96000):
return self.getSongDetail(songId, br)['url']
def getSongLyric(self, songId):
try:
hasLyric = False
hasTLyric = False
lyricUrl = HOST + '/api/song/lyric?id=%s&lv=-1&kv=-1&tv=-1' % (songId)
rawResult = self.rawHttpReq(lyricUrl)[1]
lyricResult = json.loads(rawResult)
if 'lrc' in lyricResult and 'lyric' in lyricResult['lrc'] and lyricResult['lrc']['lyric']:
hasLyric = lyricResult['lrc']['lyric']
if 'tlyric' in lyricResult and 'lyric' in lyricResult['tlyric'] and lyricResult['tlyric']['lyric']:
hasTLyric = lyricResult['tlyric']['lyric']
return (lyricResult, hasLyric, hasTLyric)
except Exception, e:
printException(Exception, e, cn('SomeErrorsWithgetSongLyric'))
def getComments(self, songId, commentThreadId = '_', offset=0, total=False, limit=10):
try:
#此处commentThreadId赋值为R_AL_3_XXX之类的可以获取其他种类的评论(歌手/专辑/歌单)
if commentThreadId == '_':
commentThreadId = 'R_SO_4_%s' % songId
commUrl = HOST + '/weapi/v1/resource/comments/%s' % commentThreadId
commReq = {
'offset' : offset,
'total' : total,
'limit' : limit
}
(commCookie, commResult) = self.encHttpReq(commUrl, commReq)
commResult = json.loads(commResult)
hotCommList = commResult.get('hotComments')
ordCommList = commResult.get('comments')
totalNum = commResult.get('total')
haveMore = commResult.get('more')
return (hotCommList, ordCommList, totalNum, haveMore)
except Exception, e:
printException(Exception, e, cn('SomeErrorsWithgetComments'))
def favorComment(self, commentId, threadId, favor = True):
try:
#必须提供threadId,否则按默认的歌曲来
favorReq = {
'commentId' : commentId,
'threadId' : threadId,
'like' : favor
}
favorUrl = HOST + '/weapi/v1/comment/%slike' % ('un','')[favor]
favorResult = json.loads(self.encHttpReq(favorUrl, favorReq)[1])
message = u''
favorSuc = False
if 'message' in favorResult:
message = favorResult['message']
else:
message = str(favorResult.get('code'))
if favorResult.get('code') == 200:
favorSuc = True
return (favorSuc, message)
except Exception, e:
printException(Exception, e, cn('SomeErrorsWithfavorComment'))
#add: 添加
#502返回码:歌曲已经存在
#成功:{"trackIds":"[715785]","code":200,"count":31,"cloudCount":0}
#del: 删除
#成功:{"code":200,"count":30,"cloudCount":0}
#失败:好像仍是返回200?
def manipulatePlaylist(self, plId, songIDList, operation):
try:
#必须提供threadId,否则按默认的歌曲来
manReq = {
'op' : operation,
'pid' : plId,
'trackIds' : u'[%s]' % (u','.join([unicode(x) for x in songIDList])),
'tracks' : '[object Object]'
}
manUrl = HOST + '/weapi/playlist/manipulate/tracks'
manResult = json.loads(self.encHttpReq(manUrl, manReq)[1])
if manResult['code'] == 200:
return -1
#-1表示成功
else:
return manResult['code']
#否则返回错误码
except Exception, e:
printException(Exception, e, cn('SomeErrorsWithmanipulatePlaylist'))
def createPlaylist(self, name, songIDList = []):
try:
#必须提供threadId,否则按默认的歌曲来
createReq = {
'name' : name,
}
createUrl = HOST + '/weapi/playlist/create'
createResult = json.loads(self.encHttpReq(createUrl, createReq)[1])
if createResult['code'] == 200:
if songIDList:
manRes = self.manipulatePlaylist(createResult['id'],songIDList, 'add')
if manRes != -1: raise Exception, manRes
return -1
else:
raise Exception, createResult['code']
except Exception, e:
printException(Exception, e, cn('SomeErrorsWithmanipulatePlaylist'))
return e
#1 单曲
#10 专辑
#100 歌手
#1000 歌单
#1002 用户
def search(self, keyword, type, limit=10, offset=0):
#待改进API,旧的API只能显示10条
try:
#必须提供threadId,否则按默认的歌曲来
searchReq = {
's' : keyword,
'type' : type,
'limit' : limit,
'offset' : offset
}
searchURL = HOST + '/api/search/get'
searchResult = json.loads(self.rawHttpReq(searchURL, 'POST', urllib.urlencode(searchReq))[1])['result']
return searchResult
except Exception, e:
printException(Exception, e, cn('SomeErrorsWithsearch'))
# ---
def rawgetUserPLs(self, uid = 0, offset=0, limit=10):
if uid == 0:
uid = self.uid
uplUrl = HOST + '/api/user/playlist/?offset=%s&limit=%s&uid=%s' % (offset, limit, uid)
return self.rawHttpReq(uplUrl)[1]
def rawgetRecomPL(self):
recUrl = HOST + '/weapi/v1/discovery/recommend/songs?csrf_token='
return self.encHttpReq(recUrl, {'': ''})[1]
def rawgetFML(self):
recUrl = HOST + '/api/radio/get'
return self.rawHttpReq(recUrl)[1]
def rawfmLike(self, songId, like=True, time=0, alg='itembased'):
likeUrl = HOST + '/api/radio/like?alg=%s&trackId=%s&like=%s&time=%s' % (alg, songId, like, time)
likeResult = self.rawHttpReq(likeUrl)[1]
return likeResult
def rawfmTrash(self, songId, time=0, alg='RT'):
trashUrl = HOST + '/api/radio/trash?alg=%s&songId=%s&time=%s' % (alg, songId, time)
trashResult = self.rawHttpReq(trashUrl)[1]
return trashResult
def rawgetPlaylist(self, plId):
plUrl = HOST + '/api/playlist/detail?id=%s' % (plId)
return self.rawHttpReq(plUrl)[1]
#rawgetSongsDetail: 获取歌曲ID列表中每首歌曲的详细信息,有可能出现查不到Url的情况
def rawgetSongsDetail(self, songIds=[]):
songsDetailUrl = HOST + '/api/song/detail?ids=[%s]' % ','.join([str(songid) for songid in songIds])
(urlCookie, urlReads) = self.rawHttpReq(songsDetailUrl)
# (urlCookie, urlReads) = self.encHttpReq(HOST + '/weapi/song/enhance/player/url?csrf_token=', mp3UrlReq)
return urlReads
#rawgetSongsUrl:
#获取歌曲ID列表中每首歌曲的Url(raw仅获取原始json未处理)
#是采取上述的模式1 现在暂时用不到
def rawgetSongsUrl(self, songIds=[], br=96000):
mp3UrlReq = {
'ids' : songIds,
'br' : br
}
(urlCookie, urlReads) = self.encHttpReq(HOST + '/weapi/song/enhance/player/url?csrf_token=', mp3UrlReq)
return urlReads
#weapi v2版的 songdetail api,暂时用不上
def newrawgetSongDetail(self, songId, br = 96000):
mp3UrlReq = {
'ids' : [songId],
}
(urlCookie, urlReads) = self.encHttpReq(HOST + '/weapi/v1/song/detail', mp3UrlReq)
# (urlCookie, urlReads) = self.rawHttpReq(HOST + '/api/v2/song/detail', 'POST', mp3UrlReq)
# return json.loads(urlReads)['data'][0]
return urlReads
def rawgetSongLyric(self, songId):
hasLyric = False
hasTLyric = False
lyricUrl = HOST + '/api/song/lyric?id=%s&lv=-1&kv=-1&tv=-1' % (songId)
RAWRESULT = self.rawHttpReq(lyricUrl)[1]
# if 'lrc' in lyricResult and lyricResult['lrc']['lyric'] is not None:
# hasLyric = True
# if 'tlyric' in lyricResult and lyricResult['tlyric']['lyric'] is not None:
# hasTLyric = True
return RAWRESULT
def rawgetComments(self, songId, commentThreadId = '_', offset=0, total=False, limit=10):
if commentThreadId == '_':
commentThreadId = 'R_SO_4_%s' % songId
commUrl = HOST + '/weapi/v1/resource/comments/%s' % commentThreadId
commReq = {
'offset' : offset,
'total' : total,
'limit' : limit
}
(commCookie, commReads) = self.encHttpReq(commUrl, commReq)
return commReads
def rawgetAlbum(self, albumId):
albumUrl = HOST + '/api/album/%s?id=%s' % (albumId, albumId)
return self.rawHttpReq(albumUrl)[1]