mirror of
https://github.com/simon-ding/polaris.git
synced 2026-04-21 19:27:30 +08:00
basic find download ability
This commit is contained in:
@@ -4,8 +4,8 @@ import (
|
||||
"fmt"
|
||||
"polaris/db"
|
||||
"polaris/log"
|
||||
"polaris/pkg/downloader"
|
||||
"polaris/pkg/torznab"
|
||||
"polaris/pkg/transmission"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pkg/errors"
|
||||
@@ -49,9 +49,9 @@ func (s *Server) AddTorznabInfo(c *gin.Context) (interface{}, error) {
|
||||
}
|
||||
|
||||
type searchAndDownloadIn struct {
|
||||
Title string `json:"title"`
|
||||
Season int `json:"season"`
|
||||
Episode int `json:"episode"`
|
||||
Title string `json:"title"`
|
||||
Season int `json:"season"`
|
||||
Episode int `json:"episode"`
|
||||
}
|
||||
|
||||
func (s *Server) SearchAndDownload(c *gin.Context) (interface{}, error) {
|
||||
@@ -59,13 +59,43 @@ func (s *Server) SearchAndDownload(c *gin.Context) (interface{}, error) {
|
||||
if err := c.ShouldBindJSON(&in); err != nil {
|
||||
return nil, errors.Wrap(err, "bind json")
|
||||
}
|
||||
tr := s.db.GetTransmission()
|
||||
trc, err := transmission.NewClient(tr.URL, tr.User, tr.Password)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "connect transmission")
|
||||
}
|
||||
|
||||
res := s.searchTvWithTorznab(in.Title, in.Season, in.Episode)
|
||||
r1 := res[0]
|
||||
t, err := downloader.DownloadByMagnet(r1.Magnet, "~")
|
||||
log.Infof("found resource to download: %v", r1)
|
||||
torrent, err := trc.Download(r1.Magnet)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "download torrent")
|
||||
return nil, errors.Wrap(err, "downloading")
|
||||
}
|
||||
s.tasks[r1.Name] = t
|
||||
s.tasks[r1.Name] = torrent
|
||||
// t, err := downloader.DownloadByMagnet(r1.Magnet, "~")
|
||||
// if err != nil {
|
||||
// return nil, errors.Wrap(err, "download torrent")
|
||||
// }
|
||||
log.Errorf("success add %s to download task", r1.Name)
|
||||
return nil, nil
|
||||
}
|
||||
}
|
||||
|
||||
type downloadClientIn struct {
|
||||
Name string `json:"name"`
|
||||
URL string `json:"url"`
|
||||
User string `json:"user"`
|
||||
Password string `json:"password"`
|
||||
Implementation string `json:"implementation"`
|
||||
}
|
||||
|
||||
func (s *Server) AddDownloadClient(c *gin.Context) (interface{}, error) {
|
||||
var in downloadClientIn
|
||||
if err := c.ShouldBindJSON(&in); err != nil {
|
||||
return nil, errors.Wrap(err, "bind json")
|
||||
}
|
||||
if err := s.db.SaveTransmission(in.Name, in.URL, in.User, in.Password); err != nil {
|
||||
return nil, errors.Wrap(err, "save transmission")
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
@@ -5,6 +5,9 @@ import (
|
||||
"polaris/log"
|
||||
"polaris/pkg/tmdb"
|
||||
|
||||
"github.com/hekmon/transmissionrpc"
|
||||
"github.com/robfig/cron"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
@@ -12,18 +15,34 @@ import (
|
||||
func NewServer(db *db.Client) *Server {
|
||||
r := gin.Default()
|
||||
return &Server{
|
||||
r: r,
|
||||
db: db,
|
||||
r: r,
|
||||
db: db,
|
||||
cron: cron.New(),
|
||||
tasks: make(map[string]*transmissionrpc.Torrent),
|
||||
}
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
r *gin.Engine
|
||||
db *db.Client
|
||||
r *gin.Engine
|
||||
db *db.Client
|
||||
cron *cron.Cron
|
||||
language string
|
||||
tasks map[string]*transmissionrpc.Torrent
|
||||
}
|
||||
|
||||
func (s *Server) scheduler() {
|
||||
s.cron.AddFunc("@every 1m", s.checkTasks)
|
||||
}
|
||||
|
||||
func (s *Server) checkTasks() {
|
||||
for name, t := range s.tasks {
|
||||
log.Infof("task %s percentage done: %f", name, *t.PercentDone)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) Serve() error {
|
||||
s.scheduler()
|
||||
|
||||
api := s.r.Group("/api/v1")
|
||||
|
||||
setting := api.Group("/setting")
|
||||
@@ -38,12 +57,21 @@ func (s *Server) Serve() error {
|
||||
tv.POST("/watchlist", HttpHandler(s.AddWatchlist))
|
||||
tv.GET("/watchlist", HttpHandler(s.GetWatchlist))
|
||||
}
|
||||
indexer := api.Group("/indexer")
|
||||
{
|
||||
indexer.POST("/add", HttpHandler(s.AddTorznabInfo))
|
||||
indexer.POST("/download", HttpHandler(s.SearchAndDownload))
|
||||
}
|
||||
|
||||
downloader := api.Group("/downloader")
|
||||
{
|
||||
downloader.POST("/add", HttpHandler(s.AddDownloadClient))
|
||||
}
|
||||
|
||||
s.language = s.db.GetLanguage()
|
||||
return s.r.Run(":8080")
|
||||
}
|
||||
|
||||
|
||||
func (s *Server) TMDB() (*tmdb.Client, error) {
|
||||
api := s.db.GetSetting(db.SettingTmdbApiKey)
|
||||
if api == "" {
|
||||
@@ -59,4 +87,3 @@ func (s *Server) MustTMDB() *tmdb.Client {
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
|
||||
@@ -42,6 +42,21 @@ func (s *Server) AddWatchlist(c *gin.Context) (interface{}, error) {
|
||||
if err := s.db.AddWatchlist(in.RootFolder, detail); err != nil {
|
||||
return nil, errors.Wrap(err, "add to list")
|
||||
}
|
||||
|
||||
for _, season := range detail.Seasons {
|
||||
seasonId := season.SeasonNumber
|
||||
for i := 1; i <= season.EpisodeCount; i++ {
|
||||
ep, err := s.MustTMDB().GetEposideDetail(int(detail.ID), seasonId, i, s.language)
|
||||
if err != nil {
|
||||
log.Errorf("get eposide detail: %v", err)
|
||||
return nil, errors.Wrap(err, "get eposide detail")
|
||||
}
|
||||
err = s.db.SaveEposideDetail(int(detail.ID), ep)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "save episode")
|
||||
}
|
||||
}
|
||||
}
|
||||
log.Infof("add tv %s to watchlist success", detail.Name)
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user