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
|
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"strings"
"time"
)
const (
wikidataSparql = "https://query.wikidata.org/sparql"
wikiBatchSize = 30
wikiDelay = 2 * time.Second // ~15 req/min, safe under 20 req/min limit
wikiMaxRetries = 3
wikiRetryBackoff = 15 * time.Second
)
var wikiClient = &http.Client{Timeout: 120 * time.Second}
// wikiAcc accumulates per-ID results from a SPARQL batch.
type wikiAcc struct {
wikiArticle string
title string
}
// type alias for SPARQL JSON response
type sparqlResponse struct {
Results struct {
BindingList []map[string]jsonNode `json:"bindings"`
} `json:"results"`
}
type jsonNode struct {
Type string `json:"type"`
Value string `json:"value"`
Lang string `json:"xml:lang,omitempty"`
}
// getMissingWikiArticles returns imdb_ids where wiki_article IS NULL.
func (a *App) getMissingWikiArticles() ([]string, error) {
rows, err := a.DB.Query(`SELECT imdb_id FROM imdb WHERE wiki_article IS NULL AND has_no_wiki_article = 0 AND imdb_id LIKE 'tt%'`)
if err != nil {
return nil, fmt.Errorf("query missing wiki articles: %w", err)
}
defer rows.Close()
var ids []string
for rows.Next() {
var id string
if err := rows.Scan(&id); err != nil {
return nil, fmt.Errorf("scan imdb_id: %w", err)
}
ids = append(ids, id)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("rows iteration: %w", err)
}
return ids, nil
}
// fetchWikiArticles queries Wikidata SPARQL in batches and updates wiki_article in the DB.
func (a *App) fetchWikiArticles() error {
ids, err := a.getMissingWikiArticles()
if err != nil {
return err
}
if len(ids) == 0 {
log.Println("fetchWikiArticles: all entries have wiki_article, skipping")
return nil
}
log.Printf("fetchWikiArticles: %d entries missing wiki_article", len(ids))
tx, err := a.DB.Begin()
if err != nil {
return fmt.Errorf("begin tx: %w", err)
}
wikiStmt, err := tx.Prepare(`UPDATE imdb SET wiki_article = ? WHERE imdb_id = ?`)
if err != nil {
tx.Rollback()
return fmt.Errorf("prepare wiki update: %w", err)
}
defer wikiStmt.Close()
noWikiStmt, err := tx.Prepare(`UPDATE imdb SET has_no_wiki_article = 1 WHERE imdb_id = ?`)
if err != nil {
tx.Rollback()
return fmt.Errorf("prepare no_wiki update: %w", err)
}
defer noWikiStmt.Close()
updated := 0
noWiki := 0
for i := 0; i < len(ids); i += wikiBatchSize {
chunk := ids[i:min(i+wikiBatchSize, len(ids))]
results, err := a.queryWikidataBatch(chunk)
if err != nil {
log.Printf("wikidata batch error at offset %d: %v", i, err)
// mark all in skipped batch as no-wiki
for _, id := range chunk {
if _, err := noWikiStmt.Exec(id); err != nil {
tx.Rollback()
return fmt.Errorf("mark no_wiki for %s: %w", id, err)
}
noWiki++
}
continue
}
for _, id := range chunk {
acc, found := results[id]
if found && acc.title != "" {
if _, err := wikiStmt.Exec(acc.title, id); err != nil {
tx.Rollback()
return fmt.Errorf("update wiki_article for %s: %w", id, err)
}
updated++
} else {
if _, err := noWikiStmt.Exec(id); err != nil {
tx.Rollback()
return fmt.Errorf("mark no_wiki for %s: %w", id, err)
}
noWiki++
}
}
done := i + len(chunk)
log.Printf("fetchWikiArticles: [%d/%d]", done, len(ids))
// rate limit between batches
if i+wikiBatchSize < len(ids) {
time.Sleep(wikiDelay)
}
}
if err := tx.Commit(); err != nil {
return fmt.Errorf("commit wiki articles: %w", err)
}
log.Printf("fetchWikiArticles: %d wiki articles updated, %d marked as no wiki", updated, noWiki)
return nil
}
// queryWikidataBatch sends a SPARQL query for the given IDs and returns a map of id -> wikiAcc.
func (a *App) queryWikidataBatch(ids []string) (map[string]wikiAcc, error) {
sparql := buildSparql(ids)
time.Sleep(wikiDelay)
endpoint := wikidataSparql + "?" + url.Values{
"query": {sparql},
"format": {"json"},
}.Encode()
raw, err := doGETWithRetry(endpoint, a.Config.UserAgent)
if err != nil {
return nil, fmt.Errorf("SPARQL request: %w", err)
}
var data sparqlResponse
if err := json.Unmarshal(raw, &data); err != nil {
return nil, fmt.Errorf("SPARQL JSON parse: %w", err)
}
results := make(map[string]wikiAcc)
for _, b := range data.Results.BindingList {
imdb := b["imdbVal"].Value
if !strings.HasPrefix(imdb, "tt") {
continue
}
if n, ok := b["article"]; ok && n.Value != "" {
// Extract title from URL: strip "https://en.wikipedia.org/wiki/"
title := strings.TrimPrefix(n.Value, "https://en.wikipedia.org/wiki/")
results[imdb] = wikiAcc{
wikiArticle: n.Value,
title: title,
}
}
}
return results, nil
}
// buildSparql creates a SPARQL query that resolves IMDb IDs to English Wikipedia article URLs.
func buildSparql(ids []string) string {
var vals strings.Builder
for _, id := range ids {
vals.WriteString(fmt.Sprintf(" \"%s\"\n", id))
}
return fmt.Sprintf(`PREFIX schema: <http://schema.org/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
SELECT ?imdbVal ?article
WHERE {
VALUES ?imdbVal {
%s
}
?item wdt:P345 ?imdbVal .
OPTIONAL {
?article schema:about ?item ;
schema:isPartOf <https://en.wikipedia.org/> .
}
}
ORDER BY ?imdbVal`, vals.String())
}
// doGET performs a GET request with the configured User-Agent.
func doGET(uri, userAgent string) ([]byte, error) {
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", userAgent)
resp, err := wikiClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(io.LimitReader(resp.Body, 2048))
return nil, fmt.Errorf("HTTP %d: %s", resp.StatusCode, body)
}
return io.ReadAll(resp.Body)
}
// doGETWithRetry retries on 5xx errors.
func doGETWithRetry(uri, userAgent string) ([]byte, error) {
var lastErr error
for attempt := 0; attempt < wikiMaxRetries; attempt++ {
if attempt > 0 {
backoff := wikiRetryBackoff * time.Duration(1<<(attempt-1))
log.Printf(" retry %d/%d after %v", attempt+1, wikiMaxRetries, backoff)
time.Sleep(backoff)
}
raw, err := doGET(uri, userAgent)
if err == nil {
return raw, nil
}
lastErr = err
if !strings.Contains(err.Error(), "HTTP 5") {
break
}
}
return nil, lastErr
}
// min returns the smaller of a and b.
func min(a, b int) int {
if a < b {
return a
}
return b
}
|