~nature/yt-cli

eef54d4d1b59a10187f3abaa9d5e980d30b3270b — LordNature 1 year, 5 months ago
main.go: Added initial framework.

This is just the structure for now. It has the ability of fetching the XML data
and sorting the data. Just have to add features and integrate MPV into it.
1 files changed, 95 insertions(+), 0 deletions(-)

A main.go
A  => main.go +95 -0
@@ 1,95 @@
package main

import (
	"os"
	"os/exec"
	"io"
	"net/http"
	"encoding/xml"
	"io/ioutil"
	"fmt"
	"strings"
)

type Channel struct {
	ID string `xml:"yt:channelId"`
	Name string `xml:"title"`
	Videos []Video `xml:"entry"`
}

type Video struct {
	XMLName xml.Name `xml:"entry"`
	ID string `xml:"videoId"`
	Title string `xml:"title"`
	Date string `xml:"published"`
}

const usage string = `Usage: yt.go [option] <selection>
add <channelID>    Adds Channel ID to subscription list.
list               Lists all videos from subscriptions.
help               Prints usage.
`

func main() {
	name := os.Args[0]
	fmt.Println(name)

	switch os.Args[1] {
	case "add":
		test, _ := readSubs()
		addChannel(test)
	case "list":
	case "help":
		fallthrough
	default:
		fmt.Printf("%s", usage)
	}
}

func readSubs() (channels []string, err error) {
	// hardcoded for now
	data, err := ioutil.ReadFile("subs")
	if err != nil {
		return
	}

	channels = strings.Split(string(data), "\n")

	return
}

func addChannel(subs []string) {
	for _, k := range subs {
		fmt.Println(k)
	}

	url := "https://www.youtube.com/feeds/videos.xml?channel_id=ID_HERE"
	req, _ := http.NewRequest("GET", url, nil)
	res, _ := http.DefaultClient.Do(req)
	defer res.Body.Close()
	body, _ := ioutil.ReadAll(res.Body)

	var channel Channel
	xml.Unmarshal(body, &channel)


	var test string
	for _, v := range channel.Videos {
		fmt.Printf("[%.10s] %.20s - %.50s\n", v.Date, channel.Name, v.Title)
		test += v.ID + "\n"
	}
	dmenuList(test)
}

func dmenuList(input string) {
	cmd := exec.Command("dmenu", "-l", "10")
	stdin, _ := cmd.StdinPipe()
	go func() {
		defer stdin.Close()
		io.WriteString(stdin, input)
	}()

	out, _ := cmd.CombinedOutput()

	fmt.Printf("%s\n", out)
}