forked from metafates/mangal
-
Notifications
You must be signed in to change notification settings - Fork 1
/
handlers.go
343 lines (295 loc) 路 7.32 KB
/
handlers.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
337
338
339
340
341
342
343
package tui
import (
"fmt"
"github.com/belphemur/mangal/anilist"
"github.com/belphemur/mangal/color"
"github.com/belphemur/mangal/downloader"
"github.com/belphemur/mangal/installer"
"github.com/belphemur/mangal/key"
"github.com/belphemur/mangal/log"
"github.com/belphemur/mangal/provider"
"github.com/belphemur/mangal/source"
"github.com/belphemur/mangal/style"
"github.com/belphemur/mangal/util"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/spf13/viper"
"golang.org/x/exp/slices"
"strings"
"sync"
)
func (b *statefulBubble) loadScrapers() tea.Cmd {
return func() tea.Msg {
b.progressStatus = "Loading scrapers"
scrapers, err := installer.Scrapers()
if err != nil {
log.Error(err)
b.errorChannel <- err
return nil
}
b.progressStatus = "Scrapers Loaded"
slices.SortFunc(scrapers, func(a, b *installer.Scraper) int {
return strings.Compare(b.Name, a.Name)
})
var items = make([]list.Item, len(scrapers))
for i, s := range scrapers {
items[i] = &listItem{
internal: s,
}
}
cmd := b.scrapersInstallC.SetItems(items)
b.scrapersLoadedChannel <- scrapers
return cmd
}
}
func (b *statefulBubble) waitForScrapersLoaded() tea.Cmd {
return func() tea.Msg {
select {
case res := <-b.scrapersLoadedChannel:
return res
case err := <-b.errorChannel:
b.lastError = err
return err
}
}
}
func (b *statefulBubble) installScraper(s *installer.Scraper) tea.Cmd {
return func() tea.Msg {
b.progressStatus = fmt.Sprintf("Installing %s", s.Name)
err := s.Install()
if err != nil {
log.Error(err)
b.errorChannel <- err
} else {
log.Info("scraper " + s.Name + " installed")
b.scraperInstalledChannel <- s
}
return nil
}
}
func (b *statefulBubble) waitForScraperInstallation() tea.Cmd {
return func() tea.Msg {
select {
case res := <-b.scraperInstalledChannel:
return res
case err := <-b.errorChannel:
b.lastError = err
return err
}
}
}
func (b *statefulBubble) loadSources(ps []*provider.Provider) tea.Cmd {
return func() tea.Msg {
var (
sources = make([]source.Source, len(ps))
wg = sync.WaitGroup{}
err error
)
wg.Add(len(ps))
for i, p := range ps {
go func(i int, p *provider.Provider) {
defer wg.Done()
if err != nil {
return
}
log.Info("loading source " + p.ID)
b.progressStatus = "Initializing source"
var s source.Source
s, err = p.CreateSource()
if err != nil {
log.Error(err)
b.errorChannel <- err
return
}
log.Info("source " + p.ID + " loaded")
sources[i] = s
}(i, p)
}
wg.Wait()
b.sourcesLoadedChannel <- sources
return nil
}
}
func (b *statefulBubble) waitForSourcesLoaded() tea.Cmd {
return func() tea.Msg {
select {
case res := <-b.sourcesLoadedChannel:
return res
case err := <-b.errorChannel:
b.lastError = err
return err
}
}
}
func (b *statefulBubble) searchManga(query string) tea.Cmd {
return func() tea.Msg {
log.Info("searching for " + query)
b.progressStatus = fmt.Sprintf("Searching among %s", util.Quantify(len(b.selectedSources), "source", "sources"))
var mangas = make([]*source.Manga, 0)
wg := sync.WaitGroup{}
wg.Add(len(b.selectedSources))
for _, s := range b.selectedSources {
go func(s source.Source) {
defer wg.Done()
sourceMangas, err := s.Search(query)
if err != nil {
log.Error(err)
b.errorChannel <- err
}
log.Infof("found %s from source %s", util.Quantify(len(sourceMangas), "manga", "mangas"), s.Name())
mangas = append(mangas, sourceMangas...)
}(s)
}
wg.Wait()
log.Infof("found %d mangas from %d sources", len(mangas), len(b.selectedSources))
b.foundMangasChannel <- mangas
return nil
}
}
func (b *statefulBubble) waitForMangas() tea.Cmd {
return func() tea.Msg {
select {
case found := <-b.foundMangasChannel:
return found
case err := <-b.errorChannel:
b.lastError = err
return err
}
}
}
func (b *statefulBubble) getChapters(manga *source.Manga) tea.Cmd {
return func() tea.Msg {
log.Info("getting chapters of " + manga.Name)
err := manga.Source.LoadChaptersOf(manga)
if err != nil {
log.Error(err)
b.errorChannel <- err
} else {
log.Infof("found %s", util.Quantify(len(manga.Chapters), "chapter", "chapters"))
b.foundChaptersChannel <- manga.Chapters
}
return nil
}
}
func (b *statefulBubble) waitForChapters() tea.Cmd {
return func() tea.Msg {
select {
case found := <-b.foundChaptersChannel:
return found
case err := <-b.errorChannel:
b.lastError = err
return err
}
}
}
func (b *statefulBubble) readChapter(chapter *source.Chapter) tea.Cmd {
return func() tea.Msg {
b.currentDownloadingChapter = chapter
err := downloader.Read(chapter, func(s string) {
b.progressStatus = s
})
if err != nil {
b.errorChannel <- err
} else {
b.chapterReadChannel <- struct{}{}
}
return nil
}
}
func (b *statefulBubble) waitForChapterRead() tea.Cmd {
return func() tea.Msg {
select {
case res := <-b.chapterReadChannel:
return res
case err := <-b.errorChannel:
b.lastError = err
return err
}
}
}
func (b *statefulBubble) downloadChapter(chapter *source.Chapter) tea.Cmd {
return func() tea.Msg {
b.currentDownloadingChapter = chapter
_, err := downloader.Download(chapter, func(s string) {
b.progressStatus = s
})
if err != nil {
if viper.GetBool(key.DownloaderStopOnError) {
b.errorChannel <- err
} else {
b.failedChapters = append(b.failedChapters, chapter)
b.chapterDownloadChannel <- struct{}{}
}
} else {
b.succededChapters = append(b.succededChapters, chapter)
b.chapterDownloadChannel <- struct{}{}
}
return nil
}
}
func (b *statefulBubble) waitForChapterDownload() tea.Cmd {
return func() tea.Msg {
select {
case res := <-b.chapterDownloadChannel:
return res
case err := <-b.errorChannel:
b.lastError = err
return err
}
}
}
func (b *statefulBubble) fetchAndSetAnilist(manga *source.Manga) tea.Cmd {
return func() tea.Msg {
alManga, err := anilist.FindClosest(manga.Name)
if err != nil {
// this error is not that important, we can ignore t
log.Warn(err)
} else {
b.closestAnilistMangaChannel <- alManga
}
return nil
}
}
func (b *statefulBubble) waitForAnilistFetchAndSet() tea.Cmd {
return func() tea.Msg {
return <-b.closestAnilistMangaChannel
}
}
func (b *statefulBubble) fetchAnilist(manga *source.Manga) tea.Cmd {
return func() tea.Msg {
log.Info("fetching anilist for " + manga.Name)
b.progressStatus = fmt.Sprintf("Fetching anilist for %s", style.Fg(color.Purple)(manga.Name))
mangas, err := anilist.SearchByName(manga.Name)
if err != nil {
log.Error(err)
b.errorChannel <- err
} else {
log.Infof("found %s", util.Quantify(len(mangas), "manga", "mangas"))
b.fetchedAnilistMangasChannel <- mangas
}
return nil
}
}
func (b *statefulBubble) waitForAnilist() tea.Cmd {
return func() tea.Msg {
select {
case found := <-b.fetchedAnilistMangasChannel:
return found
case err := <-b.errorChannel:
b.lastError = err
return err
}
}
}
func (b *statefulBubble) selectChapterBy(f func(chapter *source.Chapter) bool) tea.Cmd {
return func() tea.Msg {
for i, item := range b.chaptersC.Items() {
chapter := item.(*listItem).internal.(*source.Chapter)
if f(chapter) {
b.chaptersC.Select(i)
return nil
}
}
return nil
}
}