summaryrefslogtreecommitdiff
path: root/src/imdbdata.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/imdbdata.go')
-rw-r--r--src/imdbdata.go32
1 files changed, 18 insertions, 14 deletions
diff --git a/src/imdbdata.go b/src/imdbdata.go
index 8313188..7a2890d 100644
--- a/src/imdbdata.go
+++ b/src/imdbdata.go
@@ -5,11 +5,11 @@ import (
"compress/gzip"
"fmt"
"io"
- "log"
"net/http"
"os"
"path/filepath"
"strconv"
+ "time"
"strings"
)
@@ -26,16 +26,19 @@ func dataPath(name string) string {
// downloadFile fetches url and writes to dst, overwriting if present.
func downloadFile(dst, url string) error {
return nil
+
resp, err := http.Get(url)
if err != nil {
- return fmt.Errorf("http get: %w", err)
+ return fmt.Errorf("request %s: %w", url, err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
- return fmt.Errorf("http %d from %s", resp.StatusCode, url)
+ body, _ := io.ReadAll(io.LimitReader(resp.Body, 2048))
+ return fmt.Errorf("HTTP %d: %s", resp.StatusCode, body)
}
+ start := time.Now()
f, err := os.Create(dst)
if err != nil {
return fmt.Errorf("create %s: %w", dst, err)
@@ -46,7 +49,8 @@ func downloadFile(dst, url string) error {
if err != nil {
return fmt.Errorf("write %s: %w", dst, err)
}
- log.Printf("downloaded %s", url)
+ logHTTPRequest("GET", url, resp.StatusCode, time.Since(start).Seconds())
+ logInfo("downloaded %s", url)
return nil
}
@@ -97,7 +101,7 @@ func (a *App) downloadImdbDatasets() error {
for _, p := range pairs {
// skip if TSV already exists (download may be stubbed out)
if _, err := os.Stat(p.tsv); err == nil {
- log.Printf("reusing existing %s", p.tsv)
+ logInfo("reusing existing %s", p.tsv)
continue
}
@@ -110,7 +114,7 @@ func (a *App) downloadImdbDatasets() error {
if err := os.Remove(p.gz); err != nil {
return fmt.Errorf("remove %s: %w", p.gz, err)
}
- log.Printf("extracted %s", p.tsv)
+ logInfo("extracted %s", p.tsv)
}
return nil
}
@@ -327,7 +331,7 @@ func (a *App) applyImdbUpdates(ratings map[string]ratingEntry, basics map[string
return fmt.Errorf("commit: %w", err)
}
- log.Printf("applyImdbUpdates: %d ratings, %d basics, %d genres updated", rCount, bCount, gCount)
+ logInfo("applyImdbUpdates: %d ratings, %d basics, %d genres updated", rCount, bCount, gCount)
return nil
}
@@ -366,10 +370,10 @@ func (a *App) fetchAndUpdateImdbData() error {
return fmt.Errorf("count genres: %w", err)
}
if missingRatings == 0 && missingGenres == 0 {
- log.Println("fetchAndUpdateImdbData: all entries complete, skipping")
+ logInfo("fetchAndUpdateImdbData: all entries complete, skipping")
return nil
}
- log.Printf("fetchAndUpdateImdbData: %d missing ratings, %d missing genres", missingRatings, missingGenres)
+ logInfo("fetchAndUpdateImdbData: %d missing ratings, %d missing genres", missingRatings, missingGenres)
// Download / refresh datasets
if err := a.downloadImdbDatasets(); err != nil {
return err
@@ -381,30 +385,30 @@ func (a *App) fetchAndUpdateImdbData() error {
return err
}
if len(ratingIDs) == 0 {
- log.Println("fetchAndUpdateImdbData: no entries need updating")
+ logInfo("fetchAndUpdateImdbData: no entries need updating")
return nil
}
- log.Printf("fetchAndUpdateImdbData: %d entries need rating update", len(ratingIDs))
+ logInfo("fetchAndUpdateImdbData: %d entries need rating update", len(ratingIDs))
// Gather all imdb_ids for basics update
allIDs, err := a.getAllImdbIDs()
if err != nil {
return err
}
- log.Printf("fetchAndUpdateImdbData: %d entries total for basics update", len(allIDs))
+ logInfo("fetchAndUpdateImdbData: %d entries total for basics update", len(allIDs))
// Parse datasets
ratings, err := a.parseTitleRatings(ratingIDs)
if err != nil {
return fmt.Errorf("parseTitleRatings: %w", err)
}
- log.Printf("fetchAndUpdateImdbData: found ratings for %d entries", len(ratings))
+ logInfo("fetchAndUpdateImdbData: found ratings for %d entries", len(ratings))
basics, err := a.parseTitleBasics(allIDs)
if err != nil {
return fmt.Errorf("parseTitleBasics: %w", err)
}
- log.Printf("fetchAndUpdateImdbData: found basics for %d entries", len(basics))
+ logInfo("fetchAndUpdateImdbData: found basics for %d entries", len(basics))
// Write to DB
if err := a.applyImdbUpdates(ratings, basics); err != nil {