@@ -4,12 +4,38 @@ import (
4
4
"errors"
5
5
"os"
6
6
"os/exec"
7
+ "strings"
7
8
"syscall"
8
9
10
+ "github.com/gilliek/go-opml/opml"
11
+ "github.com/go-ini/ini"
9
12
log "github.com/sirupsen/logrus"
10
13
"github.com/urfave/cli/v2"
11
14
)
12
15
16
+ const Version = "0.2.0"
17
+
18
+ func addFeed (name string , url string ) error {
19
+ homePath , err := os .UserHomeDir ()
20
+ if err != nil {
21
+ log .Error ("Failed to get home path" )
22
+ os .Exit (1 )
23
+ }
24
+ cfg , err := ini .Load (homePath + "/.config/rssnix/config.ini" )
25
+ for _ , key := range cfg .Section ("feeds" ).Keys () {
26
+ if key .Name () == name {
27
+ return errors .New ("Feed named '" + name + "' already exists" )
28
+ }
29
+ }
30
+ file , err := os .OpenFile (homePath + "/.config/rssnix/config.ini" , os .O_APPEND | os .O_WRONLY , 0644 )
31
+ if err != nil {
32
+ return err
33
+ }
34
+ defer file .Close ()
35
+ _ , err = file .WriteString ("\n " + name + " = " + url )
36
+ return err
37
+ }
38
+
13
39
func main () {
14
40
syscall .Umask (0 )
15
41
LoadConfig ()
@@ -75,18 +101,68 @@ func main() {
75
101
if cCtx .Args ().Len () != 2 {
76
102
return errors .New ("exactly two arguments are required, first being feed name, second being URL" )
77
103
}
78
- homePath , err := os .UserHomeDir ()
79
- if err != nil {
80
- log .Error ("Failed to get home path" )
81
- os .Exit (1 )
104
+ return addFeed (cCtx .Args ().Get (0 ), cCtx .Args ().Get (1 ))
105
+ },
106
+ },
107
+ {
108
+ Name : "import" ,
109
+ Aliases : []string {"i" },
110
+ Usage : "import an OPML file" ,
111
+ Action : func (cCtx * cli.Context ) error {
112
+ if cCtx .Args ().Len () != 1 {
113
+ return errors .New ("argument specifying OPML file path or URL is required" )
82
114
}
83
- file , err := os . OpenFile ( homePath + "/.config/rssnix/config.ini" , os . O_APPEND | os . O_WRONLY , 0644 )
115
+ doc , err := opml . NewOPMLFromFile ( cCtx . Args (). Get ( 0 ) )
84
116
if err != nil {
85
- return err
117
+ doc , err = opml .NewOPMLFromURL (cCtx .Args ().Get (0 ))
118
+ if err != nil {
119
+ return err
120
+ }
86
121
}
87
- defer file .Close ()
88
- _ , err = file .WriteString ("\n " + cCtx .Args ().Get (0 ) + " = " + cCtx .Args ().Get (1 ))
89
- return err
122
+ for _ , outline := range doc .Body .Outlines {
123
+ if len (outline .XMLURL ) > 0 {
124
+ var title string
125
+ if len (outline .Title ) > 0 {
126
+ title = outline .Title
127
+ } else if len (outline .Text ) > 0 {
128
+ title = outline .Text
129
+ } else {
130
+ continue
131
+ }
132
+ err = addFeed (strings .ReplaceAll (title , " " , "-" ), outline .XMLURL )
133
+ if err != nil {
134
+ log .Error ("Failed to add feed titled '" + title + "', error: " + err .Error ())
135
+ continue
136
+ }
137
+ }
138
+ for _ , innerOutline := range outline .Outlines {
139
+ if len (innerOutline .XMLURL ) > 0 {
140
+ var title string
141
+ if len (outline .Title ) > 0 {
142
+ title = outline .Title
143
+ } else if len (outline .Text ) > 0 {
144
+ title = outline .Text
145
+ } else {
146
+ continue
147
+ }
148
+ err = addFeed (strings .ReplaceAll (title , " " , "-" ), innerOutline .XMLURL )
149
+ if err != nil {
150
+ log .Error ("Failed to add feed titled '" + title + "', error: " + err .Error ())
151
+ continue
152
+ }
153
+ }
154
+ }
155
+ }
156
+ return nil
157
+ },
158
+ },
159
+ {
160
+ Name : "version" ,
161
+ Aliases : []string {"v" },
162
+ Usage : "display the version" ,
163
+ Action : func (cCtx * cli.Context ) error {
164
+ log .Info (Version )
165
+ return nil
90
166
},
91
167
},
92
168
},
0 commit comments