-
Notifications
You must be signed in to change notification settings - Fork 2k
增加城市疫情查询 #146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
增加城市疫情查询 #146
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2833df3
增加城市疫情查询
sky-rainy e5038b6
增加城市疫情查询
sky-rainy a3b2bf2
增加城市疫情查询
sky-rainy f794c11
增加城市疫情查询
sky-rainy 91e6e60
增加查询城市疫情
sky-rainy 299ab8f
增加城市查询疫情
sky-rainy f7b5de6
增加城市疫情查询
sky-rainy adc73ab
Update epidemic.go
fumiama File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| // Package epidemic 城市疫情查询 | ||
| package epidemic | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "strconv" | ||
|
|
||
| control "github.com/FloatTech/zbputils/control" | ||
| "github.com/FloatTech/zbputils/control/order" | ||
| "github.com/FloatTech/zbputils/process" | ||
| "github.com/FloatTech/zbputils/web" | ||
| log "github.com/sirupsen/logrus" | ||
| zero "github.com/wdvxdr1123/ZeroBot" | ||
| "github.com/wdvxdr1123/ZeroBot/message" | ||
| ) | ||
|
|
||
| const ( | ||
| servicename = "epidemic" | ||
| txurl = "https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5" | ||
| ) | ||
|
|
||
| // result 疫情查询结果 | ||
| type result struct { | ||
| Ret int `json:"ret"` | ||
| 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"` | ||
| } `json:"today"` | ||
| Total struct { | ||
| NowConfirm int `json:"nowConfirm"` | ||
| Confirm int `json:"confirm"` | ||
| Dead int `json:"dead"` | ||
| Heal int `json:"heal"` | ||
| Grade string `json:"grade"` | ||
| } `json:"total"` | ||
| Children []*area `json:"children"` | ||
| } | ||
|
|
||
| func init() { | ||
| engine := control.Register(servicename, order.AcquirePrio(), &control.Options{ | ||
| DisableOnDefault: true, | ||
| Help: "本插件用于查询城市疫情状况\n" + | ||
| "使用方法列如: 北京疫情 \n", | ||
| }) | ||
| engine.OnSuffix("疫情").SetBlock(true). | ||
| Handle(func(ctx *zero.Ctx) { | ||
| text := ctx.State["args"].(string) | ||
| if text == "" { | ||
| ctx.SendChain(message.Text("你还没有输入城市名字呢!")) | ||
| return | ||
| } | ||
| process.SleepAbout1sTo2s() | ||
| data, times := queryEpidemic(text) | ||
| if data == nil { | ||
| ctx.SendChain(message.Text("没有找到【" + text + "】城市的疫情数据.")) | ||
| return | ||
| } | ||
| msgtext := "【" + data.Name + "】疫情数据:\n" + | ||
| "新增:" + strconv.Itoa(data.Today.Confirm) + | ||
| " ,现有确诊:" + strconv.Itoa(data.Total.NowConfirm) + | ||
| " ,治愈:" + strconv.Itoa(data.Total.Heal) + | ||
| " ,死亡:" + strconv.Itoa(data.Total.Dead) + " " + data.Total.Grade | ||
| ctx.SendChain( | ||
| message.Text(msgtext), | ||
| message.Text("\n"), | ||
| message.Text("更新时间:"+times), | ||
| message.Text("\n"), | ||
| message.Text("温馨提示:请大家做好防疫工作,出门带好口罩!"), | ||
| ) | ||
| }) | ||
| } | ||
|
|
||
| // 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) { | ||
| response, err := web.GetData(txurl) | ||
| if err != nil { | ||
| log.Errorln("[txurl-err]:", err) | ||
| return nil, "" | ||
| } | ||
| var r result | ||
| err = json.Unmarshal(response, &r) | ||
| if err != nil { | ||
| log.Errorln("[txjson-Result-err]:", err) | ||
| return nil, "" | ||
| } | ||
| var e epidemic | ||
| err = json.Unmarshal([]byte(r.Data), &e) | ||
| if err != nil { | ||
| log.Errorln("[txjson-Epidemic-err]:", err) | ||
| return nil, "" | ||
| } | ||
| citydata = rcity(&e.AreaTree[0], findCityName) | ||
| return citydata, e.LastUpdateTime | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.