ref: 119516fa88ef2d320d8e4ef12e0af257747f8a82
parent: 9a326ae49510014a9e9cadd4db4b6139a31e4be4
author: halfwit <michaelmisch1985@gmail.com>
date: Mon Jan 14 17:05:53 PST 2019
Add call.go
--- a/README.md
+++ b/README.md
@@ -24,29 +24,31 @@
```
# Usage
- ytcli [ -trpucfa ] KEYWORD
+ ytcli [ -trpucf ] [ -a apikey ] KEYWORD
# Search for a list of videos matching string
-ytcli <string>
+ytcli <query>
# Search for videos related to url
ytcli -r 'https://youtube.com/someurl'
# Search for playlists matching strings
-ytcli -p <string>
+ytcli -p <query>
# List of videos by user
-ytcli -u <user>
+ytcli -u <user name>
# List of playlits by user
-ytcli -u -p <user>
+ytcli -u -p <user name>
# List of videos by channel
-ytcli -c <channel>
+ytcli -c <channel name>
+# List of playlists by channel
+ytcli -c -p <channel name>
# Link to channels' RSS feed
-ytcli -f <channel>
+ytcli -f <channel name>
# Normally ytcli will query the factotum for an API key (this requires plan9port, plan9, etc)
# To set a key explicitely:
@@ -55,4 +57,5 @@
# Include thumbnails
-t
+```
--- /dev/null
+++ b/TODO
@@ -1,0 +1,1 @@
+Helper functions
--- a/call.go
+++ b/call.go
@@ -9,6 +9,8 @@
"google.golang.org/api/youtube/v3"
)
+var yt = "https://youtu.be/"
+
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)
@@ -18,7 +20,12 @@
}
for _, item := range response.Items {
if item.Id.Kind == "youtube#playlist" {
- results["https://youtu.be/" + item.Id.PlaylistId] = item.Snippet.Title
+ if *thumbs {
+ key := yt + item.Id.PlaylistId + " " + item.Snippet.Thumbnails.Default.Url
+ results[key] = item.Snippet.Title
+ } else {
+ results[yt + item.Id.PlaylistId] = item.Snippet.Title
+ }
}
}
return results
@@ -39,7 +46,12 @@
}
for _, item := range response.Items {
if item.Id.Kind == "youtube#video" {
- results["https://youtu.be/" + item.Id.VideoId] = item.Snippet.Title
+ if *thumbs {
+ key := yt + item.Id.VideoId + " " + item.Snippet.Thumbnails.Default.Url
+ results[key] = item.Snippet.Title
+ } else {
+ results[yt + item.Id.VideoId] = item.Snippet.Title
+ }
}
}
return results
@@ -54,7 +66,12 @@
}
for _, item := range response.Items {
if item.Id.Kind == "youtube#video" {
- results["https://youtu.be/" + item.Id.VideoId] = item.Snippet.Title
+ if *thumbs {
+ key := yt + item.Id.VideoId + " " + item.Snippet.Thumbnails.Default.Url
+ results[key] = item.Snippet.Title
+ } else {
+ results[yt + item.Id.VideoId] = item.Snippet.Title
+ }
}
}
return results
@@ -79,11 +96,11 @@
r := regexp.MustCompile(`^((?:https?:)?\/\/)?((?:www|m)\.)?((?:youtube\.com|youtu.be))(\/(?:[\w\-]+\?v=|embed\/|v\/)?)([\w\-]+)(\S+)?$`)
vid := r.FindStringSubmatch(query)
call := service.Videos.List("id,snippet").Id(vid[5])
- id, err := call.Do()
+ response, err := call.Do()
if err != nil {
log.Fatal(err)
}
- for _, item := range id.Items {
+ for _, item := range response.Items {
results[feed + item.Snippet.ChannelId] = item.Snippet.ChannelTitle
}
}
@@ -95,12 +112,17 @@
r := regexp.MustCompile(`^((?:https?:)?\/\/)?((?:www|m)\.)?((?:youtube\.com|youtu.be))(\/(?:[\w\-]+\?v=|embed\/|v\/)?)([\w\-]+)(\S+)?$`)
vid := r.FindStringSubmatch(query)
call := service.Search.List("id,snippet").RelatedToVideoId(vid[5]).MaxResults(*nresults).Type("video")
- id, err := call.Do()
+ response, err := call.Do()
if err != nil {
log.Fatal(err)
}
- for _, item := range id.Items {
- results[item.Id.VideoId] = item.Snippet.Title
+ for _, item := range response.Items {
+ if *thumbs {
+ key := yt + item.Id.VideoId + " " + item.Snippet.Thumbnails.Default.Url
+ results[key] = item.Snippet.Title
+ } else {
+ results[yt + item.Id.VideoId] = item.Snippet.Title
+ }
}
return results
}
@@ -116,6 +138,7 @@
return channelSearch(query, service)
case *related:
return relatedSearch(query, service)
+ case *username:
}
return normalSearch(query, service)
--- a/ytcli.go
+++ b/ytcli.go
@@ -14,14 +14,15 @@
)
var (
- //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")
+ username = flag.Bool("u", false, "List videos uploaded by user")
channel = flag.Bool("c", false, "List videos uploaded by channel")
- //thumbs = flag.Bool("t", false, "Return link to thumbnail as well")
related = flag.Bool("r", false, "List videos related to <URL>")
feed = flag.Bool("f", false, "Return link to RSS feed of user/channel (Can be used with -c, -u, or a video URL)")
+ thumbs = flag.Bool("t", false, "Return link to thumbnail as well")
+
)