-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcrawler.go
More file actions
224 lines (193 loc) · 5.34 KB
/
crawler.go
File metadata and controls
224 lines (193 loc) · 5.34 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package gophertv
import (
"fmt"
"log"
"net/http"
"strings"
"appengine"
"appengine/urlfetch"
"code.google.com/p/google-api-go-client/googleapi/transport"
"code.google.com/p/google-api-go-client/youtube/v3"
)
// TODO (sunil): Read it from Conf or commandline param
const developerKey = "xxxxx"
const maxResults = 50
func getYoutubeService(c appengine.Context) (*youtube.Service, error) {
client := &http.Client{
Transport: &transport.APIKey{
Key: developerKey,
Transport: &urlfetch.Transport{Context: c},
},
}
return youtube.New(client)
}
// searchPlaylists fetches playlists for a given query.
func searchPlaylists(c appengine.Context, query string) ([]string, error) {
service, err := getYoutubeService(c)
if err != nil {
log.Printf("error creating youtube service :: %v", err)
return nil, err
}
nextPageToken := ""
var playlists []string
for {
// Make the API call to YouTube.
call := service.Search.List("id").
Q(query).
Type("playlist").
PageToken(nextPageToken).
MaxResults(maxResults)
response, err := call.Do()
if err != nil {
log.Fatalf("Error making search API call: %v", err)
}
// Iterate through each item and add it to the correct list.
for _, item := range response.Items {
switch item.Id.Kind {
case "youtube#playlist":
playlists = append(playlists, item.Id.PlaylistId)
}
}
nextPageToken = response.NextPageToken
if nextPageToken == "" {
break
}
}
return playlists, nil
}
func printPlaylists(l []*youtube.SearchResult) {
for i, item := range l {
fmt.Printf(`%2d
ID: %v
Title: %v
Description: %v
`, i, item.Id.PlaylistId, item.Snippet.Title, item.Snippet.Description)
}
fmt.Printf("\n\n")
}
func fetchPlaylist(c appengine.Context, plID string) ([]*youtube.PlaylistItem, error) {
service, err := getYoutubeService(c)
if err != nil {
log.Printf("error creating youtube service :: %v", err)
return nil, err
}
// Group video, channel, and playlist results in separate lists.
var plItems []*youtube.PlaylistItem
nextPageToken := ""
for {
// Make the API call to YouTube.
call := service.PlaylistItems.List("id, snippet, contentDetails, status").
PlaylistId(plID).
MaxResults(maxResults).
PageToken(nextPageToken)
response, err := call.Do()
if err != nil {
log.Fatalf("Error making search API call: %v", err)
}
// Iterate through each item and add it to the correct list.
for _, item := range response.Items {
switch item.Kind {
case "youtube#playlistItem":
plItems = append(plItems, item)
}
}
nextPageToken = response.NextPageToken
if nextPageToken == "" {
break
}
}
return plItems, nil
}
func printPlaylistItems(items []*youtube.PlaylistItem) {
for i, item := range items {
i++
log.Printf(`%2d:
Snippet: %+v
Status: %+v
ContentDetails: %v
ID: %v \n`, i, item.Snippet.ResourceId.VideoId, item.Status, item.ContentDetails, item.Id)
}
}
func getVideoIDs(c appengine.Context, plID string) ([]string, error) {
service, err := getYoutubeService(c)
if err != nil {
log.Printf("error creating youtube service :: %v", err)
return nil, err
}
// Group video, channel, and playlist results in separate lists.
var videoIDs []string
nextPageToken := ""
for {
// Make the API call to YouTube.
call := service.PlaylistItems.List("snippet").
PlaylistId(plID).
MaxResults(maxResults).
PageToken(nextPageToken)
response, err := call.Do()
if err != nil {
log.Fatalf("Error making search API call: %v", err)
}
// Iterate through each item and add it to the correct list.
for _, item := range response.Items {
switch item.Kind {
case "youtube#playlistItem":
videoIDs = append(videoIDs, item.Snippet.ResourceId.VideoId)
}
}
nextPageToken = response.NextPageToken
if nextPageToken == "" {
break
}
}
return videoIDs, nil
}
func fetchVideos(c appengine.Context, ids ...string) ([]*youtube.Video, error) {
service, err := getYoutubeService(c)
if err != nil {
log.Printf("error creating youtube service :: %v", err)
return nil, err
}
var videos []*youtube.Video
idStr := strings.Join(ids, ",")
log.Printf("video id str: %s", idStr)
nextPageToken := ""
for {
// Make the API call to YouTube.
var call *youtube.VideosListCall
part := "id, snippet, contentDetails, statistics"
if nextPageToken == "" {
call = service.Videos.List(part).Id(idStr).MaxResults(maxResults)
} else {
call = service.Videos.List(part).
Id(idStr).
MaxResults(maxResults).
PageToken(nextPageToken)
}
response, err := call.Do()
if err != nil {
return nil, err
}
// Iterate through each item and add it to the correct list.
for _, item := range response.Items {
switch item.Kind {
case "youtube#video":
videos = append(videos, item)
}
}
nextPageToken = response.NextPageToken
if nextPageToken == "" {
break
}
}
return videos, nil
}
func printVideos(videos ...*youtube.Video) {
for i, v := range videos {
log.Printf(`%2d:
ID: %v
Snippet: %+v
Statistics: %+v
ContentDetails: %v
`, i, v.Id, v.Snippet, v.Statistics, v.ContentDetails)
}
}