-
Notifications
You must be signed in to change notification settings - Fork 0
/
once.go
226 lines (197 loc) · 5.82 KB
/
once.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package main
/*
* RSS4Transmission
* Copyright (c) 2023 Aaron Turner <aturner at synfin dot net>
*
* This program is free software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or with the authors permission any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import (
"fmt"
"os"
"time"
"github.com/manifoldco/promptui"
"github.com/mmcdole/gofeed"
)
type Feeds map[string]*gofeed.Feed
type OnceCmd struct {
Feed []string `kong:"help='Limit scraping to the given feed(s)'"`
Download bool `kong:"short='d',help='Download torrent file instead of torrenting',xor='action'"`
DownloadPath string `kong:"short='p',help='Path to download torrent files to ($PWD)'"`
Interactive bool `kong:"short='i',help='Interactive mode',xor='action'"`
NoAction bool `kong:"short='n',help='Just print results and take no action',xor='action'"`
Skip bool `kong:"short='s',help='Just skip any matching torrents',xor='action'"`
}
func (cmd *OnceCmd) Run(ctx *RunContext) error {
var err error
var filePath string
log.Debugf("Starting our run...")
if ctx.Cli.Once.DownloadPath == "" {
ctx.Cli.Once.DownloadPath = os.Getenv("PWD")
}
// we cache gofeed results for each URL so we can re-use the feed results without hitting
// the RSS multiple times
feeds := Feeds{}
quit := false
for name, feed := range ctx.Config.Feeds {
if quit {
break
}
log.Debugf("Processing %s: %v", name, feed)
// have we already fetched this RSS feed?
if f, ok := feeds[feed.URL]; !ok {
p := gofeed.NewParser()
if feeds[feed.URL], err = p.ParseURL(feed.URL); err != nil {
log.WithError(err).Warnf("Unable to process URL: %s", feed.URL)
continue
}
} else if f == nil {
// we can have the same URL in multiple feeds, so we need to skip them too
continue
}
for _, item := range feed.NewItems(name, feeds[feed.URL]) {
if quit {
break
}
if ctx.Cache.Exists(name, item) {
log.Debugf("Skipping due to cache hit: %s", item.Item.Title)
continue
}
if ctx.Cli.Once.NoAction {
log.Infof("%s match: %s", name, item.Item.Title)
} else if ctx.Cli.Once.Skip {
// add to cache and do nothing
ctx.Cache.AddItem(item)
} else if ctx.Cli.Once.Download {
if filePath, err = item.Download(ctx, ctx.Cli.Once.DownloadPath); err != nil {
log.WithError(err).Errorf("Unable to download: %s", filePath)
continue
}
} else if ctx.Cli.Once.Interactive {
switch prompt(name, item.Item.Title) {
case Download:
if filePath, err = item.Download(ctx, ctx.Cli.Once.DownloadPath); err != nil {
log.WithError(err).Errorf("Unable to download: %s", filePath)
continue
}
case Torrent:
if err = item.Torrent(ctx, feed.DownloadPath); err != nil {
log.WithError(err).Errorf("Unable to torrent: %s", name)
continue
}
case Skip:
// add to cache and do nothing
ctx.Cache.AddItem(item)
continue
case SkipOnce:
continue // don't add to the cache
case Quit:
quit = true
default:
log.Errorf("Unknown reply")
}
} else {
if err = item.Torrent(ctx, feed.DownloadPath); err != nil {
log.WithError(err).Errorf("Unable to torrent: %s", name)
continue
}
}
}
}
cacheTime := time.Duration(ctx.Konf.Int("SeenCacheDays")) * time.Duration(24) * time.Hour
if err = ctx.Cache.SaveCache(cacheTime); err != nil {
return fmt.Errorf("Unable to save seen cache: %s", err.Error())
}
return nil
}
type selectOptions struct {
Name string
Value SelectType
}
type SelectType int
const (
Torrent SelectType = iota
Download
Skip
SkipOnce
Quit
)
var selectItems = []selectOptions{
{
Name: "Torrent",
Value: Torrent,
},
{
Name: "Download",
Value: Download,
},
{
Name: "Skip",
Value: Skip,
},
{
Name: "Skip Once",
Value: SkipOnce,
},
{
Name: "Quit",
Value: Quit,
},
}
func makeSelectTemplate(label string) *promptui.SelectTemplates {
return &promptui.SelectTemplates{
Label: "{{ . }}",
Active: promptui.IconSelect + " {{ .Name | cyan }}",
Inactive: " {{ .Name }}",
Selected: promptui.IconGood + fmt.Sprintf(" %s {{ .Name }}", label),
}
}
func prompt(feed, name string) SelectType {
var i int
var err error
label := fmt.Sprintf("[%s] download %s?", feed, name)
sel := promptui.Select{
Label: label,
Items: selectItems,
Stdout: &BellSkipper{},
HideSelected: false,
Templates: makeSelectTemplate(label),
}
if i, _, err = sel.Run(); err != nil {
log.WithError(err).Fatalf("Unable to select option")
}
return selectItems[i].Value
}
/*
* BellSkipper implements an io.WriteCloser that skips the terminal bell
* character (ASCII code 7), and writes the rest to os.Stderr. It is used to
* replace readline.Stdout, that is the package used by promptui to display the
* prompts.
*
* This is a workaround for the bell issue documented in
* https://github.com/manifoldco/promptui/issues/49#issuecomment-573814976
*/
type BellSkipper struct{}
// Write implements an io.WriterCloser over os.Stderr, but it skips the terminal
// bell character.
func (bs *BellSkipper) Write(b []byte) (int, error) {
const charBell = 7 // c.f. readline.CharBell
if len(b) == 1 && b[0] == charBell {
return 0, nil
}
return os.Stderr.Write(b)
}
// Close implements an io.WriterCloser over os.Stderr.
func (bs *BellSkipper) Close() error {
return os.Stderr.Close()
}