forked from andreyvit/telegramapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
export.go
102 lines (85 loc) · 2.07 KB
/
export.go
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"bytes"
"regexp"
"strings"
"time"
"github.com/andreyvit/telegramapi"
)
type Format int
const (
FormatFavorites Format = iota
)
var dividerRegexp = regexp.MustCompile(`^((\*\s*){2,}|={2,}|-{2,})$`)
var dividerRegexpStart = regexp.MustCompile(`^((\*\s*){2,}|={2,}|-{2,})\s`)
type Exporter struct {
UserNameAliases map[string]string
Format Format
TimeZone *time.Location
}
type exportState struct {
lastDate Date
hadMsgs bool
}
func (exp *Exporter) Export(chat *telegramapi.Chat) string {
var buf bytes.Buffer
var state exportState
for _, msg := range chat.Messages.Messages {
exp.exportMsg(&buf, &state, msg)
}
return buf.String()
}
func (exp *Exporter) exportMsg(w *bytes.Buffer, state *exportState, msg *telegramapi.Message) {
date := MakeDate(msg.Date.Add(-4 * time.Hour).In(exp.TimeZone).Date())
if state.lastDate.IsZero() || !date.Equal(state.lastDate) {
if !state.lastDate.IsZero() {
w.WriteString("\n\n")
}
state.lastDate = date
w.WriteString("### ")
w.WriteString(date.String())
w.WriteString(" ###\n\n")
state.hadMsgs = false
}
text := msg.Text
text = strings.Replace(text, "\n", "\n ", -1)
isMultiline := strings.Contains(text, "\n")
if dividerRegexp.MatchString(text) {
if state.hadMsgs {
w.WriteString("\n")
w.WriteString("* * *\n")
w.WriteString("\n")
state.hadMsgs = false
}
return
} else if dividerRegexpStart.MatchString(text) {
comment := strings.TrimSpace(dividerRegexpStart.ReplaceAllString(text, ""))
w.WriteString("\n")
w.WriteString("* * * " + comment + "\n")
w.WriteString("\n")
state.hadMsgs = false
return
}
from, tm := msg.From, msg.Date
if msg.FwdFrom != nil {
from = msg.FwdFrom
tm = msg.FwdDate
}
name := from.Name()
if alias := exp.UserNameAliases[name]; alias != "" {
name = alias
}
w.WriteString(name)
if false {
w.WriteString(" (")
w.WriteString(tm.Format("2006-01-02 15:04:05"))
w.WriteString(")")
}
w.WriteString(": ")
w.WriteString(text)
w.WriteString("\n")
if isMultiline {
w.WriteString("\n")
}
state.hadMsgs = true
}