~badt/glavar

ref: 7f094d75b4139928e94262b0e81681664867c4c8 glavar/fthinoporo/main.go -rw-r--r-- 2.8 KiB
7f094d75Ian P Badtrousers glavar: authumnuscha got fucked 4 months ago
                                                                                
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main

import (
	"database/sql"
	"fmt"
	"io/ioutil"
	"os"
	"regexp"
	"strings"
	"time"

	_ "github.com/mattn/go-sqlite3"
	"jaytaylor.com/html2text"
)

const (
	sql_CREATE_TABLE_logec = `
CREATE TABLE "logec" (
	"time"	INTEGER NOT NULL,
	"login"	TEXT,
	"text"	TEXT,
	PRIMARY KEY("time")
);`

	sql_INSERT_MESSAGE = `
INSERT INTO "logec" ("time", "login", "text") VALUES (?, ?, ?);`
)

type M struct {
	T     int64  `sql:"time"`
	Login string `sql:"login"`
	Text  string `sql:"text"`
}

func feed(bus chan M) {
	path := os.Getenv("LOGEC")
	for i := 1; ; i++ {
		b, err := ioutil.ReadFile(fmt.Sprintf("%s/%d.html", path, i))
		if err != nil {
			break
		}
		tr := strings.Split(string(b), "</tr>")
		for _, cellb := range tr {
			if strings.Contains(cellb, "data-total-pages") {
				continue
			}
			td := strings.Split(cellb, "</td>")
			if len(td) == 0 || td[0] == "" {
				continue
			}

			t, login, opus := gett(td[0]), get(td[1]), get(td[2])
			opus, _ = html2text.FromString(opus, html2text.Options{})
			bus <- M{t.UnixNano(), login, opus}
		}
	}
	close(bus)
}

func main() {
	os.Remove("logec.sqlite")
	db, err := sql.Open("sqlite3", "logec.sqlite")
	if err != nil {
		panic(err)
	}
	defer db.Close()
	if _, err := db.Exec(sql_CREATE_TABLE_logec); err != nil {
		panic(err)
	}

	bus, auto := make(chan M), make(chan M)
	go feed(bus)
	go func() {
		dedup := make([]M, 1, 200)
		for m := range bus {
			tail := dedup[len(dedup)-1]
			// duplicate
			if tail.Text == m.Text {
				continue
			}
			// to be ordered
			if tail.T == m.T {
				dedup = append(dedup, m)
				continue
			}

			if len(dedup) > 1 {
				qsize := time.Second / time.Duration(len(dedup))
				for i, mm := range dedup {
					if mm.T == 0 {
						break
					}
					mm.T -= int64(time.Duration(i) * qsize)
					auto <- mm
				}
				dedup = dedup[:1]
				dedup[0] = m
				continue
			}

			if dedup[0].T != 0 {
				auto <- dedup[0]
			}
			dedup[0] = m
		}
		if len(dedup) > 1 {
			qsize := time.Second / time.Duration(len(dedup))
			for i, mm := range dedup {
				if mm.T == 0 {
					break
				}
				mm.T -= int64(time.Duration(i) * qsize)
				auto <- mm
			}
		}
		close(auto)
	}()

	tx, _ := db.Begin()
	stmt, err := tx.Prepare(sql_INSERT_MESSAGE)
	if err != nil {
		panic(err)
	}
	defer stmt.Close()

	pushed := map[int64]bool{}
	for m := range auto {
		if _, ok := pushed[m.T]; ok {
			continue
		}
		_, err = stmt.Exec(m.T, m.Login, m.Text)
		if err != nil {
			fmt.Println("!", m, err)
			continue
		}
		pushed[m.T] = true
	}
	tx.Commit()
}

var timeRx = regexp.MustCompile(`data-param='([^']+)'`)

func gett(s string) time.Time {
	m := timeRx.FindAllStringSubmatch(s, -1)
	tstr := m[0][1]
	t, _ := time.Parse("2006-01-02 15:04", tstr)
	return t
}

func get(s string) string {
	y := strings.Split(s, "<td class='  '>")
	return strings.TrimSpace(strings.Join(y[1:], ""))
}