Feature: Get size by api for ptp btn and ggn (#66)

* chore: add package

* feat: get size by api for ptp and btn

* feat: download and parse torrent if not api

* feat: bypass tls check and load meta from file

* fix: no invite command needed for btn

* feat: add ggn api

* feat: imrpove logging

* feat: build request url

* feat: improve err logging
This commit is contained in:
Ludvig Lundgren 2022-01-05 23:52:29 +01:00 committed by GitHub
parent d2aa7c1e7e
commit 2ea2293745
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
32 changed files with 2181 additions and 99 deletions

View file

@ -5,10 +5,8 @@ import (
"os"
"path"
"github.com/anacrolix/torrent/metainfo"
"github.com/rs/zerolog/log"
"github.com/autobrr/autobrr/internal/client"
"github.com/autobrr/autobrr/internal/domain"
)
@ -32,25 +30,30 @@ func (s *service) RunActions(actions []domain.Action, release domain.Release) er
s.bus.Publish("release:update-push-status", release.ID, domain.ReleasePushStatusApproved)
case domain.ActionTypeExec:
if tmpFile == "" {
tmpFile, hash, err = downloadFile(release.TorrentURL)
if release.TorrentTmpFile == "" {
t, err := release.DownloadTorrentFile(nil)
if err != nil {
log.Error().Stack().Err(err)
return err
}
tmpFile = t.TmpFileName
}
go func(release domain.Release, action domain.Action, tmpFile string) {
s.execCmd(release, action, tmpFile)
s.bus.Publish("release:update-push-status", release.ID, domain.ReleasePushStatusApproved)
}(release, action, tmpFile)
case domain.ActionTypeWatchFolder:
if tmpFile == "" {
tmpFile, hash, err = downloadFile(release.TorrentURL)
if release.TorrentTmpFile == "" {
t, err := release.DownloadTorrentFile(nil)
if err != nil {
log.Error().Stack().Err(err)
return err
}
tmpFile = t.TmpFileName
}
s.watchFolder(action.WatchFolder, tmpFile)
s.bus.Publish("release:update-push-status", release.ID, domain.ReleasePushStatusApproved)
@ -65,12 +68,14 @@ func (s *service) RunActions(actions []domain.Action, release domain.Release) er
s.bus.Publish("release:update-push-status-rejected", release.ID, "deluge busy")
continue
}
if tmpFile == "" {
tmpFile, hash, err = downloadFile(release.TorrentURL)
if release.TorrentTmpFile == "" {
t, err := release.DownloadTorrentFile(nil)
if err != nil {
log.Error().Stack().Err(err)
return err
}
tmpFile = t.TmpFileName
}
go func(action domain.Action, tmpFile string) {
@ -92,12 +97,15 @@ func (s *service) RunActions(actions []domain.Action, release domain.Release) er
continue
}
if tmpFile == "" {
tmpFile, hash, err = downloadFile(release.TorrentURL)
if release.TorrentTmpFile == "" {
t, err := release.DownloadTorrentFile(nil)
if err != nil {
log.Error().Stack().Err(err)
return err
}
tmpFile = t.TmpFileName
hash = t.MetaInfo.HashInfoBytes().String()
}
go func(action domain.Action, hash string, tmpFile string) {
@ -145,33 +153,43 @@ func (s *service) RunActions(actions []domain.Action, release domain.Release) er
return nil
}
// downloadFile returns tmpFile, hash, error
func downloadFile(url string) (string, string, error) {
// create http client
c := client.NewHttpClient()
func (s *service) CheckCanDownload(actions []domain.Action) bool {
for _, action := range actions {
if !action.Enabled {
// only run active actions
continue
}
// download torrent file
// TODO check extra headers, cookie
res, err := c.DownloadFile(url, nil)
if err != nil {
log.Error().Stack().Err(err).Msgf("could not download file: %v", url)
return "", "", err
log.Debug().Msgf("action-service: check can download action: %v", action.Name)
switch action.Type {
case domain.ActionTypeDelugeV1, domain.ActionTypeDelugeV2:
canDownload, err := s.delugeCheckRulesCanDownload(action)
if err != nil {
log.Error().Stack().Err(err).Msgf("error checking client rules: %v", action.Name)
continue
}
if !canDownload {
continue
}
return true
case domain.ActionTypeQbittorrent:
canDownload, err := s.qbittorrentCheckRulesCanDownload(action)
if err != nil {
log.Error().Stack().Err(err).Msgf("error checking client rules: %v", action.Name)
continue
}
if !canDownload {
continue
}
return true
}
}
// match more filters like torrent size
// Get meta info from file to find out the hash for later use
meta, err := metainfo.LoadFromFile(res.FileName)
//meta, err := metainfo.Load(res.Body)
if err != nil {
log.Error().Stack().Err(err).Msgf("metainfo could not open file: %v", res.FileName)
return "", "", err
}
// torrent info hash used for re-announce
hash := meta.HashInfoBytes().String()
return res.FileName, hash, nil
return false
}
func (s *service) test(name string) {
@ -190,10 +208,10 @@ func (s *service) watchFolder(dir string, torrentFile string) {
defer original.Close()
_, tmpFileName := path.Split(torrentFile)
fullFileName := path.Join(dir, tmpFileName)
fullFileName := path.Join(dir, tmpFileName+".torrent")
// Create new file
newFile, err := os.Create(fullFileName + ".torrent")
newFile, err := os.Create(fullFileName)
if err != nil {
log.Error().Stack().Err(err).Msgf("could not create new temp file '%v'", fullFileName)
return

View file

@ -17,6 +17,7 @@ type Service interface {
ToggleEnabled(actionID int) error
RunActions(actions []domain.Action, release domain.Release) error
CheckCanDownload(actions []domain.Action) bool
}
type service struct {