-
Notifications
You must be signed in to change notification settings - Fork 1
/
searcher.go
228 lines (179 loc) · 4.93 KB
/
searcher.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
package manganatoapi
import (
"errors"
"sync"
"github.com/gocolly/colly"
"github.com/gocolly/colly/extensions"
)
var (
// throws when no results are found
ErrPageNotFound = errors.New("this page does not exist or has been deleted")
c *colly.Collector
wg sync.WaitGroup
)
// provides methods for fetching data from https://manganato.com
type Searcher struct {
MethodsDescription map[string]string
}
// any struct types with ID implement Searchable
type Searchable interface {
getID(string)
}
func initCrawler() {
c = colly.NewCollector(
colly.AllowedDomains(
manganatoURL,
readManganatoURL,
),
colly.MaxDepth(2),
colly.Async(true),
// colly.Debugger(&debug.LogDebugger{}),
)
extensions.RandomUserAgent(c)
}
func cloneCrawler() *colly.Collector {
c2 := c.Clone()
extensions.RandomUserAgent(c2)
return c2
}
func deleteCrawler() {
c = nil
}
// returns a new Searcher
func NewSearcher() Searcher {
methodDescription := map[string]string{
"SearchManga": "receives name of a manga user wants to search for and returns a list of mangas that match the name",
"PickManga": "receives the id of the specific manga then returns that manga if found",
"ReadMangaChapter": "receives the manga id and chapter id then returns pages of that specific chapter",
"PickAuthor": "receives the id of the author then returns a list of mangas by him/her",
"PickGenre": "receives genre id then returns a list of mangas with that genre",
"SearchLatestUpdatedManga": "returns list of latest updated mangas from the first page of https://manganato.com",
"IsSearchable": "returns whether the struct type implements Searchable interface",
}
return Searcher{
MethodsDescription: methodDescription,
}
}
// receives name of a manga user wants to search for
// and returns a list of mangas that match the name.
// Each manga will have ID, Name, Author and Updated fields
//
// https://manganato.com/search/story/<name>
func (s *Searcher) SearchManga(name string) (*[]Manga, error) {
initCrawler()
defer deleteCrawler()
tmp := getMangaList(changeSpaceToUnderscore(name))
if len(tmp) == 0 {
return nil, ErrPageNotFound
}
mgs := []Manga{}
for _, mg := range tmp {
wg.Add(1)
go func(m Manga, c2 *colly.Collector) {
defer wg.Done()
createAuthor(&m, c2)
mgs = append(mgs, m)
}(mg, cloneCrawler())
}
wg.Wait()
return &mgs, nil
}
// receives the id of the specific manga
// then returns that manga if found.
// Every fields in Manga struct will be setted
//
// https://readmanganato.com/manga-<id>
func (s *Searcher) PickManga(id string) (*Manga, error) {
initCrawler()
defer deleteCrawler()
m := Manga{
ID: id,
}
tmp := m
m.getMangaByID()
if m.compareManga(&tmp) {
return nil, ErrPageNotFound
}
return &m, nil
}
// receives the manga id and chapter id
// then returns pages of that specific chapter.
// Each page will have ID and ImageURL setted
//
// https://readmanganato.com/manga-<mangaId>/chapter-<chapterId>
func (s *Searcher) ReadMangaChapter(mangaId, chapterId string) (*[]Page, error) {
initCrawler()
defer deleteCrawler()
ch := Chapter{
ID: chapterId,
MangaID: mangaId,
}
ch.getChapterByID()
if len(ch.Pages) == 0 {
return nil, ErrPageNotFound
}
return &ch.Pages, nil
}
// receives the id of the author then returns a list of mangas by him/her.
// Each manga will have ID, Name, Author and Updated fields
//
// https://manganato.com/author/story/<authorId>
func (s *Searcher) PickAuthor(authorId string) (*[]Manga, error) {
initCrawler()
defer deleteCrawler()
a := Author{
ID: authorId,
}
a.getMangaListByAuthorID()
if len(a.Mangas) == 0 {
return nil, ErrPageNotFound
}
for i, mg := range a.Mangas {
wg.Add(1)
go func(m Manga, index int, c2 *colly.Collector) {
defer wg.Done()
createAuthor(&m, c2)
a.Mangas[index] = m
}(mg, i, cloneCrawler())
}
wg.Wait()
return &a.Mangas, nil
}
// receives genre id then returns a list of mangas with that genre.
// Each manga will have ID, Name, Author(Name only), Updated,
//
// Views and Description fields
//
// https://manganato.com/genre-<genreId>
func (s *Searcher) PickGenre(genreId string) (*[]Manga, error) {
initCrawler()
defer deleteCrawler()
g := Genre{
ID: genreId,
}
g.getMangaListByGenreID()
if len(g.Mangas) == 0 {
return nil, ErrPageNotFound
}
return &g.Mangas, nil
}
// returns list of latest updated mangas
// from the first page of https://manganato.com.
// Each manga will have ID, Name and Author(Name only) fields
func (s *Searcher) SearchLatestUpdatedManga() (*[]Manga, error) {
initCrawler()
defer deleteCrawler()
tmp := getLatestUpdatedManga()
if len(tmp) == 0 {
return nil, ErrPageNotFound
}
return &tmp, nil
}
// returns whether the struct type implements Searchable interface
func (s *Searcher) IsSearchable(any interface{}) bool {
switch any.(type) {
case Searchable:
return true
}
return false
}