-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsong.go
147 lines (128 loc) · 2.87 KB
/
song.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 main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"sync"
"time"
"layeh.com/gumble/gumble"
"github.com/pborman/uuid"
)
var ErrInternal = errors.New("Internal server error")
// Used for grabbing information from the *.info.json file provided by
// youtube-dl
type Info struct {
Title *string `json:"title"`
Duration *float64 `json:"duration"`
}
type Song struct {
rwMutex sync.RWMutex
sender *gumble.User
url string
songpath *string
infopath *string
title *string
duration *time.Duration
}
func NewSong(sender *gumble.User, url string) *Song {
song := Song{
sender: sender,
url: url,
}
return &song
}
func (song *Song) Download() error {
song.rwMutex.RLock()
url := song.url
song.rwMutex.RUnlock()
id := uuid.New()
songpath, err := filepath.Abs(fmt.Sprintf("%s/%s.mp3", config.Cache.Directory, id))
if err != nil {
log.Printf("%s\n", err)
return ErrInternal
}
infopath, err := filepath.Abs(fmt.Sprintf("%s/%s.mp3.info.json", config.Cache.Directory, id))
if err != nil {
log.Printf("%s\n", err)
return ErrInternal
}
log.Printf("File will be saved to: %s\n", songpath)
log.Printf("Info will be saved to: %s\n", infopath)
cmd := exec.Command("youtube-dl",
"--max-filesize", config.Cache.MaxFilesize,
"--extract-audio",
"--no-playlist",
"--write-info-json",
"--audio-format", "mp3",
"--audio-quality", "0",
"-o", songpath,
url)
out, err := cmd.Output()
if err != nil {
log.Printf("An error occurred downloading the link:\n%s\n", out)
return errors.New("Unable to obtain audio from the specified link.")
}
blob, err := ioutil.ReadFile(infopath)
if err != nil {
log.Printf("%s\n", err)
return ErrInternal
}
var info Info
err = json.Unmarshal(blob, &info)
if err != nil {
log.Printf("%s\n", err)
return ErrInternal
}
song.rwMutex.Lock()
song.songpath = &songpath
song.infopath = &infopath
song.title = info.Title
if info.Duration != nil {
duration := time.Duration(float64(time.Second) * *info.Duration)
song.duration = &duration
}
song.rwMutex.Unlock()
return nil
}
func (song *Song) Delete() error {
song.rwMutex.Lock()
defer song.rwMutex.Unlock()
if song.songpath != nil {
if err := os.Remove(*song.songpath); err != nil {
return err
}
song.songpath = nil
}
if song.infopath != nil {
if err := os.Remove(*song.infopath); err != nil {
return err
}
song.infopath = nil
}
return nil
}
func (song *Song) Title() *string {
song.rwMutex.RLock()
defer song.rwMutex.RUnlock()
return song.title
}
func (song *Song) Duration() *time.Duration {
song.rwMutex.RLock()
defer song.rwMutex.RUnlock()
return song.duration
}
func (song *Song) Sender() *gumble.User {
song.rwMutex.RLock()
defer song.rwMutex.RUnlock()
return song.sender
}
func (song *Song) URL() string {
song.rwMutex.RLock()
defer song.rwMutex.RUnlock()
return song.url
}