File size: 4,235 Bytes
866e5a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8cef93b
 
 
866e5a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main

import (
	"encoding/json"
	"fmt"
	"io"
	"math/rand"
	"net/http"
	"strings"
	"time"

	"github.com/daniwalter001/jackett_fiber/types/rd"
	"github.com/gofiber/fiber/v2"
)

var keys = []string{
	"TUCIGWCX5VJCPB5YPAD64NB25TZFFGAWGDVHELHZDLNUJEGX45BA",
	"T5Y37CBYWLSOR7DUGM4TWHCEI5FDJ63H2JIBMRGTZHOURSDWI3ZQ"}

var rdApikey = keys[rand.Intn(len(keys))]

func bearer() string {
	return fmt.Sprintf("Bearer %s", rdApikey)
}

func checkTorrentFileinRD(hash string) (rd.AvailabilityResponse, rd.RdError) {
	if len(hash) == 0 {
		return rd.AvailabilityResponse{}, rd.RdError{}

	}
	api := fmt.Sprintf("https://api.real-debrid.com/rest/1.0/torrents/instantAvailability/%s", hash)

	request := fiber.Get(api).Timeout(5 * time.Second)
	request.Set("Authorization", bearer())

	status, data, err := request.Bytes()

	if err != nil {
		return rd.AvailabilityResponse{}, rd.RdError{}
	}

	if status >= 400 {
		var resErr rd.RdError
		json.Unmarshal(data, &resErr)
		return rd.AvailabilityResponse{}, resErr
	}
	var resJson rd.AvailabilityResponse
	json.Unmarshal(data, &resJson)
	return resJson, rd.RdError{}

}

func addTorrentFileinRD2(magnet string) (rd.AddTorrentResponse, rd.RdError) {
	if len(magnet) == 0 {
		return rd.AddTorrentResponse{}, rd.RdError{Error: "magnet not defined"}
	}

	url := "https://api.real-debrid.com/rest/1.0/torrents/addMagnet"

	payload := strings.NewReader(fmt.Sprintf("magnet=%s", magnet))

	req, _ := http.NewRequest("POST", url, payload)

	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	req.Header.Add("User-Agent", "insomnia/8.6.1")
	req.Header.Add("Authorization", bearer())

	res, _ := http.DefaultClient.Do(req)

	body, _ := io.ReadAll(res.Body)

	if res.StatusCode >= 400 {
		var resErr rd.RdError
		json.Unmarshal(body, &resErr)
		return rd.AddTorrentResponse{}, resErr
	}
	var resJson rd.AddTorrentResponse
	json.Unmarshal(body, &resJson)
	defer res.Body.Close()
	return resJson, rd.RdError{}

}

func getTorrentInfofromRD(id string) (rd.TorrentInfoResponse, rd.RdError) {
	if len(id) == 0 {
		return rd.TorrentInfoResponse{}, rd.RdError{}

	}

	api := fmt.Sprintf("https://api.real-debrid.com/rest/1.0/torrents/info/%s", id)

	request := fiber.Get(api).Timeout(5 * time.Second)
	request.Set("Authorization", bearer())

	status, data, err := request.Bytes()

	if err != nil {
		return rd.TorrentInfoResponse{}, rd.RdError{}
	}

	if status != fiber.StatusOK {
		var resErr rd.RdError
		json.Unmarshal(data, &resErr)
		return rd.TorrentInfoResponse{}, resErr
	}
	var resJson rd.TorrentInfoResponse
	json.Unmarshal(data, &resJson)
	return resJson, rd.RdError{}

}

func selectFilefromRD(id string, files string) (bool, rd.RdError) {
	if len(id) == 0 {
		return false, rd.RdError{Error: "id not defined"}
	}
	if len(files) == 0 {
		files = "all"
	}

	api := fmt.Sprintf("https://api.real-debrid.com/rest/1.0/torrents/selectFiles/%s", id)

	payload := strings.NewReader(fmt.Sprintf("files=%s", files))

	req, _ := http.NewRequest("POST", api, payload)

	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	req.Header.Add("User-Agent", "insomnia/8.6.1")
	req.Header.Add("Authorization", bearer())

	res, _ := http.DefaultClient.Do(req)

	body, _ := io.ReadAll(res.Body)

	if res.StatusCode >= 400 {
		var resErr rd.RdError
		json.Unmarshal(body, &resErr)
		return false, resErr
	}
	defer res.Body.Close()
	return true, rd.RdError{}

}

func unrestrictLinkfromRD(link string) (rd.UnrestrictLinkResponse, rd.RdError) {
	if len(link) == 0 {
		return rd.UnrestrictLinkResponse{}, rd.RdError{}
	}

	api := "https://api.real-debrid.com/rest/1.0/unrestrict/link"

	payload := strings.NewReader(fmt.Sprintf("link=%s", link))

	req, _ := http.NewRequest("POST", api, payload)

	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	req.Header.Add("User-Agent", "insomnia/8.6.1")
	req.Header.Add("Authorization", bearer())

	res, _ := http.DefaultClient.Do(req)

	body, _ := io.ReadAll(res.Body)

	if res.StatusCode >= 400 {
		var resErr rd.RdError
		json.Unmarshal(body, &resErr)
		return rd.UnrestrictLinkResponse{}, resErr
	}

	var resJson rd.UnrestrictLinkResponse
	json.Unmarshal(body, &resJson)
	defer res.Body.Close()
	return resJson, rd.RdError{}

}