mfeed/main.go
2023-11-09 07:51:26 +01:00

132 lines
2.5 KiB
Go

package main
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"strconv"
miniflux "miniflux.app/client"
)
// Crreate the config struct
type Configuration struct {
Instance string
User string
Pass_Eval []string
}
func main() {
// 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),
)
// check if atleast one option is provided
if len(os.Args) < 2 {
fmt.Println("No option provided!")
return
}
// 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
func feeds(client *miniflux.Client) {
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)
}
}
// 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)
}