Skip to content

Commit 4355f46

Browse files
authored
Merge pull request #125 from browningluke/fix/cubari/non-proxied-pages
fix(cubari): Identify proxied pages correctly
2 parents 83ce64e + 22c2115 commit 4355f46

File tree

5 files changed

+55
-14
lines changed

5 files changed

+55
-14
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ $ mangathr <COMMAND> -s SOURCE QUERY
2424

2525
If Go is installed, install it with:
2626
```
27-
$ go install github.com/browningluke/mangathr/cmd/mangathr@latest
27+
$ go install github.com/browningluke/mangathr/v2/cmd/mangathr@latest
2828
```
2929

3030
### Pre-build binary

internal/logging/logging.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,15 @@ func Errorln(a ...interface{}) {
104104
func Errorf(format string, a ...interface{}) {
105105
output(errorLogger, fmt.Sprintf("%s%s%s\n", "\033[31m", fmt.Sprintf(format, a...), "\033[0m"))
106106
}
107+
108+
// Fatal
109+
110+
func Fatalln(a ...interface{}) {
111+
output(errorLogger, fmt.Sprintf("%s%s%s\n", "\033[31m", fmt.Sprint(a...), "\033[0m"))
112+
os.Exit(1)
113+
}
114+
115+
func Fatalf(format string, a ...interface{}) {
116+
output(errorLogger, fmt.Sprintf("%s%s%s\n", "\033[31m", fmt.Sprintf(format, a...), "\033[0m"))
117+
os.Exit(1)
118+
}

internal/rester/rester.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package rester
33
import (
44
"context"
55
"crypto/tls"
6-
"errors"
76
"fmt"
87
"github.com/browningluke/mangathr/v2/internal/logging"
98
"io"
@@ -48,7 +47,7 @@ func New() *RESTer {
4847

4948
func (r RESTer) Do(retries int, timeout string) (interface{}, Response) {
5049
if retries == 0 {
51-
panic(errors.New("retried too many times, giving up"))
50+
logging.Fatalln("retried too many times, giving up")
5251
}
5352
body, res, err := r.job()
5453

@@ -70,7 +69,7 @@ func (r RESTer) Do(retries int, timeout string) (interface{}, Response) {
7069

7170
func (r RESTer) DoWithHelperFunc(retries int, timeout string, f func(res Response, err error)) interface{} {
7271
if retries == 0 {
73-
panic(errors.New("retried too many times, giving up"))
72+
logging.Fatalln("retried too many times, giving up")
7473
}
7574
body, res, err := r.job()
7675

internal/sources/cubari/pages.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,35 @@ func (m *Scraper) addPagesToChapter(chapter *manga.Chapter) *logging.ScraperErro
1616
// Get pages from proxy URL
1717
// (if using GIST provider)
1818
if m.provider == GIST {
19-
jsonResp, _ := rester.New().Get(fmt.Sprintf("%s%s", SITEROOT, pages[0]),
20-
map[string]string{}, []rester.QueryParam{}).Do(4, "100ms")
21-
jsonString := jsonResp.(string)
19+
// Check if pages contain a valid URL
20+
allValid := true
2221

23-
urls, ok := parseImgurStyle([]byte(jsonString))
24-
if !ok {
25-
return &logging.ScraperError{
26-
Error: errors.New("failed to get imgur URLs from proxy"),
27-
Message: "An error occurred while getting pages from imgur",
28-
Code: 0,
22+
for _, u := range pages {
23+
if !utils.IsValidURL(u) {
24+
allValid = false
25+
break
2926
}
3027
}
3128

32-
pages = urls
29+
// If not all are valid urls, that means they are
30+
// being proxied through Cubari so handle accordingly.
31+
if !allValid {
32+
logging.Debugln(fmt.Sprintf("Found pages: %v", pages))
33+
jsonResp, _ := rester.New().Get(fmt.Sprintf("%s%s", SITEROOT, pages[0]),
34+
map[string]string{}, []rester.QueryParam{}).Do(4, "100ms")
35+
jsonString := jsonResp.(string)
36+
37+
urls, ok := parseImgurStyle([]byte(jsonString))
38+
if !ok {
39+
return &logging.ScraperError{
40+
Error: errors.New("failed to get imgur URLs from proxy"),
41+
Message: "An error occurred while getting pages from imgur",
42+
Code: 0,
43+
}
44+
}
45+
46+
pages = urls
47+
}
3348
}
3449

3550
// (if using MANGASEE provider)

internal/utils/utils.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"github.com/google/uuid"
66
"github.com/schollz/progressbar/v3"
7+
"net/url"
78
"os"
89
"os/signal"
910
"path/filepath"
@@ -139,3 +140,17 @@ func IsValidUUID(u string) bool {
139140
_, err := uuid.Parse(u)
140141
return err == nil
141142
}
143+
144+
func IsValidURL(str string) bool {
145+
u, err := url.ParseRequestURI(str)
146+
if err != nil {
147+
return false
148+
}
149+
150+
// Check if the scheme and host are present
151+
if u.Scheme == "" || u.Host == "" {
152+
return false
153+
}
154+
155+
return true
156+
}

0 commit comments

Comments
 (0)