This repository has been archived by the owner on Sep 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
111 lines (91 loc) · 2.63 KB
/
http.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
package parser
import (
"fmt"
"io"
"net/http"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/rl404/go-malscraper/errors"
"github.com/rl404/go-malscraper/model"
)
const malURL string = "https://myanimelist.net"
// Mockable functions.
var httpRequest = http.NewRequest
var parseHTML = goquery.NewDocumentFromReader
var timeSince = time.Since
func (p *Parser) getBody(url string) (io.ReadCloser, int, error) {
// Prepare request.
request, err := httpRequest("GET", url, nil)
if err != nil {
p.logger.Error("failed preparing request: %s", err.Error())
return nil, http.StatusInternalServerError, errors.ErrPrepareRequest
}
// Do request.
t := time.Now()
resp, err := p.http.Do(request)
if err != nil {
return nil, http.StatusInternalServerError, errors.ErrHTTPRequest
}
// Header check.
p.logger.Debug("%s %v (%s)", url, resp.StatusCode, timeSince(t).Truncate(time.Microsecond))
if resp.StatusCode != http.StatusOK {
resp.Body.Close()
return nil, resp.StatusCode, errors.ErrNot200
}
return resp.Body, resp.StatusCode, nil
}
func (p *Parser) getDoc(url string, area string) (*goquery.Selection, int, error) {
// Get HTML body.
p.logger.Trace("parsing %s", url)
body, code, err := p.getBody(url)
if err != nil {
return nil, code, err
}
defer body.Close()
// Parse HTML.
doc, err := parseHTML(body)
if err != nil {
p.logger.Error("failed parsing body: %s", err.Error())
return nil, http.StatusInternalServerError, errors.ErrParseBody
}
return doc.Find(area).First(), http.StatusOK, nil
}
func (p *Parser) queryToMap(queryObj model.Query) map[string]interface{} {
query := make(map[string]interface{})
for i, c := range []string{"a", "b", "c", "d", "e", "f", "g"} {
query[fmt.Sprintf("c[%v]", i)] = c
}
query["q"] = strings.Replace(queryObj.Title, " ", "+", -1)
query["show"] = 50 * (queryObj.Page - 1)
query["type"] = queryObj.Type
query["score"] = queryObj.Score
query["status"] = queryObj.Status
query["r"] = queryObj.Rating
query["p"] = queryObj.ProducerID
query["mid"] = queryObj.MagazineID
if queryObj.FirstLetter != "" {
query["letter"] = strings.ToUpper(queryObj.FirstLetter)
}
excludeGenre := 0
if queryObj.ExcludeGenre {
excludeGenre = 1
}
query["gx"] = excludeGenre
if !queryObj.StartDate.IsZero() {
sYear, sMonth, sDay := queryObj.StartDate.Date()
query["sd"] = sDay
query["sm"] = int(sMonth)
query["sy"] = sYear
}
if !queryObj.EndDate.IsZero() {
eYear, eMonth, eDay := queryObj.EndDate.Date()
query["ed"] = eDay
query["em"] = int(eMonth)
query["ey"] = eYear
}
for i, g := range queryObj.GenreIDs {
query[fmt.Sprintf("genre[%v]", i)] = g
}
return query
}