5 "github.com/EliasFleckenstein03/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 lock, err := Cache.Lock("anime-titles.dat.gz"); err != nil {
22 fh, err := Cache.Open("anime-titles.dat.gz")
32 // Returns true if the titles database is up-to-date (newer than 24 hours).
33 func TitlesUpToDate() (ok bool) {
34 return time.Now().Sub(titlesDB.UpdateTime) < 24*time.Hour
37 // Returns the number of anime in the titles database
38 func AnimeCount() int {
39 return len(titlesDB.AnimeMap)
42 // Downloads a new anime-titles database if the database is outdated.
44 // Saves the database as anime-titles.dat.gz in the cache dir.
45 func (adb *AniDB) UpdateTitles() error {
46 // needs the AniDB for the Logger
48 // too new, no need to update
53 switch lock, err := Cache.Lock("anime-titles.dat.gz"); {
54 case os.IsNotExist(err):
55 // we're creating it now
62 c := &http.Client{Transport: &http.Transport{DisableCompression: true}}
64 adb.Logger.Printf("HTTP>>> %s", titles.DataDumpURL)
66 resp, err := c.Get(titles.DataDumpURL)
68 adb.Logger.Printf("HTTP<<< %s", resp.Status)
71 defer resp.Body.Close()
74 adb.Logger.Printf("HTTP--- %s", resp.Status)
76 _, err = io.Copy(&buf, resp.Body)
78 adb.Logger.Printf("HTTP--- %v", err)
82 fh, err := Cache.Create("anime-titles.dat.gz")
87 _, err = io.Copy(fh, &buf)
93 adb.Logger.Printf("HTTP<<< Titles version %s", titlesDB.UpdateTime)
95 return RefreshTitles()