Skip to content

Commit

Permalink
feat: better prowlarr support
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-ding committed Nov 4, 2024
1 parent b176253 commit c17cf75
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 10 deletions.
16 changes: 14 additions & 2 deletions pkg/prowlarr/prowlarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ import (
"golift.io/starr/prowlarr"
)

type ProwlarrSupportType string

const (
TV ProwlarrSupportType = "tv"
Movie ProwlarrSupportType = "movie"
)

type Client struct {
p *prowlarr.Prowlarr
apiKey string
Expand All @@ -24,7 +31,7 @@ func New(apiKey, url string) *Client {
return &Client{p: p, apiKey: apiKey, url: url}
}

func (c *Client) GetIndexers() ([]*db.TorznabInfo, error) {
func (c *Client) GetIndexers(t ProwlarrSupportType) ([]*db.TorznabInfo, error) {
ins, err := c.p.GetIndexers()
if err != nil {
return nil, err
Expand All @@ -34,6 +41,11 @@ func (c *Client) GetIndexers() ([]*db.TorznabInfo, error) {
if !in.Enable {
continue
}
if t == "tv" && len(in.Capabilities.TvSearchParams) == 0 { //no tv resource in this indexer
continue
} else if t == "movie" && len(in.Capabilities.MovieSearchParams) == 0 { //no movie resource in this indexer
continue
}
seedRatio := 0.0
for _, f := range in.Fields {
if f.Name == "torrentBaseSettings.seedRatio" && f.Value != nil {
Expand All @@ -57,7 +69,7 @@ func (c *Client) GetIndexers() ([]*db.TorznabInfo, error) {
}

indexers = append(indexers, &db.TorznabInfo{
Indexers: &entIndexer,
Indexers: &entIndexer,
TorznabSetting: setting,
})
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/prowlarr/prowlarr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

func Test111(t *testing.T) {
c := New("", "http://10.0.0.8:9696/")
apis , err := c.GetIndexers()
apis , err := c.GetIndexers("tv")
log.Infof("errors: %v", err)
log.Infof("indexers: %+v", apis[0])
}
8 changes: 7 additions & 1 deletion pkg/torznab/torznab.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,20 @@ func (r *Response) ToResults(indexer *db.TorznabInfo) []Result {
// log.Warnf("converting link to magnet error, error: %v, link: %v", err, item.Link)
// continue
// }
imdb := ""
if item.GetAttr("imdbid") != "" {
imdb = item.GetAttr("imdbid")
} else if item.GetAttr("imdb") != "" {
imdb = item.GetAttr("imdb")
}
r := Result{
Name: item.Title,
Link: item.Link,
Size: mustAtoI(item.Size),
Seeders: mustAtoI(item.GetAttr("seeders")),
Peers: mustAtoI(item.GetAttr("peers")),
Category: mustAtoI(item.GetAttr("category")),
ImdbId: item.GetAttr("imdbid"),
ImdbId: imdb,
DownloadVolumeFactor: tryParseFloat(item.GetAttr("downloadvolumefactor")),
UploadVolumeFactor: tryParseFloat(item.GetAttr("uploadvolumefactor")),
Source: indexer.Name,
Expand Down
10 changes: 5 additions & 5 deletions server/core/torrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func SearchTvSeries(db1 *db.Client, param *SearchParam) ([]torznab.Result, error
}
log.Debugf("check tv series %s, season %d, episode %v", series.NameEn, param.SeasonNum, param.Episodes)

res := searchWithTorznab(db1, series.NameEn, series.NameCn, series.OriginalName)
res := searchWithTorznab(db1, prowlarr.TV, series.NameEn, series.NameCn, series.OriginalName)

var filtered []torznab.Result
for _, r := range res {
Expand Down Expand Up @@ -171,9 +171,9 @@ func SearchMovie(db1 *db.Client, param *SearchParam) ([]torznab.Result, error) {
return nil, errors.New("no media found of id")
}

res := searchWithTorznab(db1, movieDetail.NameEn, movieDetail.NameCn, movieDetail.OriginalName)
res := searchWithTorznab(db1, prowlarr.Movie, movieDetail.NameEn, movieDetail.NameCn, movieDetail.OriginalName)
if movieDetail.Extras.IsJav() {
res1 := searchWithTorznab(db1, movieDetail.Extras.JavId)
res1 := searchWithTorznab(db1, prowlarr.Movie, movieDetail.Extras.JavId)
res = append(res, res1...)
}

Expand Down Expand Up @@ -227,15 +227,15 @@ func SearchMovie(db1 *db.Client, param *SearchParam) ([]torznab.Result, error) {

}

func searchWithTorznab(db *db.Client, queries ...string) []torznab.Result {
func searchWithTorznab(db *db.Client, t prowlarr.ProwlarrSupportType, queries ...string) []torznab.Result {

var res []torznab.Result
allTorznab := db.GetAllTorznabInfo()

p, err := db.GetProwlarrSetting()
if err == nil { //prowlarr exists
c := prowlarr.New(p.ApiKey, p.URL)
all, err := c.GetIndexers()
all, err := c.GetIndexers(t)
if err != nil {
log.Warnf("get prowlarr all indexer error: %v", err)
} else {
Expand Down
2 changes: 1 addition & 1 deletion server/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func (s *Server) SaveProwlarrSetting(c *gin.Context) (interface{}, error) {
return nil, err
}
client := prowlarr.New(in.ApiKey, in.URL)
if _, err := client.GetIndexers(); err != nil {
if _, err := client.GetIndexers(prowlarr.TV); err != nil {
return nil, errors.Wrap(err, "connect to prowlarr error")
}
err := s.db.SaveProwlarrSetting(&in)
Expand Down

0 comments on commit c17cf75

Please sign in to comment.