5 "github.com/Kovensky/go-anidb/titles"
12 var titlesFileData []byte
13 var titlesFileDataLock sync.Mutex
14 var titlesDB = &titles.TitlesDatabase{}
16 // Loads the anime-titles database from the given io.Reader.
18 // Caches the io.Reader's contents on memory, which gets saved
20 func LoadTitles(src io.Reader) error {
22 _, err := io.Copy(&buf, src)
23 if err != nil && err != io.EOF {
27 titlesFileDataLock.Lock()
28 defer titlesFileDataLock.Unlock()
30 titlesFileData = buf.Bytes()
32 titlesDB.LoadDB(bytes.NewReader(titlesFileData))
37 // Saves the currently cached anime-titles database to the given io.Writer.
38 func DumpTitles(dst io.Writer) (int64, error) {
39 return io.Copy(dst, bytes.NewReader(titlesFileData))
42 // Returns true if the titles database is up-to-date (newer than 24 hours).
43 func TitlesUpToDate() (ok bool) {
44 return time.Now().Sub(titlesDB.UpdateTime) < 24*time.Hour
47 // Downloads a new anime-titles database if the database is outdated.
49 // Caches the contents on memory, which gets saved by DumpCaches.
50 func UpdateTitles() error {
51 // too new, no need to update
56 resp, err := http.Get(titles.DataDumpURL)
60 defer resp.Body.Close()
62 return LoadTitles(resp.Body)