-
Notifications
You must be signed in to change notification settings - Fork 48
/
mangadex.go
223 lines (206 loc) · 5.7 KB
/
mangadex.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package sites
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"github.com/Girbons/comics-downloader/pkg/config"
"github.com/Girbons/comics-downloader/pkg/core"
"github.com/Girbons/comics-downloader/pkg/util"
)
// Mangadex represents a mangadex instance.
type Mangadex struct {
country string
baseURL string
options *config.Options
}
// NewMangadex returns a Mangadex instance
func NewMangadex(options *config.Options) *Mangadex {
return &Mangadex{
country: strings.ToLower(options.Country),
options: options,
}
}
func (m *Mangadex) getManga(mangaID string) (title string, err error) {
url := fmt.Sprintf("https://api.mangadex.org/manga/%s", mangaID)
res, err := http.Get(url)
if err != nil {
return "", err
}
defer res.Body.Close()
var mangaRes struct {
Result string `json:"result"`
Data struct {
Attributes struct {
Titles map[string]string `json:"title"`
} `json:"attributes"`
} `json:"data"`
}
if err := json.NewDecoder(res.Body).Decode(&mangaRes); err != nil {
return "", err
}
if mangaRes.Result != "ok" {
return "", fmt.Errorf("Unexpected response")
}
for lang, t := range mangaRes.Data.Attributes.Titles {
title = t
if m.country == "" || m.country == lang {
break
}
}
return title, nil
}
// Get a list of chapter IDs of a manga.
func (m *Mangadex) getChapters(mangaID string) ([]string, error) {
url := fmt.Sprintf("https://api.mangadex.org/manga/%s/aggregate?", mangaID)
if m.country != "" {
url += fmt.Sprintf("&translatedLanguage[]=%s", m.country)
}
res, err := http.Get(url)
if err != nil {
return nil, err
}
defer res.Body.Close()
var chaptersRes struct {
Result string `json:"result"`
Volumes map[string]struct {
Name string `json:"volume"`
Chapters map[string]struct {
ID string `json:"id"`
Name string `json:"chapter"`
} `json:"chapters"`
} `json:"volumes"`
}
if err := json.NewDecoder(res.Body).Decode(&chaptersRes); err != nil {
// This is not ideal. MangaDex returns an empty array (`[]`) when no volume
// exists and a `map[string]interface{}` otherwise.
return []string{}, nil
}
if chaptersRes.Result != "ok" {
return nil, fmt.Errorf("Unexpected response")
}
var ids []string
for _, v := range chaptersRes.Volumes {
for _, c := range v.Chapters {
url := fmt.Sprintf("https://mangadex.org/chapter/%s", c.ID)
ids = append(ids, url)
}
}
return ids, nil
}
// Get a list of a chapters images.
func (m *Mangadex) getChapter(chapterID string) (mangaID, volume, chapter, title string, images []string, err error) {
url := fmt.Sprintf("https://api.mangadex.org/chapter/%s", chapterID)
res, err := http.Get(url)
if err != nil {
return "", "", "", "", nil, err
}
defer res.Body.Close()
var chapterRes struct {
Result string `json:"result"`
Data struct {
Attributes struct {
Volume string `json:"volume"`
Chapter string `json:"chapter"`
Title string `json:"title"`
Hash string `json:"hash"`
Data []string `json:"data"`
} `json:"attributes"`
Relationships []struct {
ID string `json:"id"`
Type string `json:"type"`
} `json:"relationships"`
} `json:"data"`
}
if err := json.NewDecoder(res.Body).Decode(&chapterRes); err != nil {
return "", "", "", "", nil, err
}
if chapterRes.Result != "ok" {
return "", "", "", "", nil, fmt.Errorf("Unexpected response")
}
var imagesRes struct {
Result string `json:"result"`
Chapter struct {
Hash string `json:"hash"`
Data []string `json:"data"`
} `json:"chapter`
}
res, err = http.Get(fmt.Sprintf("https://api.mangadex.org/at-home/server/%s", chapterID))
if err := json.NewDecoder(res.Body).Decode(&imagesRes); err != nil {
return "", "", "", "", nil, err
}
for _, file := range imagesRes.Chapter.Data {
imageUrl := fmt.Sprintf("https://uploads.mangadex.org/data/%s/%s", imagesRes.Chapter.Hash, file)
images = append(images, imageUrl)
}
if m.options.Debug {
m.options.Logger.Debug(fmt.Sprintf("Image Links found: %s", strings.Join(images, " ")))
}
for _, rel := range chapterRes.Data.Relationships {
if rel.Type == "manga" {
mangaID = rel.ID
break
}
}
return mangaID, chapterRes.Data.Attributes.Volume, chapterRes.Data.Attributes.Chapter, chapterRes.Data.Attributes.Title, images, nil
}
// RetrieveIssueLinks retrieve the issue links for the given comic.
func (m *Mangadex) RetrieveIssueLinks() ([]string, error) {
parts := util.TrimAndSplitURL(m.options.URL)
if len(parts) < 5 {
return nil, errors.New("URL not supported")
}
switch parts[3] {
case "chapter":
return []string{m.options.URL}, nil
case "title":
return m.getChapters(parts[4])
default:
return nil, errors.New("URL not supported")
}
}
// GetInfo extracts the basic info from the given url.
func (m *Mangadex) GetInfo(url string) (string, string) {
parts := util.TrimAndSplitURL(url)
if len(parts) < 5 {
return "", ""
}
switch parts[3] {
case "chapter":
mangaID, volume, chapter, title, _, err := m.getChapter(parts[4])
if err != nil {
return "", ""
}
chapterTitle := fmt.Sprintf("Vol %s Chapter %s", volume, chapter)
if title != "" {
chapterTitle += fmt.Sprintf(", %s", title)
}
mangaTitle, err := m.getManga(mangaID)
if err != nil {
return "", chapterTitle
}
return mangaTitle, chapterTitle
case "title":
mangaTitle, err := m.getManga(parts[4])
if err != nil {
return "", ""
}
return mangaTitle, ""
default:
return "", ""
}
}
// Initialize loads links and metadata from mangadex
func (m *Mangadex) Initialize(comic *core.Comic) error {
parts := util.TrimAndSplitURL(comic.URLSource)
if len(parts) < 4 {
return fmt.Errorf("URL not supported")
}
_, _, _, _, images, err := m.getChapter(parts[4])
if err != nil {
return err
}
comic.Links = images
return nil
}