-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
70 lines (59 loc) · 1.5 KB
/
search.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"google.golang.org/api/googleapi/transport"
"google.golang.org/api/youtube/v3"
)
const maxResults = 50
func YTSearch(query string) (string, error) {
client := &http.Client{
Transport: &transport.APIKey{Key: YoutubeKey},
}
service, err := youtube.New(client)
if err != nil {
return "", errors.New("Error creating new YouTube client: " + err.Error())
}
// Make the API call to YouTube.
call := service.Search.List("id,snippet").
Q(query).
MaxResults(maxResults)
response, err := call.Do()
if err != nil {
return "", errors.New("Error making search API call: " + err.Error())
}
// Group video, channel, and playlist results in separate lists.
videos := make(map[string]string)
// Iterate through each item and add it to the correct list.
for _, item := range response.Items {
switch item.Id.Kind {
case "youtube#video":
videos[item.Id.VideoId] = item.Snippet.Title
default:
continue
}
}
for _, item := range response.Items {
if strings.Contains(item.Id.Kind, "video") {
return item.Id.VideoId, nil
}
}
return "", errors.New("Video not found")
}
func DevKey() (token string) {
token = os.Getenv("YOUTUBE_TOKEN")
if (strings.Contains(token, "$") || token == "") && len(os.Args) > 1 {
token = os.Args[1]
} else {
b, err := ioutil.ReadFile("/youtube.txt")
if err != nil {
fmt.Println(err)
}
token = strings.TrimSpace(strings.Trim(strings.Split(string(b), ";")[0], "\n"))
}
return
}