-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
62 lines (50 loc) · 1.17 KB
/
main.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
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"google.golang.org/api/option"
"google.golang.org/api/youtube/v3"
)
// YTVideo : contains useful info about a given video
type YTVideo struct {
ID string
Title string
}
func main() {
var playListID, apiKey string
flag.StringVar(&playListID, "id", "", "Playlist ID")
flag.StringVar(&apiKey, "api", "", "Youtube API Key")
flag.Parse()
ctx := context.Background()
youtubeService, err := youtube.NewService(ctx, option.WithAPIKey(apiKey))
if err != nil {
fmt.Print(err)
}
call := youtubeService.PlaylistItems.List("snippet")
call = call.PlaylistId(playListID)
call = call.MaxResults(50)
playList, err := call.Do()
if err != nil {
fmt.Print(err)
}
var videos []YTVideo
for ok := true; ok; ok = !(playList.NextPageToken == "") {
for _, item := range playList.Items {
var video YTVideo
video.ID = item.Id
video.Title = item.Snippet.Title
videos = append(videos, video)
}
playList, err = call.PageToken(playList.NextPageToken).Do()
if err != nil {
fmt.Print(err)
}
}
jsonVideos, err := json.Marshal(videos)
if err != nil {
fmt.Print(err)
}
fmt.Println(string(jsonVideos))
}