-
Notifications
You must be signed in to change notification settings - Fork 147
/
api.py
367 lines (324 loc) · 14.5 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
# -*- coding:utf-8 -*-
import json
import requests
from .utils import PixivError, JsonDict
class BasePixivAPI(object):
access_token = None
user_id = 0
refresh_token = None
def parse_json(self, json_str):
"""parse str into JsonDict"""
def _obj_hook(pairs):
"""convert json object to python object"""
o = JsonDict()
for k, v in pairs.items():
o[str(k)] = v
return o
return json.loads(json_str, object_hook=_obj_hook)
def require_auth(self):
if self.access_token is None:
raise PixivError('Authentication required! Call login() or set_auth() first!')
def requests_call(self, method, url, headers={}, params=None, data=None):
""" requests http/https call for Pixiv API """
req_header = {
'Referer': 'http://spapi.pixiv.net/',
'User-Agent': 'PixivIOSApp/5.8.3',
}
# override use user headers
for k, v in list(headers.items()):
req_header[k] = v
try:
if (method == 'GET'):
return requests.get(url, params=params, headers=req_header)
elif (method == 'POST'):
return requests.post(url, params=params, data=data, headers=req_header)
elif (method == 'DELETE'):
return requests.delete(url, params=params, data=data, headers=req_header)
except Exception as e:
raise PixivError('requests %s %s error: %s' % (method, url, e))
raise PixivError('Unknow method: %s' % method)
def set_auth(self, access_token, refresh_token=None):
self.access_token = access_token
self.refresh_token = refresh_token
def login(self, username, password):
return self.auth(username=username, password=password)
def auth(self, username=None, password=None, refresh_token=None):
"""Login with password, or use the refresh_token to acquire a new bearer token"""
url = 'https://oauth.secure.pixiv.net/auth/token'
headers = {
'Referer': 'http://www.pixiv.net/',
}
data = {
'client_id': 'bYGKuGVw91e0NMfPGp44euvGt59s',
'client_secret': 'HP3RmkgAmEGro0gn1x9ioawQE8WMfvLXDz3ZqxpK',
}
if (username is not None) and (password is not None):
data['grant_type'] = 'password'
data['username'] = username
data['password'] = password
elif (refresh_token is not None) or (self.refresh_token is not None):
data['grant_type'] = 'refresh_token'
data['refresh_token'] = refresh_token or self.refresh_token
else:
raise PixivError('[ERROR] auth() but no password or refresh_token is set.')
r = self.requests_call('POST', url, headers=headers, data=data)
if (r.status_code not in [200, 301, 302]):
if data['grant_type'] == 'password':
raise PixivError('[ERROR] auth() failed! check username and password.\nHTTP %s: %s' % (r.status_code, r.text), header=r.headers, body=r.text)
else:
raise PixivError('[ERROR] auth() failed! check refresh_token.\nHTTP %s: %s' % (r.status_code, r.text), header=r.headers, body=r.text)
token = None
try:
# get access_token
token = self.parse_json(r.text)
self.access_token = token.response.access_token
self.user_id = token.response.user.id
self.refresh_token = token.response.refresh_token
except:
raise PixivError('Get access_token error! Response: %s' % (token), header=r.headers, body=r.text)
# return auth/token response
return token
# Public-API
class PixivAPI(BasePixivAPI):
# Check auth and set BearerToken to headers
def auth_requests_call(self, method, url, headers={}, params=None, data=None):
self.require_auth()
headers['Authorization'] = 'Bearer %s' % self.access_token
return self.requests_call(method, url, headers, params, data)
def parse_result(self, req):
try:
return self.parse_json(req.text)
except Exception as e:
raise PixivError("parse_json() error: %s" % (e), header=req.headers, body=req.text)
def bad_words(self):
url = 'https://public-api.secure.pixiv.net/v1.1/bad_words.json'
r = self.auth_requests_call('GET', url)
return self.parse_result(r)
# 作品详细
def works(self, illust_id):
url = 'https://public-api.secure.pixiv.net/v1/works/%d.json' % (illust_id)
params = {
'image_sizes': 'px_128x128,small,medium,large,px_480mw',
'include_stats': 'true',
}
r = self.auth_requests_call('GET', url, params=params)
return self.parse_result(r)
# 用户资料
def users(self, author_id):
url = 'https://public-api.secure.pixiv.net/v1/users/%d.json' % (author_id)
params = {
'profile_image_sizes': 'px_170x170,px_50x50',
'image_sizes': 'px_128x128,small,medium,large,px_480mw',
'include_stats': 1,
'include_profile': 1,
'include_workspace': 1,
'include_contacts': 1,
}
r = self.auth_requests_call('GET', url, params=params)
return self.parse_result(r)
# 我的订阅
def me_feeds(self, show_r18=1, max_id=None):
url = 'https://public-api.secure.pixiv.net/v1/me/feeds.json'
params = {
'relation': 'all',
'type': 'touch_nottext',
'show_r18': show_r18,
}
if max_id:
params['max_id'] = max_id
r = self.auth_requests_call('GET', url, params=params)
return self.parse_result(r)
# 获取收藏夹
# publicity: public, private
def me_favorite_works(self, page=1, per_page=50, publicity='public', image_sizes=['px_128x128', 'px_480mw', 'large']):
url = 'https://public-api.secure.pixiv.net/v1/me/favorite_works.json'
params = {
'page': page,
'per_page': per_page,
'publicity': publicity,
'image_sizes': ','.join(image_sizes),
}
r = self.auth_requests_call('GET', url, params=params)
return self.parse_result(r)
# 添加收藏
# publicity: public, private
def me_favorite_works_add(self, work_id, publicity='public'):
url = 'https://public-api.secure.pixiv.net/v1/me/favorite_works.json'
params = {
'work_id': work_id,
'publicity': publicity,
}
r = self.auth_requests_call('POST', url, params=params)
return self.parse_result(r)
# 删除收藏
# publicity: public, private
def me_favorite_works_delete(self, ids, publicity='public'):
url = 'https://public-api.secure.pixiv.net/v1/me/favorite_works.json'
if isinstance(ids, list):
params = {'ids': ",".join(map(str, ids)), 'publicity': publicity}
else:
params = {'ids': ids, 'publicity': publicity}
r = self.auth_requests_call('DELETE', url, params=params)
return self.parse_result(r)
# 关注的新作品 (New -> Follow)
def me_following_works(self, page=1, per_page=30,
image_sizes=['px_128x128', 'px_480mw', 'large'],
include_stats=True, include_sanity_level=True):
url = 'https://public-api.secure.pixiv.net/v1/me/following/works.json'
params = {
'page': page,
'per_page': per_page,
'image_sizes': ','.join(image_sizes),
'include_stats': include_stats,
'include_sanity_level': include_sanity_level,
}
r = self.auth_requests_call('GET', url, params=params)
return self.parse_result(r)
# 获取关注用户
def me_following(self, page=1, per_page=30, publicity='public'):
url = 'https://public-api.secure.pixiv.net/v1/me/following.json'
params = {
'page': page,
'per_page': per_page,
'publicity': publicity,
}
r = self.auth_requests_call('GET', url, params=params)
return self.parse_result(r)
# 关注用户
# publicity: public, private
def me_favorite_users_follow(self, user_id, publicity='public'):
url = 'https://public-api.secure.pixiv.net/v1/me/favorite-users.json'
params = {
'target_user_id': user_id,
'publicity': publicity
}
r = self.auth_requests_call('POST', url, params=params)
return self.parse_result(r)
# 解除关注用户
def me_favorite_users_unfollow(self, user_ids, publicity='public'):
url = 'https://public-api.secure.pixiv.net/v1/me/favorite-users.json'
if type(user_ids) == list:
params = {'delete_ids': ",".join(map(str, user_ids)), 'publicity': publicity}
else:
params = {'delete_ids': user_ids, 'publicity': publicity}
r = self.auth_requests_call('DELETE', url, params=params)
return self.parse_result(r)
# 用户作品列表
def users_works(self, author_id, page=1, per_page=30,
image_sizes=['px_128x128', 'px_480mw', 'large'],
include_stats=True, include_sanity_level=True):
url = 'https://public-api.secure.pixiv.net/v1/users/%d/works.json' % (author_id)
params = {
'page': page,
'per_page': per_page,
'include_stats': include_stats,
'include_sanity_level': include_sanity_level,
'image_sizes': ','.join(image_sizes),
}
r = self.auth_requests_call('GET', url, params=params)
return self.parse_result(r)
# 用户收藏
def users_favorite_works(self, author_id, page=1, per_page=30,
image_sizes=['px_128x128', 'px_480mw', 'large'],
include_sanity_level=True):
url = 'https://public-api.secure.pixiv.net/v1/users/%d/favorite_works.json' % (author_id)
params = {
'page': page,
'per_page': per_page,
'include_sanity_level': include_sanity_level,
'image_sizes': ','.join(image_sizes),
}
r = self.auth_requests_call('GET', url, params=params)
return self.parse_result(r)
# 用户活动
def users_feeds(self, author_id, show_r18=1, max_id=None):
url = 'https://public-api.secure.pixiv.net/v1/users/%d/feeds.json' % (author_id)
params = {
'relation': 'all',
'type': 'touch_nottext',
'show_r18': show_r18,
}
if max_id:
params['max_id'] = max_id
r = self.auth_requests_call('GET', url, params=params)
return self.parse_result(r)
# 用户关注的用户
def users_following(self, author_id, page=1, per_page=30):
url = 'https://public-api.secure.pixiv.net/v1/users/%d/following.json' % (author_id)
params = {
'page': page,
'per_page': per_page,
}
r = self.auth_requests_call('GET', url, params=params)
return self.parse_result(r)
# 排行榜/过去排行榜
# ranking_type: [all, illust, manga, ugoira]
# mode: [daily, weekly, monthly, rookie, original, male, female, daily_r18, weekly_r18, male_r18, female_r18, r18g]
# for 'illust' & 'manga': [daily, weekly, monthly, rookie, daily_r18, weekly_r18, r18g]
# for 'ugoira': [daily, weekly, daily_r18, weekly_r18],
# page: [1-n]
# date: '2015-04-01' (仅过去排行榜)
def ranking(self, ranking_type='all', mode='daily', page=1, per_page=50, date=None,
image_sizes=['px_128x128', 'px_480mw', 'large'],
profile_image_sizes=['px_170x170', 'px_50x50'],
include_stats=True, include_sanity_level=True):
url = 'https://public-api.secure.pixiv.net/v1/ranking/%s.json' % (ranking_type)
params = {
'mode': mode,
'page': page,
'per_page': per_page,
'include_stats': include_stats,
'include_sanity_level': include_sanity_level,
'image_sizes': ','.join(image_sizes),
'profile_image_sizes': ','.join(profile_image_sizes),
}
if date:
params['date'] = date
r = self.auth_requests_call('GET', url, params=params)
return self.parse_result(r)
# alias for old API ranking_all()
def ranking_all(self, mode='daily', page=1, per_page=50, date=None,
image_sizes=['px_128x128', 'px_480mw', 'large'],
profile_image_sizes=['px_170x170', 'px_50x50'],
include_stats=True, include_sanity_level=True):
return self.ranking(ranking_type='all', mode=mode, page=page, per_page=per_page, date=date,
image_sizes=image_sizes, profile_image_sizes=profile_image_sizes,
include_stats=include_stats, include_sanity_level=include_sanity_level)
# 作品搜索
def search_works(self, query, page=1, per_page=30, mode='text',
period='all', order='desc', sort='date',
types=['illustration', 'manga', 'ugoira'],
image_sizes=['px_128x128', 'px_480mw', 'large'],
include_stats=True, include_sanity_level=True):
url = 'https://public-api.secure.pixiv.net/v1/search/works.json'
params = {
'q': query,
'page': page,
'per_page': per_page,
'period': period,
'order': order,
'sort': sort,
'mode': mode,
'types': ','.join(types),
'include_stats': include_stats,
'include_sanity_level': include_sanity_level,
'image_sizes': ','.join(image_sizes),
}
r = self.auth_requests_call('GET', url, params=params)
return self.parse_result(r)
# 最新作品 (New -> Everyone)
def latest_works(self, page=1, per_page=30,
image_sizes=['px_128x128', 'px_480mw', 'large'],
profile_image_sizes=['px_170x170', 'px_50x50'],
include_stats=True, include_sanity_level=True):
url = 'https://public-api.secure.pixiv.net/v1/works.json'
params = {
'page': page,
'per_page': per_page,
'include_stats': include_stats,
'include_sanity_level': include_sanity_level,
'image_sizes': ','.join(image_sizes),
'profile_image_sizes': ','.join(profile_image_sizes),
}
r = self.auth_requests_call('GET', url, params=params)
return self.parse_result(r)