forked from nilaoda/BBDown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MediaListFetcher.cs
77 lines (73 loc) · 3.52 KB
/
MediaListFetcher.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
using BBDown.Core.Entity;
using System.Text.Json;
using static BBDown.Core.Entity.Entity;
using static BBDown.Core.Util.HTTPUtil;
namespace BBDown.Core.Fetcher
{
/// <summary>
/// 合集解析
/// https://space.bilibili.com/23630128/channel/collectiondetail?sid=2045
/// https://www.bilibili.com/medialist/play/23630128?business=space_collection&business_id=2045 (无法从该链接打开合集)
/// </summary>
public class MediaListFetcher : IFetcher
{
public async Task<VInfo> FetchAsync(string id)
{
id = id[10..];
var api = $"https://api.bilibili.com/x/v1/medialist/info?type=8&biz_id={id}&tid=0";
var json = await GetWebSourceAsync(api);
using var infoJson = JsonDocument.Parse(json);
var data = infoJson.RootElement.GetProperty("data");
var listTitle = data.GetProperty("title").GetString()!;
var intro = data.GetProperty("intro").GetString()!;
long pubTime = data.GetProperty("ctime").GetInt64()!;
List<Page> pagesInfo = new();
bool hasMore = true;
var oid = "";
int index = 1;
while (hasMore)
{
var listApi = $"https://api.bilibili.com/x/v2/medialist/resource/list?type=8&oid={oid}&otype=2&biz_id={id}&with_current=true&mobi_app=web&ps=20&direction=false&sort_field=1&tid=0&desc=false";
json = await GetWebSourceAsync(listApi);
using var listJson = JsonDocument.Parse(json);
data = listJson.RootElement.GetProperty("data");
hasMore = data.GetProperty("has_more").GetBoolean();
foreach (var m in data.GetProperty("media_list").EnumerateArray())
{
var pageCount = m.GetProperty("page").GetInt32();
var desc = m.GetProperty("intro").GetString()!;
var ownerName = m.GetProperty("upper").GetProperty("name").ToString();
var ownerMid = m.GetProperty("upper").GetProperty("mid").ToString();
foreach (var page in m.GetProperty("pages").EnumerateArray())
{
Page p = new(index++,
m.GetProperty("id").ToString(),
page.GetProperty("id").ToString(),
"", //epid
pageCount == 1 ? m.GetProperty("title").ToString() : $"{m.GetProperty("title")}_P{page.GetProperty("page")}_{page.GetProperty("title")}", //单P使用外层标题 多P则拼接内层子标题
page.GetProperty("duration").GetInt32(),
page.GetProperty("dimension").GetProperty("width").ToString() + "x" + page.GetProperty("dimension").GetProperty("height").ToString(),
m.GetProperty("pubtime").GetInt64(),
m.GetProperty("cover").ToString(),
desc,
ownerName,
ownerMid);
if (!pagesInfo.Contains(p)) pagesInfo.Add(p);
else index--;
}
oid = m.GetProperty("id").ToString();
}
}
var info = new VInfo
{
Title = listTitle.Trim(),
Desc = intro.Trim(),
Pic = "",
PubTime = pubTime,
PagesInfo = pagesInfo,
IsBangumi = false
};
return info;
}
}
}