This repository has been archived by the owner on Aug 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolver.go
243 lines (212 loc) · 5.68 KB
/
resolver.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package main
import (
"encoding/json"
"errors"
"io"
"net/http"
"net/url"
"strings"
)
const UserAgent = "linux:org.jlortiz.multiSav:v0.7.0 (by /u/jlortiz)"
type extType int
const (
EXT_NONE extType = iota
EXT_PICTURE
EXT_VIDEO
)
func getExtType(ext string) extType {
switch ext {
case "fmp4":
fallthrough
case "mp4":
fallthrough
case "webm":
fallthrough
case "gif":
fallthrough
case "m3u8":
fallthrough
case "mov":
return EXT_VIDEO
case "png":
fallthrough
case "jpg":
fallthrough
case "jpeg":
fallthrough
case "bmp":
return EXT_PICTURE
}
return EXT_NONE
}
type StripQueryResolver struct{}
func (StripQueryResolver) GetResolvableDomains() []string {
// Some discord images seem to require the query, but some don't work with it
// return []string{"media.discordapp.net"}
return nil
}
func (StripQueryResolver) ResolveURL(s string) (string, ImageEntry) {
ind := strings.LastIndexByte(s, '?')
if ind == -1 {
return RESOLVE_FINAL, nil
}
return s[:ind], nil
}
func (StripQueryResolver) GetRequest(u string) (*http.Response, error) {
return http.DefaultClient.Get(u)
}
type BlockingResolver struct{}
// TODO: Detect when the user's DNS is blocking something and prompt them to switch.
// It seems that catbox.moe likes to lose images... should I add a handler for it?
func (BlockingResolver) GetResolvableDomains() []string {
return nil // []string{"files.catbox.moe"}
}
func (BlockingResolver) ResolveURL(string) (string, ImageEntry) {
return RESOLVE_FINAL, nil
}
func (BlockingResolver) GetRequest(u string) (*http.Response, error) {
URL, err := url.Parse(u)
if err != nil {
return nil, err
}
return nil, errors.New("Cannot handle domain " + URL.Host)
}
func findByProps(u, p string) (string, error) {
req, _ := http.NewRequest("GET", u, http.NoBody)
req.Header.Set("User-Agent", UserAgent)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
data, err := io.ReadAll(resp.Body)
if err != nil && err != io.ErrUnexpectedEOF {
return "", err
}
s := string(data)
ind := strings.Index(s, "property=\""+p+"\" ")
if ind == -1 {
p += ":url"
ind = strings.Index(s, "property=\""+p+"\" ")
if ind == -1 {
return "", errors.New("property not found")
}
}
s = s[ind:]
ind = strings.Index(s, "content=\"")
if ind == -1 {
return "", errors.New("property has no associated content")
}
s = s[ind+len("content=\""):]
ind = strings.IndexByte(s, '"')
s = s[:ind]
return strings.Clone(s), nil
}
type PropOGVideoResolver struct{}
func (PropOGVideoResolver) GetResolvableDomains() []string {
return []string{}
}
func (PropOGVideoResolver) ResolveURL(u string) (string, ImageEntry) {
s, _ := findByProps(u, "og:video")
return s, nil
}
func (PropOGVideoResolver) GetRequest(u string) (*http.Response, error) {
return http.DefaultClient.Get(u)
}
type PropOGImageResolver struct{}
func (PropOGImageResolver) GetResolvableDomains() []string {
return []string{"gelbooru.com", "www.gelbooru.com", "danbooru.donmai.us", "ibb.co"}
}
func (PropOGImageResolver) ResolveURL(u string) (string, ImageEntry) {
s, _ := findByProps(u, "og:image")
return s, nil
}
func (PropOGImageResolver) GetRequest(u string) (*http.Response, error) {
req, _ := http.NewRequest("GET", u, http.NoBody)
req.Header.Set("User-Agent", UserAgent)
return http.DefaultClient.Do(req)
}
type RedgifsResolver struct{}
func (RedgifsResolver) GetResolvableDomains() []string {
return []string{"redgifs.com", "www.redgifs.com", "*.redgifs.com", "gfycat.com", "www.gfycat.com"}
}
var redgifs_auth = ""
func (RedgifsResolver) ResolveURL(u string) (string, ImageEntry) {
if strings.Contains(u, "thumbs") {
return RESOLVE_FINAL, nil
}
if strings.Contains(u, "i.redgifs") {
r, _ := http.Head(u)
if r == nil || r.StatusCode != 200 {
return "", nil
}
u = r.Request.URL.String()
}
var auth struct {
Token string
}
if redgifs_auth == "" {
req, _ := http.NewRequest("GET", "https://api.redgifs.com/v2/auth/temporary", http.NoBody)
req.Header.Set("User-Agent", UserAgent)
resp, err := http.DefaultClient.Do(req)
if err != nil || resp.StatusCode/100 != 2 {
return "", nil
}
d := json.NewDecoder(resp.Body)
err = d.Decode(&auth)
if err != nil {
return "", nil
}
auth.Token = "Bearer " + auth.Token
} else {
auth.Token = redgifs_auth
}
ind := strings.LastIndexByte(u, '/')
req, _ := http.NewRequest("GET", "https://api.redgifs.com/v2/gifs/"+u[ind+1:], http.NoBody)
req.Header.Set("Authorization", auth.Token)
req.Header.Set("User-Agent", UserAgent)
resp, err := http.DefaultClient.Do(req)
if err != nil || resp.StatusCode/100 != 2 {
if resp.StatusCode == 401 && redgifs_auth != "" {
redgifs_auth = ""
return RedgifsResolver{}.ResolveURL(u)
}
return "", nil
}
if redgifs_auth == "" {
redgifs_auth = auth.Token
}
var output struct {
Gif struct {
Urls struct {
HD string
}
}
}
d := json.NewDecoder(resp.Body)
d.Decode(&output)
return output.Gif.Urls.HD, nil
}
func (RedgifsResolver) GetRequest(u string) (*http.Response, error) {
req, _ := http.NewRequest("GET", u, http.NoBody)
req.Header.Set("User-Agent", UserAgent)
if redgifs_auth != "" {
req.Header.Set("Authorization", redgifs_auth)
}
return http.DefaultClient.Do(req)
}
type RetryWOQueryResolver struct{}
func (RetryWOQueryResolver) GetResolvableDomains() []string {
return []string{"cdn.discordapp.com", "media.discordapp.net"}
}
func (RetryWOQueryResolver) ResolveURL(string) (string, ImageEntry) { return "", nil }
func (RetryWOQueryResolver) GetRequest(u string) (*http.Response, error) {
resp, err := http.Get(u)
if err != nil || resp.StatusCode/100 == 2 {
return resp, err
}
ind := strings.IndexByte(u, '?')
if ind == -1 {
return resp, err
}
return http.Get(u[:ind])
}