Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit de4340c

Browse files
committed
rewrite rss item parsing, suport nyaa site
1 parent 0628b76 commit de4340c

File tree

1 file changed

+65
-33
lines changed

1 file changed

+65
-33
lines changed

server/server_rss.go

+65-33
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,81 @@ import (
1212
)
1313

1414
var (
15-
magnetExp = regexp.MustCompile(`magnet:[^< ]+`)
15+
magnetExp = regexp.MustCompile(`magnet:[^< ]+`)
16+
hashinfoExp = regexp.MustCompile(`[0-9a-zA-Z]{40}`)
17+
torrentExp = regexp.MustCompile(`\.torrent`)
1618
)
1719

1820
type rssJSONItem struct {
1921
Name string `json:"name"`
2022
Magnet string `json:"magnet"`
21-
InfoHash string `json:"hashinfo"`
23+
InfoHash string `json:"infohash"`
2224
Published string `json:"published"`
2325
URL string `json:"url"`
2426
Torrent string `json:"torrent"`
2527
publishedParsed *time.Time
2628
}
2729

30+
func (ritem *rssJSONItem) findFromFeedItem(i *gofeed.Item) (found bool) {
31+
32+
for _, ex := range []string{"torrent", "nyaa"} {
33+
if ok := ritem.readExtention(i, ex); ok {
34+
break
35+
}
36+
}
37+
38+
// some sites put it under enclosures
39+
for _, e := range i.Enclosures {
40+
if strings.HasPrefix(e.URL, "magnet:") {
41+
ritem.Magnet = e.URL
42+
} else if e.Type == "application/x-bittorrent" {
43+
ritem.Torrent = e.URL
44+
}
45+
}
46+
47+
// maybe the Link is a torrent file
48+
if torrentExp.MatchString(i.Link) {
49+
ritem.Torrent = i.Link
50+
}
51+
52+
// not found magnet/torrent, try to find them in the description
53+
if ritem.Magnet == "" && ritem.InfoHash == "" && ritem.Torrent == "" {
54+
55+
// try to find magnet in description
56+
if s := magnetExp.FindString(i.Description); s != "" {
57+
ritem.Magnet = s
58+
}
59+
60+
// try to find hashinfo in description
61+
if s := hashinfoExp.FindString(i.Description); s != "" {
62+
ritem.InfoHash = s
63+
}
64+
65+
//still not found?, well... whatever
66+
}
67+
68+
return (ritem.Magnet != "" || ritem.InfoHash != "" || ritem.Torrent != "")
69+
}
70+
71+
func (r *rssJSONItem) readExtention(i *gofeed.Item, ext string) (found bool) {
72+
73+
// There are no starndards for rss feeds contains torrents or magnets
74+
// Heres some sites putting info in the extentions
75+
if etor, ok := i.Extensions[ext]; ok {
76+
if e, ok := etor["magnetURI"]; ok && len(e) > 0 {
77+
r.Magnet = e[0].Value
78+
found = true
79+
}
80+
81+
if e, ok := etor["infoHash"]; ok && len(e) > 0 {
82+
r.InfoHash = e[0].Value
83+
found = true
84+
}
85+
}
86+
87+
return
88+
}
89+
2890
func (s *Server) updateRSS() {
2991
fp := gofeed.NewParser()
3092
fp.Client = &http.Client{
@@ -97,37 +159,7 @@ func (s *Server) serveRSS(w http.ResponseWriter, r *http.Request) {
97159
publishedParsed: i.PublishedParsed,
98160
}
99161

100-
// not sure which is the the torrent feed standard
101-
// here is how to get magnet from struct of https://eztv.io/ezrss.xml
102-
if etor, ok := i.Extensions["torrent"]; ok {
103-
if e, ok := etor["magnetURI"]; ok && len(e) > 0 {
104-
ritem.Magnet = e[0].Value
105-
}
106-
if e, ok := etor["infoHash"]; ok && len(e) > 0 {
107-
ritem.InfoHash = e[0].Value
108-
}
109-
} else {
110-
// some sites put it under enclosures
111-
for _, e := range i.Enclosures {
112-
if strings.HasPrefix(e.URL, "magnet:") {
113-
ritem.Magnet = e.URL
114-
} else if e.Type == "application/x-bittorrent" {
115-
ritem.Torrent = e.URL
116-
}
117-
}
118-
119-
// not found magnet/torrent,
120-
if ritem.Magnet == "" && ritem.InfoHash == "" && ritem.Torrent == "" {
121-
122-
// find magnet in description
123-
if s := magnetExp.FindString(i.Description); s != "" {
124-
ritem.Magnet = s
125-
} else {
126-
//fallback to the link (likely not a magnet feed)
127-
ritem.Magnet = i.Link
128-
}
129-
}
130-
}
162+
ritem.findFromFeedItem(i)
131163
results = append(results, ritem)
132164
}
133165

0 commit comments

Comments
 (0)