This repository has been archived by the owner on Dec 3, 2019. It is now read-only.
/
debrid.go
150 lines (124 loc) · 3.37 KB
/
debrid.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
142
143
144
145
146
147
148
149
150
package alldebrid
import (
"encoding/json"
"fmt"
"github.com/andelf/go-curl"
"github.com/usineur/go-debrid/utils"
"html"
"os"
"sort"
"strings"
"time"
)
type service struct {
Link string
Host interface{} // could be a string or a boolean
Filename string
Icon string
Streaming interface{} // could be []string or map[string]inteface{}
Nb int
Error string
Filesize interface{}
}
func getDownloadLink(link string) (string, string, bool, error) {
fields := map[string]string{
"json": "true",
"link": link,
}
var s service
if res, _, err := sendRequest("/service.php", fields, nil); err != nil {
return "", "", false, err
} else if res == "login" {
if err := getCookie(); err != nil {
return "", "", false, err
}
return getDownloadLink(link)
} else if err := json.Unmarshal([]byte(res), &s); err != nil {
return "", "", false, err
} else if s.Error != "" {
return "", "", false, fmt.Errorf(s.Error)
} else if s.Link != "" {
return s.Link, s.Filename, false, nil
} else {
link, suffix := getStreamLink(s.Streaming)
return link, html.UnescapeString(s.Filename) + suffix, true, nil
}
}
func getStreamLink(streaming interface{}) (string, string) {
sLinks := streaming.(map[string]interface{})
var description []string
choice := -1
suffix := ".mp4"
err := fmt.Errorf("")
for i := range sLinks {
description = append(description, i)
}
sort.Strings(description)
fmt.Println("Only stream links are available. Please choose one entry.")
for i, j := range description {
fmt.Printf("\t%v - %v\n", i, j)
}
for err != nil {
if choice, err = utils.GetChoice(len(description)); err != nil || choice == -1 {
choice = -1
err = fmt.Errorf("Invalid choice")
fmt.Println(err)
} else {
err = nil
}
}
if strings.HasPrefix(description[choice], "audio") {
suffix = ".mp3"
}
return sLinks[description[choice]].(string), suffix
}
func DebridLink(link string, filename string) error {
if url, originalFilename, stream, err := getDownloadLink(link); err != nil {
return err
} else {
if filename == "" {
filename = originalFilename
}
filename = strings.Replace(filename, "/", "-", -1)
fp, _ := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, 0644)
defer fp.Close()
if stream {
fmt.Printf("Downloading of \"%v\"\n", filename)
if err := netcat(fp, url); err != nil {
return err
} else {
fmt.Printf("\nDownload finished\n")
return nil
}
}
easy := curl.EasyInit()
defer easy.Cleanup()
started := int64(0)
easy.Setopt(curl.OPT_URL, url)
easy.Setopt(curl.OPT_VERBOSE, false)
easy.Setopt(curl.OPT_NOPROGRESS, false)
easy.Setopt(curl.OPT_PROGRESSFUNCTION, func(dltotal, dlnow, _, _ float64, _ interface{}) bool {
if started == 0 {
started = time.Now().Unix()
}
percentage := dlnow / dltotal * 100
speed := dlnow / 1048576 / float64((time.Now().Unix() - started))
fmt.Printf("Downloading %s: %3.2f%%, Speed: %.1fMB/s \r", filename, percentage, speed)
return true
})
easy.Setopt(curl.OPT_WRITEFUNCTION, func(ptr []byte, userdata interface{}) bool {
file := userdata.(*os.File)
if _, err := file.Write(ptr); err != nil {
return false
}
return true
})
easy.Setopt(curl.OPT_WRITEDATA, fp)
if err := easy.Perform(); err != nil {
fmt.Printf("\n%v", err.Error())
}
time.Sleep(1000000000) // wait gorotine
fmt.Println()
return nil
}
}