-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprovider.go
336 lines (276 loc) · 7.07 KB
/
provider.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package luaprovider
import (
"context"
"fmt"
"io"
"net/http"
"github.com/mangalorg/libmangal"
"github.com/philippgille/gokv"
"github.com/pkg/errors"
"github.com/samber/lo"
"github.com/yuin/gluamapper"
lua "github.com/yuin/gopher-lua"
)
var _ libmangal.Provider = (*provider)(nil)
type provider struct {
store gokv.Store
info libmangal.ProviderInfo
options Options
state *lua.LState
logger *libmangal.Logger
fnSearchMangas,
fnMangaVolumes,
fnVolumeChapters,
fnChapterPages *lua.LFunction
}
func (p *provider) String() string {
return p.info.Name
}
func (p *provider) Close() error {
p.state.Close()
return p.store.Close()
}
func (p *provider) Info() libmangal.ProviderInfo {
return p.info
}
type IntoLValue interface {
IntoLValue() lua.LValue
}
// loadItems will run the given lua function,
// perform type checking and apply conversion function for each item.
func loadItems[Input IntoLValue, Output any](
ctx context.Context,
logger *libmangal.Logger,
state *lua.LState,
lfunc *lua.LFunction,
convert func(int, *lua.LTable) (Output, error),
args ...Input,
) ([]Output, error) {
state.SetContext(ctx)
err := state.CallByParam(lua.P{
Fn: lfunc,
NRet: 1,
Protect: true,
}, lo.Map(args, func(arg Input, _ int) lua.LValue {
return arg.IntoLValue()
})...)
if err != nil {
return nil, err
}
output := state.CheckTable(-1)
var values []lua.LValue
output.ForEach(func(_ lua.LValue, value lua.LValue) {
values = append(values, value)
})
var items = make([]Output, len(values))
for i, value := range values {
logger.Log(fmt.Sprintf("Parsing item %d", i))
table, ok := value.(*lua.LTable)
if !ok {
return nil, errors.Wrapf(fmt.Errorf("expected table, got %s", value.Type().String()), "parsing item %d", i)
}
item, err := convert(i, table)
if err != nil {
return nil, errors.Wrapf(err, "parsing item %d", i)
}
items[i] = item
}
logger.Log(fmt.Sprintf("Found %d items", len(items)))
return items, nil
}
// luaString wraps string to make IntoLValue method available.
// Required by the loadItems function.
type luaString string
func (l luaString) IntoLValue() lua.LValue {
return lua.LString(l)
}
func (p *provider) SetLogger(logger *libmangal.Logger) {
p.logger = logger
}
func (p *provider) SearchMangas(
ctx context.Context,
query string,
) ([]libmangal.Manga, error) {
p.logger.Log(fmt.Sprintf("Searching mangas with %q", query))
return loadItems(
ctx,
p.logger,
p.state,
p.fnSearchMangas,
func(i int, table *lua.LTable) (libmangal.Manga, error) {
var manga luaManga
if err := gluamapper.Map(table, &manga); err != nil {
return luaManga{}, err
}
if manga.ID == "" {
return luaManga{}, errors.New("id must be non-empty")
}
if manga.Title == "" {
return luaManga{}, errors.New("title must be non-empty")
}
manga.table = table
return manga, nil
},
luaString(query),
)
}
func (p *provider) MangaVolumes(
ctx context.Context,
manga libmangal.Manga,
) ([]libmangal.Volume, error) {
m, ok := manga.(luaManga)
if !ok {
return nil, fmt.Errorf("unexpected manga type: %T", manga)
}
return p.mangaVolumes(ctx, m)
}
func (p *provider) mangaVolumes(
ctx context.Context,
manga luaManga,
) ([]libmangal.Volume, error) {
p.logger.Log(fmt.Sprintf("Fetching volumes for %q", manga.Title))
return loadItems(
ctx,
p.logger,
p.state,
p.fnMangaVolumes,
func(_ int, table *lua.LTable) (libmangal.Volume, error) {
var volume luaVolume
if err := gluamapper.Map(table, &volume); err != nil {
return luaVolume{}, err
}
if volume.Number <= 0 {
return luaVolume{}, fmt.Errorf("invalid volume number: %d", volume.Number)
}
volume.table = table
volume.manga = &manga
return volume, nil
},
manga,
)
}
func (p *provider) VolumeChapters(
ctx context.Context,
volume libmangal.Volume,
) ([]libmangal.Chapter, error) {
v, ok := volume.(luaVolume)
if !ok {
return nil, fmt.Errorf("unexpected volume type: %T", volume)
}
return p.volumeChapters(ctx, v)
}
func (p *provider) volumeChapters(
ctx context.Context,
volume luaVolume,
) ([]libmangal.Chapter, error) {
p.logger.Log(fmt.Sprintf("Fetching chapters for volume %d", volume.Number))
return loadItems(
ctx,
p.logger,
p.state,
p.fnVolumeChapters,
func(i int, table *lua.LTable) (libmangal.Chapter, error) {
var chapter luaChapter
if err := gluamapper.Map(table, &chapter); err != nil {
return luaChapter{}, err
}
if chapter.Title == "" {
return luaChapter{}, errors.New("title must be non-empty")
}
if chapter.Number == 0 {
chapter.Number = float32(i)
}
chapter.table = table
chapter.volume = &volume
return chapter, nil
},
volume,
)
}
func (p *provider) ChapterPages(
ctx context.Context,
chapter libmangal.Chapter,
) ([]libmangal.Page, error) {
c, ok := chapter.(luaChapter)
if !ok {
return nil, fmt.Errorf("unexpected chapter type: %T", chapter)
}
return p.chapterPages(ctx, c)
}
func (p *provider) chapterPages(
ctx context.Context,
chapter luaChapter,
) ([]libmangal.Page, error) {
p.logger.Log(fmt.Sprintf("Fetching pages for %q", chapter.Title))
return loadItems(
ctx,
p.logger,
p.state,
p.fnChapterPages,
func(i int, table *lua.LTable) (libmangal.Page, error) {
var page luaPage
if err := gluamapper.Map(table, &page); err != nil {
return luaPage{}, err
}
page.chapter = &chapter
if page.URL == "" {
return luaPage{}, errors.New("url must be set")
}
if page.Extension == "" {
page.Extension = ".jpg"
}
if !fileExtensionRegex.MatchString(page.Extension) {
return luaPage{}, fmt.Errorf("invalid page extension: %s", page.Extension)
}
if page.Headers == nil {
page.Headers = make(map[string]string)
page.Headers["Referer"] = page.chapter.URL
page.Headers["Accept"] = "image/webp,image/apng,image/*,*/*;q=0.8"
// TODO: generate random user-agent
page.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
}
return page, nil
},
chapter,
)
}
func (p *provider) GetPageImage(
ctx context.Context,
page libmangal.Page,
) ([]byte, error) {
page_, ok := page.(luaPage)
if !ok {
return nil, fmt.Errorf("unexpected page type: %T", page)
}
return p.getPageImage(ctx, page_)
}
func (p *provider) getPageImage(
ctx context.Context,
page luaPage,
) ([]byte, error) {
p.logger.Log(fmt.Sprintf("Making HTTP GET request for %q", page.URL))
request, err := http.NewRequestWithContext(ctx, http.MethodGet, page.URL, nil)
if err != nil {
return nil, err
}
for key, value := range page.Headers {
request.Header.Set(key, value)
}
for key, value := range page.Cookies {
request.AddCookie(&http.Cookie{Name: key, Value: value})
}
response, err := p.options.HTTPClient.Do(request)
if err != nil {
return nil, err
}
defer response.Body.Close()
p.logger.Log("Got response")
if response.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", response.StatusCode)
}
image, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}
return image, nil
}