-
Notifications
You must be signed in to change notification settings - Fork 0
/
nct.go
80 lines (65 loc) · 1.55 KB
/
nct.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
package main
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
"net/http"
"strings"
)
const (
nctWmUrl = "https://m.nhaccuatui.com/bai-hat/%s"
nctLinkInfo = "https://m.nhaccuatui.com/ajax/get-media-info?key1=%s&key2=&key3="
)
type NCT struct {
Url string
}
type NCTResponse struct {
Data struct {
Title string `json:"title"`
Author string `json:"singerTitle"`
DownloadUrl string `json:"location"`
} `json:"data"`
}
func NewNCTHandler(url string) *NCT {
return &NCT{url}
}
func (nct *NCT) GetDownloadObject() (*DownloadObject, error) {
response, err := nct.Parse()
if err != nil {
return nil, err
}
return &DownloadObject{
Url: nct.Url,
Title: response.Data.Title,
Author: response.Data.Author,
DownloadUrl: response.Data.DownloadUrl,
Type: "mp3",
}, nil
}
func (nct *NCT) Parse() (*NCTResponse, error) {
response := &NCTResponse{}
if nct.Url == "" {
return nil, errors.New("Empty Url")
}
urlList := strings.Split(nct.Url, "/")
if len(urlList) < 5 {
return nil, errors.New("Invalid Url")
}
videoID := urlList[4][:]
respBytes, err := GetBody(fmt.Sprintf(nctWmUrl, videoID))
keyPattern := "songencryptkey=\"([a-zA-Z0-9]*)\""
key, err := parseResponse(respBytes, keyPattern)
if err != nil {
return nil, err
}
downloadSourceUrl := fmt.Sprintf(nctLinkInfo, key)
res, err := http.Get(downloadSourceUrl)
if err != nil {
return nil, err
}
defer res.Body.Close()
if err := json.NewDecoder(res.Body).Decode(&response); err != nil {
return nil, err
}
return response, nil
}