-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo.go
147 lines (122 loc) · 3.51 KB
/
video.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
135
136
137
138
139
140
141
142
143
144
145
146
147
package yt
import (
"context"
"fmt"
"net/url"
"strings"
"github.com/axatol/guosheng/pkg/discord"
"github.com/axatol/guosheng/pkg/util"
"github.com/bwmarrin/discordgo"
)
type Video struct {
ID string `json:"id"`
ETag string `json:"etag"`
Title string `json:"title"`
ChannelID string `json:"channel_id"`
ChannelTitle string `json:"channel_title"`
DurationRaw string `json:"duration"`
}
func (v *Video) ToMap() map[string]string {
return map[string]string{
"id": v.ID,
"etag": v.ETag,
"title": v.Title,
"channel_id": v.ChannelID,
"channel_title": v.ChannelTitle,
"duration": v.DurationRaw,
}
}
func (v *Video) VideoURL() string {
return fmt.Sprintf("https://youtube.com/watch?v=%s", v.ID)
}
func (v *Video) ChannelURL() string {
return fmt.Sprintf("https://youtube.com/channel/%s", v.ChannelID)
}
func (v *Video) Duration() *util.ISODuration {
if v.DurationRaw == "" {
return nil
}
duration, err := util.ParseISODuration(v.DurationRaw)
if err != nil {
return nil
}
return duration
}
func (v *Video) AsMessageEmbed() *discordgo.MessageEmbed {
uploader := util.MDLink(v.ChannelTitle, v.ChannelURL())
duration := v.Duration().String()
if duration == "" {
duration = "?"
}
return discord.NewMessageEmbed().
SetTitle(v.Title).
SetURL(v.VideoURL()).
AddField("Uploader", uploader).
AddField("Duration", duration).
Embed()
}
func (c *Client) GetVideosByIDs(ctx context.Context, ids ...string) ([]Video, error) {
query := c.service.Videos.
List([]string{"snippet", "contentDetails"}).
Context(ctx).
Id(ids...).
MaxResults(int64(len(ids)))
response, err := query.Do()
if err != nil {
return nil, fmt.Errorf("failed to query video by id %s: %s", strings.Join(ids, ","), err)
}
if len(response.Items) < 1 {
return nil, fmt.Errorf("video by id %s not found", strings.Join(ids, ","))
}
result := make([]Video, len(response.Items))
for i, item := range response.Items {
result[i] = Video{
ID: item.Id,
ETag: item.Etag,
Title: item.Snippet.Title,
ChannelID: item.Snippet.ChannelId,
ChannelTitle: item.Snippet.ChannelTitle,
DurationRaw: item.ContentDetails.Duration,
}
}
return result, nil
}
func (c *Client) GetVideoByID(ctx context.Context, id string) (*Video, error) {
results, err := c.GetVideosByIDs(ctx, id)
if err != nil {
return nil, err
}
return &results[0], nil
}
func (c *Client) GetVideoByURL(ctx context.Context, rawURL string) (*Video, error) {
u, err := url.Parse(NormaliseURL(rawURL))
if err != nil {
return nil, fmt.Errorf("failed to parse url %s: %s", rawURL, err)
}
if !u.Query().Has("v") {
return nil, fmt.Errorf("video id not found in url %s", u.String())
}
return c.GetVideoByID(ctx, u.Query().Get("v"))
}
func (c *Client) SearchVideo(ctx context.Context, search string, limit int64) ([]Video, error) {
query := c.service.Search.List([]string{"snippet"}).Context(ctx).Q(search).SafeSearch("none").Type("video").MaxResults(limit)
response, err := query.Do()
if err != nil {
return nil, fmt.Errorf("failed to search video with query %s: %s", search, err)
}
if len(response.Items) < 1 {
return nil, nil
}
result := make([]Video, len(response.Items))
for i, item := range response.Items {
result[i] = Video{
ID: item.Id.VideoId,
ETag: item.Etag,
Title: item.Snippet.Title,
ChannelID: item.Snippet.ChannelId,
ChannelTitle: item.Snippet.ChannelTitle,
DurationRaw: "",
}
}
return result, nil
}