-
Notifications
You must be signed in to change notification settings - Fork 2k
/
epidemic.go
127 lines (117 loc) · 2.98 KB
/
epidemic.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
// Package epidemic 城市疫情查询
package epidemic
import (
"encoding/json"
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message"
"github.com/FloatTech/zbputils/binary"
"github.com/FloatTech/zbputils/control"
"github.com/FloatTech/zbputils/control/order"
"github.com/FloatTech/zbputils/ctxext"
"github.com/FloatTech/zbputils/web"
)
const (
servicename = "epidemic"
txurl = "https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5"
)
// result 疫情查询结果
type result struct {
Data string `json:"data"`
}
// epidemic 疫情数据
type epidemic struct {
LastUpdateTime string `json:"lastUpdateTime"`
AreaTree []*area `json:"areaTree"`
}
// area 城市疫情数据
type area struct {
Name string `json:"name"`
Today struct {
Confirm int `json:"confirm"`
Wzzadd int `json:"wzz_add"`
} `json:"today"`
Total struct {
NowConfirm int `json:"nowConfirm"`
Confirm int `json:"confirm"`
Dead int `json:"dead"`
Heal int `json:"heal"`
Grade string `json:"grade"`
Wzz int `json:"wzz"`
} `json:"total"`
Children []*area `json:"children"`
}
func init() {
engine := control.Register(servicename, order.AcquirePrio(), &control.Options{
DisableOnDefault: false,
Help: "城市疫情查询\n" +
"- xxx疫情\n",
})
engine.OnSuffix("疫情").SetBlock(true).Limit(ctxext.LimitByUser).
Handle(func(ctx *zero.Ctx) {
city := ctx.State["args"].(string)
if city == "" {
ctx.SendChain(message.Text("你还没有输入城市名字呢!"))
return
}
data, time, err := queryEpidemic(city)
if err != nil {
ctx.SendChain(message.Text("ERROR:", err))
return
}
if data == nil {
ctx.SendChain(message.Text("没有找到【", city, "】城市的疫情数据."))
return
}
ctx.SendChain(
message.Text(
"【", data.Name, "】疫情数据\n",
"新增人数:", data.Today.Confirm, "\n",
"现有确诊:", data.Total.NowConfirm, "\n",
"累计确诊:", data.Total.Confirm, "\n",
"治愈人数:", data.Total.Heal, "\n",
"死亡人数:", data.Total.Dead, "\n",
"无症状人数:", data.Total.Wzz, "\n",
"新增无症状:", data.Today.Wzzadd, "\n",
"更新时间:\n『", time, "』",
),
)
})
}
// rcity 查找城市
func rcity(a *area, cityName string) *area {
if a == nil {
return nil
}
if a.Name == cityName {
return a
}
for _, v := range a.Children {
if v.Name == cityName {
return v
}
c := rcity(v, cityName)
if c != nil {
return c
}
}
return nil
}
// queryEpidemic 查询城市疫情
func queryEpidemic(findCityName string) (citydata *area, times string, err error) {
data, err := web.GetData(txurl)
if err != nil {
return
}
var r result
err = json.Unmarshal(data, &r)
if err != nil {
return
}
var e epidemic
err = json.Unmarshal(binary.StringToBytes(r.Data), &e)
if err != nil {
return
}
citydata = rcity(e.AreaTree[0], findCityName)
return citydata, e.LastUpdateTime, nil
}