5 "github.com/Kovensky/go-anidb/titles"
12 var titlesDB = &titles.TitlesDatabase{}
14 // Loads the database from anime-titles.dat.gz in the cache dir.
15 func RefreshTitles() error {
16 if flock := lockFile(cachePath("anime-titles.dat.gz")); flock != nil {
21 fh, err := cache.Open("anime-titles.dat.gz")
31 // Returns true if the titles database is up-to-date (newer than 24 hours).
32 func TitlesUpToDate() (ok bool) {
33 return time.Now().Sub(titlesDB.UpdateTime) < 24*time.Hour
36 // Downloads a new anime-titles database if the database is outdated.
38 // Saves the database as anime-titles.dat.gz in the cache dir.
39 func UpdateTitles() error {
40 // too new, no need to update
45 if flock := lockFile(cachePath("anime-titles.dat.gz")); flock != nil {
50 c := &http.Client{Transport: &http.Transport{DisableCompression: true}}
52 log.Printf("HTTP>>> %s", titles.DataDumpURL)
54 resp, err := c.Get(titles.DataDumpURL)
56 log.Printf("HTTP<<< %s", resp.Status)
59 defer resp.Body.Close()
62 log.Printf("HTTP--- %s", resp.Status)
64 _, err = io.Copy(&buf, resp.Body)
66 log.Printf("HTTP--- %v", err)
70 fh, err := cache.Create("anime-titles.dat.gz")
75 _, err = io.Copy(fh, &buf)
81 log.Printf("HTTP<<< Titles version %s", titlesDB.UpdateTime)
83 return RefreshTitles()