go-ttrss

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

commit 978ea0724b5ef2afbb9748f79a187c03c6d0e1a5
parent a646d5e10b13f876aa24ab1aeec3940585d2053a
Author: Paco Esteban <paco@e1e0.net>
Date:   Fri, 31 Jul 2020 17:47:19 +0200

implement subscribe to feed

Diffstat:
Mcmd/ttrss-cli/main.go | 19++++++++++++++++++-
Mpkg/ttrss/ttrss.go | 32+++++++++++++++++++++++++++++---
2 files changed, 47 insertions(+), 4 deletions(-)

diff --git a/cmd/ttrss-cli/main.go b/cmd/ttrss-cli/main.go @@ -78,8 +78,25 @@ func main() { Name: "subscribe", Aliases: []string{"sub"}, Usage: "subscribe to feed", + Flags: []cli.Flag{ + &cli.IntFlag{ + Name: "c", + Aliases: []string{"cat", "category"}, + Value: -0, // Uncategorized + Usage: "Category id", + }, + &cli.StringFlag{ + Name: "url", + Usage: "Feed URL", + Required: true, + }, + }, Action: func(c *cli.Context) error { - ttrssCli.Subscribe() + id, err := ttrssCli.Subscribe(c.Int("c"), c.String("url")) + if err != nil { + log.Fatal(err) + } + fmt.Printf("Subscribed to feed %s with id %d\n", c.String("url"), id) return nil }, }, diff --git a/pkg/ttrss/ttrss.go b/pkg/ttrss/ttrss.go @@ -68,6 +68,15 @@ type Category struct { OrderId int `json:"order_id"` } +type FeedSub struct { + Status FeedStatus `json:"status"` +} + +type FeedStatus struct { + Code int `json:"code"` + FeedId int `json:"feed_id"` +} + // JSON response returned by the TTRSS API. type Resp struct { Seq int @@ -224,11 +233,28 @@ func (c *Client) List(category int) ([]Feed, error) { } // Subscribe to feed -func (c *Client) Subscribe() error { +func (c *Client) Subscribe(category int, url string) (int, error) { if err := c.login(); err != nil { - return err + return -1, err } - return nil + + feedMap := map[string]interface{}{ + "op": "subscribeToFeed", + "feed_url": url, + "category_id": category, + } + resp, err := c.apiCall(feedMap) + if err != nil { + return -1, err + } + + var feed FeedSub + if err = json.Unmarshal(resp.Content, &feed); err != nil { + err = fmt.Errorf("api error JSON unmarshall feeds: %v", err) + return -1, err + } + + return feed.Status.FeedId, nil } // Unsubscribe from feed