-
Notifications
You must be signed in to change notification settings - Fork 1
/
twitter.go
405 lines (392 loc) · 11.3 KB
/
twitter.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
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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"strconv"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"golang.org/x/net/html"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
type Tweet struct {
Success bool
URL string
NitterURL string
Fullname string
Username string
Time string
Comments int
Retweets int
Quotes int
Likes int
Content string
Images []string
Videos []string
MediaNum int8
}
type Nitter struct {
Host string
Enable bool
OK int64
Fail int64
}
var nitterI = 0
var nitters []Nitter = []Nitter{}
func initNitter() {
for _, v := range config.Nitter {
var nitter Nitter = Nitter{
Host: v,
Enable: true,
OK: 0,
Fail: 0,
}
nitters = append(nitters, nitter)
}
}
func nowNitter() Nitter {
var enableCount int8 = 0
for _, v := range nitters {
if v.Enable {
enableCount++
}
}
if enableCount == 0 {
log.Println("所有的 Nitter Host 均遇到错误,重置。")
for i, _ := range nitters {
nitters[i].Enable = true
}
}
var nitter Nitter = nitters[nitterI]
nitterI++
if nitterI >= len(nitters) {
nitterI = 0
}
var nextNitter Nitter = nitters[nitterI]
if !nextNitter.Enable {
nitterI++
}
if nitterI >= len(nitters) {
nitterI = 0
}
return nitter
}
func nitterInfo() string {
var infos []string = []string{
fmt.Sprintf("[Nitter 状态 (%d)]", len(nitters)),
}
for _, v := range nitters {
var percentage int = 0
if v.OK+v.Fail > 0 {
percentage = int(v.OK * 100 / (v.OK + v.Fail))
}
var nameU []string = strings.Split(v.Host, ".")
var name string = v.Host
if len(nameU) >= 2 {
name = nameU[len(nameU)-2]
}
var enable string = "启用"
if !v.Enable {
enable = "禁用"
}
var info string = fmt.Sprintf("%s (%s): 成功 %d 失败 %d (%d %%)", name, enable, v.OK, v.Fail, percentage)
infos = append(infos, info)
}
return strings.Join(infos, "\n")
}
func tweetPush(update tgbotapi.Update, bot *tgbotapi.BotAPI, text string, toChannel bool, toChat string) {
var tweet Tweet = tweetGET(text)
if !tweet.Success {
log.Println("推特解析失敗。")
return
}
toChatID, _ := strconv.ParseInt(toChat, 10, 64)
var timeStr string = timeFormat(tweet.Time)
var statText string = fmt.Sprintf("💬%d 🔁%d 🔀%d 💖%d %s", tweet.Comments, tweet.Retweets, tweet.Quotes, tweet.Likes, timeStr)
var enter = "\n"
if strings.Contains(tweet.Content, "\n") {
enter += "\n"
}
text = text + enter + tweet.Content + enter + statText
var msg tgbotapi.Chattable
var mode = 0
if tweet.MediaNum == 0 {
// 文字
text = config.Head.Text + text
if toChannel {
msg = tgbotapi.NewMessageToChannel(toChat, text)
} else {
msg = tgbotapi.NewMessage(toChatID, text)
}
mode = 1
} else if tweet.MediaNum == 1 {
// 單一附件
if len(tweet.Images) > 0 {
var file tgbotapi.FileURL = tgbotapi.FileURL(tweet.Images[0])
var photoMsg tgbotapi.PhotoConfig
if toChannel {
photoMsg = tgbotapi.NewPhotoToChannel(toChat, file)
} else {
photoMsg = tgbotapi.NewPhoto(toChatID, file)
}
text = config.Head.Photo + text
photoMsg.Caption = text
msg = photoMsg
mode = 2
} else if len(tweet.Videos) > 0 {
var file tgbotapi.FileURL = tgbotapi.FileURL(tweet.Videos[0])
var videoMsg tgbotapi.VideoConfig = tgbotapi.NewVideo(toChatID, file)
text = config.Head.Video + text
videoMsg.Caption = text
msg = videoMsg
mode = 3
}
} else if tweet.MediaNum > 1 {
// 多附件
var files []interface{} = []interface{}{}
var isCaption = false
if len(tweet.Videos) > 0 {
if len(config.Head.Video) > 0 {
text = config.Head.Video + text
} else if len(config.Head.Photo) > 0 {
text = config.Head.Photo + text
}
}
for _, v := range tweet.Images {
var file tgbotapi.FileURL = tgbotapi.FileURL(v)
var photo tgbotapi.InputMediaPhoto = tgbotapi.NewInputMediaPhoto(file)
if !isCaption {
photo.Caption = text
isCaption = true
}
files = append(files, photo)
}
for _, v := range tweet.Videos {
var file tgbotapi.FileURL = tgbotapi.FileURL(v)
var video tgbotapi.InputMediaVideo = tgbotapi.NewInputMediaVideo(file)
if !isCaption {
video.Caption = text
isCaption = true
}
files = append(files, video)
}
var mediaGroupMsg tgbotapi.MediaGroupConfig = tgbotapi.NewMediaGroup(toChatID, files)
msg = mediaGroupMsg
mode = 5
}
if _, err := bot.Send(msg); err != nil {
dataCounts[2]++
log.Printf("向 %d 傳送 %s类型 訊息失敗[T]: %s\n", toChatID, modeString[mode], err)
health(false)
} else {
dataCounts[1]++
log.Printf("已向 %d 傳送 %s类型 訊息: %s\n", toChatID, modeString[mode], text)
health(true)
}
}
func timeFormat(timeStr string) string {
// "Jul 6, 2023 \xc2\xb7 5:49 PM UTC" as "Jan 02, 2006 \xc2\xb7 3:04 PM UTC": cannot parse "6, 2023 \xc2\xb7 5:49 PM UTC" as "02"
var layout string = "Jan 2, 2006 · 3:04 PM UTC" // UTC = GMT + 0
nTime, err := time.Parse(layout, timeStr)
if err != nil {
log.Println("時間格式化失敗", timeStr, err)
return timeStr
}
nTime = nTime.Add(time.Hour * time.Duration(config.TimeZone))
layout = "2006年1月2日 PM3:04"
var newStr string = nTime.Format(layout)
var th int = nTime.Hour()
var thC string = "AM"
if th >= 12 {
thC = "PM"
}
if th >= 0 && th < 6 {
newStr = strings.Replace(newStr, thC, " 夜间", 1)
} else if th >= 6 && th < 8 {
newStr = strings.Replace(newStr, thC, " 早晨", 1)
} else if th >= 8 && th < 11 {
newStr = strings.Replace(newStr, thC, " 上午", 1)
} else if th >= 11 && th < 13 {
newStr = strings.Replace(newStr, thC, " 中午", 1)
} else if th >= 13 && th < 18 {
newStr = strings.Replace(newStr, thC, " 下午", 1)
} else if th >= 18 && th < 24 {
newStr = strings.Replace(newStr, thC, " 晚上", 1)
}
if len(newStr) == 0 {
return timeStr
}
var timeZoneStr string = ""
if config.TimeZone < 0 {
timeZoneStr = fmt.Sprintf("%d", config.TimeZone)
} else if config.TimeZone > 0 {
timeZoneStr = fmt.Sprintf("+%d", config.TimeZone)
}
newStr += " (GMT" + timeZoneStr + ")"
var timeEmoji []string = []string{"🕛", "🕐", "🕑", "🕒", "🕓", "🕔", "🕕", "🕖", "🕗", "🕘", "🕙", "🕚"}
var timeEmojiN string = timeEmoji[3]
if th >= 12 {
timeEmojiN = timeEmoji[th-12]
} else {
timeEmojiN = timeEmoji[th]
}
return timeEmojiN + newStr
}
func tweetGETchk(url string) bool {
if !strings.Contains(url, "://twitter.com/") {
return false
}
if len(strings.Split(url, " ")) != 1 {
return false
}
return true
}
func tweetGET(url string) Tweet {
var tweet Tweet = Tweet{
Success: false,
URL: url,
NitterURL: "",
Fullname: "",
Username: "",
Time: "",
Comments: 0,
Retweets: 0,
Quotes: 0,
Likes: 0,
Content: "",
Images: []string{},
Videos: []string{},
}
if url == "" {
flag.Usage()
return tweet
}
log.Println("目標連結:", tweet.URL)
if strings.Contains(tweet.URL, "twitter.com/") {
log.Println("這是一個推特連結,開始清理額外引數。")
tweet.URL = strings.Split(tweet.URL, "?")[0]
log.Println("目標連結:", tweet.URL)
var nitter Nitter = nowNitter()
log.Println("選擇 Nitter Node:", nitter.Host)
tweet.NitterURL = strings.Replace(tweet.URL, "twitter.com/", nitter.Host+"/", 1)
log.Println("正在載入:", tweet.NitterURL)
res, err := http.Get(tweet.NitterURL)
if err != nil {
log.Println("載入失敗:", tweet.NitterURL)
nitters[nitterI].Enable = false
nitters[nitterI].Fail++
return tweet
}
defer res.Body.Close()
if res.StatusCode != 200 {
log.Println("載入失敗:", res.StatusCode, res.Status)
nitters[nitterI].Enable = false
nitters[nitterI].Fail++
return tweet
}
log.Println("載入成功:", res.StatusCode, res.Status)
// Load the HTML document
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
log.Println("解析資料失敗:", err)
}
log.Println("解析資料成功,解析推文...")
var tweetUsernames []*html.Node = doc.Find(".main-tweet .timeline-item .tweet-body .tweet-header .username").Nodes
if len(tweetUsernames) == 0 {
log.Println("解析推文作者帳號失敗")
nitters[nitterI].Enable = false
nitters[nitterI].Fail++
return tweet
} else {
tweet.Username = tweetUsernames[0].FirstChild.Data
log.Println("推文作者帳號:", tweet.Username)
}
var tweetFullnames []*html.Node = doc.Find(".main-tweet .timeline-item .tweet-body .tweet-header .fullname").Nodes
if len(tweetFullnames) == 0 {
log.Println("解析推文作者暱稱失敗")
tweet.Fullname = tweetUsernames[0].FirstChild.Data
log.Println("使用帳號名:", tweet.Username)
} else {
tweet.Fullname = tweetFullnames[0].FirstChild.Data
log.Println("推文作者暱稱:", tweet.Fullname)
}
var tweetTimes []*html.Node = doc.Find(".main-tweet .timeline-item .tweet-body .tweet-published").Nodes
if len(tweetTimes) == 0 {
log.Println("解析推文時間失敗")
} else {
tweet.Time = tweetTimes[0].FirstChild.Data
log.Println("推文時間:", tweet.Time)
}
var tweetStats []*html.Node = doc.Find(".main-tweet .timeline-item .tweet-body .tweet-stats .icon-container").Nodes
if len(tweetStats) == 0 {
log.Println("解析推文統計失敗")
} else {
var tweetStatTitle []string = []string{"回覆", "轉推", "引用", "喜歡"}
var tweetStatNum []int = []int{0, 0, 0, 0}
var i int = 0
for _, tweetStat := range tweetStats {
if i >= 4 {
break
}
num, err := strconv.Atoi(strings.Replace(strings.Replace(tweetStat.LastChild.Data, " ", "", -1), ",", "", -1))
if err != nil {
num = 0
}
tweetStatNum[i] = num
log.Println(tweetStatTitle[i], ":", tweetStatNum[i])
i++
}
tweet.Comments = tweetStatNum[0]
tweet.Retweets = tweetStatNum[1]
tweet.Quotes = tweetStatNum[2]
tweet.Likes = tweetStatNum[3]
}
var tweetContents []*html.Node = doc.Find(".main-tweet .timeline-item .tweet-body .tweet-content").Nodes
if len(tweetContents) == 0 {
log.Println("解析推文內容失敗")
} else {
var tweetContent *html.Node = tweetContents[0]
for c := tweetContent.FirstChild; c != nil; c = c.NextSibling {
if c.Type == html.TextNode {
tweet.Content += c.Data
} else if c.Type == html.ElementNode {
if c.Data == "a" {
tweet.Content += c.FirstChild.Data
}
}
}
log.Println("推文內容:", tweet.Content)
}
var images []*html.Node = doc.Find(".main-tweet .timeline-item .tweet-body .attachments .attachment .still-image").Nodes
if len(images) > 0 {
for _, image := range images {
for _, attr := range image.Attr {
if attr.Key == "href" {
var imageURL string = fmt.Sprintf("https://%s%s\n", config.Nitter[nitterI], attr.Val)
tweet.MediaNum++
tweet.Images = append(tweet.Images, imageURL)
log.Println("推文附圖:", imageURL)
}
}
}
}
var videos []*html.Node = doc.Find(".main-tweet .timeline-item .tweet-body .video-container video").Nodes
if len(videos) > 0 {
for _, video := range videos {
for _, attr := range video.Attr {
if attr.Key == "data-url" {
var videoURL string = fmt.Sprintf("https://%s%s\n", config.Nitter[nitterI], attr.Val)
tweet.MediaNum++
tweet.Videos = append(tweet.Videos, videoURL)
log.Println("推文附影片: ", videoURL)
}
}
}
}
}
nitters[nitterI].OK++
tweet.Success = true
return tweet
}