]> git.lizzy.rs Git - go-anidb.git/commitdiff
anidb: Add logging to titlecache, be more careful with caching
authorDiogo Franco (Kovensky) <diogomfranco@gmail.com>
Tue, 16 Jul 2013 16:20:22 +0000 (13:20 -0300)
committerDiogo Franco (Kovensky) <diogomfranco@gmail.com>
Tue, 16 Jul 2013 16:20:22 +0000 (13:20 -0300)
Only cache the new anime-titles.dat.gz (thus truncating an older one) if
we can download it entirely to memory (it's little less than 600KB).

titlecache.go

index d32175e7e29e98b16eb04c498e426205436d8a7b..5901fd4e104d52c3af7179c2256ffc010bda04cf 100644 (file)
@@ -1,8 +1,10 @@
 package anidb
 
 import (
+       "bytes"
        "github.com/Kovensky/go-anidb/titles"
        "io"
+       "log"
        "net/http"
        "time"
 )
@@ -47,21 +49,36 @@ func UpdateTitles() error {
 
        c := &http.Client{Transport: &http.Transport{DisableCompression: true}}
 
+       log.Printf("HTTP>>> %s", titles.DataDumpURL)
+
        resp, err := c.Get(titles.DataDumpURL)
        if err != nil {
+               log.Printf("HTTP<<< %s", resp.Status)
                return err
        }
        defer resp.Body.Close()
 
+       buf := bytes.Buffer{}
+       log.Printf("HTTP--- %s", resp.Status)
+
+       _, err = io.Copy(&buf, resp.Body)
+       if err != nil {
+               log.Printf("HTTP--- %v", err)
+               return err
+       }
+
        fh, err := cache.Create("anime-titles.dat.gz")
        if err != nil {
                return err
        }
 
-       _, err = io.Copy(fh, resp.Body)
+       _, err = io.Copy(fh, &buf)
        if err != nil {
                return err
        }
 
+       defer func() {
+               log.Printf("HTTP<<< Titles version %s", titlesDB.UpdateTime)
+       }()
        return RefreshTitles()
 }