-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
185 lines (181 loc) · 4.27 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
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
package main
import (
"database/sql"
"fmt"
"github.com/PuerkitoBio/goquery"
"github.com/labstack/echo/v4"
_ "github.com/mattn/go-sqlite3"
"log"
"net/http"
"net/url"
"pixiv-like2tgchan-bot/config"
"regexp"
"time"
)
type Like struct {
title string
description string
url string
pubDate string
link string
pid string
src []string
}
func init() {
// 设置日志格式
log.SetFlags(log.LstdFlags | log.Lshortfile)
}
func Client() (http.Client, error) {
if config.UseProxy == false {
return http.Client{}, nil
}
// 设置clash代理
uri, err := url.Parse(config.ProxyURL)
if err != nil {
log.Fatal(err)
return http.Client{}, nil
}
client := http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(uri),
},
}
return client, nil
}
// GetLikes 获取用户收藏
func GetLikes() ([]Like, error) {
client, err := Client()
if err != nil {
log.Fatal(err)
}
resp, err := client.Get(config.RssURL)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
// Load the XML document
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
log.Fatal(err)
}
likes := make([]Like, 0)
doc.Find("item").Each(func(i int, s *goquery.Selection) {
// For each item found, get the content
title := regexp.MustCompile("CDATA\\[(.*)\\]\\]>").FindStringSubmatch(s.Find("title").Text())[1]
description := s.Find("description").Text()
pubDate := s.Find("pubDate").Text()
link := s.Find("guid").Text()
pid := regexp.MustCompile("(\\d+)").FindStringSubmatch(link)[1]
srcs := make([]string, 0)
s.Find("img").Each(func(i int, s *goquery.Selection) {
src, _ := s.Attr("src")
srcs = append(srcs, src)
})
like := Like{
title: title,
description: description,
pubDate: pubDate,
link: link,
pid: pid,
src: srcs,
}
likes = append(likes, like)
//fmt.Println(i, title, pubDate, link, description)
})
// log.Println(likes)
return likes, nil
}
func GetDb() *sql.DB {
db, err := sql.Open("sqlite3", "./foo.db")
if err != nil {
log.Fatal(err)
}
return db
}
func InitDb(db *sql.DB) *sql.DB {
table := `CREATE TABLE IF NOT EXISTS likes (
title TEXT NOT NULL,
description TEXT NOT NULL,
pubDate DATE NULL,
link TEXT NOT NULL UNIQUE
);`
_, err := db.Exec(table)
//defer db.Close()
if err != nil {
log.Fatal(err)
}
return db
}
func SaveLike(db *sql.DB, like Like) {
sqlStr := `INSERT INTO likes (title, description, pubDate, link) VALUES (?, ?, ?, ?)`
_, err := db.Exec(sqlStr, like.title, like.description, like.pubDate, like.link)
//defer db.Close()
if err != nil {
log.Fatal(err)
}
}
func Check(db *sql.DB, link string) bool {
sqlStr := `SELECT * FROM likes WHERE link = ?`
row := db.QueryRow(sqlStr, link)
err := row.Scan(&link)
if err == sql.ErrNoRows {
return false
}
return true
}
func SendMessage(like Like) {
client, err := Client()
if err != nil {
log.Fatal(err)
}
formData := url.Values{
"chat_id": {config.ChatID},
"caption": {like.title + "\n" + like.link},
"photo": {like.src[0]},
"reply_markup": {fmt.Sprintf(`{"inline_keyboard":[[{"text":"🌏","url":"%s"},{"text":"⤵","url":"https://pixiv.re/%s.jpg"}]]}`, like.link, like.pid)},
}
tgBotUrl := "https://api.telegram.org/bot" + config.TgBotToken + "/sendPhoto"
resp, err := client.PostForm(tgBotUrl, formData)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
//body, err := io.ReadAll(resp.Body)
//log.Println(string(body))
if err != nil {
log.Fatal(err)
}
//log.Println(like)
}
func StartTask() {
cursor := GetDb()
cursor = InitDb(cursor)
// 定时运行
for {
likes, err := GetLikes()
//log.Println(likes)
if err != nil {
log.Fatal(err)
}
for i, like := range likes {
// 如果数据库中存在该条记录,不处理
if Check(cursor, like.link) {
// SendMessage(like)
log.Println(i+1, "已存在", like.link)
} else {
SendMessage(like)
SaveLike(cursor, like)
log.Println(i+1, "保存成功", like.link)
}
}
log.Printf("refresh %d minutes later...", config.RefreshTime)
time.Sleep(time.Duration(config.RefreshTime) * time.Minute)
}
}
func main() {
e := echo.New()
e.Static("/", "public")
go StartTask()
log.Println("start task...")
e.Logger.Fatal(e.Start(":9004"))
}