This repository has been archived by the owner on Feb 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutube_handler.go
76 lines (64 loc) · 1.63 KB
/
youtube_handler.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
71
72
73
74
75
76
package youtube
import (
"context"
"flag"
log "github.com/sirupsen/logrus"
"google.golang.org/api/option"
"os"
"google.golang.org/api/youtube/v3"
)
var (
maxResults = flag.Int64("max-results", 5, "Max YouTube results")
Yservice *youtube.Service
)
// ClientInit initialize a youtube service.
// Assign Yservice global variable to fetched service
func ClientInit() {
youtubeToken, exist := os.LookupEnv("YOUTUBE_TOKEN")
if !exist {
log.Fatal("Missing environment variable : YOUTUBE_TOKEN")
}
flag.Parse()
ctx := context.Background()
opt := option.WithAPIKey(youtubeToken)
service, err := youtube.NewService(ctx, opt)
if err != nil {
log.Fatalf("Error creating new YouTube client: %v", err)
}
Yservice = service
}
// SearchByKeyword let you search a youtube video from keywords.
// Takes a keyword as argument.
// Return a pointer of an Video array.
func SearchByKeywords(keyword string) *[]Video {
var parts []string
parts = append(parts, "id")
parts = append(parts, "snippet")
// Make the API call to YouTube.
//query := flag.String("query", "Google", "Search term")
call := Yservice.Search.List(parts).
Q(keyword).
MaxResults(*maxResults)
response, err := call.Do()
if err != nil {
return nil
}
var videos []Video
// Iterate through each item and add it to the correct list.
for _, item := range response.Items {
switch item.Id.Kind {
case "youtube#video":
video := Video{
Id: item.Id.VideoId,
Title: item.Snippet.Title,
}
videos = append(videos, video)
}
}
return &videos
}
// Video object to reflect the data gathered from the api.
type Video struct {
Id string
Title string
}