This commit is contained in:
shoopea 2019-05-11 16:36:29 +08:00
parent f66a73f5f7
commit 05cc83d5f7

40
td.go
View File

@ -35,15 +35,13 @@ func ListenCW(c *tdlib.Client) {
t := time.Now()
m := ChatWarsMessage{
UserID64: ownUserID64,
SenderUserID64: int64(updateMsg.Message.SenderUserID),
Date: updateMsg.Message.Date,
ID64: updateMsg.Message.ID,
ChatID64: updateMsg.Message.ChatID,
Text: txt,
}
m.Date = time.Unix(int64(updateMsg.Message.Date), 0)
MQCWMsgQueue <- m
fmt.Printf("[%d-%02d-%02d %02d:%02d:%02d-00:00]", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
@ -73,7 +71,6 @@ func getHistory(c *tdlib.Client, chatID64 *int64, senderUserID64 *int64) {
var msgCount int32 = 0
var msgParsed int32 = 0
var lastParsedID64 int64 = int64(math.MaxInt64)
var loopCount int32 = 1
var lastParsedTime time.Time = time.Now()
var chat int64
var m ChatWarsMessage
@ -84,27 +81,14 @@ func getHistory(c *tdlib.Client, chatID64 *int64, senderUserID64 *int64) {
chat = *chatID64
}
if *senderUserID64 != 0 {
userDetails, err := c.GetUser(int32(*senderUserID64))
failOnError(err, "getHistory : GetUser")
chatDetails, err := c.GetChat(chat)
failOnError(err, "getHistory : GetChat")
fmt.Printf("Exporting historic messages for chat %d (%s) from user %d (%s)...\n", chat, chatDetails.Title, *senderUserID64, userDetails.Username)
} else {
chatDetails, err := c.GetChat(chat)
failOnError(err, "getHistory : GetChat")
fmt.Printf("Exporting historic messages for chat %d (%s) ...\n", chat, chatDetails.Title)
}
fmt.Printf("Exporting historic messages for chat %d from user %d ...\n", chat, *senderUserID64)
for lastParsedID64 >= 0 && loopCount > 0 {
msgs, err := c.GetChatHistory(chat, lastParsedID64, 0, 33, false)
loopCount = 0
for lastParsedID64 >= 0 {
msgs, err := c.GetChatHistory(chat, lastParsedID64, 0, 50, 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)
loopCount = 1
fmt.Printf("Waiting : %d messages retrieved out of %d dating back %s ...\n", msgCount, msgParsed, lastParsedTime.Format(time.RFC3339))
} else {
logOnError(err, "Cannot get history")
lastParsedID64 = -1
@ -117,24 +101,21 @@ func getHistory(c *tdlib.Client, chatID64 *int64, senderUserID64 *int64) {
case tdlib.MessageTextType:
if msg.ForwardInfo == nil {
m = ChatWarsMessage{
UserID64: ownUserID64,
SenderUserID64: int64(msg.SenderUserID),
Date: msg.Date,
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{
UserID64: int64(msg.SenderUserID),
SenderUserID64: int64(msg.ForwardInfo.(*tdlib.MessageForwardedFromUser).SenderUserID),
Date: msg.ForwardInfo.(*tdlib.MessageForwardedFromUser).Date,
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,
@ -147,15 +128,14 @@ func getHistory(c *tdlib.Client, chatID64 *int64, senderUserID64 *int64) {
msgCount = msgCount + 1
}
default:
log.Printf("getHistory(%d) : no handler for %s\n", msg.ID, msg.Content.GetMessageContentEnum())
log.Printf("getHistory : no handler for %s\n", msg.Content.GetMessageContentEnum())
}
if m.ID64 < lastParsedID64 {
lastParsedID64 = msg.ID
lastParsedTime = m.Date
loopCount = loopCount + 1
lastParsedTime = time.Unix(int64(m.Date), 0)
}
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)
fmt.Printf("Waiting : %d messages retrieved out of %d dating back %s ...\n", msgCount, msgParsed, lastParsedTime.Format(time.RFC3339))
}
}