83 lines
1.9 KiB
Go
83 lines
1.9 KiB
Go
package indexer
|
|
|
|
import (
|
|
"encoding/xml"
|
|
|
|
pb "homelab.lan/music-agregator/gen/music_agregator/indexer/v1"
|
|
"homelab.lan/music-agregator/internal/tracker/rutracker"
|
|
)
|
|
|
|
type SearchResult struct {
|
|
XMLName xml.Name `xml:"rss"`
|
|
Items []Item `xml:"channel>item"`
|
|
}
|
|
|
|
type Item struct {
|
|
Title string `xml:"title"`
|
|
Link string `xml:"link"`
|
|
Guid string `xml:"guid"`
|
|
PubDate string `xml:"pubDate"`
|
|
Size int64 `xml:"size"`
|
|
Description string `xml:"description"`
|
|
Categories []string `xml:"category"`
|
|
Enclosure Enclosure `xml:"enclosure"`
|
|
TorznabAttrs []TorznabAttr `xml:"attr"`
|
|
}
|
|
|
|
type Enclosure struct {
|
|
URL string `xml:"url,attr"`
|
|
Length int64 `xml:"length,attr"`
|
|
Type string `xml:"type,attr"`
|
|
}
|
|
|
|
type TorznabAttr struct {
|
|
Name string `xml:"name,attr"`
|
|
Value string `xml:"value,attr"`
|
|
}
|
|
|
|
var (
|
|
parserFactory = rutracker.NewParserFactory()
|
|
filter = rutracker.NewFilter()
|
|
)
|
|
|
|
func (sr *SearchResult) ToProto() *pb.SearchResponse {
|
|
var pbItems []*pb.SearchItem
|
|
|
|
for _, item := range sr.Items {
|
|
if !filter.IsKnownCategory(item.Categories) {
|
|
continue
|
|
}
|
|
|
|
pbAttrs := make([]*pb.TorznabAttr, len(item.TorznabAttrs))
|
|
for j, attr := range item.TorznabAttrs {
|
|
pbAttrs[j] = &pb.TorznabAttr{
|
|
Name: attr.Name,
|
|
Value: attr.Value,
|
|
}
|
|
}
|
|
|
|
release := parserFactory.GetParser(item.Categories).Parse(item.Title)
|
|
|
|
pbItems = append(pbItems, &pb.SearchItem{
|
|
Title: item.Title,
|
|
DownloadLink: item.Link,
|
|
TorrentPageUrl: item.Guid,
|
|
PubDate: item.PubDate,
|
|
Size: item.Size,
|
|
Description: item.Description,
|
|
Categories: item.Categories,
|
|
Enclosure: &pb.Enclosure{
|
|
Url: item.Enclosure.URL,
|
|
Length: item.Enclosure.Length,
|
|
Type: item.Enclosure.Type,
|
|
},
|
|
TorznabAttrs: pbAttrs,
|
|
Release: release.ToProto(),
|
|
})
|
|
}
|
|
|
|
return &pb.SearchResponse{
|
|
Result: pbItems,
|
|
}
|
|
}
|