-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
84 lines (66 loc) · 1.56 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"fmt"
"net/http"
"encoding/json"
"github.com/mmcdole/gofeed"
)
func check(e error) {
if e != nil {
panic(e)
}
}
type Feed struct {
Updated string `json:"updated,"`
Shows []Show `json:"show,omitempty"`
}
type Show struct {
Title string `json:"name,omitempty"`
Magnet string `json:"magnet,omitempty"`
Seeds string `json:"seeds,omitempty"`
Peers string `json:"peers,omitempty"`
Verified string `json:"verified,omitempty"`
Pubdate string `json:"pubdate,omitempty"`
FileName string `json:"filename,omitempty"`
}
func fromItem(item *gofeed.Item) Show {
s := Show{}
s.Title = item.Title
s.Pubdate = item.Published
for _, ext := range item.Extensions {
s.FileName = ext["fileName"][0].Value
s.Magnet = ext["magnetURI"][0].Value
s.Seeds = ext["seeds"][0].Value
s.Peers = ext["peers"][0].Value
s.Verified = ext["verified"][0].Value
}
return s
}
func parseFeed(feed *gofeed.Feed) Feed {
f := Feed{}
f.Updated = feed.Updated
s := make([]Show, 0)
for _, link := range feed.Items {
show := fromItem(link)
s = append(s, show)
}
f.Shows = s
return f
}
func feedToJSON(feed Feed) string {
b, err := json.Marshal(feed)
check(err)
return string(b)
}
func main() {
// TODO: write --help flag
// TODO: write --output flag
// TODO: add error handling in the case of the feed not being available
fp := gofeed.NewParser()
rss, e := http.Get("https://eztv.ag/ezrss.xml") // TODO: make this a flag
check(e)
feed, e := fp.Parse(rss.Body)
check(e)
parsedFeed := parseFeed(feed)
fmt.Println(feedToJSON(parsedFeed))
}