Skip to content

torznab permalinks #411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions internal/model/torrents.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ func (t *Torrent) AfterFind(_ *gorm.DB) error {
return nil
}

func (t Torrent) PermaLink(baseURL NullString) NullString {
if !baseURL.Valid {
return NullString{}
}

return NewNullString(baseURL.String + "/webui/torrents/permalink/" + t.InfoHash.String())
}

// Seeders returns the highest number of seeders from all sources
// todo: Add up bloom filters
func (t Torrent) Seeders() NullUint {
Expand Down
17 changes: 14 additions & 3 deletions internal/torznab/adapter/search_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func torrentContentResultToTorznabResult(
) torznab.SearchResult {
entries := make([]torznab.SearchResultItem, 0, len(res.Items))
for _, item := range res.Items {
entries = append(entries, torrentContentResultItemToTorznabResultItem(item))
entries = append(entries, torrentContentResultItemToTorznabResultItem(item, req.Profile))
}

return torznab.SearchResult{
Expand All @@ -29,7 +29,9 @@ func torrentContentResultToTorznabResult(
}
}

func torrentContentResultItemToTorznabResultItem(item search.TorrentContentResultItem) torznab.SearchResultItem {
func torrentContentResultItemToTorznabResultItem(
item search.TorrentContentResultItem, profile torznab.Profile,
) torznab.SearchResultItem {
category := "Unknown"
if item.ContentType.Valid {
category = item.ContentType.ContentType.Label()
Expand Down Expand Up @@ -80,6 +82,7 @@ func torrentContentResultItemToTorznabResultItem(item search.TorrentContentResul
AttrValue: item.PublishedAt.Format(torznab.RssDateDefaultFormat),
},
}

seeders := item.Torrent.Seeders()
leechers := item.Torrent.Leechers()

Expand Down Expand Up @@ -169,7 +172,7 @@ func torrentContentResultItemToTorznabResultItem(item search.TorrentContentResul
})
}

return torznab.SearchResultItem{
res := torznab.SearchResultItem{
Title: item.Torrent.Name,
Size: item.Torrent.Size,
Category: category,
Expand All @@ -182,4 +185,12 @@ func torrentContentResultItemToTorznabResultItem(item search.TorrentContentResul
},
TorznabAttrs: attrs,
}

permalink := item.Torrent.PermaLink(profile.BaseURL)
if permalink.Valid {
res.Comments = permalink.String
res.Link = permalink.String
}

return res
}
12 changes: 11 additions & 1 deletion internal/torznab/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ package torznab
import (
"strings"

"github.com/bitmagnet-io/bitmagnet/internal/model"
"github.com/bitmagnet-io/bitmagnet/internal/slice"
)

type Config struct {
BaseURL string
Profiles []Profile
}

const configBaseURLUndefined = "__undefined__"

func (c Config) MergeDefaults() Config {
c.Profiles = slice.Map(c.Profiles, func(profile Profile) Profile {
return profile.MergeDefaults()
Expand All @@ -19,12 +23,18 @@ func (c Config) MergeDefaults() Config {
}

func NewDefaultConfig() Config {
return Config{}
return Config{
BaseURL: configBaseURLUndefined,
}
}

func (c Config) GetProfile(name string) (Profile, bool) {
for _, p := range c.Profiles {
if strings.EqualFold(p.ID, name) {
if c.BaseURL != configBaseURLUndefined && !p.BaseURL.Valid {
p.BaseURL = model.NewNullString(c.BaseURL)
}

return p, true
}
}
Expand Down
8 changes: 7 additions & 1 deletion internal/torznab/profile.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package torznab

import "strings"
import (
"strings"

"github.com/bitmagnet-io/bitmagnet/internal/model"
)

type Profile struct {
ID string `validate:"required"`
Expand All @@ -9,13 +13,15 @@ type Profile struct {
DefaultLimit uint
MaxLimit uint
Tags []string
BaseURL model.NullString
}

var ProfileDefault = Profile{
ID: "default",
Title: "bitmagnet",
DefaultLimit: 100,
MaxLimit: 100,
BaseURL: model.NullString{},
}

func (p Profile) MergeDefaults() Profile {
Expand Down