-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
129 lines (117 loc) · 3.57 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"regexp"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
runewidth "github.com/mattn/go-runewidth"
)
const (
url = "https://ncov.dxy.cn/ncovh5/view/pneumonia"
)
type Provinces []Province
type Province struct {
ProvinceName string `json:"provinceName"`
ProvinceShortName string `json:"provinceShortName"`
CurrentConfirmedCount int `json:"currentConfirmedCount"`
ConfirmedCount int `json:"confirmedCount"`
SuspectedCount int `json:"suspectedCount"`
CuredCount int `json:"curedCount"`
DeadCount int `json:"deadCount"`
Comment string `json:"comment"`
LocationId int `json:"locationId"`
Citys []City `json:"cities"`
}
type City struct {
CityName string `json:"cityName"`
CurrentConfirmedCount int `json:"currentConfirmedCount"`
ConfirmedCount int `json:"confirmedCount"`
SuspectedCount int `json:"suspectedCount"`
CuredCount int `json:"curedCount"`
DeadCount int `json:"deadCount"`
LocationId int `json:"locationId"`
}
func formatStr(s string, width int) string {
return runewidth.FillRight(s, width)
}
const (
provCol1 = 20
provCol2 = 10
cityCol1 = 18
cityCol2 = 10
timeFormatFile = "20060102"
)
func (ps *Provinces) getPrintData() string {
var str []string
title := []string{
formatStr("地区", provCol1),
formatStr("现存确诊", provCol2),
formatStr("累计确诊", provCol2),
formatStr("死亡", provCol2),
formatStr("治愈", provCol2),
}
str = append(str, strings.Join(title, ""))
str = append(str, strings.Repeat("=", 60))
for _, p := range *ps {
s1 := formatStr(p.ProvinceName, provCol1) +
formatStr(fmt.Sprintf("%d", p.CurrentConfirmedCount), provCol2) +
formatStr(fmt.Sprintf("%d", p.ConfirmedCount), provCol2) +
formatStr(fmt.Sprintf("%d", p.DeadCount), provCol2) +
formatStr(fmt.Sprintf("%d", p.CuredCount), provCol2)
str = append(str, s1)
for _, c := range p.Citys {
s2 := "|-" + formatStr(c.CityName, cityCol1) +
formatStr(fmt.Sprintf("%d", c.CurrentConfirmedCount), cityCol2) +
formatStr(fmt.Sprintf("%d", c.ConfirmedCount), cityCol2) +
formatStr(fmt.Sprintf("%d", c.DeadCount), cityCol2) +
formatStr(fmt.Sprintf("%d", c.CuredCount), cityCol2)
str = append(str, s2)
}
}
return strings.Join(str, "\n")
}
func main() {
dataStr := scrapeHtml(url)
provinces := jsonToData(&dataStr)
formatData := provinces.getPrintData()
fmt.Println(formatData)
filename := fmt.Sprintf("新冠实时疫情(%s).txt", time.Now().Format(timeFormatFile))
ioutil.WriteFile(filename, []byte(formatData), 0644)
}
func jsonToData(s *string) Provinces {
var p Provinces
err := json.Unmarshal([]byte(*s), &p)
if err != nil {
log.Fatal(err)
}
return p
}
func scrapeHtml(url string) string {
var ret string
resp, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
log.Fatalf("status coder error: %d %s", resp.StatusCode, resp.Status)
}
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
log.Fatal(err)
}
doc.Find("#getAreaStat").Each(func(i int, s *goquery.Selection) {
rawStr := s.Text()
if len(rawStr) > 0 {
r, _ := regexp.Compile("\\[(.*)\\]")
ret = r.FindString(rawStr)
ret = strings.Replace(ret, `\r\n`, "", -1)
}
})
return ret
}