mirror of
https://github.com/simon-ding/polaris.git
synced 2026-02-22 19:50:57 +08:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1359df599b | ||
|
|
16ca00d19c | ||
|
|
f4b8d03cfc | ||
|
|
8811b89889 |
@@ -9,23 +9,20 @@ import (
|
||||
)
|
||||
|
||||
type Metadata struct {
|
||||
NameEn string
|
||||
NameCn string
|
||||
Season int
|
||||
Episode int
|
||||
Resolution string
|
||||
NameEn string
|
||||
NameCn string
|
||||
Season int
|
||||
Episode int
|
||||
Resolution string
|
||||
IsSeasonPack bool
|
||||
}
|
||||
|
||||
func ParseTv(name string) *Metadata {
|
||||
name = strings.ToLower(name)
|
||||
if utils.IsASCII(name) { //english name
|
||||
return parseEnglishName(name)
|
||||
}
|
||||
if utils.ContainsChineseChar(name) {
|
||||
return parseChineseName(name)
|
||||
}
|
||||
return nil
|
||||
return parseEnglishName(name)
|
||||
}
|
||||
|
||||
func parseEnglishName(name string) *Metadata {
|
||||
@@ -158,9 +155,9 @@ func parseChineseName(name string) *Metadata {
|
||||
re3 := regexp.MustCompile(`[^\d\w]\d{1,2}[^\d\w]`)
|
||||
epNums := re3.FindAllString(name, -1)
|
||||
if len(epNums) > 0 {
|
||||
|
||||
|
||||
re3 := regexp.MustCompile(`\d{1,2}`)
|
||||
epNum := re3.FindAllString(epNums[0],-1)[0]
|
||||
epNum := re3.FindAllString(epNums[0], -1)[0]
|
||||
n, err := strconv.Atoi(epNum)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("convert %s error: %v", epNum, err))
|
||||
@@ -203,10 +200,10 @@ func parseChineseName(name string) *Metadata {
|
||||
}
|
||||
}
|
||||
|
||||
if meta.IsSeasonPack && meta.Episode != 0{
|
||||
if meta.IsSeasonPack && meta.Episode != 0 {
|
||||
meta.Season = meta.Episode
|
||||
meta.Episode = -1
|
||||
}
|
||||
}
|
||||
|
||||
//tv name
|
||||
title := name
|
||||
|
||||
@@ -28,7 +28,7 @@ type LocalStorage struct {
|
||||
|
||||
func (l *LocalStorage) Move(src, dest string) error {
|
||||
targetDir := filepath.Join(l.dir, dest)
|
||||
os.MkdirAll(targetDir, 0655)
|
||||
os.MkdirAll(filepath.Dir(targetDir), 0655)
|
||||
err := filepath.Walk(src, func(path string, info fs.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -76,7 +76,7 @@ func (r *Response) ToResults() []Result {
|
||||
for _, item := range r.Channel.Item {
|
||||
r := Result{
|
||||
Name: item.Title,
|
||||
Magnet: item.Link,
|
||||
Link: item.Link,
|
||||
Size: mustAtoI(item.Size),
|
||||
Seeders: mustAtoI(item.GetAttr("seeders")),
|
||||
Peers: mustAtoI(item.GetAttr("peers")),
|
||||
@@ -129,7 +129,7 @@ func Search(torznabUrl, api, keyWord string) ([]Result, error) {
|
||||
|
||||
type Result struct {
|
||||
Name string
|
||||
Magnet string
|
||||
Link string
|
||||
Size int
|
||||
Seeders int
|
||||
Peers int
|
||||
|
||||
@@ -3,8 +3,11 @@ package transmission
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"polaris/log"
|
||||
"strings"
|
||||
|
||||
"github.com/hekmon/transmissionrpc/v3"
|
||||
"github.com/pkg/errors"
|
||||
@@ -34,20 +37,42 @@ type Config struct {
|
||||
Password string `json:"password"`
|
||||
}
|
||||
type Client struct {
|
||||
c *transmissionrpc.Client
|
||||
c *transmissionrpc.Client
|
||||
cfg Config
|
||||
}
|
||||
|
||||
func (c *Client) Download(magnet, dir string) (*Torrent, error) {
|
||||
func (c *Client) Download(link, dir string) (*Torrent, error) {
|
||||
if strings.HasPrefix(link, "http") {
|
||||
client := &http.Client{
|
||||
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
||||
return http.ErrUseLastResponse
|
||||
},
|
||||
}
|
||||
resp, err:=client.Get(link)
|
||||
if err == nil {
|
||||
if resp.StatusCode == http.StatusFound {
|
||||
loc, err := resp.Location()
|
||||
if err == nil {
|
||||
link = loc.String()
|
||||
log.Warnf("transimision redirect to url: %v", link)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
t, err := c.c.TorrentAdd(context.TODO(), transmissionrpc.TorrentAddPayload{
|
||||
Filename: &magnet,
|
||||
Filename: &link,
|
||||
DownloadDir: &dir,
|
||||
})
|
||||
log.Infof("get torrent info: %+v", t)
|
||||
if t.ID == nil {
|
||||
return nil, fmt.Errorf("download torrent error: %v", link)
|
||||
}
|
||||
|
||||
return &Torrent{
|
||||
ID: *t.ID,
|
||||
c: c.c,
|
||||
ID: *t.ID,
|
||||
c: c.c,
|
||||
Config: c.cfg,
|
||||
}, err
|
||||
}
|
||||
@@ -95,7 +120,7 @@ func (t *Torrent) Progress() int {
|
||||
if t.getTorrent().PercentComplete != nil && *t.getTorrent().PercentComplete >= 1 {
|
||||
return 100
|
||||
}
|
||||
|
||||
|
||||
if t.getTorrent().PercentComplete != nil {
|
||||
p := int(*t.getTorrent().PercentComplete * 100)
|
||||
if p == 100 {
|
||||
@@ -143,4 +168,4 @@ func ReloadTorrent(s string) (*Torrent, error) {
|
||||
return nil, errors.Wrap(err, "reload client")
|
||||
}
|
||||
return &torrent, nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,7 +31,11 @@ func SearchEpisode(db1 *db.Client, seriesId, seasonNum, episodeNum int, checkRes
|
||||
|
||||
var filtered []torznab.Result
|
||||
for _, r := range res {
|
||||
//log.Infof("torrent resource: %+v", r)
|
||||
meta := metadata.ParseTv(r.Name)
|
||||
if meta == nil { //cannot parse name
|
||||
continue
|
||||
}
|
||||
if meta.Season != seasonNum {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -80,7 +80,7 @@ func (s *Server) searchAndDownloadSeasonPackage(seriesId, seasonNum int) (*strin
|
||||
}
|
||||
|
||||
r1 := res[0]
|
||||
log.Infof("found resource to download: %v", r1)
|
||||
log.Infof("found resource to download: %+v", r1)
|
||||
|
||||
downloadDir := s.db.GetDownloadDir()
|
||||
size := utils.AvailableSpace(downloadDir)
|
||||
@@ -89,7 +89,7 @@ func (s *Server) searchAndDownloadSeasonPackage(seriesId, seasonNum int) (*strin
|
||||
return nil, errors.New("no enough space")
|
||||
}
|
||||
|
||||
torrent, err := trc.Download(r1.Magnet, s.db.GetDownloadDir())
|
||||
torrent, err := trc.Download(r1.Link, s.db.GetDownloadDir())
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "downloading")
|
||||
}
|
||||
@@ -99,7 +99,7 @@ func (s *Server) searchAndDownloadSeasonPackage(seriesId, seasonNum int) (*strin
|
||||
if series == nil {
|
||||
return nil, fmt.Errorf("no tv series of id %v", seriesId)
|
||||
}
|
||||
dir := fmt.Sprintf("%s/Season %02d", series.TargetDir, seasonNum)
|
||||
dir := fmt.Sprintf("%s/Season %02d/", series.TargetDir, seasonNum)
|
||||
|
||||
history, err := s.db.SaveHistoryRecord(ent.History{
|
||||
MediaID: seriesId,
|
||||
@@ -143,14 +143,14 @@ func (s *Server) searchAndDownload(seriesId, seasonNum, episodeNum int) (*string
|
||||
return nil, err
|
||||
}
|
||||
r1 := res[0]
|
||||
log.Infof("found resource to download: %v", r1)
|
||||
torrent, err := trc.Download(r1.Magnet, s.db.GetDownloadDir())
|
||||
log.Infof("found resource to download: %+v", r1)
|
||||
torrent, err := trc.Download(r1.Link, s.db.GetDownloadDir())
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "downloading")
|
||||
}
|
||||
torrent.Start()
|
||||
|
||||
dir := fmt.Sprintf("%s/Season %02d", series.TargetDir, seasonNum)
|
||||
dir := fmt.Sprintf("%s/Season %02d/", series.TargetDir, seasonNum)
|
||||
|
||||
history, err := s.db.SaveHistoryRecord(ent.History{
|
||||
MediaID: ep.MediaID,
|
||||
@@ -195,7 +195,7 @@ func (s *Server) SearchAvailableEpisodeResource(c *gin.Context) (interface{}, er
|
||||
Size: r.Size,
|
||||
Seeders: r.Seeders,
|
||||
Peers: r.Peers,
|
||||
Link: r.Magnet,
|
||||
Link: r.Link,
|
||||
})
|
||||
}
|
||||
if len(searchResults) == 0 {
|
||||
@@ -268,7 +268,7 @@ func (s *Server) SearchAvailableMovies(c *gin.Context) (interface{}, error) {
|
||||
Size: r.Size,
|
||||
Seeders: r.Seeders,
|
||||
Peers: r.Peers,
|
||||
Link: r.Magnet,
|
||||
Link: r.Link,
|
||||
})
|
||||
}
|
||||
if len(searchResults) == 0 {
|
||||
|
||||
@@ -294,7 +294,7 @@ func (s *Server) downloadMovieSingleEpisode(ep *ent.Episode) error {
|
||||
}
|
||||
r1 := res[0]
|
||||
log.Infof("begin download torrent resource: %v", r1.Name)
|
||||
torrent, err := trc.Download(r1.Magnet, s.db.GetDownloadDir())
|
||||
torrent, err := trc.Download(r1.Link, s.db.GetDownloadDir())
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "downloading")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user