fix organizer diagnostics and site integrations

This commit is contained in:
ShukeBta
2026-06-21 09:44:12 +08:00
parent 6e61c8b545
commit 7b881d031c
37 changed files with 2012 additions and 164 deletions
+54 -9
View File
@@ -33,6 +33,7 @@ type SiteService struct {
log *zap.Logger
repo *repository.Container
flareSolverrURL string
apiRateLimiter siteAPIRateLimiter
}
// ResolveDownloadURL converts tracker-specific search result URLs into a URL
@@ -70,15 +71,34 @@ func (s *SiteService) ResolveDownloadURL(ctx context.Context, raw string) string
defer cancel()
resolved, err := adapter.GetDownloadURL(resolveCtx, cfg, id)
if err != nil || resolved == "" {
s.log.Warn("resolve PT download URL failed",
zap.String("site", matched.Name),
zap.String("raw", raw),
zap.Error(err))
if s.log != nil {
s.log.Warn("resolve PT download URL failed",
zap.String("site", matched.Name),
zap.String("raw", redactSensitiveDownloadURL(raw)),
zap.Error(err))
}
return raw
}
return resolved
}
func redactSensitiveDownloadURL(raw string) string {
raw = strings.TrimSpace(raw)
if raw == "" {
return ""
}
if strings.HasPrefix(strings.ToLower(raw), "magnet:") {
return "magnet:?xt=***"
}
u, err := url.Parse(raw)
if err != nil || u.Host == "" {
return "[redacted-download-url]"
}
u.RawQuery = ""
u.Fragment = ""
return u.String()
}
func (s *SiteService) FetchTorrentFile(ctx context.Context, raw string) ([]byte, string, error) {
matched := s.matchSiteForURL(ctx, raw)
if matched == nil {
@@ -175,7 +195,12 @@ func torrentFilename(rawURL, disposition string) string {
// NewSiteService is the constructor.
func NewSiteService(log *zap.Logger, repo *repository.Container, flareSolverrURL string) *SiteService {
return &SiteService{log: log, repo: repo, flareSolverrURL: flareSolverrURL}
return &SiteService{
log: log,
repo: repo,
flareSolverrURL: flareSolverrURL,
apiRateLimiter: newPersistentSiteAPIRateLimiter(repo),
}
}
// Create persists a new site.
@@ -308,7 +333,7 @@ func (s *SiteService) TestConnection(ctx context.Context, id string) (bool, stri
}).Error
return true, "连接成功", nil
} else {
if site.Type == "mteam" {
if site.Type == "mteam" || site.Type == "yemapt" || isYemaPTURL(site.URL) {
s.log.Warn("site adapter authenticate failed",
zap.String("site", site.Name),
zap.String("type", site.Type),
@@ -364,6 +389,7 @@ type SearchResult struct {
SiteName string `json:"site_name"`
SiteID string `json:"site_id"`
Title string `json:"title"`
Subtitle string `json:"subtitle,omitempty"`
TorrentURL string `json:"torrent_url"`
DownloadURL string `json:"download_url"`
Category string `json:"category,omitempty"`
@@ -386,15 +412,18 @@ func (s *SiteService) Search(ctx context.Context, keyword string) ([]SearchResul
}
var (
mu sync.Mutex
wg sync.WaitGroup
results []SearchResult
mu sync.Mutex
wg sync.WaitGroup
enabledCount int
failedCount int
results []SearchResult
)
for i := range sites {
if !sites[i].Enabled {
continue
}
enabledCount++
wg.Add(1)
go func(site model.Site) {
defer wg.Done()
@@ -416,10 +445,15 @@ func (s *SiteService) Search(ctx context.Context, keyword string) ([]SearchResul
result, err := adapter.Search(ctxWithTimeout, cfg, keyword, 1)
if err != nil {
mu.Lock()
failedCount++
mu.Unlock()
s.log.Warn("site search failed",
zap.String("site", site.Name),
zap.String("type", site.Type),
zap.String("url", site.URL),
zap.String("keyword", keyword),
zap.Duration("timeout", timeout),
zap.Error(err))
return
}
@@ -436,6 +470,7 @@ func (s *SiteService) Search(ctx context.Context, keyword string) ([]SearchResul
SiteName: site.Name,
SiteID: site.ID,
Title: item.Title,
Subtitle: item.Subtitle,
TorrentURL: item.DetailURL,
DownloadURL: item.DownloadURL,
Category: item.Category,
@@ -459,6 +494,13 @@ func (s *SiteService) Search(ctx context.Context, keyword string) ([]SearchResul
sort.Slice(results, func(i, j int) bool {
return results[i].Seeders > results[j].Seeders
})
if s.log != nil {
s.log.Info("site search completed",
zap.String("keyword", keyword),
zap.Int("enabled_sites", enabledCount),
zap.Int("failed_sites", failedCount),
zap.Int("results_count", len(results)))
}
return results, nil
}
@@ -486,6 +528,7 @@ func (svc *SiteService) siteModelToConfig(s *model.Site) SiteConfig {
}
return SiteConfig{
SiteID: s.ID,
Name: s.Name,
Type: s.Type,
URL: s.URL,
@@ -498,5 +541,7 @@ func (svc *SiteService) siteModelToConfig(s *model.Site) SiteConfig {
Extra: extra,
FlareSolverrURL: flareSolverrURL,
UseProxy: s.UseProxy,
RateLimit: s.RateLimit,
rateLimiter: svc.apiRateLimiter,
}
}