-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
81 lines (75 loc) · 2.08 KB
/
Copy pathmain.go
File metadata and controls
81 lines (75 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Package main 是一个解析消息为纯文本的示例, 可通过引用 package 的方式扩展更多处理逻辑.
package main
import (
"flag"
"fmt"
"strconv"
"strings"
"time"
"github.com/QQBackup/ntdb-plaintext-extracter/model"
"github.com/QQBackup/ntdb-plaintext-extracter/ntdb"
)
func main() {
pg := flag.Bool("pg", false, "print all available group IDs")
g := flag.Int64("g", 0, "print messages in this group")
ver := flag.Int("ver", 0, "the database version, try another if panic (available: 0-2)")
flag.Parse()
chats := flag.Args() // 聊天记录文件路径 (.db)
switch *ver {
case 0:
domain[model.GroupMessageTableRow](*pg, *g, chats)
case 1:
domain[model.GroupMessageTableRowVer1](*pg, *g, chats)
case 2:
domain[model.GroupMessageTableRowVer2](*pg, *g, chats)
default:
panic("unsupported version: " + strconv.Itoa(*ver))
}
}
func domain[G model.GroupMessageTables](pg bool, g int64, chats []string) {
for _, name := range chats {
if strings.HasSuffix(name, ".db") {
fmt.Println("---------------", name, "---------------")
db, err := ntdb.NewNTDatabase[G](name, time.Hour)
if err != nil {
panic(err)
}
if pg {
gids, err := db.GetAllGroupIDs()
if err != nil {
panic(err)
}
for _, gid := range gids {
fmt.Println(gid)
}
continue
}
err = db.RangeMessages(g, func(lng *G) error {
ln := model.ToSmallestGroupMessageTable(lng)
inf, err := db.GetGroupUserInfoByUserID(ln.UserID) // this is slow, use cache in production env.
if err != nil {
return err
}
msb := strings.Builder{}
msb.WriteString("【")
msb.WriteString(inf.Nickname)
msb.WriteString("(")
msb.WriteString(strconv.FormatInt(inf.Uin, 10))
msb.WriteString(")】")
msb.WriteString(ln.Msg.String())
msb.WriteByte('\n')
msb.WriteByte('\n')
fmt.Print(msb.String())
return nil
})
if err != nil {
fmt.Println("[HINT] failed to parse group messages, maybe try a different -ver parameter?")
fmt.Println()
panic(err)
}
_ = db.Close()
continue
}
panic("unsupported file " + name)
}
}