diff options
Diffstat (limited to 'src/wikidata.go')
| -rw-r--r-- | src/wikidata.go | 41 |
1 files changed, 40 insertions, 1 deletions
diff --git a/src/wikidata.go b/src/wikidata.go index e6c2e0e..59c8188 100644 --- a/src/wikidata.go +++ b/src/wikidata.go @@ -126,7 +126,7 @@ func (a *App) getExistingWikiArticles() ([]existingWikiArticle, error) { WHERE wiki_article IS NOT NULL AND wiki_status_code != 404 AND (synopsis IS NULL OR description IS NULL OR year IS NULL - OR poster_url IS NULL OR license IS NULL OR license_url IS NULL OR num_accolades IS NULL) + OR poster_url IS NULL OR license IS NULL OR license_url IS NULL OR num_accolades IS NULL OR has_people = 0) `) if err != nil { return nil, fmt.Errorf("query existing wiki articles: %w", err) @@ -261,10 +261,49 @@ func (a *App) wikiDataConsumer(artCh <-chan wikiArticleFetch, done chan<- struct entry.License, entry.LicenseURL, entry.NumAccolades, art.imdbID) updated++ } + + // Insert people (actors, directors, screenwriters) + if statusCode == 200 && len(entry.People) > 0 { + if err := a.insertWikiPeople(art.imdbID, entry.People); err != nil { + log.Printf("insert people error %s: %v", art.imdbID, err) + } + a.DB.Exec(`UPDATE imdb SET has_people = 1 WHERE imdb_id = ?`, art.imdbID) + } } log.Printf("fetchWikiArticlesData: %d updated, %d skipped (non-200)", updated, skipped) } + +// insertWikiPeople upserts people into people/who tables. +func (a *App) insertWikiPeople(imdbID string, people []wikiPerson) error { + // Get the DB row id for this imdb_id + var dbID int + if err := a.DB.Get(&dbID, `SELECT id FROM imdb WHERE imdb_id = ?`, imdbID); err != nil { + return fmt.Errorf("lookup imdb id for %s: %w", imdbID, err) + } + + for _, p := range people { + // Upsert person + var personID int + err := a.DB.Get(&personID, `SELECT id FROM people WHERE name = ?`, p.Name) + if err != nil { + // Insert new person + result, err := a.DB.Exec(`INSERT INTO people (name) VALUES (?)`, p.Name) + if err != nil { + continue // skip on conflict + } + if id, err := result.LastInsertId(); err == nil { + personID = int(id) + } + } + + // Insert who relationship (unique on imdb_id, people_id, profession_id) + a.DB.Exec(`INSERT IGNORE INTO who (imdb_id, people_id, profession_id) VALUES (?, ?, ?)`, + dbID, personID, p.Profession) + } + + return nil +} // fetchWikiArticlesData fetches wiki article data from the custom server for all // entries that have a wiki_article but need data extraction. Callable independently. func (a *App) fetchWikiArticlesData() error { |
