-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
155 lines (140 loc) · 5.65 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
# 学习制作网易云音乐客户端。
# 此文件实现登陆查询等一系列功能。
__author__ = 'weiy'
"""
4.10日。
"""
import urllib.parse
import requests
import hashlib
import json
def shotlist(lst):
"""列表去重。"""
temp1 = sorted(list(set(lst)))
return temp1
class WebApi:
"""一些功能。"""
default_timeout = 10
headers = {
'Accept': '*/*',
'Accept-Encoding': 'gzip,deflate,sdch',
'Accept-Language': 'zh-CN,zh;q=0.8',
'Proxy-Connection': 'keep-alive',
'Content-Type': 'application/x-www-form-urlencoded',
'Host': 'music.163.com',
'Referer': 'http://music.163.com/',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36'
}
def __init__(self):
self.cookies = {
'appver': '1.6.1.82809',
'os': 'pc'
}
def httpRequest(self, action, method="GET", add=None, data=None, headers=headers, cookies='',\
timeout=default_timeout, urlencode='utf-8'):
"""
默认以get方式请求,
GET方式附加内容用add参数,POST方式提交内容用data参数。
编码用urlencode参数,默认utf-8。
GET方式返回json形式请求的内容。
POST方式返回cookies和json形式的内容。(0,1)
默认cookies为空。
"""
if method.upper() == 'GET':
if add:
html = requests.get(action, params=add, headers=headers, cookies=cookies, timeout=timeout)
else:
html = requests.get(action, headers=headers, cookies=cookies, timeout=timeout)
html.encoding = urlencode
return json.loads(html.text)
elif method.upper() == 'POST':
if data:
html = requests.post(action, data=data, headers=headers, cookies=cookies, timeout=timeout)
else:
html = requests.post(action, headers=headers, cookies=cookies, timeout=timeout)
html.encoding = urlencode
return html.cookies, json.loads(html.text)
def login(self, username, password):
"""
以网易账号登陆,其他的登陆待写。返回cookies和json形式内容。
"""
data = {
'username': username,
'password': hashlib.md5(password.encode('utf-8')).hexdigest(),
'remeberLogin': 'true'
}
cki = self.httpRequest('http://music.163.com/api/login', method="POST", data=data)
cki[0].set('appver', self.cookies['appver'], domain='music.163.com')
cki[0].set('os', self.cookies['os'], domain='music.163.com')
return cki[0], cki[1]
def user_playlist(self, uid, offset=0):
"""
个人歌单。
"""
url = 'http://music.163.com/api/user/playlist/?offset=%s&limit=1000&uid=%s' % (offset, uid)
html = self.httpRequest(url, method='GET', cookies=self.cookies)
return html['playlist']
def all_playlist(self, cat='全部歌单', types='all', offset=0, index=1):
"""
全部歌单。列表字典形式。
"""
url = 'http://music.163.com/api/playlist/list?cat=%s&type=%s&order=%s&offset=%d&total=true&limit=30&index=%d)'\
% (urllib.parse.quote(cat), types, types, offset, index)
html = self.httpRequest(url, method='GET', cookies=self.cookies)
return html['playlists']
def details_playlist(self, id):
"""
歌单详情。
"""
url = 'http://music.163.com/api/playlist/detail?id=%d' % (id)
html = self.httpRequest(url, method="GET", cookies=self.cookies)
return html['result']
def search(self, s, offset=0, limit=100, total='true', stype=1):
"""
搜索.
type类型: 单曲(1), 专辑(10), 歌手(100), 歌单(1000), 用户(1002)
"""
url = 'http://music.163.com/api/search/get/web'
data = {
's': s,
'offset': offset,
'total': total,
'limit': limit,
'type': stype
}
html = self.httpRequest(url, method='POST', data=data, cookies=self.cookies)
try:
return html[1]['result']
except:
return "Not Found!"
def details_search(self, id):
"""
搜索结果详情,返回歌曲URL。
"""
id = str(id)
url = "http://music.163.com//api/song/detail/?id=%s&ids=%s" % (id, urllib.parse.quote('[%s]' % (id)))
html = self.httpRequest(url, method='GET', cookies=self.cookies)
return html['songs'][0]['mp3Url']
def newsong(self, areaID=0, offset=0, total='true', limit=100):
"""
最新音乐--新歌速递。
areaID(0全部, 9华语, 96欧美, 16韩国, 8日本。)
"""
url = 'http://music.163.com/api/discovery/new/songs?areaId=%d&offset=%d&total=%s&limit=%d' %\
(areaID, offset, total, limit)
html = self.httpRequest(url, method='GET', cookies=self.cookies)
return html['data']
def fnewsong(self, year=2015, month=4, area='ALL'):
"""
最新音乐--新碟上架。
area(ALL全部, ZH华语, EA欧美, KR韩国, 日本JP)
"""
url = 'http://music.163.com/api/discovery/new/albums/area?year=%d&month=%d&area=%s&type=hot&offset=0&total=true&limit=20&rcmd=true' \
% (year, month, area)
html = self.httpRequest(url, method="GET", cookies=self.cookies)
return html['monthData']
if __name__ == '__main__':
main = WebApi()
req = main.newsong()
for i in req:
print(i)