-
Notifications
You must be signed in to change notification settings - Fork 0
/
requests.go
203 lines (184 loc) · 6.24 KB
/
requests.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
package NovelUpdatesClient
import (
"fmt"
"github.com/PuerkitoBio/goquery"
"net/url"
"strconv"
"strings"
)
func doFetchKeyValueRequestWith(req Requester, selector string, valueAttr string) (r []*KeyValueResult, err error) {
// Create Document From URL
doc, err := createDocumentFromRequest(req, "https://www.novelupdates.com/series-finder")
if err != nil {
return nil, err
}
// Find & Extract Data
doc.Find(selector).Each(func(i int, s *goquery.Selection) {
r = append(r, &KeyValueResult{
Name: s.Text(),
Value: s.AttrOr(valueAttr, ""),
})
})
return r, nil
}
func DoFetchNovelTypeRequestWith(req Requester) (r []*KeyValueResult, err error) {
return doFetchKeyValueRequestWith(req, ".rankfl > div:nth-child(3) a", "genreid")
}
func DoFetchLanguageRequestWith(req Requester) (r []*KeyValueResult, err error) {
return doFetchKeyValueRequestWith(req, "div.g-cols:nth-child(5) a", "genreid")
}
func DoFetchGenresRequestWith(req Requester) (r []*KeyValueResult, err error) {
return doFetchKeyValueRequestWith(req, "div.g-cols:nth-child(24) a", "genreid")
}
func DoFetchTagsRequestWith(req Requester) (r []*KeyValueResult, err error) {
return doFetchKeyValueRequestWith(req, "select#tags_include option", "value")
}
func DoFetchStoryStatusRequestWith(req Requester) (r []*KeyValueResult, err error) {
return doFetchKeyValueRequestWith(req, ".storystatus option", "value")
}
func DoFetchSortByRequestWith(req Requester) (r []*KeyValueResult, err error) {
return doFetchKeyValueRequestWith(req, ".sortresults option", "value")
}
func DoFetchOrderByRequestWith(req Requester) (r []*KeyValueResult, err error) {
return doFetchKeyValueRequestWith(req, ".sortorder option", "value")
}
func DoSearchRequestWith(req Requester, q *SearchQuery) (r []*SearchResult, err error) {
doc, err := createDocumentFromRequest(req, fmt.Sprintf("https://www.novelupdates.com/series-finder/?%s", buildSearchStringFromQuery(q)))
if err != nil {
return nil, err
}
// Find & Extract Data
doc.Find("div.search_main_box_nu").Each(func(i int, s *goquery.Selection) {
///////////////////////////////////////////////////////////
Title := "ERR"
{
oTitle := s.Find(".search_title").Text()
oTitle = strings.Trim(oTitle, " ")
if oTitle != "" {
Title = oTitle
}
}
///////////////////////////////////////////////////////////
ID := "ERR"
URL := "ERR"
{
oURL := s.Find(".search_title a").AttrOr("href", "")
oURL = strings.Trim(oURL, " ")
if _, err := url.ParseRequestURI(oURL); oURL != "" && err == nil {
URL = oURL
}
oURLSplit := strings.Split(oURL, "/")
if len(oURLSplit) == 6 {
if oURL != "" {
ID = oURLSplit[4]
}
}
}
///////////////////////////////////////////////////////////
Chapters := -1
{
oChapters := s.Find(".search_stats span:nth-child(1)").Text()
oChapters = strings.Replace(oChapters, "Chapters", "", -1)
oChapters = strings.Trim(oChapters, " ")
if Chapters, err = strconv.Atoi(oChapters); err != nil {
Chapters = -1
}
}
///////////////////////////////////////////////////////////
ReleaseFrequency := -1.0
{
oReleaseFrequency := s.Find(".search_stats span:nth-child(2)").Text()
oReleaseFrequency = strings.Replace(oReleaseFrequency, "Day(s)", "", -1)
oReleaseFrequency = strings.Replace(oReleaseFrequency, "Every", "", -1)
oReleaseFrequency = strings.Trim(oReleaseFrequency, " ")
if ReleaseFrequency, err = strconv.ParseFloat(oReleaseFrequency, 64); err != nil {
ReleaseFrequency = -1.0
}
}
///////////////////////////////////////////////////////////
Readers := -1
{
oReaders := s.Find(".search_stats span:nth-child(3)").Text()
oReaders = strings.Replace(oReaders, "Readers", "", -1)
oReaders = strings.Trim(oReaders, " ")
if Readers, err = strconv.Atoi(oReaders); err != nil {
Readers = -1.0
}
}
///////////////////////////////////////////////////////////
Reviews := -1
{
oReviews := s.Find(".search_stats span:nth-child(4)").Text()
oReviews = strings.Replace(oReviews, "Reviews", "", -1)
oReviews = strings.Trim(oReviews, " ")
if Reviews, err = strconv.Atoi(oReviews); err != nil {
Reviews = -1.0
}
}
///////////////////////////////////////////////////////////
LastUpdated := "00-00-0000"
{
oLastUpdated := s.Find(".search_stats span:nth-child(5)").Text()
oLastUpdated = strings.Trim(oLastUpdated, " ")
if oLastUpdated != "" {
LastUpdated = oLastUpdated
}
}
///////////////////////////////////////////////////////////
var genreList GenreList
{
s.Find(".search_genre a").Each(func(i int, selection *goquery.Selection) {
oGenre := selection.Text()
oGenre = strings.Trim(oGenre, " ")
if genre, ok := DisplayStringToGenre[oGenre]; ok && oGenre != "" {
genreList = append(genreList, genre)
}
})
}
///////////////////////////////////////////////////////////
Rating := -1.0
ShortLanguage := "ERR"
{
oRating := s.Find(".search_ratings").Text()
oRatingSplit := strings.Split(oRating, " ")
if len(oRatingSplit) == 2 {
oShortLanguage := oRatingSplit[0]
oShortLanguage = strings.Trim(oShortLanguage, " ")
if oShortLanguage != "" {
ShortLanguage = oShortLanguage
}
oRating = strings.Trim(oRatingSplit[1], "()")
if Rating, err = strconv.ParseFloat(oRating, 64); err != nil {
Rating = -1.0
}
}
}
///////////////////////////////////////////////////////////
Description := "ERR"
{
oDescription := s.Find(".search_body_nu").Contents().FilterFunction(func(i int, s *goquery.Selection) bool {
return !s.Is(".search_title,.search_stats,.search_genre,div,.dots,.list")
}).Text()
oDescription = strings.Replace(oDescription, "<<less", "", -1)
oDescription = strings.Trim(oDescription, " \n")
if oDescription != "" {
Description = oDescription
}
}
r = append(r, &SearchResult{
Title: Title,
ID: ID,
URL: URL,
Chapters: Chapters,
ReleaseFrequencyInDays: ReleaseFrequency,
Readers: Readers,
Reviews: Reviews,
LastUpdated: LastUpdated,
Genre: genreList,
Description: Description,
ShortLanguage: ShortLanguage,
Rating: Rating,
})
})
return r, nil
}