-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
128 lines (113 loc) · 2.97 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"regexp"
"golang.org/x/net/html"
)
type Download struct {
Id string `json:"id"`
CreatedAt string `json:"created_at"`
Type string `json:"type"`
Hash string `json:"hash"`
Name string `json:"name"`
Size string `json:"size"`
}
type Premiumize struct {
DownloadList []Download `json:"content"`
Client *http.Client
Urls map[string]*url.URL
Endpoints map[string]string
Pin string `json:"pin"`
CustomerId string `json:"customer_id"`
}
func loadConfigurationFile(filename string) (file []byte, err error) {
file, err = ioutil.ReadFile(filename)
if err != nil {
fmt.Printf("Cannot read from config file: %v\n", err)
os.Exit(1)
}
return
}
func NewPremiumize() (premiumize *Premiumize) {
data, _ := loadConfigurationFile("config.json")
json.Unmarshal(data, &premiumize)
folderListUrl, _ := url.Parse("https://www.premiumize.me/api/folder/list")
detailUrl, _ := url.Parse("https://www.premiumize.me/browsetorrent")
createUrl, _ := url.Parse("https://www.premiumize.me/api/transfer/create")
premiumize.Urls = map[string]*url.URL{
"folderListUrl": folderListUrl,
"detailUrl": detailUrl,
"createUrl": createUrl,
}
return
}
func parseDetailPage(responseBody io.ReadCloser) (hrefs []string) {
z := html.NewTokenizer(responseBody)
for {
tt := z.Next()
switch {
case tt == html.ErrorToken:
return
case tt == html.StartTagToken:
t := z.Token()
isAnchor := t.Data == "a"
if isAnchor {
for _, a := range t.Attr {
if a.Key == "href" {
re := regexp.MustCompile("^https.*(mkv|mp4)")
if re.FindStringSubmatch(a.Val) != nil {
hrefs = append(hrefs, a.Val)
}
}
break
}
}
}
}
}
func (premiumize *Premiumize) getDownloadList() {
res, _ := http.PostForm(premiumize.Urls["folderListUrl"].String(),
url.Values{"customer_id": {premiumize.CustomerId}, "pin": {premiumize.Pin}})
responseBody, err := ioutil.ReadAll(res.Body)
if err == nil {
defer res.Body.Close()
}
json.Unmarshal(responseBody, &premiumize)
}
func (premiumize *Premiumize) GetDownloadLinks() (downloadLinks []string) {
premiumize.getDownloadList()
for _, download := range premiumize.DownloadList {
detailUrl := premiumize.Urls["detailUrl"]
q := detailUrl.Query()
q.Set("hash", download.Hash)
detailUrl.RawQuery = q.Encode()
res, _ := http.PostForm(detailUrl.String(),
url.Values{"customer_id": {premiumize.CustomerId}, "pin": {premiumize.Pin}})
hrefs := parseDetailPage(res.Body)
downloadLinks = append(downloadLinks, hrefs...)
}
return
}
func debug(res *http.Response) {
body, _ := ioutil.ReadAll(res.Body)
fmt.Printf("Statuscode: %v\n", res.StatusCode)
fmt.Println()
for k, v := range res.Header {
fmt.Printf("%s: %s\n", k, v)
}
fmt.Println()
fmt.Printf("%s\n", body)
}
func main() {
p := NewPremiumize()
res := p.GetDownloadLinks()
for _, d := range res {
fmt.Println(d)
}
}