-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.go
140 lines (117 loc) · 2.82 KB
/
search.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
package main
import (
"context"
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/joho/godotenv"
dl "github.com/kkdai/youtube/v2"
"google.golang.org/api/option"
"google.golang.org/api/youtube/v3"
"io"
"os"
"regexp"
)
var (
YoutubeKey string
)
func init() {
// Load .env file
err := godotenv.Load()
if err != nil {
fmt.Println("Error loading .env file")
}
// Get bot token from .env
YoutubeKey = os.Getenv("YOUTUBE_KEY")
}
func ValidateQuery(query string) bool {
pattern := regexp.MustCompile(`(?i)desingerica|pljugica`)
return pattern.MatchString(query)
}
func YoutubeSearch(i *discordgo.InteractionCreate, s *discordgo.Session, query string) string {
ctx := context.Background()
service, err := youtube.NewService(ctx, option.WithAPIKey(YoutubeKey))
if err != nil {
fmt.Println("Error creating YouTube service: ", err)
return ""
}
call := service.Search.List([]string{"id", "snippet"}).Q(query).MaxResults(1)
response, err := call.Do()
if err != nil {
fmt.Println("Error making search call: ", err)
return ""
}
var (
videoId, audioTitle string
)
for _, item := range response.Items {
videoId = item.Id.VideoId
audioTitle = item.Snippet.Title
}
dpMode := GetDPMode()
if dpMode {
allowed := ValidateQuery(audioTitle)
if !allowed {
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Bato, može samo Desingerica i Pljugica. DP mode je uključen.",
},
})
if err != nil {
return ""
}
return ""
}
}
if videoId == "" {
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "No video found for that search.",
},
})
if err != nil {
return ""
}
return ""
}
ytClient := dl.Client{}
vid, err := ytClient.GetVideo(videoId)
if err != nil {
for vid == nil {
vid, err = ytClient.GetVideo(videoId)
if err != nil {
fmt.Println("Error getting video: ", err)
}
}
}
formats := vid.Formats.WithAudioChannels()
stream, _, err := ytClient.GetStream(vid, &formats[0])
if err != nil {
fmt.Println("Error getting stream: ", err)
return ""
}
defer func(stream io.ReadCloser) {
err := stream.Close()
if err != nil {
fmt.Println("Error closing stream: ", err)
}
}(stream)
file, err := os.Create("audio/" + audioTitle + ".mp3")
if err != nil {
fmt.Println("Error creating file: ", err)
return ""
}
defer func(file *os.File) {
err := file.Close()
if err != nil {
fmt.Println("Error closing file: ", err)
}
}(file)
_, err = io.Copy(file, stream)
if err != nil {
fmt.Println("Error copying stream to file: ", err)
return ""
}
return audioTitle
}