-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzing.go
87 lines (71 loc) · 1.68 KB
/
zing.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
package main
import (
"encoding/json"
"errors"
"fmt"
"net/http"
)
const (
zingLinkInfo = "https://mp3.zing.vn/xhr/media/get-url-download?type=audio&sig=%s&code=%s"
zingLinkDownload = "https://mp3.zing.vn%s"
)
type Zing struct {
Url string
}
type ZingResponse struct {
Data struct {
Title string `json:"title"`
Author string `json:"artist"`
Sources struct {
Url Source `json:"128"`
Url2 Source `json:"320"`
Url3 Source `json:"lossless"`
} `json:"sources"`
} `json:"data"`
}
type Source struct {
Link string `json:"link"`
}
func NewZingHandler(url string) *Zing {
return &Zing{url}
}
func (z *Zing) GetDownloadObject() (*DownloadObject, error) {
response, err := z.Parse()
if err != nil {
return nil, err
}
return &DownloadObject{
Url: z.Url,
Title: response.Data.Title,
Author: response.Data.Author,
DownloadUrl: fmt.Sprintf(zingLinkDownload, response.Data.Sources.Url.Link),
Type: "mp3",
}, nil
}
func (zing *Zing) Parse() (*ZingResponse, error) {
response := &ZingResponse{}
if zing.Url == "" {
return nil, errors.New("Empty Url")
}
respBytes, err := GetBody(zing.Url)
sigPattern := "data-sig=\"([a-zA-Z0-9]*)\""
sig, err := parseResponse(respBytes, sigPattern)
if err != nil {
return nil, err
}
codePattern := "data-code=\"([a-zA-Z0-9]*)\""
code, err := parseResponse(respBytes, codePattern)
if err != nil {
return nil, err
}
downloadSourceUrl := fmt.Sprintf(zingLinkInfo, sig, code)
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
}