This repository has been archived by the owner on Mar 11, 2022. It is now read-only.
/
voicehandler-soundcloud.go
81 lines (66 loc) · 1.82 KB
/
voicehandler-soundcloud.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
package main
import (
"errors"
"regexp"
"strconv"
"github.com/JoshuaDoes/goprobe"
)
// SoundCloud exports the methods required to access the SoundCloud service
type SoundCloud struct {
}
// GetName returns the service's name
func (*SoundCloud) GetName() string {
return "SoundCloud"
}
// GetColor returns the service's color
func (*SoundCloud) GetColor() int {
return 0xFF7700
}
// TestURL tests if the given URL is a SoundCloud track URL
func (*SoundCloud) TestURL(url string) (bool, error) {
test, err := regexp.MatchString("^(https?:\\/\\/)?(www.)?(m\\.)?soundcloud\\.com\\/[\\w\\-\\.]+(\\/)+[\\w\\-\\.]+/?$", url)
return test, err
}
// GetMetadata returns the metadata for a given SoundCloud track URL
func (*SoundCloud) GetMetadata(url string) (*Metadata, error) {
trackInfo, err := botData.BotClients.SoundCloud.GetTrackInfo(url)
if err != nil {
return nil, err
}
streamURL := trackInfo.DownloadURL
probe, err := goprobe.ProbeMedia(streamURL)
if err != nil {
return nil, err
}
if len(probe.Streams) == 0 {
return nil, errors.New("goprobe: no media streams found")
}
audioStream := &goprobe.Stream{}
for _, stream := range probe.Streams {
if stream.CodecType == "audio" {
audioStream = stream
break
}
}
if audioStream == nil {
return nil, errors.New("goprobe: no audio streams found")
}
duration, err := strconv.ParseFloat(audioStream.Duration, 64)
if err != nil {
return nil, err
}
metadata := &Metadata{
Title: trackInfo.Title,
DisplayURL: url,
StreamURL: trackInfo.DownloadURL,
Duration: duration,
ArtworkURL: trackInfo.ArtURL,
ThumbnailURL: trackInfo.ArtURL,
}
trackArtist := &MetadataArtist{
Name: trackInfo.Artist,
URL: "https://soundcloud.com/" + trackInfo.Artist,
}
metadata.Artists = append(metadata.Artists, *trackArtist)
return metadata, nil
}