-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.go
executable file
·206 lines (154 loc) · 3.46 KB
/
models.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package bemogo
import (
"fmt"
"sync"
"time"
dgo "github.com/bwmarrin/discordgo"
"github.com/kkdai/youtube/v2"
)
var (
ErrInvalidURL = fmt.Errorf("invalid url: cannot extract the id from the url")
ErrCannotGetVideo = fmt.Errorf("cannot get the video: this video is restricted or does not exist")
ErrCannotGetVideoSource = fmt.Errorf("cannot get the video source")
)
type song struct {
videoID,
title,
author,
description,
source,
thumbnailURL string
duration time.Duration
createdAt time.Time
}
func SongFromID(videoURL string) (song, error) {
videoID, err := youtube.ExtractVideoID(videoURL)
if err != nil {
fmt.Println("invalid url: ", err)
return song{}, ErrInvalidURL
}
client := youtube.Client{}
video, err := client.GetVideo(videoID)
if err != nil {
fmt.Println("cannot get video: ", err)
return song{}, ErrCannotGetVideo
}
formats := video.Formats.WithAudioChannels()
url, err := client.GetStreamURL(video, &formats[0])
if err != nil {
fmt.Println("cannot get video source: ", err)
return song{}, err
}
var thumbnailURL string
if len(video.Thumbnails) >= 1 {
thumbnailURL = video.Thumbnails[len(video.Thumbnails)-1].URL
}
newSong := song{
videoID: videoID,
title: video.Title,
author: video.Author,
description: video.Description,
source: url,
thumbnailURL: thumbnailURL,
duration: video.Duration,
createdAt: time.Now(),
}
return newSong, nil
}
func (s song) URL() string {
return fmt.Sprintf("https://youtu.be/%s", s.videoID)
}
func (s song) Embed() *dgo.MessageEmbed {
return &dgo.MessageEmbed{
URL: s.URL(),
Title: s.title,
Description: fmt.Sprintf("By %s", s.author),
Thumbnail: &dgo.MessageEmbedThumbnail{URL: s.thumbnailURL},
Footer: &dgo.MessageEmbedFooter{Text: s.duration.String()},
}
}
type playlist struct {
bucle int
isPlaying bool
currentSong *song
songs []song
createdAt time.Time
mu sync.Mutex
stop chan bool
}
func NewPlaylist() *playlist {
songs := make([]song, 0)
stop := make(chan bool)
return &playlist{
songs: songs,
stop: stop,
createdAt: time.Now(),
}
}
func (pl *playlist) SongsEmbed() []*dgo.MessageEmbed {
embeds := make([]*dgo.MessageEmbed, len(pl.songs))
for i, song := range pl.songs {
embeds[i] = song.Embed()
}
return embeds
}
func (pl *playlist) CurrentSong() *song {
return pl.currentSong
}
func (pl *playlist) GetSong(i int64) *song {
if i > int64(len(pl.songs)-1) {
return nil
}
return &pl.songs[i]
}
func (pl *playlist) NextSong() *song {
if len(pl.songs) == 0 {
return nil
}
if len(pl.songs) == 1 {
return pl.currentSong
}
index := 0
for i, song := range pl.songs {
if pl.currentSong == nil {
break
}
if song.videoID == pl.currentSong.videoID {
index = i
}
}
if index == len(pl.songs)-1 {
index = -1
}
index += 1
return &pl.songs[index]
}
func (pl *playlist) PushSong(s song) {
pl.mu.Lock()
pl.songs = append(pl.songs, s)
pl.mu.Unlock()
}
func (pl *playlist) PopSong(i int) {
if len(pl.songs) == 0 {
return
}
pl.mu.Lock()
if i == -1 {
i = len(pl.songs) - 1
}
pl.songs = append(pl.songs[:i], pl.songs[i+1:]...)
pl.mu.Unlock()
}
func (pl *playlist) TogglePause() bool {
pl.mu.Lock()
pl.isPlaying = !pl.isPlaying
pl.mu.Unlock()
return pl.isPlaying
}
func (pl *playlist) JumpPrevious(n int) {}
func (pl *playlist) JumpNext(n int) {}
func (pl *playlist) SetBucle(n int) {
pl.mu.Lock()
pl.bucle = n
pl.mu.Unlock()
}