-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
92 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,4 +20,3 @@ fyne-cross/ | |
|
||
# binary | ||
comics-downloader | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package http | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
// ComicClient is the custom client. | ||
type ComicClient struct { | ||
Client *http.Client | ||
} | ||
|
||
// NewComicClient returns a ComicClient instance. | ||
func NewComicClient() *ComicClient { | ||
return &ComicClient{ | ||
Client: &http.Client{}, | ||
} | ||
} | ||
|
||
// PrepareRequest setup a `GET` request with customs headers. | ||
func (c *ComicClient) PrepareRequest(link string) (*http.Request, error) { | ||
req, err := http.NewRequest("GET", link, nil) | ||
|
||
if strings.Contains(link, "mangakakalot") { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
Girbons
Author
Owner
|
||
// avoid that MangaKakalot forbids the request. | ||
req.Header.Add("Referer", link) | ||
} | ||
|
||
return req, err | ||
} | ||
|
||
// GET Performs a Get request.. | ||
func (c *ComicClient) Get(link string) (*http.Response, error) { | ||
request, err := c.PrepareRequest(link) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return c.Client.Do(request) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package http | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestPrepareRequestMangakakalot(t *testing.T) { | ||
cc := NewComicClient() | ||
link := "http://mangakakalot.com" | ||
req, err := cc.PrepareRequest(link) | ||
|
||
assert.Equal(t, req.Header["Referer"], []string{link}) | ||
assert.Nil(t, err) | ||
} | ||
|
||
func TestPrepareRequest(t *testing.T) { | ||
cc := NewComicClient() | ||
link := "http://foo.com" | ||
req, err := cc.PrepareRequest(link) | ||
|
||
assert.Equal(t, len(req.Header["Referer"]), 0) | ||
assert.Nil(t, err) | ||
} |
Why check the link here? I recall it didn't break the other sites when I added it.
Since Mangakakalot and the other sites' image urls are on random domains the if statement is false, the Referer header doesn't get added and the response's body is
error code: 1020
.