@@ -12,19 +12,81 @@ import (
12
12
)
13
13
14
14
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` )
16
18
)
17
19
18
20
type rssJSONItem struct {
19
21
Name string `json:"name"`
20
22
Magnet string `json:"magnet"`
21
- InfoHash string `json:"hashinfo "`
23
+ InfoHash string `json:"infohash "`
22
24
Published string `json:"published"`
23
25
URL string `json:"url"`
24
26
Torrent string `json:"torrent"`
25
27
publishedParsed * time.Time
26
28
}
27
29
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
+
28
90
func (s * Server ) updateRSS () {
29
91
fp := gofeed .NewParser ()
30
92
fp .Client = & http.Client {
@@ -97,37 +159,7 @@ func (s *Server) serveRSS(w http.ResponseWriter, r *http.Request) {
97
159
publishedParsed : i .PublishedParsed ,
98
160
}
99
161
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 )
131
163
results = append (results , ritem )
132
164
}
133
165
0 commit comments