Skip to content

Commit

Permalink
override relevance with publisted_at
Browse files Browse the repository at this point in the history
  • Loading branch information
rraymondgh committed Jan 27, 2025
1 parent 9caa0db commit 075fecf
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 75 deletions.
8 changes: 0 additions & 8 deletions internal/boilerplate/config/configresolver/coerce.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,15 @@ import (
"strconv"
"strings"
"time"

"github.com/bitmagnet-io/bitmagnet/internal/database/search"
)

var durationType = reflect.TypeOf(time.Duration(0))
var orderByType = reflect.TypeOf(search.TorrentContentOrderByRelevance)
var orderDirectionType = reflect.TypeOf(search.OrderDirectionDescending)

func coerceStringValue(stringValue string, valueType reflect.Type) (interface{}, error) {
// todo Fill this out
switch valueType {
case durationType:
return time.ParseDuration(stringValue)
case orderByType:
return search.ParseTorrentContentOrderBy(stringValue)
case orderDirectionType:
return search.ParseOrderDirection(stringValue)
}
switch valueType.Kind() {
case reflect.String:
Expand Down
10 changes: 7 additions & 3 deletions internal/torznab/adapter/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ func (a adapter) searchRequestOptions(r torznab.SearchRequest) ([]query.Option,
}
}
if r.Query != "" {
options = append(options, query.QueryString(r.Query), query.OrderBy(r.OrderBy.Clauses(r.OrderDirection)...))
order := search.TorrentContentOrderByRelevance
if r.Profile.DisableOrderByRelevance {
order = search.TorrentContentOrderByPublishedAt
}
options = append(options, query.QueryString(r.Query), query.OrderBy(order.Clauses(search.OrderDirectionDescending)...))
}
var catsCriteria []query.Criteria
for _, cat := range r.Cats {
Expand Down Expand Up @@ -169,8 +173,8 @@ func (a adapter) searchRequestOptions(r torznab.SearchRequest) ([]query.Option,
options = append(options, query.Offset(r.Offset.Uint))
}

if len(r.Tags) > 0 {
options = append(options, query.Where(search.TorrentTagCriteria(r.Tags...)))
if len(r.Profile.Tags) > 0 {
options = append(options, query.Where(search.TorrentTagCriteria(r.Profile.Tags...)))
}
return options, nil
}
Expand Down
34 changes: 6 additions & 28 deletions internal/torznab/config.go
Original file line number Diff line number Diff line change
@@ -1,50 +1,28 @@
package torznab

import (
"github.com/bitmagnet-io/bitmagnet/internal/database/search"
)

type Profile struct {
Name string `validate:"required"`
OrderBy search.TorrentContentOrderBy
OrderDirection search.OrderDirection
Tags []string
Name string `validate:"required"`
DisableOrderByRelevance bool
LogRequest bool
Tags []string
}

type Config struct {
DefaultProfile Profile
Profiles []Profile
}

// force in defaulting of []Profiles. Name is mandated by validator
func (p Profile) completeProfile() Profile {
orderBy := search.TorrentContentOrderByRelevance
direction := search.OrderDirectionDescending
if p.OrderBy != "" {
orderBy = p.OrderBy
}
if p.OrderDirection != "" {
direction = p.OrderDirection
}
return Profile{
Name: p.Name,
OrderBy: orderBy,
OrderDirection: direction,
Tags: p.Tags,
}
}

func NewDefaultConfig() Config {
return Config{
DefaultProfile: Profile{Name: "default"}.completeProfile(),
DefaultProfile: Profile{Name: "default"},
}

}

func (c *Config) Map() map[string]Profile {
profileMap := make(map[string]Profile, len(c.Profiles))
for _, profile := range c.Profiles {
profileMap[profile.Name] = profile.completeProfile()
profileMap[profile.Name] = profile
}

return profileMap
Expand Down
41 changes: 21 additions & 20 deletions internal/torznab/httpserver/httpserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ import (
"github.com/bitmagnet-io/bitmagnet/internal/torznab"
"github.com/gin-gonic/gin"
"go.uber.org/fx"
"go.uber.org/zap"
)

type Params struct {
fx.In
Client lazy.Lazy[torznab.Client]
Config torznab.Config
Log *zap.SugaredLogger
}

type Result struct {
Expand All @@ -30,13 +32,15 @@ func New(p Params) Result {
Option: builder{
client: p.Client,
config: p.Config,
log: p.Log,
},
}
}

type builder struct {
client lazy.Lazy[torznab.Client]
config torznab.Config
log *zap.SugaredLogger
}

func (builder) Key() string {
Expand All @@ -46,6 +50,7 @@ func (builder) Key() string {
type torznabworker struct {
client torznab.Client
profile torznab.Profile
log *zap.SugaredLogger
}

func (w torznabworker) writeInternalError(c *gin.Context, err error) {
Expand Down Expand Up @@ -73,11 +78,10 @@ func (w torznabworker) writeErr(c *gin.Context, err error) {
}
}

func (w torznabworker) permaLinkBase() string {
return "/webui/torrents/permalink/"
}

func (w torznabworker) get(c *gin.Context) {
if w.profile.LogRequest {
w.log.Infof("[%s] %s", c.ClientIP(), c.Request.URL.RawQuery)
}
tp := c.Query(torznab.ParamType)
if tp == "" {
w.writeErr(c, torznab.Error{
Expand Down Expand Up @@ -138,19 +142,16 @@ func (w torznabworker) get(c *gin.Context) {
offset.Uint = uint(intOffset)
}
result, searchErr := w.client.Search(c, torznab.SearchRequest{
Query: c.Query(torznab.ParamQuery),
Type: tp,
Cats: cats,
ImdbId: imdbId,
TmdbId: tmdbId,
Season: season,
Episode: episode,
Limit: limit,
Offset: offset,
OrderBy: w.profile.OrderBy,
OrderDirection: w.profile.OrderDirection,
Tags: w.profile.Tags,
PermaLinkBase: w.permaLinkBase(),
Query: c.Query(torznab.ParamQuery),
Type: tp,
Cats: cats,
ImdbId: imdbId,
TmdbId: tmdbId,
Season: season,
Episode: episode,
Limit: limit,
Offset: offset,
Profile: w.profile,
})
if searchErr != nil {
w.writeErr(c, fmt.Errorf("failed to search: %w", searchErr))
Expand All @@ -167,12 +168,11 @@ func (w torznabworker) getDefault(profile torznab.Profile) gin.HandlerFunc {

func (w torznabworker) getWithProfile(profiles map[string]torznab.Profile) gin.HandlerFunc {
handler := func(c *gin.Context) {
profileName := c.Param("profile")
profile, ok := profiles[profileName]
profile, ok := profiles[c.Param("profile")]
if !ok {
w.writeErr(c, torznab.Error{
Code: 200,
Description: fmt.Sprintf("profile not found (%s)", profileName),
Description: fmt.Sprintf("profile not found (%s)", c.Param("profile")),
})
return
}
Expand All @@ -190,6 +190,7 @@ func (b builder) Apply(e *gin.Engine) error {
}
worker := torznabworker{
client: client,
log: b.log,
}

e.GET("/torznab/api/*any", worker.getDefault(b.config.DefaultProfile))
Expand Down
28 changes: 12 additions & 16 deletions internal/torznab/request.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
package torznab

import (
"github.com/bitmagnet-io/bitmagnet/internal/database/search"
"github.com/bitmagnet-io/bitmagnet/internal/model"
)

type SearchRequest struct {
Query string
Type string
Cats []int
ImdbId model.NullString
TmdbId model.NullString
Season model.NullInt
Episode model.NullInt
Attrs []string
Extended bool
Limit model.NullUint
Offset model.NullUint
OrderBy search.TorrentContentOrderBy
OrderDirection search.OrderDirection
Tags []string
PermaLinkBase string
Query string
Type string
Cats []int
ImdbId model.NullString
TmdbId model.NullString
Season model.NullInt
Episode model.NullInt
Attrs []string
Extended bool
Limit model.NullUint
Offset model.NullUint
Profile Profile
}

0 comments on commit 075fecf

Please sign in to comment.