-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.go
186 lines (150 loc) · 4.61 KB
/
client.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
package libmangal
import (
"context"
"fmt"
"github.com/spf13/afero"
"golang.org/x/sync/errgroup"
)
// NewClient creates a new client from ProviderLoader.
// ClientOptions must be non-nil. Use DefaultClientOptions for defaults.
// It will validate ProviderLoader.Info and load the provider.
func NewClient(
ctx context.Context,
loader ProviderLoader,
options ClientOptions,
) (*Client, error) {
if err := loader.Info().Validate(); err != nil {
return nil, err
}
provider, err := loader.Load(ctx)
if err != nil {
return nil, err
}
return &Client{
provider: provider,
options: options,
}, nil
}
// Client is the wrapper around Provider with the extended functionality.
// It's the core of the libmangal
type Client struct {
provider Provider
options ClientOptions
}
func (c *Client) FS() afero.Fs {
return c.options.FS
}
func (c *Client) Anilist() *Anilist {
return c.options.Anilist
}
func (c *Client) SetLogFunc(log LogFunc) {
c.options.Log = log
}
// SearchMangas searches for mangas with the given query
func (c *Client) SearchMangas(ctx context.Context, query string) ([]Manga, error) {
return c.provider.SearchMangas(ctx, c.options.Log, query)
}
// MangaVolumes gets chapters of the given manga
func (c *Client) MangaVolumes(ctx context.Context, manga Manga) ([]Volume, error) {
return c.provider.MangaVolumes(ctx, c.options.Log, manga)
}
// VolumeChapters gets chapters of the given manga
func (c *Client) VolumeChapters(ctx context.Context, volume Volume) ([]Chapter, error) {
return c.provider.VolumeChapters(ctx, c.options.Log, volume)
}
// ChapterPages gets pages of the given chapter
func (c *Client) ChapterPages(ctx context.Context, chapter Chapter) ([]Page, error) {
return c.provider.ChapterPages(ctx, c.options.Log, chapter)
}
func (c *Client) String() string {
return c.provider.Info().Name
}
// Info returns info about provider
func (c *Client) Info() ProviderInfo {
return c.provider.Info()
}
// DownloadChapter downloads and saves chapter to the specified
// directory in the given format.
//
// It will return resulting chapter path joined with DownloadOptions.Directory
func (c *Client) DownloadChapter(
ctx context.Context,
chapter Chapter,
options DownloadOptions,
) (string, error) {
c.options.Log(fmt.Sprintf("Downloading chapter %q as %s", chapter, options.Format))
tmpClient := Client{
provider: c.provider,
options: c.options,
}
tmpClient.options.FS = afero.NewMemMapFs()
path, err := tmpClient.downloadChapterWithMetadata(ctx, chapter, options, func(path string) (bool, error) {
return afero.Exists(c.options.FS, path)
})
if err != nil {
return "", err
}
if err := mergeDirectories(
c.FS(), options.Directory,
tmpClient.FS(), options.Directory,
); err != nil {
return "", err
}
if options.ReadAfter {
return path, c.readChapter(ctx, path, chapter, options.ReadIncognito)
}
return path, nil
}
// DownloadPagesInBatch downloads multiple pages in batch
// by calling DownloadPage for each page in a separate goroutines.
// If any of the pages fails to download it will stop downloading other pages
// and return error immediately
func (c *Client) DownloadPagesInBatch(
ctx context.Context,
pages []Page,
) ([]PageWithImage, error) {
c.options.Log(fmt.Sprintf("Downloading %d pages", len(pages)))
g, _ := errgroup.WithContext(ctx)
downloadedPages := make([]PageWithImage, len(pages))
for i, page := range pages {
// https://github.com/golang/go/wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables
i, page := i, page
g.Go(func() error {
c.options.Log(fmt.Sprintf("Page #%03d: downloading", i+1))
downloaded, err := c.DownloadPage(ctx, page)
if err != nil {
return err
}
c.options.Log(fmt.Sprintf("Page #%03d: done", i+1))
downloadedPages[i] = downloaded
return nil
})
}
if err := g.Wait(); err != nil {
return nil, err
}
return downloadedPages, nil
}
// DownloadPage downloads a page contents (image)
func (c *Client) DownloadPage(ctx context.Context, page Page) (PageWithImage, error) {
if withImage, ok := page.(PageWithImage); ok {
return withImage, nil
}
image, err := c.provider.GetPageImage(ctx, c.options.Log, page)
if err != nil {
return nil, err
}
return &pageWithImage{
Page: page,
image: image,
}, nil
}
func (c *Client) ComputeMangaFilename(manga Manga) string {
return c.options.MangaNameTemplate(c.String(), manga)
}
func (c *Client) ComputeVolumeFilename(volume Volume) string {
return c.options.VolumeNameTemplate(c.String(), volume)
}
func (c *Client) ComputeChapterFilename(chapter Chapter, format Format) string {
return c.options.ChapterNameTemplate(c.String(), chapter) + format.Extension()
}