Compare commits
2 Commits
98eb833dc0
...
54bbbf84f4
Author | SHA1 | Date | |
---|---|---|---|
54bbbf84f4 | |||
e5b4639cab |
@ -4,6 +4,7 @@ import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strconv"
|
||||
@ -34,6 +35,10 @@ func Refresh(env *web.Env) error {
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("got HTTP error %s", resp.Status)
|
||||
}
|
||||
|
||||
// Read all the file (~5MB) in memory
|
||||
// We do that because the ~1 000 000 upserts take too long, and the IMDB
|
||||
// server will cut our connection after ~2h
|
||||
|
@ -160,18 +160,41 @@ func (s *Show) GetImageURL(imgType string) string {
|
||||
|
||||
// downloadImages will download the show images
|
||||
func (s *Show) downloadImages(env *web.Env) {
|
||||
// Download the banner
|
||||
err := web.Download(s.Show.Banner, s.imgFile("banner"), false)
|
||||
if err != nil {
|
||||
env.Log.Errorf("failed to dowload banner: %s", err)
|
||||
// Create the directory for the show if it doesn't exist
|
||||
if _, err := os.Stat(s.imgDirectory()); err != nil {
|
||||
if err = os.Mkdir(s.imgDirectory(), os.ModePerm); err != nil {
|
||||
env.Log.Warnf("couldn't create show directory %s: %s", s.imgDirectory(), err)
|
||||
return
|
||||
}
|
||||
err = web.Download(s.Show.Fanart, s.imgFile("fanart"), false)
|
||||
if err != nil {
|
||||
env.Log.Errorf("failed to dowload fanart: %s", err)
|
||||
}
|
||||
err = web.Download(s.Show.Poster, s.imgFile("poster"), true)
|
||||
if err != nil {
|
||||
env.Log.Errorf("failed to dowload poster: %s", err)
|
||||
// Download the show images
|
||||
for _, img := range []struct {
|
||||
url string
|
||||
urlType string
|
||||
scale bool
|
||||
}{
|
||||
{
|
||||
url: s.Show.Banner,
|
||||
urlType: "banner",
|
||||
scale: false,
|
||||
},
|
||||
{
|
||||
url: s.Show.Fanart,
|
||||
urlType: "fanart",
|
||||
scale: false,
|
||||
},
|
||||
{
|
||||
url: s.Show.Poster,
|
||||
urlType: "poster",
|
||||
scale: true,
|
||||
},
|
||||
} {
|
||||
if img.url == "" {
|
||||
continue
|
||||
}
|
||||
if err := web.Download(img.url, s.imgFile(img.urlType), img.scale); err != nil {
|
||||
env.Log.Errorf("failed to dowload %s: %s", img.urlType, err)
|
||||
}
|
||||
}
|
||||
|
||||
// Download episode thumbs
|
||||
@ -180,16 +203,21 @@ func (s *Show) downloadImages(env *web.Env) {
|
||||
continue
|
||||
}
|
||||
|
||||
err = web.Download(e.Thumb, s.imgFile(fmt.Sprintf("%d-%d", e.Season, e.Episode)), false)
|
||||
err := web.Download(e.Thumb, s.imgFile(fmt.Sprintf("%d-%d", e.Season, e.Episode)), false)
|
||||
if err != nil {
|
||||
env.Log.Errorf("failed to dowload the thumb for season %d episode %d: %s", e.Season, e.Episode, err)
|
||||
env.Log.Errorf("failed to dowload the thumb for season %d episode %d ( %s ) : %s", e.Season, e.Episode, e.Thumb, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// imgURL returns the default image url
|
||||
func (s *Show) imgURL(imgType string) string {
|
||||
return fmt.Sprintf("shows/%s-%s.jpg", s.ImdbID, imgType)
|
||||
return fmt.Sprintf("shows/%s/%s.jpg", s.ImdbID, imgType)
|
||||
}
|
||||
|
||||
// imgDirectory returns the directory containing all the show images
|
||||
func (s *Show) imgDirectory() string {
|
||||
return filepath.Join(s.publicDir, "img", fmt.Sprintf("shows/%s", s.ImdbID))
|
||||
}
|
||||
|
||||
// imgFile returns the image location on disk
|
||||
|
@ -1,6 +1,7 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
"image/jpeg"
|
||||
"net/http"
|
||||
@ -26,6 +27,10 @@ var Download = func(srcURL, dest string, scale bool) error {
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("got HTTP error %s", resp.Status)
|
||||
}
|
||||
|
||||
image, _, err := image.Decode(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
|
Loading…
x
Reference in New Issue
Block a user