Skip to content

Commit

Permalink
Implement OPML importing and version printing
Browse files Browse the repository at this point in the history
  • Loading branch information
jafarlihi committed Nov 22, 2022
1 parent 528a8da commit 72bc579
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 9 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ You need to have Go >=1.19 installed.
`add [feed name] [feed url]`
- Adds a new feed to the config file

`import [OPML URL or file path]`
- Imports feeds from OPML file

`version`
- Prints the rssnix version

## Config

Config file is expected to be at `~/.config/rssnix/config.ini`.
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/PuerkitoBio/goquery v1.5.1 // indirect
github.com/andybalholm/cascadia v1.1.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/gilliek/go-opml v1.0.0 // indirect
github.com/go-ini/ini v1.67.0 // indirect
github.com/json-iterator/go v1.1.10 // indirect
github.com/mmcdole/gofeed v1.1.3 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHH
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gilliek/go-opml v1.0.0 h1:X8xVjtySRXU/x6KvaiXkn7OV3a4DHqxY8Rpv6U/JvCY=
github.com/gilliek/go-opml v1.0.0/go.mod h1:fOxmtlzyBvUjU6bjpdjyxCGlWz+pgtAHrHf/xRZl3lk=
github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
Expand Down
94 changes: 85 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,38 @@ import (
"errors"
"os"
"os/exec"
"strings"
"syscall"

"github.com/gilliek/go-opml/opml"
"github.com/go-ini/ini"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)

const Version = "0.2.0"

func addFeed(name string, url string) error {
homePath, err := os.UserHomeDir()
if err != nil {
log.Error("Failed to get home path")
os.Exit(1)
}
cfg, err := ini.Load(homePath + "/.config/rssnix/config.ini")
for _, key := range cfg.Section("feeds").Keys() {
if key.Name() == name {
return errors.New("Feed named '" + name + "' already exists")
}
}
file, err := os.OpenFile(homePath+"/.config/rssnix/config.ini", os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer file.Close()
_, err = file.WriteString("\n" + name + " = " + url)
return err
}

func main() {
syscall.Umask(0)
LoadConfig()
Expand Down Expand Up @@ -75,18 +101,68 @@ func main() {
if cCtx.Args().Len() != 2 {
return errors.New("exactly two arguments are required, first being feed name, second being URL")
}
homePath, err := os.UserHomeDir()
if err != nil {
log.Error("Failed to get home path")
os.Exit(1)
return addFeed(cCtx.Args().Get(0), cCtx.Args().Get(1))
},
},
{
Name: "import",
Aliases: []string{"i"},
Usage: "import an OPML file",
Action: func(cCtx *cli.Context) error {
if cCtx.Args().Len() != 1 {
return errors.New("argument specifying OPML file path or URL is required")
}
file, err := os.OpenFile(homePath+"/.config/rssnix/config.ini", os.O_APPEND|os.O_WRONLY, 0644)
doc, err := opml.NewOPMLFromFile(cCtx.Args().Get(0))
if err != nil {
return err
doc, err = opml.NewOPMLFromURL(cCtx.Args().Get(0))
if err != nil {
return err
}
}
defer file.Close()
_, err = file.WriteString("\n" + cCtx.Args().Get(0) + " = " + cCtx.Args().Get(1))
return err
for _, outline := range doc.Body.Outlines {
if len(outline.XMLURL) > 0 {
var title string
if len(outline.Title) > 0 {
title = outline.Title
} else if len(outline.Text) > 0 {
title = outline.Text
} else {
continue
}
err = addFeed(strings.ReplaceAll(title, " ", "-"), outline.XMLURL)
if err != nil {
log.Error("Failed to add feed titled '" + title + "', error: " + err.Error())
continue
}
}
for _, innerOutline := range outline.Outlines {
if len(innerOutline.XMLURL) > 0 {
var title string
if len(outline.Title) > 0 {
title = outline.Title
} else if len(outline.Text) > 0 {
title = outline.Text
} else {
continue
}
err = addFeed(strings.ReplaceAll(title, " ", "-"), innerOutline.XMLURL)
if err != nil {
log.Error("Failed to add feed titled '" + title + "', error: " + err.Error())
continue
}
}
}
}
return nil
},
},
{
Name: "version",
Aliases: []string{"v"},
Usage: "display the version",
Action: func(cCtx *cli.Context) error {
log.Info(Version)
return nil
},
},
},
Expand Down

0 comments on commit 72bc579

Please sign in to comment.