go-ttrss

incomplete CLI interface to manage your TTRSS account
git clone https://git.e1e0.net/go-ttrss.git
Log | Files | Refs | README

main.go (3636B)


      1 // Copyright (c) 2020 Paco Esteban <paco@e1e0.net>
      2 
      3 // Permission to use, copy, modify, and distribute this software for any
      4 // purpose with or without fee is hereby granted, provided that the above
      5 // copyright notice and this permission notice appear in all copies.
      6 
      7 // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
      8 // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
      9 // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     10 // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     11 // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     12 // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     13 // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     14 package main
     15 
     16 import (
     17 	"fmt"
     18 	"log"
     19 	"os"
     20 	"regexp"
     21 	"strconv"
     22 
     23 	"git.e1e0.net/go-ttrss/pkg/ttrss"
     24 	"github.com/urfave/cli/v2"
     25 )
     26 
     27 func main() {
     28 
     29 	ttrssCli, err := ttrss.New()
     30 	if err != nil {
     31 		log.Fatal(err)
     32 	}
     33 
     34 	app := &cli.App{
     35 		Name:    "ttrss-cli",
     36 		Usage:   "interact with your TTRSS instance from the command line",
     37 		Version: "v0.2",
     38 		Commands: []*cli.Command{
     39 			{
     40 				Name:    "list",
     41 				Aliases: []string{"ls"},
     42 				Usage:   "list feeds",
     43 				Flags: []cli.Flag{
     44 					&cli.IntFlag{
     45 						Name:    "c",
     46 						Aliases: []string{"cat", "category"},
     47 						Value:   -3, // all feeds by default
     48 						Usage:   "Category id",
     49 					},
     50 				},
     51 				Action: func(c *cli.Context) error {
     52 					feeds, err := ttrssCli.List(c.Int("c"))
     53 					if err != nil {
     54 						log.Fatal(err)
     55 					}
     56 
     57 					for _, feed := range feeds {
     58 						fmt.Printf("(%5d)\t%s \n", feed.Id, feed.Title)
     59 					}
     60 					return nil
     61 				},
     62 			},
     63 			{
     64 				Name:    "categories",
     65 				Aliases: []string{"cat"},
     66 				Usage:   "list categories",
     67 				Action: func(c *cli.Context) error {
     68 					categories, err := ttrssCli.Categories()
     69 					if err != nil {
     70 						log.Fatal(err)
     71 					}
     72 
     73 					for _, cat := range categories {
     74 						fmt.Printf("(%5d)\t%s \n", cat.Id, cat.Title)
     75 					}
     76 					return nil
     77 				},
     78 			},
     79 			{
     80 				Name:    "subscribe",
     81 				Aliases: []string{"sub"},
     82 				Usage:   "subscribe to feed",
     83 				Flags: []cli.Flag{
     84 					&cli.IntFlag{
     85 						Name:    "c",
     86 						Aliases: []string{"cat", "category"},
     87 						Value:   -0, // Uncategorized
     88 						Usage:   "Category id",
     89 					},
     90 					&cli.StringFlag{
     91 						Name:     "url",
     92 						Usage:    "Feed URL",
     93 						Required: true,
     94 					},
     95 				},
     96 				Action: func(c *cli.Context) error {
     97 					match, err := regexp.MatchString(`https://.*\.youtube\..*`, c.String("url"))
     98 					if err != nil {
     99 						log.Print(err)
    100 					}
    101 					var id int
    102 					var ytCatId int
    103 					if match {
    104 						ytCatId, err = strconv.Atoi(os.Getenv("TTRSS_CAT_ID"))
    105 						if err != nil {
    106 							log.Print(err)
    107 						}
    108 						id, err = ttrssCli.SubscribeYoutube(ytCatId, c.String("url"))
    109 					} else {
    110 						id, err = ttrssCli.Subscribe(c.Int("c"), c.String("url"))
    111 					}
    112 
    113 					if err != nil {
    114 						log.Fatal(err)
    115 					}
    116 					fmt.Printf("Subscribed to feed %s with id %d\n", c.String("url"), id)
    117 					return nil
    118 				},
    119 			},
    120 			{
    121 				Name:    "unsubscribe",
    122 				Aliases: []string{"rm", "unsub"},
    123 				Usage:   "unsubscribe from feed",
    124 				Flags: []cli.Flag{
    125 					&cli.IntFlag{
    126 						Name:     "id",
    127 						Aliases:  []string{"i"},
    128 						Usage:    "Feed id",
    129 						Required: true,
    130 					},
    131 				},
    132 				Action: func(c *cli.Context) error {
    133 					err = ttrssCli.Unsubscribe(c.Int("id"))
    134 					if err != nil {
    135 						log.Fatal(err)
    136 					}
    137 					fmt.Printf("Unsubscribed from feed %d\n", c.Int("id"))
    138 					return nil
    139 				},
    140 			},
    141 		},
    142 	}
    143 
    144 	err = app.Run(os.Args)
    145 	if err != nil {
    146 		log.Fatal(err)
    147 	}
    148 }