-
Notifications
You must be signed in to change notification settings - Fork 0
/
youtube.go
84 lines (73 loc) · 2.22 KB
/
youtube.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
package main
import (
"fmt"
"log"
"net/http"
"strings"
"google.golang.org/api/googleapi/transport"
"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([]string{part})
call = call.PlaylistId(playlistId)
if pageToken != "" {
call = call.PageToken(pageToken)
}
response, err := call.Do()
if err != nil {
panic(err)
}
return response
}
func retrievePlaylist(playlistId string) []lessonData {
fmt.Println("[YT] Retrieving playlist", playlistId)
client := &http.Client{
Transport: &transport.APIKey{Key: codyConf.DeveloperKey},
}
service, err := youtube.New(client)
if err != nil {
log.Fatalf("error creating new YouTube client: %v", err)
}
playlistData := []lessonData{}
nextPageToken := ""
pageCount := 1
for {
fmt.Println("[YT] Retrieving playlist page", pageCount)
// Retrieve next set of items in the playlist.
playlistResponse := playlistItemsList(service, "snippet", playlistId, nextPageToken)
for _, playlistItem := range playlistResponse.Items {
// videoTitle := playlistItem.Snippet.Title
videoId := playlistItem.Snippet.ResourceId.VideoId
fmt.Println("[YT] Processing playlist item", videoId)
videoDesc := playlistItem.Snippet.Description
playlistDatum := lessonData{
Video: videoId,
}
parseDescription(&playlistDatum, videoDesc)
playlistData = append(playlistData, playlistDatum)
}
// 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 == "" {
fmt.Println("[YT] Done processing playlist")
break
}
}
return playlistData
}
func parseDescription(ld *lessonData, description string) {
fmt.Println("[YT] Input to parseDescription:", description)
newlineSplit := strings.Split(description, "\n")
if len(newlineSplit) < 5 {
fmt.Println("[YT] Description is malformed.")
return
}
ld.Id = newlineSplit[0]
ld.Title = newlineSplit[1]
ld.Description = newlineSplit[2]
ld.VApp = ld.Id
ld.Slides = newlineSplit[4]
ld.PDF = newlineSplit[5]
}