From 53a41c81ff013a7d79d4186f9a6c1785c6eefa7f Mon Sep 17 00:00:00 2001 From: liyp Date: Sun, 21 Apr 2024 17:13:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=8E=B7=E5=8F=96=E5=8E=86?= =?UTF-8?q?=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 {