-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutube_playlists.go
134 lines (108 loc) · 4.12 KB
/
youtube_playlists.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
//"encoding/json"
"flag"
"fmt"
"log"
"os"
"strconv"
"time"
"google.golang.org/api/youtube/v3"
)
// Retrieve playlistItems in the specified playlist
func playlistItemsList(service *youtube.Service, part string, playlistId string, pageToken string) *youtube.PlaylistItemListResponse {
call := service.PlaylistItems.List(part)
call = call.PlaylistId(playlistId)
if pageToken != "" {
call = call.PageToken(pageToken)
}
response, err := call.Do()
handleError(err, "")
return response
}
// Retrieve resource for the authenticated user's playlists
func playlistsListMine(service *youtube.Service, part string, pageToken string) *youtube.PlaylistListResponse {
call := service.Playlists.List(part)
call = call.Mine(true)
if pageToken != "" {
call = call.PageToken(pageToken)
}
response, err := call.Do()
handleError(err, "")
return response
}
// Get single video
func videoList(service *youtube.Service, part string, id string) *youtube.VideoListResponse {
maxRes := int64(50) // Default is 5, acceptable values: 1 to 50 inclusive
call := service.Videos.List(part)
call = call.Id(id).MaxResults(maxRes)
response, err := call.Do()
handleError(err, "")
return response
}
func printPlaylistVideos(service *youtube.Service, playlistId string, f *os.File) {
nextPageToken := ""
for {
// Retrieve next set of items in the playlist.
playlistResponse := playlistItemsList(service, "snippet", playlistId, nextPageToken)
for _, playlistItem := range playlistResponse.Items {
title := playlistItem.Snippet.Title
videoId := playlistItem.Snippet.ResourceId.VideoId
videoResponse := videoList(service, "statistics", videoId)
// If video is removed/private, statistics will be empty
viewCount := ""
likeCount := ""
dislikeCount := ""
for _, videoItem := range videoResponse.Items {
viewCount = strconv.Itoa(int(videoItem.Statistics.ViewCount))
likeCount = strconv.Itoa(int(videoItem.Statistics.LikeCount))
dislikeCount = strconv.Itoa(int(videoItem.Statistics.DislikeCount))
}
fmt.Fprintf(f, "%v, %v, %v, %v, %v \r\n", title, videoId,
viewCount, likeCount, dislikeCount)
}
// Set the token to retrieve the next page of results
// or exit the loop if all results have been retrieved.
nextPageToken = playlistResponse.NextPageToken
if nextPageToken == "" {
break
}
}
}
func main() {
// Parse playlist id if given
var cmdPlaylistId string
flag.StringVar(&cmdPlaylistId, "l", "", "Optional PlaylistId string")
flag.Parse()
// Get Client
client := getClient(youtube.YoutubeReadonlyScope)
service, err := youtube.New(client)
if err != nil {
log.Fatalf("Error creating YouTube client: %v", err)
}
// Create and open file for results
t := time.Now()
outputFileName := fmt.Sprintf("%s%s.txt", t.Format("./results2006-01-02"), cmdPlaylistId)
f, err := os.Create(outputFileName)
if err != nil {
log.Fatalf("Error creating file: %v", err)
}
defer f.Close()
nextPageToken := ""
for {
response := playlistsListMine(service, "snippet", nextPageToken)
for _, playlist := range response.Items {
playlistId := playlist.Id
if (cmdPlaylistId == playlistId || cmdPlaylistId == "") {
playlistTitle := playlist.Snippet.Title
fmt.Fprintf(f, "================================\r\n")
fmt.Fprintf(f, "Videos in list %s\r\n", playlistTitle)
printPlaylistVideos(service, playlistId, f)
}
}
nextPageToken = response.NextPageToken
if nextPageToken == "" {
break
}
}
}