-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathyoutube.go
125 lines (101 loc) · 2.94 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
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
package main
import (
"context"
"strconv"
"time"
option "google.golang.org/api/option"
youtube "google.golang.org/api/youtube/v3"
)
// YouTubeVideo is a distilled record of YouTube video metadata
type YouTubeVideo struct {
ID string
Title string
VideoViews uint64
ChannelID string
ChannelTitle string
TotalComments uint64
TotalLikes uint64
TotalDislikes uint64
Thumbnail string
PublishedAt string
}
// YouTubeGetCommentsV2 pulls the comments for a given YouTube video
func (ytv YouTubeVideo) GetComments() CommentList {
videoID := ytv.ID
var comments = []*Comment{}
ctx := context.Background()
youtubeService, err := youtube.NewService(ctx, option.WithAPIKey(GetConfigString("ytkey")))
if err != nil {
panic(err)
}
pageToken := ""
errCount := 0
for pageToken != "EOL" {
results, err := youtubeService.CommentThreads.List([]string{"id", "snippet", "replies"}).TextFormat("plainText").MaxResults(100).VideoId(videoID).PageToken(pageToken).Do()
if err != nil {
LogMsg(err.Error())
if errCount > 3 {
LogMsg("YouTube API error limit hit.")
break
}
errCount = errCount + 1
continue
}
if len(results.Items) > 0 {
for _, item := range results.Items {
tempComments := []*youtube.Comment{}
tempComments = append(tempComments, item.Snippet.TopLevelComment)
if item.Replies != nil {
for _, reply := range item.Replies.Comments {
tempComments = append(tempComments, reply)
}
}
for _, c := range tempComments {
thisComment := &Comment{
ID: c.Id,
Published: c.Snippet.PublishedAt,
Title: "",
Content: c.Snippet.TextDisplay,
AuthorName: c.Snippet.AuthorDisplayName,
Likes: c.Snippet.LikeCount,
}
comments = append(comments, thisComment)
}
}
}
pageToken = results.NextPageToken
if pageToken == "" {
pageToken = "EOL"
}
}
return CommentList{Comments: comments}
}
// GetMetadata returns a subset of video information from the YouTube API
func (ytv *YouTubeVideo) GetMetadata() bool {
videoID := ytv.ID
ctx := context.Background()
youtubeService, err := youtube.NewService(ctx, option.WithAPIKey(GetConfigString("ytkey")))
if err != nil {
panic(err)
}
call := youtubeService.Videos.List([]string{"id", "snippet", "statistics"}).Id(videoID)
resp, err := call.Do()
if err != nil {
panic(err)
}
if len(resp.Items) > 0 {
video := resp.Items[0]
t, _ := time.Parse(time.RFC3339Nano, video.Snippet.PublishedAt)
ytv.Title = video.Snippet.Title
ytv.ChannelID = video.Snippet.ChannelId
ytv.ChannelTitle = video.Snippet.ChannelTitle
ytv.TotalComments = video.Statistics.CommentCount
ytv.PublishedAt = strconv.FormatInt(t.Unix(), 10)
ytv.VideoViews = video.Statistics.ViewCount
ytv.Thumbnail = video.Snippet.Thumbnails.High.Url
ytv.TotalLikes = video.Statistics.LikeCount
ytv.TotalDislikes = video.Statistics.DislikeCount
return true
}
return false
}