From d0c8dc15c2ccff994b71cb8e5a561c902893f735 Mon Sep 17 00:00:00 2001 From: shoopea Date: Sun, 29 Dec 2019 11:52:50 +0800 Subject: [PATCH] test --- def.go | 4 ++++ main.go | 40 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/def.go b/def.go index 3926e26..84e0653 100644 --- a/def.go +++ b/def.go @@ -4,6 +4,10 @@ import ( "time" ) +type DataBackup struct { + Messages []ChatWarsMessage `json:"messages"` +} + type ChatWarsMessage struct { TGUserID64 int64 `json:"tg_user_id"` TGSenderUserID64 int64 `json:"tg_sender_user_id"` diff --git a/main.go b/main.go index 901eb41..411a8aa 100644 --- a/main.go +++ b/main.go @@ -1,11 +1,14 @@ package main import ( - // "encoding/json" + "bytes" + "encoding/json" "fmt" "log" "math" + "os" "time" + "zip" "github.com/Arman92/go-tdlib" ) @@ -72,7 +75,7 @@ func main() { fmt.Println("Got ChatWars message") - time.Sleep(11 * time.Second) + time.Sleep(1 * time.Second) fmt.Println("Now getting history...") @@ -97,6 +100,10 @@ func getHistory(c *tdlib.Client, chatID64 *int64) { failOnError(err, "getHistory : GetChat") fmt.Printf("Exporting historic messages for chat %d (%s) ...\n", chat, chatDetails.Title) + bkp := DataBackup{} + s := new([]ChatWarsMessage) + msgs := *s + for lastParsedID64 >= 0 { prevLastParsedID64 := lastParsedID64 msgs, err := c.GetChatHistory(chat, lastParsedID64, 0, 99, false) @@ -143,7 +150,7 @@ func getHistory(c *tdlib.Client, chatID64 *int64) { } if m.ID64 != 0 { - // export msg here + msgs = append(msgs, m) msgCount = msgCount + 1 } default: @@ -177,6 +184,33 @@ func getHistory(c *tdlib.Client, chatID64 *int64) { } log.Printf("Exported %d messages.\n", msgCount) fmt.Printf("Exported %d messages.\n", msgCount) + + bkp.Messages = msgs + b, err := json.Marshal(bkp) + logOnError(err, "getHistory : Marshal") + + zbuf := new(bytes.Buffer) + zw := zip.NewWriter(zbuf) + zf, err := zw.Create(`backup.json`) + logOnError(err, "getHistory : Create") + + _, err = zf.Write(b) + logOnError(err, "getHistory : Write") + + err = zw.Close() + logOnError(err, "getHistory : Close") + + f, err := os.Create("./backup.zip") + logOnError(err, "getHistory : Create") + + defer f.Close() + + w := bufio.NewWriter(f) + zbuf.WriteTo(w) + w.Flush() + + return + } func OwnUserID(c *tdlib.Client) int32 {