canape/backend/models/torrent_video.go
Grégoire Delattre 7be65b6a9a Get the video images from the models
Return the video details embedded in the torrents

This requires the eventers to have the app env
2020-04-13 17:38:47 +02:00

69 lines
1.6 KiB
Go

package models
import (
"github.com/jmoiron/sqlx"
"github.com/odwrtw/papi"
polochon "github.com/odwrtw/polochon/lib"
"github.com/sirupsen/logrus"
)
// TorrentVideo reprensents a torrent embeding the video inforamtions
type TorrentVideo struct {
*papi.Torrent
Img string `json:"img"`
Video polochon.Video `json:"video,omitempty"`
}
// NewTorrentVideo returns a new TorrentVideo
func NewTorrentVideo(t *papi.Torrent) *TorrentVideo {
torrent := &polochon.Torrent{
ImdbID: t.ImdbID,
Type: polochon.VideoType(t.Type),
Quality: polochon.Quality(t.Quality),
Season: t.Season,
Episode: t.Episode,
}
return &TorrentVideo{
Torrent: t,
Video: torrent.Video(),
}
}
// Update updates the Torrent video with the database details
func (t *TorrentVideo) Update(detailer polochon.Detailer, db *sqlx.DB, log *logrus.Entry) {
if t.Video == nil {
return
}
// TODO: refresh the video in db if not found
err := detailer.GetDetails(t.Video, log)
if err != nil {
log.WithField("function", "TorrentVideo.Update").Errorf(err.Error())
}
switch v := t.Video.(type) {
case *polochon.ShowEpisode:
if v.Show != nil {
if err := GetShow(db, v.Show); err != nil {
return
}
t.Img = v.Show.Poster
v.Show = nil
}
case *polochon.Movie:
t.Img = v.Thumb
}
}
// NewTorrentVideos returns a new slice of TorrentVideo from papi torrents
func NewTorrentVideos(detailer polochon.Detailer, db *sqlx.DB, log *logrus.Entry, torrents []*papi.Torrent) []*TorrentVideo {
tv := make([]*TorrentVideo, len(torrents))
for i := range torrents {
tv[i] = NewTorrentVideo(torrents[i])
tv[i].Update(detailer, db, log)
}
return tv
}