This repository has been archived by the owner on Dec 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
youtube.go
169 lines (144 loc) · 4.18 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
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
package main
import (
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/google/google-api-go-client/googleapi/transport"
"github.com/rylio/ytdl"
"google.golang.org/api/youtube/v3"
"log"
"net/http"
"strconv"
"strings"
"time"
)
func getDuration(stringRawFull, stringRawOffset string) (stringRemain string) {
// stringRawFull format: P1DT3H45M2S or PT3H45M2S
// stringRawOffset format: 4325s (at seconds)
var stringFull string
var duration TimeDuration
var partial time.Duration
stringFull = strings.Replace(stringRawFull, "P", "", 1)
stringFull = strings.Replace(stringFull, "T", "", 1)
stringFull = strings.ToLower(stringFull)
var secondsFull, secondsOffset int
value := strings.Split(stringFull, "d")
if len(value) == 2 {
secondsFull, _ = strconv.Atoi(value[0])
// get the days in seconds
secondsFull = secondsFull * 86400
// get the format 1h1m1s in seconds
partial, _ = time.ParseDuration(value[1])
secondsFull = secondsFull + int(partial.Seconds())
} else {
partial, _ = time.ParseDuration(stringFull)
secondsFull = int(partial.Seconds())
}
if stringRawOffset != "" {
value = strings.Split(stringRawOffset, "s")
if len(value) == 2 {
secondsOffset, _ = strconv.Atoi(value[0])
}
}
// substact the time offset
duration.Second = secondsFull - secondsOffset
if duration.Second <= 0 {
return "0:00"
}
// print the time
t := AddTimeDuration(duration)
if t.Day == 0 && t.Hour == 0 {
return fmt.Sprintf("%02d:%02d", t.Minute, t.Second)
}
if t.Day == 0 {
return fmt.Sprintf("%02d:%02d:%02d", t.Hour, t.Minute, t.Second)
}
return fmt.Sprintf("%d:%02d:%02d:%02d", t.Day, t.Hour, t.Minute, t.Second)
}
func YoutubeFind(searchString string, v *VoiceInstance, m *discordgo.MessageCreate) (song_struct PkgSong, err error) { //(url, title, time string, err error)
client := &http.Client{
Transport: &transport.APIKey{Key: o.YoutubeToken},
}
service, err := youtube.New(client)
if err != nil {
//log.Fatalf("Error creating new YouTube client: %v", err)
return
}
var timeOffset string
if strings.Contains(searchString, "?t=") || strings.Contains(searchString, "&feature=youtu.be&t=") {
var split []string
switch {
case strings.Contains(searchString, "?t="):
split = strings.Split(searchString, "?t=")
break
case strings.Contains(searchString, "&feature=youtu.be&t="):
split = strings.Split(searchString, "&feature=youtu.be&t=")
break
}
searchString = split[0]
timeOffset = split[1]
if !strings.ContainsAny(timeOffset, "h | m | s") {
timeOffset = timeOffset + "s" // secons
}
}
call := service.Search.List("id,snippet").Q(searchString).MaxResults(1)
response, err := call.Do()
if err != nil {
//log.Fatalf("Error making search API call: %v", err)
return
}
var (
audioId, audioTitle string //, fileVideoID string
)
for _, item := range response.Items {
audioId = item.Id.VideoId
audioTitle = item.Snippet.Title
}
if audioId == "" {
ChMessageSend(m.ChannelID, "Sorry, I can't found this song.")
return
}
vid, err := ytdl.GetVideoInfo("https://www.youtube.com/watch?v=" + audioId)
if err != nil {
//ChMessageSend(textChannelID, "Sorry, nothing found for query: "+strings.Trim(searchString, " "))
return
}
format := vid.Formats.Extremes(ytdl.FormatAudioBitrateKey, true)[0]
videoURL, err := vid.GetDownloadURL(format)
//log.Println(err)
videos := service.Videos.List("contentDetails").Id(vid.ID)
resp, err := videos.Do()
duration := resp.Items[0].ContentDetails.Duration
durationString := getDuration(duration, timeOffset)
var videoURLString string
if videoURL != nil {
if timeOffset != "" {
offset, _ := time.ParseDuration(timeOffset)
query := videoURL.Query()
query.Set("begin", fmt.Sprint(int64(offset/time.Millisecond)))
videoURL.RawQuery = query.Encode()
}
videoURLString = videoURL.String()
} else {
log.Println("Video URL not found")
}
guildID := SearchGuild(m.ChannelID)
member, _ := v.session.GuildMember(guildID, m.Author.ID)
name := ""
if member.Nick == "" {
name = m.Author.Username
} else {
name = member.Nick
}
song := Song{
m.ChannelID,
name,
m.Author.ID,
vid.ID,
audioTitle,
durationString,
videoURLString,
}
song_struct.data = song
song_struct.v = v
return
}