/
main.go
238 lines (181 loc) · 4.78 KB
/
main.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package main
import (
"context"
"database/sql"
"flag"
"fmt"
"log"
"os"
"os/signal"
"sync"
"github.com/UTD-JLA/botsu/internal/activities"
"github.com/UTD-JLA/botsu/internal/guilds"
"github.com/UTD-JLA/botsu/internal/users"
_ "github.com/glebarez/go-sqlite"
"github.com/jackc/pgx/v5/pgxpool"
)
var dbLocation = flag.String("db", "bot_data.db", "Database file location")
var maxConcurrency = flag.Int("max-concurrency", 3, "Maximum number of concurrent requests")
var populateVideoMeta = flag.Bool("vid-meta", true, "Populate video metadata")
var pgDBURL = flag.String("pgdb", "", "Postgres database URL")
type aibUser struct {
ID string
Timezone *string
ReadingSpeedCharsPerMinute *float32
ReadingSpeedBookPagesPerMinute *float32
ReadingSpeedMangaPagesPerMinute *float32
DailyGoal *uint64
}
type aibGuild struct {
ID string
Timezone *string
}
func main() {
flag.Parse()
db, err := sql.Open("sqlite", *dbLocation)
if err != nil {
log.Fatal(err)
}
ctx, cancel := context.WithCancel(context.Background())
pgPool, err := pgxpool.New(ctx, *pgDBURL)
if err != nil {
log.Fatal(err)
}
activityRepo := activities.NewActivityRepository(pgPool)
userRepo := users.NewUserRepository(pgPool)
guildRepo := guilds.NewGuildRepository(pgPool)
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt)
go func() {
<-sigs
fmt.Println("Interrupted")
cancel()
}()
userRows, err := db.QueryContext(ctx, "SELECT user_id, time_zone, reading_speed, reading_speed_book_pages, reading_speed_pages, daily_goal FROM user_configs")
if err != nil {
log.Fatal(err)
}
for userRows.Next() {
var user aibUser
err = userRows.Scan(
&user.ID,
&user.Timezone,
&user.ReadingSpeedCharsPerMinute,
&user.ReadingSpeedBookPagesPerMinute,
&user.ReadingSpeedMangaPagesPerMinute,
&user.DailyGoal,
)
if err != nil {
log.Fatal(err)
}
newUser := users.NewUser(user.ID)
newUser.Timezone = user.Timezone
if user.DailyGoal != nil {
newUser.DailyGoal = int(*user.DailyGoal)
}
if user.ReadingSpeedCharsPerMinute != nil {
newUser.VisualNovelReadingSpeed = *user.ReadingSpeedCharsPerMinute
}
if user.ReadingSpeedBookPagesPerMinute != nil {
newUser.BookReadingSpeed = *user.ReadingSpeedBookPagesPerMinute
}
if user.ReadingSpeedMangaPagesPerMinute != nil {
newUser.MangaReadingSpeed = *user.ReadingSpeedMangaPagesPerMinute
}
err = userRepo.Create(ctx, newUser)
if err != nil {
log.Println(err)
}
}
guildRows, err := db.QueryContext(ctx, "SELECT guild_id, time_zone FROM guild_configs")
if err != nil {
log.Fatal(err)
}
for guildRows.Next() {
var guild aibGuild
err = guildRows.Scan(
&guild.ID,
&guild.Timezone,
)
if err != nil {
log.Fatal(err)
}
newGuild := guilds.NewGuild(guild.ID)
newGuild.Timezone = guild.Timezone
err = guildRepo.Create(ctx, newGuild)
if err != nil {
log.Println(err)
}
}
activityRows, err := db.QueryContext(ctx, "SELECT id, user_id, name, type, url, date, duration, raw_duration, raw_duration_unit, speed FROM activities")
if err != nil {
log.Fatal(err)
}
i := 0
sem := make(chan struct{}, *maxConcurrency)
wg := sync.WaitGroup{}
for activityRows.Next() {
fmt.Printf("Processing row %d\n", i)
var activity aibActivity
err = activityRows.Scan(
&activity.ID,
&activity.UserID,
&activity.Name,
&activity.Type,
&activity.URL,
&activity.Date,
&activity.Duration,
&activity.RawDuration,
&activity.RawDurationUnit,
&activity.Speed,
)
if err != nil {
log.Fatal(err)
}
// get tags, relations are in tags_to_activities (tag_id, activity_id)
// and tags (id, name)
tagsRows, err := db.QueryContext(ctx, `
SELECT tags.name
FROM tags_to_activities
INNER JOIN tags ON tags_to_activities.tag_id = tags.id
WHERE tags_to_activities.activity_id = ?
`, activity.ID)
if err != nil {
log.Fatal(err)
}
for tagsRows.Next() {
var tag string
err = tagsRows.Scan(&tag)
if err != nil {
log.Fatal(err)
}
activity.Tags = append(activity.Tags, tag)
}
newActivity := activity.asNewFormatActivity()
if *populateVideoMeta && newActivity.MediaType != nil &&
*newActivity.MediaType == activities.ActivityMediaTypeVideo && activity.URL != nil {
fmt.Printf("Processing video %s\n", *activity.URL)
wg.Add(1)
sem <- struct{}{}
go func() {
defer wg.Done()
defer func() { <-sem }()
err = populateVideoMetadata(ctx, newActivity, *activity.URL)
if err != nil {
log.Println(err)
}
err = activityRepo.Create(ctx, newActivity)
if err != nil {
log.Println(err)
}
}()
} else {
err = activityRepo.Create(ctx, newActivity)
if err != nil {
log.Println(err)
}
}
i++
}
wg.Wait()
}