Skip to content

Commit

Permalink
Mangadex URL fixes (#49)
Browse files Browse the repository at this point in the history
* Support mangadex.cc and mangadex.org

This update re-adds support for "mangadex.org", and maintains support for the previous URL "mangadex.cc"

* Mangadex testing fixes
  • Loading branch information
turtletowerz authored and Girbons committed Jan 17, 2020
1 parent b510340 commit 9e90770
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ You can invoke the `--help`:
|https://www.mangareader.net/ |✓|✗|✓|
|http://www.mangatown.com/ |✓|✗|✓|
|https://mangadex.cc/ |✓|✓|✓|

|https://mangadex.org/ |✓|✓|✓|

### Checking for mangas using a Raspberry Pi

Expand Down
1 change: 1 addition & 0 deletions pkg/sites/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var SupportedSites = []string{
"www.mangatown.com",
"www.mangahere.cc",
"mangadex.cc",
"mangadex.org",
}

// DisabledSites are the sites that are currently disabled.
Expand Down
4 changes: 2 additions & 2 deletions pkg/sites/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func LoadComicFromSource(source, url, country, format, imagesFormat string, all,
siteSource = &Mangareader{}
case "www.mangatown.com":
siteSource = &Mangatown{}
case "mangadex.cc":
siteSource = NewMangadex(country)
case "mangadex.cc", "mangadex.org":
siteSource = NewMangadex(country, source)
default:
err = fmt.Errorf("It was not possible to determine the source")
return collection, err
Expand Down
9 changes: 6 additions & 3 deletions pkg/sites/mangadex.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@ import (

type Mangadex struct {
country string
baseURL string
Client *mangadex.Client
}

// NewMangadex returns a Mangadex instance
func NewMangadex(country string) *Mangadex {
func NewMangadex(country, source string) *Mangadex {
mangadexBase := "https://"+source+"/"
return &Mangadex{
country: country,
baseURL: mangadexBase,
Client: mangadex.New(
mangadex.WithBase("https://mangadex.cc/"),
mangadex.WithBase(mangadexBase),
),
}
}
Expand Down Expand Up @@ -58,7 +61,7 @@ func (m *Mangadex) RetrieveIssueLinks(url string, all, last bool) ([]string, err
if m.country != "" && c.LangCode != m.country {
continue
}
urls = append(urls, "https://mangadex.cc/chapter"+c.ID.String())
urls = append(urls, m.baseURL+"chapter/"+c.ID.String())
}
if len(urls) == 0 {
return nil, errors.New("no chapters found")
Expand Down
39 changes: 21 additions & 18 deletions pkg/sites/mangadex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@ import (
"github.com/stretchr/testify/assert"
)

const testMangadexBase string = "mangadex.org"
const testMangadexURL string = "https://"+testMangadexBase+"/"

func TestMangadexGetInfo(t *testing.T) {
md := NewMangadex("")
md := NewMangadex("", testMangadexBase)

name, issueNumber := md.GetInfo("https://mangadex.cc/chapter/155061/1")
name, issueNumber := md.GetInfo(testMangadexURL+"chapter/155061/1")
assert.Equal(t, "Naruto", name)
assert.Equal(t, "Vol 60 Chapter 575, A Will of Stone", issueNumber)
}

func TestMangadexSetup(t *testing.T) {
md := NewMangadex("")
md := NewMangadex("", testMangadexBase)
comic := new(core.Comic)

comic.URLSource = "https://mangadex.cc/chapter/155061/1"
comic.URLSource = testMangadexURL+"chapter/155061/1"

err := md.Initialize(comic)

Expand All @@ -28,44 +31,44 @@ func TestMangadexSetup(t *testing.T) {
}

func TestMangadexRetrieveIssueLinks(t *testing.T) {
md := NewMangadex("")
urls, err := md.RetrieveIssueLinks("https://mangadex.cc/chapter/155061/", false, false)
md := NewMangadex("", testMangadexBase)
urls, err := md.RetrieveIssueLinks(testMangadexURL+"chapter/155061/", false, false)
assert.Nil(t, err)
assert.Equal(t, 1, len(urls))
}

func TestMangadexRetrieveIssueLinksAllChapter(t *testing.T) {
md := NewMangadex("gb")
urls, err := md.RetrieveIssueLinks("https://mangadex.cc/title/5/naruto/", true, false)
md := NewMangadex("gb", testMangadexBase)
urls, err := md.RetrieveIssueLinks(testMangadexURL+"title/5/naruto/", true, false)
assert.Nil(t, err)
assert.Len(t, urls, 569)
}

func TestMangadexRetrieveIssueLinksLastChapter(t *testing.T) {
md := NewMangadex("gb")
urls, err := md.RetrieveIssueLinks("https://mangadex.cc/title/5/naruto/", false, true)
md := NewMangadex("gb", testMangadexBase)
urls, err := md.RetrieveIssueLinks(testMangadexURL+"title/5/naruto/", false, true)
assert.Nil(t, err)
assert.Len(t, urls, 1)
assert.Equal(t, "https://mangadex.cc/chapter670438", urls[0])
assert.Equal(t, testMangadexURL+"chapter/670438", urls[0])
}

func TestMangadexUnsupportedURL(t *testing.T) {
md := NewMangadex("")
_, err := md.RetrieveIssueLinks("https://mangadex.cc/", false, false)
md := NewMangadex("", testMangadexBase)
_, err := md.RetrieveIssueLinks(testMangadexURL, false, false)
assert.EqualError(t, err, "URL not supported")
_, err = md.RetrieveIssueLinks("https://mangadex.cc/test/0/", false, false)
_, err = md.RetrieveIssueLinks(testMangadexURL+"test/0/", false, false)
assert.EqualError(t, err, "URL not supported")
}

func TestMangadexNoManga(t *testing.T) {
md := NewMangadex("")
_, err := md.RetrieveIssueLinks("https://mangadex.cc/title/0/", false, false)
md := NewMangadex("", testMangadexBase)
_, err := md.RetrieveIssueLinks(testMangadexURL+"title/0/", false, false)
assert.Error(t, err)
assert.Contains(t, err.Error(), "Manga ID does not exist")
}

func TestMangadexNoChapters(t *testing.T) {
md := NewMangadex("xyz")
_, err := md.RetrieveIssueLinks("https://mangadex.cc/title/5/naruto/", true, false)
md := NewMangadex("xyz", testMangadexBase)
_, err := md.RetrieveIssueLinks(testMangadexURL+"title/5/naruto/", true, false)
assert.EqualError(t, err, "no chapters found")
}

0 comments on commit 9e90770

Please sign in to comment.