From 53a41c81ff013a7d79d4186f9a6c1785c6eefa7f Mon Sep 17 00:00:00 2001 From: liyp Date: Sun, 21 Apr 2024 17:13:20 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E5=8E=86=E5=8F=B2=E6=95=B0=E6=8D=AE=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 3 +- tools/get_history_message.go | 140 +++++++++++++++++++++++++++++++++++ workers/pkg.go | 3 +- 3 files changed, 144 insertions(+), 2 deletions(-) create mode 100644 tools/get_history_message.go diff --git a/main.go b/main.go index 6e649f7..94a1c6c 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,6 @@ package main import ( "database/sql" - "encoding/json" "fmt" "go-bot/config" "go-bot/utils" @@ -11,6 +10,8 @@ import ( "net/http" "time" + "github.com/goccy/go-json" + _ "github.com/mattn/go-sqlite3" ) diff --git a/tools/get_history_message.go b/tools/get_history_message.go new file mode 100644 index 0000000..a8f280a --- /dev/null +++ b/tools/get_history_message.go @@ -0,0 +1,140 @@ +package main + +import ( + "database/sql" + "fmt" + "io" + "log" + "net/http" + "time" + + "github.com/goccy/go-json" + _ "github.com/mattn/go-sqlite3" +) + +func main() { + db, err := sql.Open("sqlite3", "./data.db") + if err != nil { + panic(err) + } + defer db.Close() + + rows, err := db.Query("SELECT DISTINCT group_id FROM messages") + if err != nil { + panic(err) + } + defer rows.Close() + var groupIDs []int + for rows.Next() { + var groupID int + err := rows.Scan(&groupID) + if err != nil { + panic(err) + } + groupIDs = append(groupIDs, groupID) + } + for _, groupID := range groupIDs { + minSeq := get_min_seqs(db, groupID) + println("minSeq:", minSeq) + // if minSeq == 0 { + // continue + // } + for minSeq != 0 { + fetchHistoryMessages(db, minSeq, groupID) + temp := get_min_seqs(db, groupID) + if temp == minSeq { + println("已获取到最久数据!") + time.Sleep(time.Second * 2) + break + } + minSeq = temp + // println("minSeq_changed:", minSeq) + + } + } + +} +func get_min_seqs(db *sql.DB, groupID int) int { + var minMessageSeq int + err := db.QueryRow("SELECT MIN(message_seq) FROM messages WHERE group_id = ?", groupID).Scan(&minMessageSeq) + if err != nil { + panic(err) + } + // println("minMessageSeq:", minMessageSeq) + var count int + err = db.QueryRow("SELECT COUNT(*) FROM messages WHERE group_id = ? AND message_seq = ?", groupID, minMessageSeq-1).Scan(&count) + if err != nil { + panic(err) + } + // println("count:", count) + if count > 0 { + return 0 + } + fmt.Printf("Group ID: %d, Min Message Seq: %d\n", groupID, minMessageSeq) + return minMessageSeq +} +func fetchHistoryMessages(db *sql.DB, message_seq int, group_id int) (int, error) { + url := fmt.Sprintf("http://127.0.0.1:5700/get_group_msg_history?message_seq=%d&group_id=%d", message_seq, group_id) + println("min message_seq :", message_seq, "min group_id: ", group_id) + println(url) + resp, err := http.Get(url) + if err != nil { + panic(err) + } + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + if err != nil { + return 0, err + } + // println("Body:", string(body)) + var messageResp map[string]interface{} + err = json.Unmarshal(body, &messageResp) + if err != nil { + return 0, err + } + data := messageResp["data"].(map[string]interface{}) + messages := data["messages"].([]interface{}) + for _, message := range messages { + messageMap := message.(map[string]interface{}) + insertMessage(db, messageMap) + + } + return 0, nil +} + +func insertMessage(db *sql.DB, data map[string]interface{}) error { + + if data["post_type"] == "message" { + // id := data["message_id"].(float64) + post_type := data["post_type"].(string) + message_type := data["message_type"].(string) + // 设置东八区时间 + loc, err := time.LoadLocation("Asia/Shanghai") + if err != nil { + log.Fatal(err) + } + message_time := (time.Unix(int64(data["time"].(float64)), 0).In(loc)).Format("2006-01-02 15:04:05") + group_id := data["group_id"].(float64) + message_id := data["message_id"].(float64) + raw_message := data["raw_message"].(string) + sender := data["sender"].(map[string]interface{}) + sender_user_id := sender["user_id"].(float64) + sender_nickname := sender["nickname"].(string) + sender_card := sender["card"].(string) + if sender_card == "" { + sender_card = sender_nickname + } + sender_role := sender["role"].(string) + message_seq := data["message_seq"].(float64) + + fmt.Println(post_type, message_type, message_time, group_id, message_id, raw_message, sender_user_id, sender_nickname, sender_card, sender_role, message_seq) + // Insert data into database + _, err = db.Exec("INSERT INTO messages ( post_type, message_type, time, group_id, message_id, raw_message, sender_user_id, sender_nickname, sender_card, sender_role, message_seq) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", + post_type, message_type, message_time, group_id, message_id, raw_message, sender_user_id, sender_nickname, sender_card, sender_role, message_seq) + fmt.Println("Data inserted successfully!") + if err != nil { + log.Fatal(err) + } + } + return nil +} diff --git a/workers/pkg.go b/workers/pkg.go index b481c52..7eeb5eb 100644 --- a/workers/pkg.go +++ b/workers/pkg.go @@ -1,12 +1,13 @@ package workers import ( - "encoding/json" "fmt" "io" "net/http" "strings" "time" + + "github.com/goccy/go-json" ) type Pkg struct { From ad6b907b823e75bf839354a3a07a840e3e3d9ee9 Mon Sep 17 00:00:00 2001 From: liyp Date: Sun, 21 Apr 2024 17:22:36 +0800 Subject: [PATCH 2/7] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=BB=B6=E8=BF=9F?= =?UTF-8?q?=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tools/get_history_message.go | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/get_history_message.go b/tools/get_history_message.go index a8f280a..1eb2617 100644 --- a/tools/get_history_message.go +++ b/tools/get_history_message.go @@ -97,6 +97,7 @@ func fetchHistoryMessages(db *sql.DB, message_seq int, group_id int) (int, error for _, message := range messages { messageMap := message.(map[string]interface{}) insertMessage(db, messageMap) + time.Sleep(time.Second * 1) } return 0, nil From 59408e0ba3ad0fb149e5b9acf095b930e91725c6 Mon Sep 17 00:00:00 2001 From: liyp Date: Sun, 21 Apr 2024 17:25:06 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=BB=B6=E8=BF=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tools/get_history_message.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/get_history_message.go b/tools/get_history_message.go index 1eb2617..c5b14e6 100644 --- a/tools/get_history_message.go +++ b/tools/get_history_message.go @@ -97,9 +97,9 @@ func fetchHistoryMessages(db *sql.DB, message_seq int, group_id int) (int, error for _, message := range messages { messageMap := message.(map[string]interface{}) insertMessage(db, messageMap) - time.Sleep(time.Second * 1) } + time.Sleep(time.Second * 1) return 0, nil } From ffab2cb534b723bac1b6568e1eef61dd8982713b Mon Sep 17 00:00:00 2001 From: liyp Date: Tue, 23 Apr 2024 20:38:37 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=E6=B7=BB=E5=8A=A0lsp=E6=8F=92=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++- config example.toml | 10 ++++++++++ go.mod | 7 ++----- workers/lsp.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ workers/newworker.go | 4 ++++ workers/pkg.go | 2 +- 6 files changed, 63 insertions(+), 7 deletions(-) create mode 100644 config example.toml create mode 100644 workers/lsp.go diff --git a/.gitignore b/.gitignore index 945acc2..74eeb37 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ data.db -test.json \ No newline at end of file +test.json +config.toml \ No newline at end of file diff --git a/config example.toml b/config example.toml new file mode 100644 index 0000000..fc3a200 --- /dev/null +++ b/config example.toml @@ -0,0 +1,10 @@ +[Server] +APIURL = "0.0.0.0:5580" +POSTURL = "http://0.0.0.0:5700" + +[Group] +AllowGroup = [] +AllowUser = [] +AllowRole = [] +BlockGroup = [] +BlockUser = [] \ No newline at end of file diff --git a/go.mod b/go.mod index 5667b21..91eeedc 100644 --- a/go.mod +++ b/go.mod @@ -1,13 +1,10 @@ module go-bot -go 1.21.0 +go 1.22.2 require github.com/BurntSushi/toml v1.3.2 -require golang.org/x/net v0.17.0 // indirect - require ( - github.com/goccy/go-json v0.10.2 // indirect - github.com/gorilla/websocket v1.5.1 + github.com/goccy/go-json v0.10.2 github.com/mattn/go-sqlite3 v1.14.22 ) diff --git a/workers/lsp.go b/workers/lsp.go new file mode 100644 index 0000000..c3c1e0c --- /dev/null +++ b/workers/lsp.go @@ -0,0 +1,44 @@ +package workers + +import ( + "fmt" + + "github.com/goccy/go-json" + + "io" + "net/http" +) + +type Lsp struct { + *StdAns +} + +func (a *Lsp) GetMsg() string { + a.AllowGroup = []string{"313047773"} + url := "https://api.lolicon.app/setu/v2?size=mini&r18=0" + resp, err := http.Get(url) + if err != nil { + return "获取失败" + } + defer resp.Body.Close() + budy, err := io.ReadAll(resp.Body) + var res map[string]interface{} + err = json.Unmarshal(budy, &res) + if err != nil { + return "解析失败" + } + code := res["error"].(string) + if code != "" { + return "获取失败" + } + data := res["data"].([]interface{}) + uid := data[0].(map[string]interface{})["uid"].(float64) + urls := data[0].(map[string]interface{})["urls"].(map[string]interface{}) + imgUrl := urls["mini"].(string) + // title := data[0].(map[string]interface{})["title"].(string) + // println("标题:" + title + "\n" + imgUrl) + msg := fmt.Sprintf("[CQ:reply,id=%s]Pixiv ID:%f", a.MID, uid) + a.SendMsg(msg) + return fmt.Sprintf("[CQ:image,file=%s,type=flash]", imgUrl) + +} diff --git a/workers/newworker.go b/workers/newworker.go index 8d14c8c..9992d51 100644 --- a/workers/newworker.go +++ b/workers/newworker.go @@ -22,6 +22,10 @@ func NewWorker(parms []string, uid, gid, role, mid, rawMsg string) Worker { return &Ip{ StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg), } + case "lsp": + return &Lsp{ + StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg), + } default: return &Emm{ StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg)} diff --git a/workers/pkg.go b/workers/pkg.go index 7eeb5eb..437111a 100644 --- a/workers/pkg.go +++ b/workers/pkg.go @@ -25,7 +25,7 @@ func (a *Pkg) GetMsg() string { parms := strings.Split(raw_msg, " ") url := "https://archlinux.org/packages/search/json/?name=" + parms[1] - if len(parms) > 2 { + if len(parms) > 2 && parms[1] != "" { url += "&repo=" + strings.ToUpper(parms[2][:1]) + parms[2][1:] } // 输出请求地址 From 46dde0cd18714996b106ebf2c16c6c23f6853885 Mon Sep 17 00:00:00 2001 From: liyp Date: Tue, 23 Apr 2024 21:32:59 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9lsp=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E5=A4=A7=E5=B0=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++- workers/lsp.go | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 74eeb37..2666aab 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ data.db test.json -config.toml \ No newline at end of file +config.toml +*.exe \ No newline at end of file diff --git a/workers/lsp.go b/workers/lsp.go index c3c1e0c..0255b5b 100644 --- a/workers/lsp.go +++ b/workers/lsp.go @@ -15,7 +15,7 @@ type Lsp struct { func (a *Lsp) GetMsg() string { a.AllowGroup = []string{"313047773"} - url := "https://api.lolicon.app/setu/v2?size=mini&r18=0" + url := "https://api.lolicon.app/setu/v2?r18=0&size=small" resp, err := http.Get(url) if err != nil { return "获取失败" @@ -34,11 +34,11 @@ func (a *Lsp) GetMsg() string { data := res["data"].([]interface{}) uid := data[0].(map[string]interface{})["uid"].(float64) urls := data[0].(map[string]interface{})["urls"].(map[string]interface{}) - imgUrl := urls["mini"].(string) + imgUrl := urls["small"].(string) // title := data[0].(map[string]interface{})["title"].(string) // println("标题:" + title + "\n" + imgUrl) - msg := fmt.Sprintf("[CQ:reply,id=%s]Pixiv ID:%f", a.MID, uid) + msg := fmt.Sprintf("[CQ:reply,id=%s]Pixiv ID:%d", a.MID, int64(uid)) a.SendMsg(msg) - return fmt.Sprintf("[CQ:image,file=%s,type=flash]", imgUrl) + return fmt.Sprintf("[CQ:image,file=%s]", imgUrl) } From 6d8a3476a871c00b75ffc0e0866b9b9801c2e0e1 Mon Sep 17 00:00:00 2001 From: liyp Date: Sat, 11 May 2024 19:38:19 +0800 Subject: [PATCH 6/7] =?UTF-8?q?pkg=E5=A2=9E=E5=8A=A0=E6=A0=B9=E6=8D=AE?= =?UTF-8?q?=E6=8A=95=E7=A5=A8=E4=BA=BA=E6=95=B0=E6=8E=92=E5=BA=8F=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E7=BB=93=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- workers/pkg.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/workers/pkg.go b/workers/pkg.go index 437111a..8280eab 100644 --- a/workers/pkg.go +++ b/workers/pkg.go @@ -21,6 +21,8 @@ func (a *Pkg) GetMsg() string { } // 去除换行符 raw_msg := strings.TrimRight(a.RawMsg, "\n") + raw_msg = strings.TrimRight(raw_msg, "\r") + fmt.Println("raw_msg:", raw_msg) parms := strings.Split(raw_msg, " ") @@ -32,12 +34,12 @@ func (a *Pkg) GetMsg() string { fmt.Println("pkg url:", url) req, err := http.Get(url) if err != nil { - return err.Error() + return "服务器网络错误!" } defer req.Body.Close() body, err := io.ReadAll(req.Body) if err != nil { - return err.Error() + return "数据解析错误!" } // fmt.Println("pkg body:", string(body)) // var pkg []Package @@ -84,8 +86,19 @@ func (a *Pkg) GetMsg() string { if err != nil { fmt.Println(err) } + // 增加根据投票人数排序 searchResults := searchMap["results"].([]interface{}) - searchResult := searchResults[0].(map[string]interface{}) + maxVotes := 0.0 + var searchResult map[string]interface{} + for _, result := range searchResults { + resultMap := result.(map[string]interface{}) + numVotes := resultMap["NumVotes"].(float64) + if numVotes > maxVotes { + maxVotes = numVotes + searchResult = resultMap + } + } + maintainer, ok := searchResult["Maintainer"].(string) if !ok { maintainer = "孤儿包" From 383cdf1e0239633aa43e83395b131b32b0e57d7f Mon Sep 17 00:00:00 2001 From: liyp Date: Thu, 27 Jun 2024 13:16:14 +0800 Subject: [PATCH 7/7] =?UTF-8?q?=E4=BC=98=E5=8C=96pkg=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- workers/pkg.go | 88 +++++++++++++++++++++++++++----------------------- 1 file changed, 48 insertions(+), 40 deletions(-) diff --git a/workers/pkg.go b/workers/pkg.go index 8280eab..2531e54 100644 --- a/workers/pkg.go +++ b/workers/pkg.go @@ -59,7 +59,7 @@ func (a *Pkg) GetMsg() string { } defer req.Body.Close() - fmt.Println("aur url:", url) + fmt.Println("aur suggest url:", url) re, err := io.ReadAll(req.Body) // fmt.Println(string(re)) if err != nil { @@ -70,26 +70,31 @@ func (a *Pkg) GetMsg() string { if err != nil { fmt.Println(err) } - if len(suggestions) > 0 { - searchUrl := "https://aur.archlinux.org/rpc/v5/search/" + suggestions[0] + "?by=name" - searchReq, err := http.Get(searchUrl) - if err != nil { - fmt.Println(err) - } - defer searchReq.Body.Close() - searchRe, err := io.ReadAll(searchReq.Body) - if err != nil { - fmt.Println(err) - } - var searchMap map[string]interface{} - err = json.Unmarshal(searchRe, &searchMap) - if err != nil { - fmt.Println(err) - } - // 增加根据投票人数排序 - searchResults := searchMap["results"].([]interface{}) - maxVotes := 0.0 - var searchResult map[string]interface{} + // println(len(suggestions)) + if len(suggestions) == 0 { + return "没有找到相关软件" + } + searchUrl := "https://aur.archlinux.org/rpc/v5/search/" + suggestions[0] + "?by=name" + searchReq, err := http.Get(searchUrl) + if err != nil { + fmt.Println(err) + } + defer searchReq.Body.Close() + searchRe, err := io.ReadAll(searchReq.Body) + if err != nil { + fmt.Println(err) + } + var searchMap map[string]interface{} + err = json.Unmarshal(searchRe, &searchMap) + if err != nil { + fmt.Println(err) + } + searchResults := searchMap["results"].([]interface{}) + // println("searchResults:", len(searchResults)) + maxVotes := 0.0 + var searchResult map[string]interface{} + // 增加根据投票人数排序 + if len(searchResults) > 1 { for _, result := range searchResults { resultMap := result.(map[string]interface{}) numVotes := resultMap["NumVotes"].(float64) @@ -98,26 +103,29 @@ func (a *Pkg) GetMsg() string { searchResult = resultMap } } - - maintainer, ok := searchResult["Maintainer"].(string) - if !ok { - maintainer = "孤儿包" - } - last_update := time.Unix(int64(searchResult["LastModified"].(float64)), 0).Format("2006-01-02 15:04:05") - var msg string - msg += "仓库:AUR\n" - msg += "包名:" + searchResult["Name"].(string) + "\n" - msg += "版本:" + searchResult["Version"].(string) + "\n" - msg += "描述:" + searchResult["Description"].(string) + "\n" - msg += "维护者:" + maintainer + "\n" - msg += "链接:" + searchResult["URL"].(string) + "\n" - msg += "更新时间:" + last_update + "\n" - - fmt.Println(msg) - return msg - } else { - return "没有找到相关软件" + fmt.Println(len(searchResult)) + } else if len(searchResults) == 1 { + searchResult = searchResults[0].(map[string]interface{}) } + maintainer, ok := searchResult["Maintainer"].(string) + if !ok { + maintainer = "孤儿包" + } + println("maintainer:", maintainer) + // last_update := '' + last_update := time.Unix(int64(searchResult["LastModified"].(float64)), 0).Format("2006-01-02 15:04:05") + + var msg string + msg += "仓库:AUR\n" + msg += "包名:" + searchResult["Name"].(string) + "\n" + msg += "版本:" + searchResult["Version"].(string) + "\n" + msg += "描述:" + searchResult["Description"].(string) + "\n" + msg += "维护者:" + maintainer + "\n" + msg += "链接:" + searchResult["URL"].(string) + "\n" + msg += "更新时间:" + last_update + + fmt.Println(msg) + return msg } result := resultSlipe[0].(map[string]interface{})