-
Notifications
You must be signed in to change notification settings - Fork 10
/
dxy_scrape.go
164 lines (150 loc) · 4.13 KB
/
dxy_scrape.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
package main
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"github.com/PuerkitoBio/goquery"
"github.com/dchest/uniuri"
"log"
"math/rand"
"net/http"
"os"
"path/filepath"
"regexp"
"time"
)
const (
url = "https://3g.dxy.cn/newh5/view/pneumonia"
)
var (
client = http.Client{
Timeout: time.Minute,
}
Logger *log.Logger
regex = regexp.MustCompile(`(?m)= (.*)}catch\(`)
enableLogger bool
scrapeSessionId = uniuri.NewLen(16)
sessionStartedAt = time.Now()
cache = map[string]string{}
interval time.Duration
logFile string
folder string
)
type ScrapeResult struct {
Time time.Time `json:"time"`
Session string `json:"session"`
SessionStarted time.Time `json:"session_started"`
Content string `json:"content"`
}
func get() {
Logger.Printf("fetching http resource")
request, err := client.Get(url)
if err != nil {
Logger.Printf("failed to fetch http resource: %v", err)
}
Logger.Printf("http resource fetched")
parse(request)
}
func parse(response *http.Response) {
responseTime := time.Now()
doc, err := goquery.NewDocumentFromReader(response.Body)
if err != nil {
Logger.Printf("failed to initialize goquery document: %v", err)
}
doc.Find(`script[id^="get"]`).Each(func(i int, selection *goquery.Selection) {
id, have := selection.Attr("id")
contents := regex.FindStringSubmatch(selection.Text())
var content string
if len(contents) == 2 {
content = contents[1]
} else {
Logger.Printf("malformed regex result: len=%v %v", len(contents), contents)
return
}
if len(content) != 0 && have {
hashBytes := sha256.Sum256([]byte(content))
hashString := hex.EncodeToString(hashBytes[:])
cacheHash, ok := cache[id]
if ok {
// have cache. compare if is new or not
if hashString == cacheHash {
// not updated. skip saving
return
} // else: updated. do saving.
} else {
// first run. save data.
Logger.Printf("(cache) first time running the app. saving both content and cache")
}
filePath := filepath.Join(folder, id+".jsonl")
fmt.Println(filePath)
file, err := os.OpenFile(filePath, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0664)
if err != nil {
Logger.Printf("failed to open data file: %v", err)
return
}
defer file.Close()
result := ScrapeResult{
Time: responseTime,
Session: scrapeSessionId,
SessionStarted: sessionStartedAt,
Content: content,
}
bytes, err := json.Marshal(result)
if err != nil {
Logger.Printf("failed to marshal json data: %v", err)
return
}
content = string(bytes) + "\n"
_, err = file.WriteString(content)
if err != nil {
Logger.Printf("failed to write to data file: %v", err)
return
} else {
Logger.Printf("successfully saved data to: %v", filePath)
}
// save current result to cache
cache[id] = hashString
} else {
Logger.Printf("suspected malformed data. len(content): %v, have id attr: %v", len(content), have)
}
})
}
func main() {
flag.BoolVar(&enableLogger, "logger", true, "enable logger or not. if set to false, program will write log to /dev/null. if set to true, program will write log to logfile")
flag.StringVar(&logFile, "log", "runtime.log", "path of the log file")
flag.StringVar(&folder, "path", "data/", "path to put the data files")
flag.DurationVar(&interval, "interval", time.Minute, "interval between http requests")
flag.Parse()
var file *os.File
if enableLogger {
var err error
file, err = os.OpenFile(logFile, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0664)
if err != nil {
log.Printf("failed to open log file %v", err)
}
defer file.Close()
} else {
var err error
file, err = os.OpenFile("/dev/null", os.O_RDWR|os.O_APPEND, 0664)
if err != nil {
log.Printf("failed to open log file %v", err)
}
defer file.Close()
}
Logger = log.New(file, "[main] ", log.LstdFlags)
err := os.MkdirAll(folder, 0755)
if err != nil {
Logger.Printf("failed to create folder: %v", err)
}
rand.Seed(time.Now().UnixNano())
ticker := time.NewTicker(interval)
get()
for {
select {
case <-ticker.C:
get()
}
}
}