mfeed/main.go

169 lines
3.4 KiB
Go
Raw Normal View History

2023-11-09 07:51:26 +01:00
package main
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"strconv"
miniflux "miniflux.app/client"
)
// Help message
const HELP = `examples:
mfeed create {category_id} {feed_url}
mfeed feeds {category_id}
usage: mfeed [-h] {feeds,categories,create} ...
feeds list all the feeds you have
categories list all the categories you have
create create a new feed
positional arguments:
{feeds, categories, create}
options:
-h, --help shows this help page`
2023-11-09 07:51:26 +01:00
// Crreate the config struct
type Configuration struct {
Instance string
User string
Pass_Eval []string
}
func main() {
// print help message based on first argument
if len(os.Args) < 2 {
fmt.Println(HELP)
return
} else if os.Args[1] == "-h" {
fmt.Println(HELP)
return
} else if os.Args[1] == "--help" {
fmt.Println(HELP)
return
}
2023-11-09 07:51:26 +01:00
// get config folder locaation
config_dir, _ := os.UserConfigDir()
// Load the config file
content, err := os.ReadFile(config_dir + "/mfeed/config.json")
if err != nil {
panic(err)
}
// load configuration file
var config Configuration
err = json.Unmarshal(content, &config)
if err != nil {
panic(err)
}
// fmt.Println(dirname)
cmd := config.Pass_Eval[0]
args := config.Pass_Eval[1:]
// load the password from the provided command
pass, err := exec.Command(cmd, args...).Output()
if err != nil {
panic(err)
}
// Connect to the miniflux instance
client := miniflux.New(
config.Instance,
config.User,
string(pass),
)
// Run fuction based on first argument
switch os.Args[1] {
case "feeds":
feeds(client)
case "categories":
categories(client)
case "create":
create(client)
default:
fmt.Println("This option doesn't exist")
}
return
}
// Print out all your feeds and their id's, or feeds in a category if provided
2023-11-09 07:51:26 +01:00
func feeds(client *miniflux.Client) {
if len(os.Args) < 3 {
feeds, err := client.Feeds()
if err != nil {
fmt.Println(err)
return
}
for _, feed := range feeds {
fmt.Printf("%d\t%s\n", feed.ID, feed.Title)
}
2023-11-09 07:51:26 +01:00
return
} else if len(os.Args) > 3 {
fmt.Println("Too many arguments provided!")
return
} else {
category, err := strconv.Atoi(os.Args[2])
if err != nil {
panic(err)
}
feeds, err := client.CategoryFeeds(int64(category))
for _, feed := range feeds {
fmt.Printf("%d\t%s\n", feed.ID, feed.Title)
}
2023-11-09 07:51:26 +01:00
}
}
// Print out all your categories and their id's
func categories(client *miniflux.Client) {
categories, err := client.Categories()
if err != nil {
fmt.Println(err)
return
}
for _, category := range categories {
fmt.Printf("%d\t%s\n", category.ID, category.Title)
}
}
// Create a feed based on the category number and feed url
func create(client *miniflux.Client) {
// check if feed category was provided
if len(os.Args) < 3 {
panic("Feed category not provided")
}
category, err := strconv.Atoi(os.Args[2])
if err != nil {
panic(err)
}
// check if feed url was provided
if len(os.Args) < 4 {
panic("Feed url not provided")
}
// Create feed request and send it
var feed_create_request miniflux.FeedCreationRequest
feed_create_request.FeedURL = os.Args[3]
feed_create_request.CategoryID = int64(category)
// Crete the feed
id, err := client.CreateFeed(&feed_create_request)
if err != nil {
panic(err)
}
// Get the new feed and prits its name and id
feed, err := client.Feed(id)
if err != nil {
panic(err)
}
fmt.Printf("Feed '%s' with id '%d' was created\n", feed.Title, feed.ID)
}