-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.go
73 lines (59 loc) · 1.57 KB
/
common.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
package ginside
import (
"context"
"fmt"
"net/http"
"strconv"
"time"
"github.com/pkg/errors"
)
func makeRequest(ctx context.Context, client *http.Client, link string) (*http.Response, error) {
req, err := http.NewRequest("GET", link, nil)
if err != nil {
return nil, err
}
req.Header.Set("Referer", headerReferer)
req.Header.Set("User-Agent", randomUserAgent())
req.Header.Set("Accept", headerAccept)
req = req.WithContext(ctx)
// do http request
res, err := client.Do(req)
if err != nil {
return nil, err
}
if res.StatusCode != 200 {
return nil, errors.New("unexpected status code: " + strconv.Itoa(res.StatusCode))
}
return res, nil
}
func parseDate(dateText string) (time.Time, error) {
date, err := time.ParseInLocation(dateFormat, dateText, dateLocation)
if err == nil {
return date, nil
}
date, err = time.ParseInLocation(dateFormatAlt, dateText, dateLocation)
if err == nil {
return date, nil
}
date, err = time.ParseInLocation(dateFormatAlt2, dateText, dateLocation)
if err == nil {
return date, nil
}
date, err = time.ParseInLocation(dateFormatShort, dateText, dateLocation)
if err == nil {
return date, nil
}
date, err = time.ParseInLocation(dateFormatShortAlt, dateText, dateLocation)
if err == nil {
return date, nil
}
date, err = time.ParseInLocation(dateFormatShortAlt2, dateText, dateLocation)
if err == nil {
return date, nil
}
date, err = time.ParseInLocation(dateFormatShortAlt3, dateText, dateLocation)
if err == nil {
return date, nil
}
return time.Time{}, fmt.Errorf("unable to parse date: %s", dateText)
}