-
Notifications
You must be signed in to change notification settings - Fork 0
/
steamRequestSnapshot.go
144 lines (135 loc) · 3.84 KB
/
steamRequestSnapshot.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"os/user"
"path/filepath"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
)
type Snapshot struct {
document *goquery.Document
request *http.Request
response *http.Response
ErrDoc error `json:"err_document"`
ErrRes error `json:"err_response"`
ErrReq error `json:"err_request"`
Method string `json:"method"`
RequestOK bool `json:"request_OK"`
ResponseOK bool `json:"response_OK"`
Status string `json:"status"`
StatusCode int `json:"status_code"`
TimeDuration time.Duration `json:"time_duration"`
TimeEnd time.Time `json:"time_end"`
TimeStart time.Time `json:"time_start"`
URL string `json:"URL"`
}
func NewSnapshot(c *http.Client, HTTPMethod, URL string, HTTPCookies *[]*http.Cookie) *Snapshot {
ok := (strings.HasPrefix(URL, "http://") || strings.HasPrefix(URL, "https://"))
if ok != true {
URL = fmt.Sprintf("https://%s", URL)
}
req, errReq := http.NewRequest(HTTPMethod, URL, nil)
if HTTPCookies != nil && req != nil {
for _, cookie := range *HTTPCookies {
req.AddCookie(cookie)
}
}
timeStart := time.Now()
res, errRes := c.Do(req)
timeEnd := time.Now()
return newSnapshot(HTTPMethod, URL, req, errReq, res, errRes, timeStart, timeEnd)
}
func newSnapshot(HTTPMethod, URL string, req *http.Request, errReq error, res *http.Response, errRes error, timeStart, timeEnd time.Time) *Snapshot {
if errReq != nil {
timeStart = time.Time{}
}
if errRes != nil {
timeEnd = time.Time{}
}
timeDuration := timeEnd.Sub(timeStart)
if (errReq != nil) && (errRes != nil) {
timeDuration = 0
}
var status string
var statusCode int
if res != nil {
status = res.Status
statusCode = res.StatusCode
}
doc, err := goquery.NewDocumentFromResponse(res)
return &Snapshot{
document: doc,
request: req,
response: res,
ErrDoc: err,
ErrReq: errReq,
ErrRes: errRes,
Method: HTTPMethod,
RequestOK: (errReq == nil),
ResponseOK: (errRes == nil),
Status: status,
StatusCode: statusCode,
TimeDuration: timeDuration,
TimeEnd: timeEnd,
TimeStart: timeStart,
URL: URL}
}
func hasVisitedURL(fullpath string, URL *url.URL) (bool, error) {
replacer := strings.NewReplacer("https://", "", "/", "", "\\", "", URL.Host, "", "=", "-", "?", ".")
filename := replacer.Replace(fmt.Sprintf("%s.json", URL.String()))
if strings.HasPrefix(filename, ".") {
filename = strings.TrimPrefix(filename, ".")
}
fullname := filepath.Join(fullpath, filename)
_, err := os.Stat(fullname)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return true, err
}
func hasVisitedURLDefault(URL *url.URL) (bool, error) {
user, err := user.Current()
if err != nil {
return false, err
}
fullpath := filepath.Join(user.HomeDir, "Desktop", "steambot", URL.Host)
return hasVisitedURL(fullpath, URL)
}
func writeSnapshot(fullpath string, s *Snapshot) error {
err := os.MkdirAll(fullpath, os.ModePerm)
if err != nil {
return err
}
if err != nil {
return err
}
b, err := json.Marshal(s)
if err != nil {
return err
}
replacer := strings.NewReplacer("https://", "", "/", "", "\\", "", s.request.URL.Host, "", "=", "-", "?", ".")
filename := replacer.Replace(fmt.Sprintf("%s.json", s.request.URL.String()))
if strings.HasPrefix(filename, ".") {
filename = strings.TrimPrefix(filename, ".")
}
fullname := filepath.Join(fullpath, filename)
err = ioutil.WriteFile(fullname, b, os.ModePerm)
return err
}
func writeSnapshotDefault(s *Snapshot) error {
user, err := user.Current()
if err != nil {
return err
}
fullpath := filepath.Join(user.HomeDir, "Desktop", "steambot", s.request.URL.Host)
return writeSnapshot(fullpath, s)
}