gocwexp/main.go

239 lines
6.3 KiB
Go
Raw Permalink Normal View History

2019-12-29 03:45:12 +01:00
package main
import (
2019-12-29 04:53:14 +01:00
"archive/zip"
2019-12-29 04:53:41 +01:00
"bufio"
2019-12-29 04:52:50 +01:00
"bytes"
"encoding/json"
2019-12-29 03:45:12 +01:00
"fmt"
2019-12-29 04:19:01 +01:00
"log"
"math"
2019-12-29 04:52:50 +01:00
"os"
2019-12-29 03:45:12 +01:00
"time"
"github.com/Arman92/go-tdlib"
)
var (
ownUserID64 = int64(0)
ownUserID32 = int32(0)
2019-12-29 04:27:08 +01:00
chtwrsID64 = int64(408101137)
2019-12-29 03:45:12 +01:00
)
func main() {
tdlib.SetLogVerbosityLevel(1)
tdlib.SetFilePath("./errors.txt")
// Create new instance of client
client := tdlib.NewClient(tdlib.Config{
APIID: "187786",
APIHash: "e782045df67ba48e441ccb105da8fc85",
SystemLanguageCode: "en",
DeviceModel: "ChatWarsExport",
SystemVersion: "0.1",
ApplicationVersion: "0.1",
UseMessageDatabase: true,
UseFileDatabase: true,
UseChatInfoDatabase: true,
UseTestDataCenter: false,
DatabaseDirectory: "./tdlib-db",
FileDirectory: "./tdlib-files",
IgnoreFileNames: false,
})
for {
currentState, _ := client.Authorize()
if currentState.GetAuthorizationStateEnum() == tdlib.AuthorizationStateWaitPhoneNumberType {
fmt.Print("Enter phone: ")
var number string
fmt.Scanln(&number)
_, err := client.SendPhoneNumber(number)
logOnError(err, "Error sending phone number.")
} else if currentState.GetAuthorizationStateEnum() == tdlib.AuthorizationStateWaitCodeType {
fmt.Print("Enter code: ")
var code string
fmt.Scanln(&code)
_, err := client.SendAuthCode(code)
logOnError(err, "Error sending auth code.")
} else if currentState.GetAuthorizationStateEnum() == tdlib.AuthorizationStateWaitPasswordType {
fmt.Print("Enter Password: ")
var password string
fmt.Scanln(&password)
_, err := client.SendAuthPassword(password)
logOnError(err, "Error sending auth password.")
} else if currentState.GetAuthorizationStateEnum() == tdlib.AuthorizationStateReadyType {
fmt.Println("Authorization Ready! Let's rock")
break
}
time.Sleep(1 * time.Second)
}
ownUserID32 = OwnUserID(client)
ownUserID64 = int64(OwnUserID(client))
2019-12-29 04:29:39 +01:00
ListenMe(client)
2019-12-29 04:20:20 +01:00
2019-12-29 04:29:39 +01:00
fmt.Println("Got ChatWars message")
2019-12-29 04:26:30 +01:00
2019-12-29 04:52:50 +01:00
time.Sleep(1 * time.Second)
2019-12-29 04:20:20 +01:00
2019-12-29 04:29:39 +01:00
fmt.Println("Now getting history...")
2019-12-29 04:20:20 +01:00
2019-12-29 04:14:36 +01:00
getHistory(client, &chtwrsID64)
2019-12-29 03:45:12 +01:00
}
2019-12-29 04:19:01 +01:00
func getHistory(c *tdlib.Client, chatID64 *int64) {
var msgCount int32 = 0
var msgParsed int32 = 0
var loopOverflow int32 = 0
var lastParsedID64 int64 = int64(math.MaxInt64)
var lastParsedTime time.Time = time.Now()
var m ChatWarsMessage
var chat int64
chat = *chatID64
fmt.Printf("Getting details for chatID64 %d ...\n", chat)
chatDetails, err := c.GetChat(chat)
failOnError(err, "getHistory : GetChat")
fmt.Printf("Exporting historic messages for chat %d (%s) ...\n", chat, chatDetails.Title)
2019-12-29 04:52:50 +01:00
bkp := DataBackup{}
s := new([]ChatWarsMessage)
2019-12-29 04:55:15 +01:00
bkpmsgs := *s
2019-12-29 04:52:50 +01:00
2019-12-29 04:19:01 +01:00
for lastParsedID64 >= 0 {
prevLastParsedID64 := lastParsedID64
msgs, err := c.GetChatHistory(chat, lastParsedID64, 0, 99, false)
if err != nil {
if err.Error() == "timeout" {
logOnError(err, "Waiting....")
fmt.Printf("Timeout : %d messages retrieved out of %d dating back %s (%d) ...\n", msgCount, msgParsed, lastParsedTime.Format(time.RFC3339), lastParsedID64)
time.Sleep(5 * time.Second)
} else {
logOnError(err, "Cannot get history")
lastParsedID64 = -1
}
} else if msgs.TotalCount > 0 {
for _, msg := range msgs.Messages {
msgParsed = msgParsed + 1
lastParsedTime = time.Unix(int64(msg.Date), 0)
switch msg.Content.GetMessageContentEnum() {
case tdlib.MessageTextType:
if msg.ForwardInfo == nil {
m = ChatWarsMessage{
TGUserID64: ownUserID64,
TGSenderUserID64: int64(msg.SenderUserID),
ID64: msg.ID,
ChatID64: msg.ChatID,
Text: msg.Content.(*tdlib.MessageText).Text.Text,
}
m.Date = time.Unix(int64(msg.Date), 0)
} else {
if msg.ForwardInfo.GetMessageForwardInfoEnum() == tdlib.MessageForwardedFromUserType {
m = ChatWarsMessage{
TGUserID64: int64(msg.SenderUserID),
TGSenderUserID64: int64(msg.ForwardInfo.(*tdlib.MessageForwardedFromUser).SenderUserID),
ID64: msg.ID,
ChatID64: 0,
Text: msg.Content.(*tdlib.MessageText).Text.Text,
}
m.Date = time.Unix(int64(msg.ForwardInfo.(*tdlib.MessageForwardedFromUser).Date), 0)
} else {
m = ChatWarsMessage{
ID64: 0,
}
}
}
if m.ID64 != 0 {
2019-12-29 04:55:15 +01:00
bkpmsgs = append(bkpmsgs, m)
2019-12-29 04:19:01 +01:00
msgCount = msgCount + 1
}
default:
log.Printf("getHistory(%d) : no handler for %s\n", msg.ID, msg.Content.GetMessageContentEnum())
}
if m.ID64 < lastParsedID64 {
lastParsedID64 = msg.ID
lastParsedTime = m.Date
}
if msgParsed%1000 == 0 {
fmt.Printf("Waiting : %d messages retrieved out of %d dating back %s (%d) ...\n", msgCount, msgParsed, lastParsedTime.Format(time.RFC3339), lastParsedID64)
}
}
} else {
lastParsedID64 = -1
}
if prevLastParsedID64 == lastParsedID64 {
loopOverflow++
if loopOverflow == 5 {
// we should be at the end !
lastParsedID64 = -1
} else {
logOnError(err, "Overflow ...")
fmt.Printf("Overflow : %d messages retrieved out of %d dating back %s (%d) ...\n", msgCount, msgParsed, lastParsedTime.Format(time.RFC3339), lastParsedID64)
time.Sleep(5 * time.Second)
}
} else {
loopOverflow = 0
}
}
log.Printf("Exported %d messages.\n", msgCount)
fmt.Printf("Exported %d messages.\n", msgCount)
2019-12-29 04:52:50 +01:00
2019-12-29 04:55:15 +01:00
bkp.Messages = bkpmsgs
2019-12-29 04:52:50 +01:00
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
2019-12-29 04:19:01 +01:00
}
func OwnUserID(c *tdlib.Client) int32 {
user, _ := c.GetMe()
return user.ID
}
2019-12-29 04:26:30 +01:00
func ListenMe(c *tdlib.Client) {
eventFilter := func(msg *tdlib.TdMessage) bool {
updateMsg := (*msg).(*tdlib.UpdateNewMessage)
2019-12-29 04:29:39 +01:00
senderUserID64 := int64(updateMsg.Message.SenderUserID)
2019-12-29 04:30:00 +01:00
if senderUserID64 == chtwrsID64 {
2019-12-29 04:26:30 +01:00
return true
} else {
return false
}
}
receiver := c.AddEventReceiver(&tdlib.UpdateNewMessage{}, eventFilter, 100)
for _ = range receiver.Chan {
log.Printf("ListenMe : updated.\n")
2019-12-29 04:29:39 +01:00
return
2019-12-29 04:26:30 +01:00
}
}