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 }