82 lines
1.8 KiB
Go
82 lines
1.8 KiB
Go
package models
|
|
|
|
import (
|
|
"database/sql"
|
|
"errors"
|
|
|
|
polochon "github.com/odwrtw/polochon/lib"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// GetTorrents implements the polochon Torrenter interface
|
|
func (b *Backend) GetTorrents(media interface{}, log *logrus.Entry) error {
|
|
switch t := media.(type) {
|
|
case *polochon.ShowEpisode:
|
|
return b.GetEpisodeTorrents(t, log)
|
|
case *polochon.Movie:
|
|
return b.GetMovieTorrents(t, log)
|
|
default:
|
|
return errors.New("invalid type")
|
|
}
|
|
}
|
|
|
|
// SearchTorrents implements the polochon Torrenter interface
|
|
func (b *Backend) SearchTorrents(s string) ([]*polochon.Torrent, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
// GetMovieTorrents fetch Torrents for movies
|
|
func (b *Backend) GetMovieTorrents(pmovie *polochon.Movie, log *logrus.Entry) error {
|
|
movieTorrents, err := GetMovieTorrents(b.Database, pmovie.ImdbID)
|
|
switch err {
|
|
case nil:
|
|
log.Debug("torrents found in backend")
|
|
case sql.ErrNoRows:
|
|
log.Debug("torrent not found in backend")
|
|
default:
|
|
// Unexpected error
|
|
return err
|
|
}
|
|
log.Debugf(
|
|
"got %d torrents for movie %s from backend",
|
|
len(movieTorrents),
|
|
pmovie.ImdbID,
|
|
)
|
|
|
|
// Add the torrents to the movie
|
|
pmovie.Torrents = movieTorrents
|
|
|
|
return nil
|
|
}
|
|
|
|
// GetEpisodeTorrents fetch Torrents for episodes
|
|
func (b *Backend) GetEpisodeTorrents(pepisode *polochon.ShowEpisode, log *logrus.Entry) error {
|
|
episodeTorrents, err := GetEpisodeTorrents(
|
|
b.Database,
|
|
pepisode.ShowImdbID,
|
|
pepisode.Season,
|
|
pepisode.Episode,
|
|
)
|
|
switch err {
|
|
case nil:
|
|
log.Debug("torrents found in backend")
|
|
case sql.ErrNoRows:
|
|
log.Debug("torrent not found in backend")
|
|
default:
|
|
// Unexpected error
|
|
return err
|
|
}
|
|
log.Debugf(
|
|
"got %d torrents for episode S%02dE%02d %s from backend",
|
|
len(episodeTorrents),
|
|
pepisode.Season,
|
|
pepisode.Episode,
|
|
pepisode.ShowImdbID,
|
|
)
|
|
|
|
// Add the torrents to the episode
|
|
pepisode.Torrents = episodeTorrents
|
|
|
|
return nil
|
|
}
|