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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
package main
import (
"compress/gzip"
"encoding/csv"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strconv"
)
const (
imdbDataDir = "../imdbdata"
basicsURL = "https://datasets.imdbws.com/title.basics.tsv.gz"
ratingsURL = "https://datasets.imdbws.com/title.ratings.tsv.gz"
)
func dataPath(name string) string {
return filepath.Join(imdbDataDir, name)
}
// downloadFile fetches url and writes to dst, overwriting if present.
func downloadFile(dst, url string) error {
resp, err := http.Get(url)
if err != nil {
return fmt.Errorf("http get: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("http %d from %s", resp.StatusCode, url)
}
f, err := os.Create(dst)
if err != nil {
return fmt.Errorf("create %s: %w", dst, err)
}
defer f.Close()
_, err = io.Copy(f, resp.Body)
if err != nil {
return fmt.Errorf("write %s: %w", dst, err)
}
log.Printf("downloaded %s", url)
return nil
}
// gunzipFile decompresses src.gz to dst.
func gunzipFile(src, dst string) error {
f, err := os.Open(src)
if err != nil {
return err
}
defer f.Close()
gz, err := gzip.NewReader(f)
if err != nil {
return err
}
defer gz.Close()
out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, gz)
if err != nil {
return fmt.Errorf("write %s: %w", dst, err)
}
return nil
}
// downloadImdbDatasets fetches and extracts the two TSV files into imdbdata/.
func (a *App) downloadImdbDatasets() error {
if err := os.MkdirAll(imdbDataDir, 0755); err != nil {
return fmt.Errorf("mkdir: %w", err)
}
type filePair struct {
url string
gz string
tsv string
}
pairs := []filePair{
{basicsURL, dataPath("title.basics.tsv.gz"), dataPath("title.basics.tsv")},
{ratingsURL, dataPath("title.ratings.tsv.gz"), dataPath("title.ratings.tsv")},
}
for _, p := range pairs {
if err := downloadFile(p.gz, p.url); err != nil {
return fmt.Errorf("download %s: %w", p.url, err)
}
if err := gunzipFile(p.gz, p.tsv); err != nil {
return fmt.Errorf("gunzip %s: %w", p.gz, err)
}
log.Printf("extracted %s", p.tsv)
}
return nil
}
// ratingEntry holds data from title.ratings.tsv for one title.
type ratingEntry struct {
AverageRating float64
NumVotes int
}
// basicEntry holds data from title.basics.tsv for one title.
type basicEntry struct {
TitleType string
PrimaryTitle string
OriginalTitle string
StartYear *int
RuntimeMinutes *int
}
// parseTSV reads a TSV file and calls fn for each row (after header).
// Only rows where keep(tconst) is true are passed to fn.
func parseTSV(path string, keep func(string) bool, fn func(record []string) error) error {
f, err := os.Open(path)
if err != nil {
return fmt.Errorf("open %s: %w", path, err)
}
defer f.Close()
r := csv.NewReader(f)
r.Comma = '\t'
r.LazyQuotes = true
r.FieldsPerRecord = -1
// skip header
if _, err := r.Read(); err != nil {
return fmt.Errorf("read header: %w", err)
}
for {
rec, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
return fmt.Errorf("read row: %w", err)
}
if len(rec) < 1 {
continue
}
tconst := rec[0]
if !keep(tconst) {
continue
}
if err := fn(rec); err != nil {
return err
}
}
return nil
}
// parseTitleRatings reads the ratings TSV and collects entries for the given ids.
func (a *App) parseTitleRatings(ids map[string]bool) (map[string]ratingEntry, error) {
result := make(map[string]ratingEntry)
err := parseTSV(dataPath("title.ratings.tsv"),
func(s string) bool { return ids[s] },
func(rec []string) error {
if len(rec) < 3 {
return nil
}
rating, err := strconv.ParseFloat(rec[1], 64)
if err != nil {
return nil
}
votes, err := strconv.Atoi(rec[2])
if err != nil {
return nil
}
result[rec[0]] = ratingEntry{AverageRating: rating, NumVotes: votes}
return nil
},
)
return result, err
}
// parseTitleBasics reads the basics TSV and collects entries for the given ids.
func (a *App) parseTitleBasics(ids map[string]bool) (map[string]basicEntry, error) {
result := make(map[string]basicEntry)
err := parseTSV(dataPath("title.basics.tsv"),
func(s string) bool { return ids[s] },
func(rec []string) error {
if len(rec) < 7 {
return nil
}
entry := basicEntry{
TitleType: rec[1],
PrimaryTitle: rec[2],
OriginalTitle: rec[3],
}
if rec[4] != "\\N" && rec[4] != "" {
v, err := strconv.Atoi(rec[4])
if err == nil {
entry.StartYear = &v
}
}
if rec[5] != "\\N" && rec[5] != "" {
v, err := strconv.Atoi(rec[5])
if err == nil {
entry.RuntimeMinutes = &v
}
}
result[rec[0]] = entry
return nil
},
)
return result, err
}
// applyImdbUpdates writes ratings and basics data into the imdb table.
func (a *App) applyImdbUpdates(ratings map[string]ratingEntry, basics map[string]basicEntry) error {
tx := a.DB.MustBegin()
rStmt, err := tx.Prepare(`UPDATE imdb SET average_rating = ?, num_votes = ? WHERE imdb_id = ?`)
if err != nil {
tx.Rollback()
return fmt.Errorf("prepare rating update: %w", err)
}
defer rStmt.Close()
bStmt, err := tx.Prepare(`
UPDATE imdb SET title_type = ?, primary_title = ?, original_title = ?,
start_year = ?, runtime_minutes = ? WHERE imdb_id = ?
`)
if err != nil {
tx.Rollback()
return fmt.Errorf("prepare basic update: %w", err)
}
defer bStmt.Close()
rCount, bCount := 0, 0
for id, r := range ratings {
dec := float64(int(r.AverageRating*10+0.5)) / 10.0
if _, err := rStmt.Exec(dec, r.NumVotes, id); err != nil {
tx.Rollback()
return fmt.Errorf("update rating %s: %w", id, err)
}
rCount++
}
for id, b := range basics {
if _, err := bStmt.Exec(
b.TitleType, b.PrimaryTitle, b.OriginalTitle,
b.StartYear, b.RuntimeMinutes, id,
); err != nil {
tx.Rollback()
return fmt.Errorf("update basic %s: %w", id, err)
}
bCount++
}
if err := tx.Commit(); err != nil {
return fmt.Errorf("commit: %w", err)
}
log.Printf("applyImdbUpdates: %d ratings, %d basics updated", rCount, bCount)
return nil
}
// getImdbIDsWithoutRating returns all imdb_id values where average_rating IS NULL.
func (a *App) getImdbIDsWithoutRating() (map[string]bool, error) {
rows, err := a.DB.Query(`SELECT imdb_id FROM imdb WHERE average_rating IS NULL`)
if err != nil {
return nil, err
}
defer rows.Close()
ids := make(map[string]bool)
for rows.Next() {
var id string
if err := rows.Scan(&id); err != nil {
return nil, err
}
ids[id] = true
}
return ids, rows.Err()
}
// fetchAndUpdateImdbData is the main entry point.
// Checks if any imdb entry lacks average_rating. If so, downloads datasets,
// parses them, and updates matching rows with ratings and basic metadata.
func (a *App) fetchAndUpdateImdbData() error {
var count int
if err := a.DB.QueryRow(`SELECT COUNT(*) FROM imdb WHERE average_rating IS NULL`).Scan(&count); err != nil {
return fmt.Errorf("count: %w", err)
}
if count == 0 {
log.Println("fetchAndUpdateImdbData: all entries have ratings, skipping")
return nil
}
log.Printf("fetchAndUpdateImdbData: %d entries without rating", count)
// Download / refresh datasets
if err := a.downloadImdbDatasets(); err != nil {
return err
}
// Gather all imdb_ids needing updates
ids, err := a.getImdbIDsWithoutRating()
if err != nil {
return err
}
if len(ids) == 0 {
log.Println("fetchAndUpdateImdbData: no entries need updating")
return nil
}
log.Printf("fetchAndUpdateImdbData: %d entries to update", len(ids))
// Parse datasets
ratings, err := a.parseTitleRatings(ids)
if err != nil {
return fmt.Errorf("parseTitleRatings: %w", err)
}
log.Printf("fetchAndUpdateImdbData: found ratings for %d entries", len(ratings))
basics, err := a.parseTitleBasics(ids)
if err != nil {
return fmt.Errorf("parseTitleBasics: %w", err)
}
log.Printf("fetchAndUpdateImdbData: found basics for %d entries", len(basics))
// Write to DB
if err := a.applyImdbUpdates(ratings, basics); err != nil {
return err
}
return nil
}
|