Download episode thumbs

This commit is contained in:
Grégoire Delattre 2019-06-27 14:31:33 +02:00
parent 502bf88feb
commit 88fc8be462
3 changed files with 34 additions and 13 deletions

View File

@ -15,8 +15,7 @@ import (
// Episode represents an episode // Episode represents an episode
type Episode struct { type Episode struct {
client *papi.Client show *Show
pShow *papi.Show
*polochon.ShowEpisode *polochon.ShowEpisode
} }
@ -33,11 +32,11 @@ func (e *Episode) MarshalJSON() ([]byte, error) {
var container string var container string
// If the episode is present, fill the downloadURL // If the episode is present, fill the downloadURL
if e.pShow != nil { if e.show.pShow != nil {
pEpisode := e.pShow.GetEpisode(e.Season, e.Episode) pEpisode := e.show.pShow.GetEpisode(e.Season, e.Episode)
if pEpisode != nil { if pEpisode != nil {
// Get the DownloadURL // Get the DownloadURL
downloadURL, _ = e.client.DownloadURL( downloadURL, _ = e.show.client.DownloadURL(
&papi.Episode{ &papi.Episode{
ShowImdbID: e.ShowImdbID, ShowImdbID: e.ShowImdbID,
Episode: e.Episode, Episode: e.Episode,
@ -52,7 +51,7 @@ func (e *Episode) MarshalJSON() ([]byte, error) {
// Append the Subtitles // Append the Subtitles
for _, l := range pEpisode.Subtitles { for _, l := range pEpisode.Subtitles {
subtitleURL, _ := e.client.SubtitleURL(pEpisode, l) subtitleURL, _ := e.show.client.SubtitleURL(pEpisode, l)
subs = append(subs, subtitles.Subtitle{ subs = append(subs, subtitles.Subtitle{
Language: l, Language: l,
URL: subtitleURL, URL: subtitleURL,
@ -72,6 +71,7 @@ func (e *Episode) MarshalJSON() ([]byte, error) {
AudioCodec string `json:"audio_codec"` AudioCodec string `json:"audio_codec"`
VideoCodec string `json:"video_codec"` VideoCodec string `json:"video_codec"`
Container string `json:"container"` Container string `json:"container"`
Thumb string `json:"thumb"`
}{ }{
alias: (*alias)(e), alias: (*alias)(e),
PolochonURL: downloadURL, PolochonURL: downloadURL,
@ -81,18 +81,22 @@ func (e *Episode) MarshalJSON() ([]byte, error) {
AudioCodec: audioCodec, AudioCodec: audioCodec,
VideoCodec: videoCodec, VideoCodec: videoCodec,
Container: container, Container: container,
Thumb: e.getThumbURL(),
} }
return json.Marshal(episodeToMarshal) return json.Marshal(episodeToMarshal)
} }
func (e *Episode) getThumbURL() string {
return e.show.GetImageURL(fmt.Sprintf("%d-%d", e.Season, e.Episode))
}
// NewEpisode returns an Episode // NewEpisode returns an Episode
func NewEpisode(client *papi.Client, pShow *papi.Show, imdbID string, season, episode int) *Episode { func NewEpisode(show *Show, season, episode int) *Episode {
return &Episode{ return &Episode{
client: client, show: show,
pShow: pShow,
ShowEpisode: &polochon.ShowEpisode{ ShowEpisode: &polochon.ShowEpisode{
ShowImdbID: imdbID, ShowImdbID: show.ImdbID,
Season: season, Season: season,
Episode: episode, Episode: episode,
}, },

View File

@ -333,7 +333,13 @@ func RefreshEpisodeHandler(env *web.Env, w http.ResponseWriter, r *http.Request)
env.Log.Warnf("Error getting show %q", err) env.Log.Warnf("Error getting show %q", err)
} }
e := NewEpisode(client, pShow, id, season, episode) s := &Show{
Show: &polochon.Show{ImdbID: id},
pShow: pShow,
client: client,
}
e := NewEpisode(s, season, episode)
// Refresh the episode // Refresh the episode
err = e.Refresh(env, env.Config.Show.Detailers) err = e.Refresh(env, env.Config.Show.Detailers)
if err != nil { if err != nil {

View File

@ -48,8 +48,7 @@ func (s *Show) MarshalJSON() ([]byte, error) {
for _, e := range s.Show.Episodes { for _, e := range s.Show.Episodes {
showToMarshal.Episodes = append(showToMarshal.Episodes, Episode{ showToMarshal.Episodes = append(showToMarshal.Episodes, Episode{
ShowEpisode: e, ShowEpisode: e,
client: s.client, show: s,
pShow: s.pShow,
}) })
} }
return json.Marshal(showToMarshal) return json.Marshal(showToMarshal)
@ -174,6 +173,18 @@ func (s *Show) downloadImages(env *web.Env) {
if err != nil { if err != nil {
env.Log.Errorf("failed to dowload poster: %s", err) env.Log.Errorf("failed to dowload poster: %s", err)
} }
// Download episode thumbs
for _, e := range s.Episodes {
if e.Thumb == "" {
continue
}
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)
}
}
} }
// imgURL returns the default image url // imgURL returns the default image url