-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.go
139 lines (114 loc) · 3.45 KB
/
functions.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
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strings"
)
func readToken(tokenPath string) (string, bool) {
if tokenPath == "" {
return "", true
}
bytes, err := ioutil.ReadFile(tokenPath)
if err != nil {
log.Println(err)
return "", false
}
return fmt.Sprintf("%s", bytes), true
}
// returns body, statusCode, contentType, stats (as unparsed json) limitReached, and bool if successful
func doRequest(urlBase64, token string, maxFetchers, referenceCountThreshold int64, enableIndexFile bool) (string, int, string, string, bool, bool) {
requestURL := fmt.Sprintf("https://api.marcobeierer.com/sitemap/v2/%s?pdfs=1&origin_system=cli&max_fetchers=%d&reference_count_threshold=%d&enable_index_file=%t", urlBase64, maxFetchers, referenceCountThreshold, enableIndexFile)
req, err := http.NewRequest("GET", requestURL, nil)
if err != nil {
// err could just be invalid method or URL parse error
log.Println(err)
return "", -1, "", "", false, false // -1 because it doesn't make sense to retry in these cases
}
if token != "" {
token = strings.TrimSuffix(token, "\n")
req.Header.Set("Authorization", "Bearer "+token)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Println(err)
return "", 0, "", "", false, false // 0 because we may retry to connect, err could for example be `connection refused`
}
defer resp.Body.Close()
contentType := resp.Header.Get("content-type")
stats := resp.Header.Get("X-Stats")
limitReached := resp.Header.Get("X-Limit-Reached") == "1"
if resp.StatusCode != http.StatusOK {
log.Printf("got status code %d, expected 200\n", resp.StatusCode)
return "", resp.StatusCode, contentType, stats, limitReached, false
}
bytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
return "", resp.StatusCode, contentType, stats, limitReached, false
}
return string(bytes), resp.StatusCode, contentType, stats, limitReached, true
}
// filename is sitemap.xml or sitemap.000001.xml, etc.
func downloadFile(urlBase64, filepathx, token string) error {
requestURL := fmt.Sprintf("https://api.marcobeierer.com/sitemap/v2/%s/%s", urlBase64, filepath.Base(filepathx))
req, err := http.NewRequest("GET", requestURL, nil)
if err != nil {
// err could just be invalid method or URL parse error
log.Println(err)
return err
}
if token != "" {
token = strings.TrimSuffix(token, "\n")
req.Header.Set("Authorization", "Bearer "+token)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Println(err)
return err
}
defer resp.Body.Close()
file, err := os.Create(filepathx)
if err != nil {
log.Println(err)
return err
}
defer file.Close()
_, err = io.Copy(file, resp.Body)
if err != nil {
log.Println(err)
return err
}
return nil
}
func getStats(urlBase64, token string) (*Stats, error) {
requestURL := fmt.Sprintf("https://api.marcobeierer.com/sitemap/v2/%s/stats", urlBase64)
req, err := http.NewRequest("GET", requestURL, nil)
if err != nil {
// err could just be invalid method or URL parse error
log.Println(err)
return nil, err
}
if token != "" {
token = strings.TrimSuffix(token, "\n")
req.Header.Set("Authorization", "Bearer "+token)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Println(err)
return nil, err
}
defer resp.Body.Close()
stats := Stats{}
err = json.NewDecoder(resp.Body).Decode(&stats)
if err != nil {
log.Println(err)
return nil, err
}
return &stats, nil
}