This repository was archived by the owner on Oct 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
151 lines (145 loc) · 3.61 KB
/
main.go
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
package main
import (
"bufio"
"compress/flate"
"encoding/xml"
"fmt"
"github.com/tidwall/gjson"
"io"
"log"
"net/http"
"net/url"
"os"
"sort"
"strconv"
"strings"
)
func main() {
args := os.Args
if len(args) > 2 || len(args) < 2 {
log.Println(args[0] + " [av/bv号/url(大小写敏感)]")
return
}
vid := matchVIDURL(parseShortURL(args[1]))
if vid == "" {
vid = args[1]
}
log.Println("开始请求获取CID...")
cidURL := "https://api.bilibili.com/x/player/pagelist?"
if strings.HasPrefix(strings.ToLower(vid), "av") {
cidURL += fmt.Sprintf("avid=%s", url.QueryEscape(vid))
} else if strings.HasPrefix(strings.ToLower(vid), "bv") {
cidURL += fmt.Sprintf("bvid=%s", url.QueryEscape(vid))
}
cidReq, err := http.NewRequest("GET", cidURL, nil)
if err != nil {
panic(err)
return
}
cidRsp, err := client.Do(cidReq)
if err != nil {
panic(err)
return
}
cidContext, err := io.ReadAll(cidRsp.Body)
if err != nil {
panic(err)
return
}
cidJson := gjson.Parse(string(cidContext))
data := cidJson.Get("data")
if len(data.Array()) <= 0 {
log.Println("未找到视频")
return
}
var pid int64
if len(data.Array()) > 1 {
data.ForEach(func(key, value gjson.Result) bool {
log.Printf("%d, Part标题: %s, PartCID: %d", value.Get("page").Int(), value.Get("part").String(), value.Get("cid").Int())
return true
})
inp, err := bufio.NewReader(os.Stdin).ReadString('\n')
if err != nil {
panic(err)
return
}
pid, err = strconv.ParseInt(strings.TrimSuffix(inp, "\r\n"), 10, 64)
if err != nil {
panic(err)
return
}
} else {
pid = data.Array()[0].Get("page").Int()
}
var cid int64
cidJson.Get("data").ForEach(func(key, value gjson.Result) bool {
if value.Get("page").Int() == pid {
cid = value.Get("cid").Int()
return false
}
return true
})
if cid == 0 {
log.Println("没有找到对应的CID")
return
}
commitReq, err := http.NewRequest("GET", fmt.Sprintf("https://comment.bilibili.com/%d.xml", cid), nil)
if err != nil {
panic(err)
return
}
res, err := client.Do(commitReq)
if err != nil {
panic(err)
return
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
panic(err)
}
}(res.Body)
flateReader := flate.NewReader(res.Body)
defer func(flateReader io.ReadCloser) {
err := flateReader.Close()
if err != nil {
panic(err)
}
}(flateReader)
context, err := io.ReadAll(flateReader)
if err != nil {
panic(err)
return
}
var commit Commit
err = xml.Unmarshal(context, &commit)
if err != nil {
panic(err)
return
}
log.Printf("获取到 %d 条弹幕, 开始输出:\n", len(commit.Comments))
var parsedComments []ParsedComment
for _, comment := range commit.Comments {
time, danmakuType, fontSize, color, sendTime, poolType, midHash, dmid := parsePAttribute(comment.P)
parsedComments = append(parsedComments, ParsedComment{
Time: time,
Type: danmakuType,
FontSize: fontSize,
Color: color,
SendTime: sendTime,
PoolType: poolType,
MidHash: midHash,
Dmid: dmid,
Content: comment.Content,
})
}
sort.Slice(parsedComments, func(i, j int) bool {
return parsedComments[i].Time < parsedComments[j].Time
})
crcinit()
for _, comment := range parsedComments {
colorCode := rgbToAnsi(comment.Color)
//fmt.Printf("Time: %.2f, Type: %d, Font Size: %d, Color: %d, Send Time: %s, Pool Type: %d, MidHash: %s, Dmid: %d\n", comment.Time, comment.Type, comment.FontSize, comment.Color, time.Unix(comment.SendTime, 0).String(), comment.PoolType, crack(comment.MidHash), comment.Dmid)
fmt.Printf("%s%s%s | UID: %s \n", colorCode, comment.Content, resetColor(), crack(comment.MidHash))
}
}