-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinks.go
141 lines (112 loc) · 3.77 KB
/
links.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
package alldebrid
import (
"encoding/json"
"errors"
"fmt"
"net/http"
)
//UnlockLinkResponse is the response of the unlock link call
type UnlockLinkResponse struct {
Status string `json:"status"`
Data unlockLinkResponseData `json:"data,omitempty"`
Error alldebridError `json:"error,omitempty"`
}
type unlockLinkResponseData struct {
Link string `json:"link"`
Host string `json:"host"`
Filename string `json:"filename"`
Streaming []interface{} `json:"streaming"`
Paws bool `json:"paws"`
Filesize int `json:"filesize"`
Streams []unlockLinkStreams `json:"streams"`
ID string `json:"id"`
}
type unlockLinkStreams struct {
ID string `json:"id"`
Ext string `json:"ext"`
Quality int `json:"quality"`
Filesize int `json:"filesize"`
Proto string `json:"proto"`
Name string `json:"name"`
Tbr float64 `json:"tbr,omitempty"`
Abr int `json:"abr,omitempty"`
}
//StreamingResponse is the response of the streaming call
type StreamingResponse struct {
Status string `json:"status"`
Data streamingResponseData `json:"data,omitempty"`
Error alldebridError `json:"error,omitempty"`
}
type streamingResponseData struct {
Link string `json:"link,omitempty"`
Filename string `json:"filename"`
Filesize float64 `json:"filesize"`
Delayed int `json:"delayed,omitempty"`
}
//DelayedResponse is the response of the delayed call
type DelayedResponse struct {
Status string `json:"status"`
Data delayedResponseData `json:"data,omitempty"`
Error alldebridError `json:"error,omitempty"`
}
type delayedResponseData struct {
Status int `json:"status"`
Speed int `json:"speed"`
TimeLeft int `json:"time_left"`
Progress int `json:"progress"`
Link string `json:"link"`
}
// UnlockLink returns a downloadable link
func (c *Client) UnlockLink(link string) (UnlockLinkResponse, error) {
resp, err := http.Get(fmt.Sprintf(linkunlock, getLinksEndpoint(), c.ic.appName, c.ic.apikey, link))
if err != nil {
return UnlockLinkResponse{}, err
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
var unlockResponse UnlockLinkResponse
err = decoder.Decode(&unlockResponse)
if err != nil {
return UnlockLinkResponse{}, err
}
if unlockResponse.Status != "success" {
return UnlockLinkResponse{}, errors.New(unlockResponse.Error.Message)
}
return unlockResponse, nil
}
//StreamingLink returns a delayed id or the direct link
func (c *Client) StreamingLink(stream, id string) (StreamingResponse, error) {
resp, err := http.Get(fmt.Sprintf(linkstreaming, getLinksEndpoint(), c.ic.appName, c.ic.apikey, stream, id))
if err != nil {
return StreamingResponse{}, err
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
var streamResponse StreamingResponse
err = decoder.Decode(&streamResponse)
if err != nil {
return StreamingResponse{}, err
}
if streamResponse.Status != "success" {
return StreamingResponse{}, errors.New(streamResponse.Error.Message)
}
return streamResponse, nil
}
// DelayedLink returns a downloadable link for the given delayed id
func (c *Client) DelayedLink(delayedID string) (DelayedResponse, error) {
resp, err := http.Get(fmt.Sprintf(linkdelayed, getLinksEndpoint(), c.ic.appName, c.ic.apikey, delayedID))
if err != nil {
return DelayedResponse{}, err
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
var delayedResponse DelayedResponse
err = decoder.Decode(&delayedResponse)
if err != nil {
return DelayedResponse{}, err
}
if delayedResponse.Status != "success" {
return DelayedResponse{}, errors.New(delayedResponse.Error.Message)
}
return delayedResponse, nil
}