1
1
package main
2
2
3
3
import (
4
+ "bufio"
4
5
"fmt"
5
- "github.com/jaytaylor/html2text "
6
+ "os "
6
7
"regexp"
7
8
"strconv"
9
+ "strings"
8
10
"time"
11
+
12
+ "github.com/jaytaylor/html2text"
9
13
)
10
14
11
- func getTelegramGroupHistory (group string , grace int ) {
12
- i := 1
15
+ func getTelegramGroupHistory (group string , grace int , dumpFlag bool ) {
13
16
graceCounter := 0
14
- ret := ""
15
- for i != 0 {
16
- messageid := strconv .Itoa (i )
17
+ dumpfile := group + ".dump"
18
+ msgtxt := ""
19
+
20
+ fmt .Println ("==== Dumping messages for " + group + " ====" )
21
+ messageCounter := readFromTelegramDump (dumpfile , dumpFlag )
22
+ messageCounter ++
23
+ for messageCounter != 0 {
24
+ messageid := strconv .Itoa (messageCounter )
17
25
body := retriveRequestBody ("https://t.me/" + group + "/" + messageid + "?embed=1" )
18
26
message := getTelegramMessage (body )
27
+
19
28
if message != "" {
20
29
for j := 0 ; j < graceCounter ; j ++ {
21
- fmt .Println ("[MESSAGE REMOVED]" )
30
+ msg := "[MESSAGE REMOVED]"
31
+ writeOnFile (dumpfile , "[" + strconv .Itoa (messageCounter - graceCounter + j )+ "] " + msg + "\n " )
32
+ fmt .Println (msg )
22
33
}
34
+
23
35
graceCounter = 0
24
36
username , nickname := getTelegramUsername (body )
25
37
date , time := getTelegramMessageDateTime (body )
38
+
26
39
if username == "" {
27
- ret = "[" + date + " " + time + "] " + nickname + ": " + message
40
+ msgtxt = "[" + date + " " + time + "] " + nickname + ": " + message
28
41
} else {
29
- ret = "[" + date + " " + time + "] " + nickname + "(" + username + "): " + message
42
+ msgtxt = "[" + date + " " + time + "] " + nickname + "(" + username + "): " + message
30
43
}
31
- msg , _ := html2text .FromString (ret )
44
+
45
+ msg , _ := html2text .FromString (msgtxt )
46
+
47
+ writeOnFile (dumpfile , "[" + messageid + "] " + msg + "\n " )
32
48
fmt .Println (msg )
33
49
} else {
34
50
graceCounter ++
35
51
if graceCounter == grace {
36
52
break
37
53
}
38
54
}
39
- i ++
55
+ messageCounter ++
40
56
time .Sleep (time .Millisecond * 500 )
41
57
}
42
58
}
@@ -88,7 +104,7 @@ func getTelegramVideo(body string) string {
88
104
}
89
105
90
106
func getTelegramUsername (body string ) (string , string ) {
91
- re := regexp .MustCompile (`class=\"tgme_widget_message_author_name\" (?:href="https://t\.me/(\w+)")? dir=\"auto\">(.*)</a> in <a` )
107
+ re := regexp .MustCompile (`class=\"tgme_widget_message_author_name\"\s? (?:href="https://t\.me/(\w+)")? dir=\"auto\">(.*)<\/(?:span>)?(?:a>)? in <a` )
92
108
match := re .FindStringSubmatch (body )
93
109
return match [1 ], match [2 ]
94
110
}
@@ -98,3 +114,20 @@ func getTelegramMessageDateTime(body string) (string, string) {
98
114
match := re .FindStringSubmatch (body )
99
115
return match [1 ], match [2 ]
100
116
}
117
+
118
+ func readFromTelegramDump (dumpfile string , dumpFlag bool ) int {
119
+ messageCounter := 0
120
+ if dumpFlag {
121
+ fmt .Println ("The dump will be saved in " + dumpfile )
122
+ if fileExists (dumpfile ) {
123
+ file , _ := os .Open (dumpfile )
124
+ scan := bufio .NewScanner (file )
125
+ for scan .Scan () {
126
+ messageSlice := strings .Split (scan .Text (), " " )
127
+ fmt .Println (strings .Join (messageSlice [1 :], " " ))
128
+ messageCounter , _ = strconv .Atoi (strings .Trim (messageSlice [0 ], "[]" ))
129
+ }
130
+ }
131
+ }
132
+ return messageCounter
133
+ }
0 commit comments