forked from mangalorg/libmangal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.go
119 lines (97 loc) · 2.84 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
package libmangal
import (
"context"
"fmt"
"io"
"github.com/luevano/libmangal/logger"
"github.com/luevano/libmangal/mangadata"
"golang.org/x/mod/semver"
)
// ProviderInfo is the passport of the provider.
type ProviderInfo struct {
// ID is the unique identifier of the provider.
ID string `json:"id"`
// Name is the non-empty name of the provider.
Name string `json:"name"`
// Version is a semantic version of the provider.
//
// "v" prefix is not permitted.
// E.g. "0.1.0" is valid, but "v0.1.0" is not.
//
// See https://semver.org/
Version string `json:"version"`
// Description of the provider. May be empty.
Description string `json:"description"`
// Website of the provider. May be empty.
Website string `json:"website"`
}
// Validate checks if the ProviderInfo is valid.
// This means that ProviderInfo.Name is non-empty
// and ProviderInfo.Version is a valid semver.
func (p ProviderInfo) Validate() error {
if p.ID == "" {
return fmt.Errorf("ID must be non-empty")
}
if p.Name == "" {
return fmt.Errorf("name must be non-empty")
}
// according to the semver specification,
// versions should not have "v" prefix. E.g. v0.1.0 isn't a valid semver,
// however, for some bizarre reason, Go semver package requires this prefix.
if !semver.IsValid("v" + p.Version) {
return fmt.Errorf("invalid semver: %s", p.Version)
}
return nil
}
// ProviderLoader gives information about provider without loading it first.
type ProviderLoader interface {
fmt.Stringer
// Info information about Provider.
Info() ProviderInfo
// Load loads the Provider.
Load(ctx context.Context) (Provider, error)
}
// Provider exposes methods for searching mangas, getting chapters, pages and images.
type Provider interface {
fmt.Stringer
io.Closer
// Info information about Provider.
Info() ProviderInfo
// SetLogger sets logger to use for this provider.
SetLogger(*logger.Logger)
// SearchMangas searches for mangas with the given query.
//
// Implementation should utilize given logger.
SearchMangas(
ctx context.Context,
query string,
) ([]mangadata.Manga, error)
// MangaVolumes gets volumes of the manga.
//
// Implementation should utilize given logger.
MangaVolumes(
ctx context.Context,
manga mangadata.Manga,
) ([]mangadata.Volume, error)
// VolumeChapters gets chapters of the given volume.
//
// Implementation should utilize given logger.
VolumeChapters(
ctx context.Context,
volume mangadata.Volume,
) ([]mangadata.Chapter, error)
// ChapterPages gets pages of the given chapter.
//
// Implementation should utilize given logger
ChapterPages(
ctx context.Context,
chapter mangadata.Chapter,
) ([]mangadata.Page, error)
// GetPageImage gets raw image contents of the given page.
//
// Implementation should utilize given logger.
GetPageImage(
ctx context.Context,
page mangadata.Page,
) ([]byte, error)
}