Skip to content

Commit

Permalink
Refactor and Fixes
Browse files Browse the repository at this point in the history
Moved everything around to make things clearer and fix a circular dependency

Also fixed a couple bugs
  • Loading branch information
rushsteve1 committed May 17, 2024
1 parent 894eafd commit a55adc7
Show file tree
Hide file tree
Showing 33 changed files with 457 additions and 235 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ We want to be respectful of MangaDex and their

- [OPDS](https://specs.opds.io/opds-1.2)
- [ATOM](https://validator.w3.org/feed/docs/atom.html)
- [OPDS-PSE](https://github.com/anansi-project/opds-pse/blob/master/v1.0.md)
- [OPDS-PSE](https://anansi-project.github.io/docs/opds-pse/specs/v1.0)
- [EPUB](https://www.w3.org/TR/epub-33/)
- [ComicInfo.xml](https://github.com/anansi-project/comicinfo/blob/main/DOCUMENTATION.md)
- [ComicInfo.xml](hhttps://anansi-project.github.io/docs/comicinfo/documentation)

## Thanks

Expand All @@ -44,4 +44,4 @@ mangadex-opds is licensed under the terms of the [AGPL license](./LICENSE.txt)
## Donations

This project does not take donations, please
[donate to MangaDex instead](https://namicomi.com/en/org/3Hb7HnWG/mangadex/subscriptions)
[donate to MangaDex instead](https://namicomi.com/en/org/3Hb7HnWG/mangadex/subscriptions)
66 changes: 0 additions & 66 deletions chapter/chapter_test.go

This file was deleted.

40 changes: 0 additions & 40 deletions manga/manga_test.go

This file was deleted.

48 changes: 40 additions & 8 deletions chapter/model.go → models/chapter.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package chapter
package models

import (
"cmp"
"log/slog"
"net/url"
"strings"
"time"
Expand All @@ -11,9 +12,11 @@ import (
)

type Chapter struct {
ID uuid.UUID `json:"id"`
Attributes ChapterAttributes `json:"attributes"`
Relationships []shared.Relationship `json:"relationships"`
ID uuid.UUID `json:"id"`
Attributes ChapterAttributes `json:"attributes"`
Relationships []Relationship `json:"relationships"`
manga *Manga
imgUrls []*url.URL
}

type ChapterAttributes struct {
Expand All @@ -35,7 +38,16 @@ func (c Chapter) URL() string {
func (c Chapter) FullTitle() string {
builder := strings.Builder{}

// TODO manga title
m := c.Manga()
var title string
if m == nil {
title = "Unknown Manga"
} else {
title = m.TrTitle()
}

builder.WriteString(title)
builder.WriteString(" - ")

if c.Attributes.Volume != "" {
builder.WriteString("[Vol. ")
Expand All @@ -47,11 +59,31 @@ func (c Chapter) FullTitle() string {
builder.WriteString(cmp.Or(c.Attributes.Chapter, "Unknown"))

if c.Attributes.Title != "" {
if c.Attributes.Chapter != "" || c.Attributes.Volume != "" {
builder.WriteString(" - ")
}
builder.WriteString(" - ")
builder.WriteString(c.Attributes.Title)
}

return strings.TrimSpace(builder.String())
}

func (c *Chapter) Manga() *Manga {
// Cache the cast manga so that we don't have to do it again
if c.manga != nil {
return c.manga
}

for _, rel := range c.Relationships {
if rel.Type == "manga" {
m, err := CastRelationship[Manga](&rel)
if err != nil {
slog.Error("error casting to manga", "error", err)
return nil
}

c.manga = &m
return &m
}
}

return nil
}
26 changes: 20 additions & 6 deletions chapter/fetch.go → models/chapter_fetch.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package chapter
package models

import (
"context"
Expand All @@ -10,7 +10,13 @@ import (
)

// Fetch gets the chapter information the MangaDex API and returns the [Chapter].
func Fetch(ctx context.Context, id uuid.UUID, queryParams url.Values) (c Chapter, err error) {
func FetchChapter(
ctx context.Context,
id uuid.UUID,
queryParams url.Values,
) (c Chapter, err error) {
shared.GlobalOptions = shared.TestOptions()

slog.InfoContext(ctx, "fetching chapter", "id", id)

queryPath, err := url.JoinPath("chapter", id.String())
Expand All @@ -26,15 +32,15 @@ func Fetch(ctx context.Context, id uuid.UUID, queryParams url.Values) (c Chapter
// https://api.mangadex.org/docs/01-concepts/reference-expansion/
// TODO optimize these
defaultParams := url.Values{
"includes[]": []string{"scanlation_group" /*"manga"*/},
"includes[]": []string{"scanlation_group", "manga"},
"translatedLanguage[]": []string{shared.GlobalOptions.Language},
}

for k, v := range defaultParams {
queryParams[k] = v
}

data, err := shared.QueryAPI[shared.Data[Chapter]](ctx, queryPath, queryParams)
data, err := shared.QueryAPI[Data[Chapter]](ctx, queryPath, queryParams)

return data.Data, err
}
Expand All @@ -55,11 +61,17 @@ type imageUrlResponse struct {
// This function uses the DataSaver and MDUploads global options
//
// See also: https://api.mangadex.org/docs/04-chapter/retrieving-chapter/
func (c Chapter) FetchImageURLs(ctx context.Context) (imgUrls []*url.URL, err error) {
// TODO support non MD-at-home
func (c *Chapter) FetchImageURLs(ctx context.Context) (imgUrls []*url.URL, err error) {
shared.GlobalOptions = shared.TestOptions()

// Image urls are cached off in the chapter so that they don't need to be fetched multiple times
if len(c.imgUrls) != 0 && c.Attributes.Pages != 0 {
return c.imgUrls, nil
}

slog.InfoContext(ctx, "fetching image urls for chapter", "id", c.ID)

// TODO support non MD-at-home
queryPath, err := url.JoinPath("at-home", "server", c.ID.String())
if err != nil {
return nil, err
Expand Down Expand Up @@ -104,5 +116,7 @@ func (c Chapter) FetchImageURLs(ctx context.Context) (imgUrls []*url.URL, err er

slog.DebugContext(ctx, "fetched image urls", "count", len(imgUrls))

c.imgUrls = imgUrls

return imgUrls, nil
}
45 changes: 45 additions & 0 deletions models/chapter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package models

import (
"context"
"testing"

"github.com/google/uuid"
)

// Girl's Last Tour chapter 43 uploaded by rozen
const ChapterID = "9a612118-1441-431a-979d-85958fb20cf2"

func Test_FetchChapter(t *testing.T) {
ctx := context.Background()

c, err := FetchChapter(ctx, uuid.MustParse(ChapterID), nil)
if err != nil {
t.Fatal(err)
}

if c.Attributes.Title == "" {
t.Fatal("no title")
}

t.Run("cast manga", func(t *testing.T) {
m := c.Manga()
if m == nil {
t.Fatal("manga did not cast")
}
})
}

func Test_FetchImageURLs(t *testing.T) {
ctx := context.Background()
c := Chapter{ID: uuid.MustParse(ChapterID)}

imgUrls, err := c.FetchImageURLs(ctx)
if err != nil {
t.Fatal(err)
}

if len(imgUrls) == 0 {
t.Fatal("no image urls")
}
}
10 changes: 7 additions & 3 deletions shared/data.go → models/data.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package shared
package models

import "cmp"
import (
"cmp"

"github.com/rushsteve1/mangadex-opds/shared"
)

type Data[T any] struct {
Result string `json:"result"`
Expand All @@ -11,5 +15,5 @@ type Data[T any] struct {

func Tr(m map[string]string) string {
// ja-ro is a special locale that MangaDex uses which should always exist
return cmp.Or(m[GlobalOptions.Language], m["ja-ro"], "Unknown")
return cmp.Or(m[shared.GlobalOptions.Language], m["ja-ro"], "Unknown")
}
Loading

0 comments on commit a55adc7

Please sign in to comment.