-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
158 lines (145 loc) · 4.06 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"flag"
"fmt"
"os"
"strings"
parsetorrentname "github.com/middelink/go-parse-torrent-name"
"github.com/middelink/go-rssfeed-filter-download/qnap"
"github.com/mmcdole/gofeed"
)
var (
baseuri string
user string
pass string
rssfeed = flag.String("rss", "http://horriblesubs.info/rss.php?res=sd", "rss feed to monitor")
filter = flag.String("filter", "1080,720", "Which resolutions we do not care for")
silent = flag.Bool("silent", false, "silence mode")
)
func init() {
defaultUrl, _ := os.LookupEnv("QNAP_URL")
defaultUser, _ := os.LookupEnv("QNAP_USER")
defaultPass, _ := os.LookupEnv("QNAP_PASS")
flag.StringVar(&baseuri, "baseuri", defaultUrl, "url for the qnap, e.g. http://192.168.1.5:8080/. Defaults to env QNAP_URL")
flag.StringVar(&user, "user", defaultUser, "qnap user to log in as, defaults to env QNAP_USER")
flag.StringVar(&pass, "pass", defaultPass, "qnap pass to log in with, defaults to env QNAP_PASS")
}
func match(title string) bool {
return false /*strings.Contains(title, "Clockwork Planet") ||
strings.Contains(title, "Tsugumomo") ||
strings.Contains(title, "Uchouten") ||
strings.Contains(title, "Alice to Zouroku") ||
strings.Contains(title, "Atom - The Beginning") ||
strings.Contains(title, "Eromanga-sensei") ||
strings.Contains(title, "Re-Creators") ||
strings.Contains(title, "Gin no Guardian") ||
strings.Contains(title, "Detective Conan") ||
strings.Contains(title, "Boku no Hero Academia") ||
strings.Contains(title, "Berserk") ||
strings.Contains(title, "Sagrada Reset")*/
}
func main() {
flag.Parse()
if baseuri == "" || user == "" || pass == "" {
baseurl, user, pass = qnap.GetDefaults("qnap-downloader")
}
if baseuri == "" || user == "" || pass == "" {
fmt.Printf("Either baseuri, user or passwd is not given\n")
os.Exit(1)
}
resolutions := make(map[string]struct{}, 8)
for _, v := range strings.Split(*filter, ",") {
resolutions[strings.TrimSpace(v)] = struct{}{}
}
qf, err := qnap.filestation.New(baseuri, user, pass)
if err != nil {
fmt.Print(err)
os.Exit(1)
}
defer qf.Close()
var items map[string]bool
if items, err = qf.GetList("/Download"); err != nil {
fmt.Print(err)
os.Exit(1)
}
//fmt.Printf("items are %v\n", items)
interest := make(map[string]struct{}, 128)
for item := range items {
//fmt.Printf("%+v\n", item)
tor, err := parsetorrentname.Parse(item)
if err != nil {
fmt.Print(err)
os.Exit(1)
}
interest[tor.Title] = struct{}{}
}
if !*silent {
fmt.Println("You seem to be interested in:")
for k := range interest {
fmt.Printf(" %v\n", k)
}
}
qd, err := qnap.downloadstation.New(baseuri, user, pass)
if err != nil {
fmt.Print(err)
os.Exit(1)
}
defer qd.Close()
var tasks map[string]qnap.TaskState
if tasks, err = qd.TaskQuery(); err != nil {
fmt.Print(err)
os.Exit(1)
}
//fmt.Printf("tasks are: %v\n", tasks)
var feed *gofeed.Feed
fp := gofeed.NewParser()
if feed, err = fp.ParseURL(*rssfeed); err != nil {
fmt.Print(err)
os.Exit(1)
}
//fmt.Printf("feed is %v\n", feed)
for _, item := range feed.Items {
if nsTorrent, ok := item.Extensions["torrent"]; ok {
if name, ok := nsTorrent["magnetURI"]; ok {
item.Link = name[0].Value
}
if name, ok := nsTorrent["fileName"]; ok {
item.Title = name[0].Value
}
}
if !*silent {
fmt.Printf("item %q\n", item.Title)
}
tor, err := parsetorrentname.Parse(item.Title)
if err != nil {
fmt.Print(err)
os.Exit(1)
}
_, interested := interest[tor.Title]
if match(item.Title) || interested {
if _, ok := items[item.Title]; ok {
if !*silent {
fmt.Printf(" already have it\n")
}
} else if _, ok := tasks[item.Title]; ok {
if !*silent {
fmt.Printf(" already have an active task\n")
}
} else if _, ok := resolutions[tor.Resolution]; ok {
if !*silent {
fmt.Printf(" filtered resolution\n")
}
} else {
if *silent {
fmt.Printf("downloading %s\n", item.Title)
} else {
fmt.Printf(" downloading\n")
}
if err = qd.TaskAddUrl(item.Link); err != nil {
fmt.Print(err)
os.Exit(1)
}
}
}
}
}