hlfw.ca

ytcli

Download patch

ref: 815776f2cbda64d9061a291ad54e3adf7a2a3bed
parent: 11b052ccd83a1c69f08fd952a3aedec532acaf72
author: halfwit <michaelmisch1985@gmail.com>
date: Mon Jan 14 12:00:06 PST 2019

Initial work on a Go binary

--- a/README.md
+++ b/README.md
@@ -1,72 +1,58 @@
-# Small Script To Interact With Youtube
+# ytcli - POSIX sh
 
-## Usage
+## Installation:
 
-```
-ytcli [-t] [action]
+	just include in your path. 
 
-Actions:
-	search	<keyword>
-		will return a list of URLs and descritions
-	user	<username>
-		will return a list of videos by user
-	playlist
-		will return a list of playlists
-	channel	<channel name>
-		will return a list of videos by channel
-	channel-id <channel name>
-		will return channel id of given channel
-	-t 
-		return thumbnail as well
-```
+## Usage: 
+	
+	ytcli [search|playlist|user|channel] [ -t ] KEYWORD
+	ytcli [channel-id] KEYWORD
 
-## Configuration
+# ytcli - Go
 
-ytcli assumes that you have an API token for using the v3 Youtube API
+## Building: 
 
-Example: 
+	`go get github.com/halfwit/ytcli`
 
-```
-# Configuration .local/cfg/ytcli.conf
-# Lines starting with # will be ignored
-key=4TNSHS543tn5hNSTH54354
-max_results=50
-```
+## Installation:
 
-## Example
+	`go install github.com/halfwit/ytcli`
+	
 
-```
-ytcli search -t koalas
+Requests:
 
-Cutest Koala Videos EVER
-https://www.youtube.com/watch?v=S4UG_l-CHF4
-https://i.ytimg.com/vi/S4UG_l-CHF4/hqdefault.jpg
-Koala Gets Kicked Out Of Tree and Cries!
-https://www.youtube.com/watch?v=O0cAx1jLbJk
-https://i.ytimg.com/vi/O0cAx1jLbJk/hqdefault.jpg
-Cute Koalas Playing 🐨 Funny Koala Bears [Funny Pets]
-https://www.youtube.com/watch?v=1R-QFQGWYuc
-https://i.ytimg.com/vi/1R-QFQGWYuc/hqdefault.jpg
-10 Fun Facts About Koalas
-https://www.youtube.com/watch?v=hoataOsqfhc
-https://i.ytimg.com/vi/hoataOsqfhc/hqdefault.jpg
-Koala fight
-https://www.youtube.com/watch?v=djK_ucSYpaw
-https://i.ytimg.com/vi/djK_ucSYpaw/hqdefault.jpg
-Koala Gives Stinky Hugs!
-https://www.youtube.com/watch?v=pw-Njg0Hess
-https://i.ytimg.com/vi/pw-Njg0Hess/hqdefault.jpg
-Fighting Koalas Shooed From Highway by Concerned Motorist
-https://www.youtube.com/watch?v=eSrpNwZUeQY
-https://i.ytimg.com/vi/eSrpNwZUeQY/hqdefault.jpg
-Cute Adorable Koala takes water from human
-https://www.youtube.com/watch?v=RXDLodcCRuM
-https://i.ytimg.com/vi/RXDLodcCRuM/hqdefault.jpg
-koala joey's most adorable home video of all time
-https://www.youtube.com/watch?v=cU8v4vZbFPc
-https://i.ytimg.com/vi/cU8v4vZbFPc/hqdefault.jpg
-La vida secreta de los koalas: LA ÉPOCA DE REPRODUCCIÓN | Grandes documentales
-https://www.youtube.com/watch?v=9F9zhc8F6Go
-https://i.ytimg.com/vi/9F9zhc8F6Go/hqdefault.jpg
-[...]
 ```
+# Usage
+ ytcli [ -trpucfa ] KEYWORD
+	
+# Search for a list of videos matching string
+ytcli <string>
+
+# Search for videos related to url
+ytcli -r 'https://youtube.com/someurl'
+
+# Search for playlists matching strings
+ytcli -p <string>
+
+# List of videos by user
+ytcli -u <user>
+
+# List of playlits by user
+ytcli -u -p <user>
+
+# List of videos by channel
+ytcli -c <channel>
+
+
+# Link to channels' RSS feed
+ytcli -f <channel>
+
+# Normally ytcli will query the factotum for an API key (this requires plan9port, plan9, etc)
+# To set a key explicitely:
+ytcli -a myapikey
+
+# Include thumbnails
+-t 
+
+
--- /dev/null
+++ b/call.go
@@ -1,0 +1,94 @@
+package main
+
+import (
+	"flag"
+	"log"
+	"strings"
+
+	"google.golang.org/api/youtube/v3"
+)
+
+func playlistSearch(query string, service *youtube.Service) map[string]string {
+	results := make(map[string]string)
+	call := service.Search.List("id,snippet").Q(query).MaxResults(*nresults)
+	response, err := call.Do()
+	if err != nil {
+		log.Fatal(err)
+	}
+	for _, item := range response.Items {
+		if item.Id.Kind == "youtube#playlist" {
+			results["https://youtu.be/" + item.Id.PlaylistId] = item.Snippet.Title
+		}
+	}
+	return results
+}
+
+func channelSearch(query string, service *youtube.Service) map[string]string {
+	// Fetch channel ID
+	results := make(map[string]string)
+	call := service.Search.List("id,snippet").Q(query).MaxResults(*nresults)
+	id, err := call.Do()
+	if err != nil {
+		log.Fatal(err)
+	}
+	call2 := service.Search.List("id,snippet").MaxResults(*nresults).ChannelId(id.Items[0].Id.ChannelId)
+	response, err := call2.Do()
+	if err != nil {
+		log.Fatal(err)
+	}
+	for _, item := range response.Items {
+		if item.Id.Kind == "youtube#video" {
+			results["https://youtu.be/" + item.Id.VideoId] = item.Snippet.Title
+		}
+	}
+	return results
+}
+
+func normalSearch(query string, service *youtube.Service) map[string]string {
+	results := make(map[string]string)
+	call := service.Search.List("id,snippet").Q(query).MaxResults(*nresults)
+	response, err := call.Do()
+	if err != nil {
+		log.Fatal(err)
+	}
+	for _, item := range response.Items {
+		if item.Id.Kind == "youtube#video" {
+			results["https://youtu.be/" + item.Id.VideoId] = item.Snippet.Title
+		}
+	}
+	return results
+}
+
+func feedSearch(query string, service *youtube.Service) {
+	//call := service.Search.Channel(query)
+	//response, err := call.Do()
+	//if err != nil {
+	//	log.Fatal(err)
+	//}
+	//url := "https://youtube.com/feeds/videos.xml?channel_id"
+	//fmt.Printf("%s=%v", url, item.Id.ChannelId)
+}
+
+func runCommand(service *youtube.Service) map[string]string {
+	query := strings.Join(flag.Args(), " ")
+	switch {
+	case *feed:
+		if *channel {
+			feedSearch(query, service)
+			return nil
+		}
+		//if user {
+			//feedearch(query, service)
+			//return nil
+		//}
+		//else {
+			//parse URL for ID 
+		//}
+	case *playlist:
+		return playlistSearch(query, service)
+	case *channel:
+		return channelSearch(query, service)
+
+	}
+	return normalSearch(query, service)
+}
--- /dev/null
+++ b/ytcli.go
@@ -1,0 +1,60 @@
+package main
+
+import (
+	"flag"
+	"fmt"
+	"log"
+	"os"
+	"os/user"
+	"net/http"
+
+	"google.golang.org/api/googleapi/transport"
+	"google.golang.org/api/youtube/v3"
+	"bitbucket.org/mischief/libauth"
+)
+
+var (
+	//related  = flag.String("r", "", "List videos related to <URL>")
+	//username = flag.String("u", "", "List videos uploaded by user")
+	apiKey	 = flag.String("a", "", "Use API key instead of factotum")
+	nresults = flag.Int64("m", 50, "Number of results per query")
+	playlist = flag.Bool("p", false, "List playlists that match query")
+	channel  = flag.Bool("c", false, "List videos uploaded by channel")
+	//thumbs   = flag.Bool("t", false, "Return link to thumbnail as well")
+	feed     = flag.Bool("f", false, "Return link to RSS feed of user/channel (Can be used with -c, -u, or a video URL)")
+)
+
+
+
+func main() {
+	flag.Parse()
+	if flag.Lookup("h") != nil {
+		flag.Usage()
+		os.Exit(1)
+	}
+	if *apiKey == "" {
+		u, err := user.Current()
+		if err != nil {
+			log.Fatal(err)
+		}
+		key, err := libauth.Getuserpasswd( "proto=pass service=ytcli user=%s", 
+			u.Username,
+		)
+		if err != nil {
+			log.Fatal(err)
+		}
+		*apiKey = key.Password
+	}
+	client := &http.Client{
+		Transport: &transport.APIKey{ Key: *apiKey },
+	}
+	service, err := youtube.New(client)
+	if err != nil {
+		log.Fatalf("Error with youtube: %v", err)
+	}
+	results := runCommand(service)
+
+	for id, title := range results {
+		fmt.Printf("%s - %v\n", title, id)
+	}
+}