forked from BernardZhao/Jukebox
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmusic.go
More file actions
75 lines (62 loc) · 1.63 KB
/
music.go
File metadata and controls
75 lines (62 loc) · 1.63 KB
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
package main
import (
"encoding/json"
"os/exec"
"sync"
)
// Song : Song information fetched from youtube-dl. More fields exist in the JSON response, but this is what we need.
type Song struct {
Title string `json:"title"`
URL string `json:"url"`
Thumbnail string `json:"thumbnail"`
WebpageURL string `json:"webpage_url"`
}
type SongCache struct {
cache map[string]Song
mux sync.RWMutex
}
var songCache = SongCache{
cache: make(map[string]Song),
}
func (sc *SongCache) Get(url string) (Song, bool) {
sc.mux.RLock()
defer sc.mux.RUnlock()
song, exists := sc.cache[url]
return song, exists
}
func (sc *SongCache) Set(url string, song Song) {
sc.mux.Lock()
defer sc.mux.Unlock()
sc.cache[url] = song
}
func fetchSong(url string) (Song, error) {
// Check cache first
if song, exists := songCache.Get(url); exists {
return song, nil
}
// If not in cache, fetch and store
song, err := fetchSongFromYoutubeDL(url)
if err == nil {
songCache.Set(url, song)
}
return song, err
}
func fetchSongFromYoutubeDL(url string) (Song, error) {
// Maybe make this function return a promise or something in the future
var songData Song
// JSON dump has no extra overhead, and we get more info that we need that might be useful
cmd := exec.Command("yt-dlp", "--no-playlist", "-J", "--youtube-skip-dash-manifest", "-f bestaudio", url)
stdOut, err := cmd.StdoutPipe()
if err != nil {
return Song{}, err
}
if err := cmd.Start(); err != nil {
return Song{}, err
}
if err := json.NewDecoder(stdOut).Decode(&songData); err != nil {
return Song{}, err
}
cmd.Wait()
// Only using these fields atm
return songData, nil
}