-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathPixivAppAPI.cs
433 lines (405 loc) · 20 KB
/
PixivAppAPI.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace PixivCS
{
public class PixivAppAPI : PixivBaseAPI
{
public PixivAppAPI(string AccessToken, string RefreshToken, string UserID, bool ExperimentalConnection = false) :
base(AccessToken, RefreshToken, UserID, ExperimentalConnection)
{ }
public PixivAppAPI() : base() { }
public PixivAppAPI(PixivBaseAPI BaseAPI) : base(BaseAPI) { }
public async Task<HttpResponseMessage> RequestCall(string Method, string Url,
Dictionary<string, string> Headers = null, List<(string, string)> Query = null,
HttpContent Body = null, bool RequireAuth = true)
{
var headers = Headers ?? new Dictionary<string, string>();
if (!(headers.ContainsKey("User-Agent") || headers.ContainsKey("user-agent")))
{
headers.Add("App-OS", "ios");
headers.Add("App-OS-Version", "10.3.1");
headers.Add("App-Version", "6.7.1");
headers.Add("User-Agent", "PixivIOSApp/6.7.1 (iOS 10.3.1; iPhone8,1)");
}
if (RequireAuth) headers.Add("Authorization", string.Format("Bearer {0}", AccessToken));
return await base.RequestCall(Method, Url, headers, Query, Body);
}
//用户详情
public async Task<Objects.UserDetail> GetUserDetailAsync(string UserID, string Filter = "for_ios",
bool RequireAuth = true)
{
string url = "https://app-api.pixiv.net/v1/user/detail";
List<(string, string)> query = new List<(string, string)>
{
("user_id", UserID),
("filter", Filter)
};
var res = await RequestCall("GET", url, Query: query, RequireAuth: RequireAuth);
return Objects.UserDetail.FromJson(await GetResponseString(res));
}
//用户作品
public async Task<Objects.UserIllusts> GetUserIllustsAsync(string UserID, string IllustType = "illust",
string Filter = "for_ios", string Offset = null, bool RequireAuth = true)
{
string url = "https://app-api.pixiv.net/v1/user/illusts";
List<(string, string)> query = new List<(string, string)>
{
("user_id", UserID),
("filter", Filter)
};
if (!string.IsNullOrEmpty(IllustType)) query.Add(("type", IllustType));
if (!string.IsNullOrEmpty(Offset)) query.Add(("offset", Offset));
var res = await RequestCall("GET", url, Query: query, RequireAuth: RequireAuth);
return Objects.UserIllusts.FromJson(await GetResponseString(res));
}
//用户收藏
public async Task<Objects.UserIllusts> GetUserBookmarksIllustAsync(string UserID, string Restrict = "public",
string Filter = "for_ios", string MaxBookmarkID = null, string Tag = null,
bool RequireAuth = true)
{
string url = "https://app-api.pixiv.net/v1/user/bookmarks/illust";
List<(string, string)> query = new List<(string, string)>
{
("user_id", UserID),
("restrict", Restrict),
("filter", Filter)
};
if (!string.IsNullOrEmpty(MaxBookmarkID)) query.Add(("max_bookmark_id", MaxBookmarkID));
if (!string.IsNullOrEmpty(Tag)) query.Add(("tag", Tag));
var res = await RequestCall("GET", url, Query: query, RequireAuth: RequireAuth);
return Objects.UserBookmarksIllust.FromJson(await GetResponseString(res));
}
//关注者的新作品
public async Task<Objects.UserIllusts> GetIllustFollowAsync(string Restrict = "public", string Offset = null,
bool RequireAuth = true)
{
string url = "https://app-api.pixiv.net/v2/illust/follow";
List<(string, string)> query = new List<(string, string)>
{
("restrict", Restrict)
};
if (!string.IsNullOrEmpty(Offset)) query.Add(("offset", Offset));
var res = await RequestCall("GET", url, Query: query, RequireAuth: RequireAuth);
return Objects.IllustFollow.FromJson(await GetResponseString(res));
}
//作品详情
public async Task<Objects.IllustDetail> GetIllustDetailAsync(string IllustID, bool RequireAuth = true)
{
string url = "https://app-api.pixiv.net/v1/illust/detail";
List<(string, string)> query = new List<(string, string)>
{
("illust_id", IllustID)
};
var res = await RequestCall("GET", url, Query: query, RequireAuth: RequireAuth);
return Objects.IllustDetail.FromJson(await GetResponseString(res));
}
//作品评论
//IncludeTotalComments决定是否在返回的JSON中包含总评论数
public async Task<Objects.IllustComments> GetIllustCommentsAsync(string IllustID, string Offset = null,
bool? IncludeTotalComments = null, bool RequireAuth = true)
{
string url = "https://app-api.pixiv.net/v1/illust/comments";
List<(string, string)> query = new List<(string, string)>
{
("illust_id", IllustID)
};
if (!string.IsNullOrEmpty(Offset)) query.Add(("offset", Offset));
if (IncludeTotalComments != null) query.Add(("include_total_comments",
IncludeTotalComments.Value ? "true" : "false"));
var res = await RequestCall("GET", url, Query: query, RequireAuth: RequireAuth);
return Objects.IllustComments.FromJson(await GetResponseString(res));
}
//发表评论
public async Task<Objects.IllustCommentAddResult> PostIllustCommentAddAsync(string IllustID, string Comment,
string ParentCommentID = null, bool RequireAuth = true)
{
string url = "https://app-api.pixiv.net/v1/illust/comment/add";
Dictionary<string, string> data = new Dictionary<string, string>
{
{ "illust_id", IllustID },
{ "comment", Comment }
};
if (!string.IsNullOrWhiteSpace(ParentCommentID))
data.Add("parent_comment_id", ParentCommentID);
var res = await RequestCall("POST", url, Body: new FormUrlEncodedContent(data),
RequireAuth: RequireAuth);
return Objects.IllustCommentAddResult.FromJson(await GetResponseString(res));
}
//相关作品
public async Task<Objects.UserIllusts> GetIllustRelatedAsync(string IllustID, string Filter = "for_ios",
List<string> SeedIllustIDs = null, bool RequireAuth = true)
{
string url = "https://app-api.pixiv.net/v2/illust/related";
List<(string, string)> query = new List<(string, string)>
{
("illust_id", IllustID),
("filter", Filter)
};
if (SeedIllustIDs != null)
{
foreach (var i in SeedIllustIDs)
query.Add(("seed_illust_ids[]", i));
}
var res = await RequestCall("GET", url, Query: query, RequireAuth: RequireAuth);
return Objects.IllustRelated.FromJson(await GetResponseString(res));
}
//首页推荐
//content_type: [illust, manga]
public async Task<Objects.IllustRecommended> GetIllustRecommendedAsync(string ContentType = "illust",
bool IncludeRankingLabel = true, string Filter = "for_ios",
string MaxBookmarkIDForRecommended = null,
string MinBookmarkIDForRecentIllust = null, string Offset = null,
bool? IncludeRankingIllusts = null, List<string> BookmarkIllustIDs = null,
string IncludePrivacyPolicy = null, bool RequireAuth = true)
{
string url = RequireAuth ? "https://app-api.pixiv.net/v1/illust/recommended" :
"https://app-api.pixiv.net/v1/illust/recommended-nologin";
List<(string, string)> query = new List<(string, string)>
{
("content_type", ContentType),
("include_ranking_label", IncludeRankingLabel ? "true" : "false"),
("filter", Filter)
};
if (!string.IsNullOrEmpty(MaxBookmarkIDForRecommended))
query.Add(("max_bookmark_id_for_recommend", MaxBookmarkIDForRecommended));
if (!string.IsNullOrEmpty(MinBookmarkIDForRecentIllust))
query.Add(("min_bookmark_id_for_recent_illust", MinBookmarkIDForRecentIllust));
if (!string.IsNullOrEmpty(Offset)) query.Add(("offset", Offset));
if (IncludeRankingIllusts != null)
query.Add(("include_ranking_illusts", IncludeRankingIllusts.Value ? "true" : "false"));
string ids = "";
if (BookmarkIllustIDs != null)
foreach (var i in BookmarkIllustIDs)
ids += (i + ",");
if (ids != "")
{
ids.TrimEnd(',');
query.Add(("bookmark_illust_ids", ids));
}
if (!string.IsNullOrEmpty(IncludePrivacyPolicy))
query.Add(("include_privacy_policy", IncludePrivacyPolicy));
var res = await RequestCall("GET", url, Query: query, RequireAuth: RequireAuth);
return Objects.IllustRecommended.FromJson(await GetResponseString(res));
}
//作品排行
//mode: [day, week, month, day_male, day_female, week_original, week_rookie, day_manga]
//date: yyyy-mm-dd
public async Task<Objects.UserIllusts> GetIllustRankingAsync(string Mode = "day", string Filter = "for_ios",
string Date = null, string Offset = null, bool RequireAuth = true)
{
string url = "https://app-api.pixiv.net/v1/illust/ranking";
List<(string, string)> query = new List<(string, string)>
{
("mode", Mode),
("filter", Filter)
};
if (!string.IsNullOrEmpty(Date)) query.Add(("date", Date));
if (!string.IsNullOrEmpty(Offset)) query.Add(("offset", Offset));
var res = await RequestCall("GET", url, Query: query, RequireAuth: RequireAuth);
return Objects.IllustRanking.FromJson(await GetResponseString(res));
}
//趋势标签
public async Task<Objects.TrendingTagsIllust> GetTrendingTagsIllustAsync(string Filter = "for_ios", bool RequireAuth = true)
{
string url = "https://app-api.pixiv.net/v1/trending-tags/illust";
List<(string, string)> query = new List<(string, string)>
{
("filter", Filter)
};
var res = await RequestCall("GET", url, Query: query, RequireAuth: RequireAuth);
return Objects.TrendingTagsIllust.FromJson(await GetResponseString(res));
}
//搜索
//search_target - 搜索类型
// partial_match_for_tags - 标签部分一致
// exact_match_for_tags - 标签完全一致
// title_and_caption - 标题说明文
//sort: [date_desc, date_asc]
//duration: [within_last_day, within_last_week, within_last_month]
public async Task<Objects.SearchIllustResult> GetSearchIllustAsync(string Word, string SearchTarget = "partial_match_for_tags",
string Sort = "date_desc", string Duration = null, string Filter = "for_ios", string Offset = null,
bool RequireAuth = true)
{
string url = "https://app-api.pixiv.net/v1/search/illust";
List<(string, string)> query = new List<(string, string)>
{
("word", Word),
("search_target", SearchTarget),
("sort", Sort),
("filter", Filter)
};
if (!string.IsNullOrEmpty(Duration)) query.Add(("duration", Duration));
if (!string.IsNullOrEmpty(Offset)) query.Add(("offset", Offset));
var res = await RequestCall("GET", url, Query: query, RequireAuth: RequireAuth);
return Objects.SearchIllustResult.FromJson(await GetResponseString(res));
}
//作品收藏详情
public async Task<Objects.IllustBookmarkDetail> GetIllustBookmarkDetailAsync(string IllustID, bool RequireAuth = true)
{
string url = "https://app-api.pixiv.net/v2/illust/bookmark/detail";
List<(string, string)> query = new List<(string, string)>
{
("illust_id", IllustID)
};
var res = await RequestCall("GET", url, Query: query, RequireAuth: RequireAuth);
return Objects.IllustBookmarkDetail.FromJson(await GetResponseString(res));
}
//新增收藏
public async Task PostIllustBookmarkAddAsync(string IllustID, string Restrict = "public",
List<string> Tags = null, bool RequireAuth = true)
{
string url = "https://app-api.pixiv.net/v2/illust/bookmark/add";
Dictionary<string, string> data = new Dictionary<string, string>
{
{ "illust_id", IllustID },
{ "restrict", Restrict }
};
string tags = "";
if (Tags != null)
foreach (var i in Tags)
tags += (i + " ");
tags = tags.Trim();
if (tags != "")
data.Add("tags", HttpUtility.UrlEncode(tags));
await RequestCall("POST", url, Body: new FormUrlEncodedContent(data),
RequireAuth: RequireAuth);
}
//删除收藏
public async Task PostIllustBookmarkDeleteAsync(string IllustID, bool RequireAuth = true)
{
string url = "https://app-api.pixiv.net/v1/illust/bookmark/delete";
Dictionary<string, string> data = new Dictionary<string, string>
{
{ "illust_id", IllustID }
};
await RequestCall("POST", url, Body: new FormUrlEncodedContent(data),
RequireAuth: RequireAuth);
}
//用户收藏标签列表
public async Task<Objects.UserBookmarkTags> GetUserBookmarkTagsIllustAsync(string Restrict = "public", string Offset = null,
bool RequireAuth = true)
{
string url = "https://app-api.pixiv.net/v1/user/bookmark-tags/illust";
List<(string, string)> query = new List<(string, string)>
{
("restrict", Restrict)
};
if (!string.IsNullOrEmpty(Offset)) query.Add(("offset", Offset));
var res = await RequestCall("GET", url, Query: query, RequireAuth: RequireAuth);
return Objects.UserBookmarkTags.FromJson(await GetResponseString(res));
}
//Following用户列表
public async Task<Objects.UserFollowList> GetUserFollowingAsync(string UserID, string Restrict = "public",
string Offset = null, bool RequireAuth = true)
{
string url = "https://app-api.pixiv.net/v1/user/following";
List<(string, string)> query = new List<(string, string)>
{
("user_id", UserID),
("restrict", Restrict)
};
if (!string.IsNullOrEmpty(Offset)) query.Add(("offset", Offset));
var res = await RequestCall("GET", url, Query: query, RequireAuth: RequireAuth);
return Objects.UserFollowList.FromJson(await GetResponseString(res));
}
//Followers用户列表
public async Task<Objects.UserFollowList> GetUserFollowerAsync(string UserID, string Restrict = "public",
string Offset = null, bool RequireAuth = true)
{
string url = "https://app-api.pixiv.net/v1/user/follower";
List<(string, string)> query = new List<(string, string)>
{
("user_id", UserID),
("restrict", Restrict)
};
if (!string.IsNullOrEmpty(Offset)) query.Add(("offset", Offset));
var res = await RequestCall("GET", url, Query: query, RequireAuth: RequireAuth);
return Objects.UserFollower.FromJson(await GetResponseString(res));
}
//关注用户
public async Task PostUserFollowAddAsync(string UserID, string Restrict = "public",
bool RequireAuth = true)
{
string url = "https://app-api.pixiv.net/v1/user/follow/add";
Dictionary<string, string> data = new Dictionary<string, string>
{
{ "user_id", UserID },
{ "restrict", Restrict }
};
await RequestCall("POST", url, Body: new FormUrlEncodedContent(data),
RequireAuth: RequireAuth);
}
//取关用户
public async Task PostUserFollowDeleteAsync(string UserID, string Restrict = "public",
bool RequireAuth = true)
{
string url = "https://app-api.pixiv.net/v1/user/follow/delete";
Dictionary<string, string> data = new Dictionary<string, string>
{
{ "user_id", UserID },
{ "restrict", Restrict }
};
await RequestCall("POST", url, Body: new FormUrlEncodedContent(data),
RequireAuth: RequireAuth);
}
//好P友
public async Task<Objects.UserFollowList> GetUserMyPixivAsync(string UserID, string Offset = null,
bool RequireAuth = true)
{
string url = "https://app-api.pixiv.net/v1/user/mypixiv";
List<(string, string)> query = new List<(string, string)>
{
("user_id", UserID)
};
if (!string.IsNullOrEmpty(Offset)) query.Add(("offset", Offset));
var res = await RequestCall("GET", url, Query: query, RequireAuth: RequireAuth);
return Objects.UserMyPixiv.FromJson(await GetResponseString(res));
}
//黑名单用户
public async Task<Objects.UserList> GetUserListAsync(string UserID, string Filter = "for_ios",
string Offset = null, bool RequireAuth = true)
{
string url = "https://app-api.pixiv.net/v2/user/list";
List<(string, string)> query = new List<(string, string)>
{
("user_id", UserID),
("filter", Filter)
};
if (!string.IsNullOrEmpty(Offset)) query.Add(("offset", Offset));
var res = await RequestCall("GET", url, Query: query, RequireAuth: RequireAuth);
return Objects.UserList.FromJson(await GetResponseString(res));
}
//Ugoira信息
public async Task<Objects.UgoiraMetadata> GetUgoiraMetadataAsync(string IllustID, bool RequireAuth = true)
{
string url = "https://app-api.pixiv.net/v1/ugoira/metadata";
List<(string, string)> query = new List<(string, string)>
{
("illust_id", IllustID)
};
var res = await RequestCall("GET", url, Query: query, RequireAuth: RequireAuth);
return Objects.UgoiraMetadata.FromJson(await GetResponseString(res));
}
//特辑详情(伪装成Chrome)
public async Task<Objects.ShowcaseArticle> GetShowcaseArticleAsync(string ShowcaseID)
{
string url = "https://www.pixiv.net/ajax/showcase/article";
Dictionary<string, string> headers = new Dictionary<string, string>
{
{ "User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36" },
{ "Referer", "https://www.pixiv.net" }
};
List<(string, string)> query = new List<(string, string)>()
{
("article_id", ShowcaseID)
};
var res = await RequestCall("GET", url, headers, Query: query, RequireAuth: false);
return Objects.ShowcaseArticle.FromJson(await GetResponseString(res));
}
}
}