Skip to content
This repository was archived by the owner on Aug 27, 2024. It is now read-only.

Commit 3a2a5ba

Browse files
committed
Fix redgifs, use reddit previews
1 parent 1566917 commit 3a2a5ba

File tree

5 files changed

+50
-12
lines changed

5 files changed

+50
-12
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ streamy/*.mp4
1414
streamy/*.gif
1515
*.code-workspace
1616
tokens_test.go
17+
__debug_bin

imgur.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (img ImgurResolver) ResolveURL(link string) (string, ImageEntry) {
3636
fallthrough
3737
case "imgur.com":
3838
ind := strings.LastIndexByte(link, '/')
39-
if link[ind-1] == 'a' || link[ind-1] == 'y' {
39+
if strings.Count(link, "/") < 4 || link[ind-1] == 'a' || link[ind-1] == 'y' {
4040
link = "https://api.imgur.com/3/album/" + link[ind+1:]
4141
} else {
4242
link = "https://api.imgur.com/3/image/" + link[ind+1:]

prodBuf.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,11 @@ func (buf *BufferedImageProducer) Get(sel int, img **rl.Image, ffmpeg *VideoRead
454454
ind2 += ind
455455
}
456456
ext := strings.ToLower(URL[ind:ind2])
457-
if ext[0] == '=' {
457+
if ext == "=png8" {
458+
ext = ".png"
459+
} else if ext == "=pjpg" {
460+
ext = ".jpg"
461+
} else if ext[0] == '=' {
458462
ext = "." + ext[1:]
459463
}
460464
<-buf.selRecv

resolver.go

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"errors"
56
"io"
67
"net/http"
@@ -118,7 +119,7 @@ func findByProps(u, p string) (string, error) {
118119
type PropOGVideoResolver struct{}
119120

120121
func (PropOGVideoResolver) GetResolvableDomains() []string {
121-
return []string{"gfycat.com", "www.gfycat.com"}
122+
return []string{}
122123
}
123124

124125
func (PropOGVideoResolver) ResolveURL(u string) (string, ImageEntry) {
@@ -150,20 +151,43 @@ func (PropOGImageResolver) GetRequest(u string) (*http.Response, error) {
150151
type RedgifsResolver struct{}
151152

152153
func (RedgifsResolver) GetResolvableDomains() []string {
153-
return []string{"redgifs.com", "www.redgifs.com", "v3.redgifs.com", "thumbs4.redgifs.com"}
154+
return []string{"redgifs.com", "www.redgifs.com", "v3.redgifs.com", "thumbs4.redgifs.com", "i.redgifs.com", "thumbs44.redgifs.com", "gfycat.com", "www.gfycat.com"}
154155
}
155156

157+
const redgifs_auth = "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJodHRwczovL3d3dy5yZWRnaWZzLmNvbS8iLCJpYXQiOjE2NzczMDI0NjQsImV4cCI6MTY3NzM4ODg2NCwic3ViIjoiY2xpZW50LzE4MjNjMzFmN2QzLTc0NWEtNjU4OS0wMDA1LWQ4ZThmZTBhNDRjMiIsInNjb3BlcyI6InJlYWQiLCJ2YWxpZF9hZGRyIjoiMTI4LjExNC4yNTUuMjQ5IiwidmFsaWRfYWdlbnQiOiJNb3ppbGxhLzUuMCAoWDExOyBVYnVudHU7IExpbnV4IHg4Nl82NDsgcnY6MTA5LjApIEdlY2tvLzIwMTAwMTAxIEZpcmVmb3gvMTEwLjAiLCJyYXRlIjotMX0.ueRQcmHhOnb3azqvBj0j5rUonjuaW2D4iBl2SnC0a0gArGCPxEyi8Ia2EuYYDE0lZf214P7BBEy6NnQXinGj_BvEcvLR3s8-pWKu3KMrT-mA6Nod5UE07XTPP5HE-g36oD8kJw5djgDhxW4KKDIhHAwUdoimC6DWDbxERtJf04hDb1q-YKbPwEUiRbIVMsrtuDTceAnZV3cA2_Ij0vbcKEpNeqnEcmF2BmbS5DE_ATppykdUw7nN31N1dke7j2ybUGQ9zZKBaOGof_dJR7xABGgOVeDD0vONgX2OAA1HZIIirVEs0TZz4in-3bHWMhdXoKZaxH60-7AeFCdqd8em_w"
158+
156159
func (RedgifsResolver) ResolveURL(u string) (string, ImageEntry) {
157-
if strings.Contains(u, "thumbs4.") {
160+
if strings.Contains(u, "thumbs4") {
158161
return RESOLVE_FINAL, nil
159162
}
160-
s, _ := findByProps(u, "og:image")
161-
if s != "" {
162-
return s, nil
163+
if strings.Contains(u, "i.redgifs") {
164+
r, _ := http.Head(u)
165+
if r == nil || r.StatusCode != 200 {
166+
return "", nil
167+
}
168+
u = r.Request.URL.String()
163169
}
164170
ind := strings.LastIndexByte(u, '/')
165-
s, _ = findByProps("https://www.redgifs.com/ifr"+u[ind:], "og:video")
166-
return s, nil
171+
req, _ := http.NewRequest("GET", "https://api.redgifs.com/v2/gifs/"+u[ind+1:], http.NoBody)
172+
req.Header.Set("Authorization", redgifs_auth)
173+
req.Header.Set("User-Agent", UserAgent)
174+
resp, err := http.DefaultClient.Do(req)
175+
if err != nil || resp.StatusCode/100 != 2 {
176+
return "", nil
177+
}
178+
data, err := io.ReadAll(resp.Body)
179+
if err != nil {
180+
return "", nil
181+
}
182+
var output struct {
183+
Gif struct {
184+
Urls struct {
185+
HD string
186+
}
187+
}
188+
}
189+
json.Unmarshal(data, &output)
190+
return output.Gif.Urls.HD, nil
167191
}
168192

169193
func (RedgifsResolver) GetRequest(u string) (*http.Response, error) {

siteReddit.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,8 +517,17 @@ func (red *RedditImageEntry) GetURL() string {
517517
red.URL = "https://reddit.com" + red.Permalink
518518
} else if strings.HasPrefix(red.URL, "/r/") || strings.HasPrefix(red.URL, "/u/") {
519519
red.URL = "https://reddit.com" + red.URL
520-
} else if strings.HasPrefix(red.URL, "https://cdn.discordapp.com") && len(red.Preview.Images) != 0 {
521-
red.URL = red.Preview.Images[0].Source.URL
520+
} else if len(red.Preview.Images) != 0 {
521+
if red.Domain == "v.redd.it" {
522+
red.URL = red.Preview.Images[0].Source.URL
523+
} else if strings.HasPrefix(red.URL, "https://cdn.discordapp.com") {
524+
red.URL = red.Preview.Images[0].Source.URL
525+
} else {
526+
h, _ := http.Head(red.URL)
527+
if h == nil || h.StatusCode != 200 || h.Request.URL.Path == "/removed.png" {
528+
red.URL = red.Preview.Images[0].Source.URL
529+
}
530+
}
522531
}
523532
red.scanned = true
524533
}

0 commit comments

Comments
 (0)