diff --git a/pkg/qbittorrent/qbittorrent.go b/pkg/qbittorrent/qbittorrent.go index d7bec75..b13e6ab 100644 --- a/pkg/qbittorrent/qbittorrent.go +++ b/pkg/qbittorrent/qbittorrent.go @@ -5,7 +5,7 @@ import ( "fmt" "polaris/pkg" "polaris/pkg/go-qbittorrent/qbt" - "time" + "polaris/pkg/utils" "github.com/pkg/errors" ) @@ -39,14 +39,14 @@ func NewClient(url, user, pass string) (*Client, error) { } func (c *Client) GetAll() ([]pkg.Torrent, error) { - tt, err :=c.c.Torrents(qbt.TorrentsOptions{}) + tt, err := c.c.Torrents(qbt.TorrentsOptions{}) if err != nil { return nil, errors.Wrap(err, "get torrents") } var res []pkg.Torrent for _, t := range tt { t1 := &Torrent{ - c: c.c, + c: c.c, Hash: t.Hash, Info: c.Info, } @@ -56,40 +56,15 @@ func (c *Client) GetAll() ([]pkg.Torrent, error) { } func (c *Client) Download(link, dir string) (pkg.Torrent, error) { - all, err := c.c.Torrents(qbt.TorrentsOptions{}) + hash, err := utils.MagnetHash(link) if err != nil { - return nil, errors.Wrap(err, "get old torrents") - } - allHash := make(map[string]bool, len(all)) - for _, t := range all { - allHash[t.Hash] = true + return nil, errors.Wrap(err, "get hash") } err = c.c.DownloadLinks([]string{link}, qbt.DownloadOptions{Savepath: &dir}) if err != nil { return nil, errors.Wrap(err, "qbt download") } - var newHash string - -loop: - for i := 0; i < 10; i++ { - time.Sleep(1 * time.Second) - all, err = c.c.Torrents(qbt.TorrentsOptions{}) - if err != nil { - return nil, errors.Wrap(err, "get new torrents") - } - - for _, t := range all { - if !allHash[t.Hash] { - newHash = t.Hash - break loop - } - } - } - - if newHash == "" { - return nil, fmt.Errorf("download torrent fail: timeout") - } - return &Torrent{Hash: newHash, c: c.c, Info: c.Info}, nil + return &Torrent{Hash: hash, c: c.c, Info: c.Info}, nil } diff --git a/pkg/torznab/torznab.go b/pkg/torznab/torznab.go index aca9a53..e5e3202 100644 --- a/pkg/torznab/torznab.go +++ b/pkg/torznab/torznab.go @@ -9,6 +9,7 @@ import ( "net/url" "polaris/db" "polaris/log" + "polaris/pkg/utils" "slices" "strconv" "time" @@ -80,9 +81,14 @@ func (r *Response) ToResults(indexer *db.TorznabInfo) []Result { if slices.Contains(item.Category, "3000") { //exclude audio files continue } + link, err := utils.Link2Magnet(item.Link) + if err != nil { + log.Warnf("converting link to magnet error, error: %v, link: %v", err, item.Link) + continue + } r := Result{ Name: item.Title, - Link: item.Link, + Link: link, Size: mustAtoI(item.Size), Seeders: mustAtoI(item.GetAttr("seeders")), Peers: mustAtoI(item.GetAttr("peers")), diff --git a/pkg/transmission/transmission.go b/pkg/transmission/transmission.go index d5e3c2a..857a643 100644 --- a/pkg/transmission/transmission.go +++ b/pkg/transmission/transmission.go @@ -4,11 +4,10 @@ import ( "context" "encoding/json" "fmt" - "net/http" "net/url" "polaris/log" "polaris/pkg" - "strings" + "polaris/pkg/utils" "github.com/hekmon/transmissionrpc/v3" "github.com/pkg/errors" @@ -54,45 +53,28 @@ func (c *Client) GetAll() ([]pkg.Torrent, error) { var torrents []pkg.Torrent for _, t := range all { torrents = append(torrents, &Torrent{ - Hash: *t.HashString, + Hash: *t.HashString, c: c.c, - Config: c.cfg, + Config: c.cfg, }) } return torrents, nil } func (c *Client) Download(link, dir string) (pkg.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) - } - } - - } - - } + hash, err := utils.MagnetHash(link) + if err != nil { + return nil, errors.Wrap(err, "get hash") + } + t, err := c.c.TorrentAdd(context.TODO(), transmissionrpc.TorrentAddPayload{ Filename: &link, DownloadDir: &dir, }) - log.Infof("get torrent info: %+v", t) - if t.HashString == nil { - return nil, fmt.Errorf("download torrent error: %v", link) - } + log.Debugf("get torrent info: %+v", t) return &Torrent{ - Hash: *t.HashString, + Hash: hash, c: c.c, Config: c.cfg, }, err @@ -100,7 +82,7 @@ func (c *Client) Download(link, dir string) (pkg.Torrent, error) { type Torrent struct { //t *transmissionrpc.Torrent - c *transmissionrpc.Client + c *transmissionrpc.Client Hash string `json:"hash"` Config } @@ -136,7 +118,6 @@ func (t *Torrent) Exists() bool { return len(r) > 0 } - func (t *Torrent) Name() (string, error) { tt, err := t.getTorrent() if err != nil { diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 03ed2d8..a06e1da 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -251,3 +251,24 @@ func Link2Magnet(link string) (string, error) { } return mg.String(), nil } + + +func MagnetHash(link string) (string, error) { + if mi, err := metainfo.ParseMagnetV2Uri(link); err != nil { + return "", errors.Errorf("magnet link is not valid: %v", err) + } else { + hash := "" + if mi.InfoHash.Unwrap().HexString() != "" { + hash = mi.InfoHash.Unwrap().HexString() + } else { + btmh := mi.V2InfoHash.Unwrap() + if btmh.HexString() != "" { + hash = btmh.HexString() + } + } + if hash == "" { + return "", errors.Errorf("magnet has no info hash: %v", link) + } + return hash, nil + } +} \ No newline at end of file diff --git a/server/activity.go b/server/activity.go index 26023cd..dd4e46c 100644 --- a/server/activity.go +++ b/server/activity.go @@ -11,7 +11,6 @@ import ( "polaris/pkg/utils" "strconv" - "github.com/anacrolix/torrent/metainfo" "github.com/gin-gonic/gin" "github.com/pkg/errors" ) @@ -123,21 +122,9 @@ func (s *Server) addTorrent2Blacklist(link string) error { if link == "" { return nil } - if mi, err := metainfo.ParseMagnetV2Uri(link); err != nil { - return errors.Errorf("magnet link is not valid: %v", err) + if hash, err := utils.MagnetHash(link); err != nil { + return err } else { - hash := "" - if mi.InfoHash.Unwrap().HexString() != "" { - hash = mi.InfoHash.Unwrap().HexString() - } else { - btmh := mi.V2InfoHash.Unwrap() - if btmh.HexString() != "" { - hash = btmh.HexString() - } - } - if hash == "" { - return errors.Errorf("magnet has no info hash: %v", link) - } item := ent.Blacklist{ Type: blacklist.TypeTorrent, Value: schema.BlacklistValue{