gocwexp/td.go

116 lines
3.4 KiB
Go
Raw Normal View History

2019-12-29 03:45:12 +01:00
package main
import (
2019-12-29 04:04:05 +01:00
// "encoding/json"
2019-12-29 03:45:12 +01:00
"fmt"
"log"
"math"
"time"
"github.com/Arman92/go-tdlib"
)
2019-12-29 04:14:36 +01:00
func getHistory(c *tdlib.Client, chatID64 *int64) {
2019-12-29 03:45:12 +01:00
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
2019-12-29 04:14:36 +01:00
var chat int64
2019-12-29 03:45:12 +01:00
2019-12-29 04:14:36 +01:00
chat = *chatID64
fmt.Printf("Getting details for chatID64 %d ...\n", chat)
2019-12-29 04:08:57 +01:00
2019-12-29 04:15:03 +01:00
chatDetails, err := c.GetChat(chat)
2019-12-29 03:45:12 +01:00
failOnError(err, "getHistory : GetChat")
2019-12-29 04:14:36 +01:00
fmt.Printf("Exporting historic messages for chat %d (%s) ...\n", chat, chatDetails.Title)
2019-12-29 03:45:12 +01:00
for lastParsedID64 >= 0 {
prevLastParsedID64 := lastParsedID64
2019-12-29 04:08:57 +01:00
msgs, err := c.GetChatHistory(chatID64, lastParsedID64, 0, 99, false)
2019-12-29 03:45:12 +01:00
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 {
// export msg here
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)
}
func OwnUserID(c *tdlib.Client) int32 {
user, _ := c.GetMe()
return user.ID
}