go-ttrss

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

commit 1d02e108adeb19a0ce672ee3df2390b6e1e4cf09
parent c6c91ca286587fcf7c1ff9997dbaf970a3920ced
Author: Paco Esteban <paco@e1e0.net>
Date:   Fri, 31 Jul 2020 20:12:12 +0200

subscribe accepts youtube addresses now

It parses them and tries to get the channel id to subscribe to feed
automagically.

Diffstat:
Mcmd/ttrss-cli/main.go | 21+++++++++++++++++++--
Mpkg/ttrss/ttrss.go | 65+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 84 insertions(+), 2 deletions(-)

diff --git a/cmd/ttrss-cli/main.go b/cmd/ttrss-cli/main.go @@ -17,6 +17,8 @@ import ( "fmt" "log" "os" + "regexp" + "strconv" "git.e1e0.net/go-ttrss/pkg/ttrss" "github.com/urfave/cli/v2" @@ -32,7 +34,7 @@ func main() { app := &cli.App{ Name: "ttrss-cli", Usage: "interact with your TTRSS instance from the command line", - Version: "v0.1", + Version: "v0.2", Commands: []*cli.Command{ { Name: "list", @@ -92,7 +94,22 @@ func main() { }, }, Action: func(c *cli.Context) error { - id, err := ttrssCli.Subscribe(c.Int("c"), c.String("url")) + match, err := regexp.MatchString(`https://.*\.youtube\..*`, c.String("url")) + if err != nil { + log.Print(err) + } + var id int + var ytCatId int + if match { + ytCatId, err = strconv.Atoi(os.Getenv("TTRSS_CAT_ID")) + if err != nil { + log.Print(err) + } + id, err = ttrssCli.SubscribeYoutube(ytCatId, c.String("url")) + } else { + id, err = ttrssCli.Subscribe(c.Int("c"), c.String("url")) + } + if err != nil { log.Fatal(err) } diff --git a/pkg/ttrss/ttrss.go b/pkg/ttrss/ttrss.go @@ -23,6 +23,7 @@ import ( "log" "net/http" "os" + "regexp" "strings" "time" ) @@ -257,6 +258,70 @@ func (c *Client) Subscribe(category int, url string) (int, error) { return feed.Status.FeedId, nil } +// Subscribe to youtube feed +func (c *Client) SubscribeYoutube(category int, url string) (int, error) { + var channelId string + // get the channel id + // things can get nasty here depending on how the url looks like + re := regexp.MustCompile(`https://.*\.youtube\.com\/channel\/(.*)`) + match := re.FindStringSubmatch(url) + if len(match) > 1 { + channelId = match[1] + } + + re = regexp.MustCompile(`https://.*\.youtube\.com\/c\/.*`) + if len(re.FindString(url)) > 0 { + // call the youtube url + resp, err := http.Get(url) + if err != nil { + return -1, fmt.Errorf("cannot get url %s: %v", url, err) + } + defer resp.Body.Close() + // Read response data in to memory + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return -1, fmt.Errorf("Error reading HTTP body from %s: %v ", url, err) + } + re = regexp.MustCompile(`,"externalId":"([^"]*)",`) + match = re.FindStringSubmatch(string(body)) + if len(match) > 1 { + channelId = match[1] + } + } + + re = regexp.MustCompile(`https://.*\.youtube\.com\/watch\?v=.*`) + if len(re.FindString(url)) > 0 { + // call the youtube url + resp, err := http.Get(url) + if err != nil { + return -1, fmt.Errorf("cannot get url %s: %v", url, err) + } + defer resp.Body.Close() + // Read response data in to memory + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return -1, fmt.Errorf("Error reading HTTP body from %s: %v ", url, err) + } + re = regexp.MustCompile(`,\\"channelId\\":\\"([^"]*)\\",`) + match = re.FindStringSubmatch(string(body)) + if len(match) > 1 { + channelId = match[1] + } + } + + // call subscribe + if len(channelId) < 1 { + return -1, fmt.Errorf("channel ID not found") + } + + channelId = "https://www.youtube.com/feeds/videos.xml?channel_id=" + channelId + id, err := c.Subscribe(category, channelId) + if err != nil { + return -1, err + } + return id, nil +} + // Unsubscribe from feed func (c *Client) Unsubscribe(id int) error { if err := c.login(); err != nil {