常态化疫情管控下,我们还是需要了解疫情发展态势,以便做出旅行甚至是通勤的决策。主动查询太心累了,那么就不妨试试使用函数计算来每日定时推送常态化疫情信息。省心、省力又便捷。
闲言少叙,我们看看如何开通阿里云函数计算FC并实现这样的函数吧。
开通函数计算FC
进入函数计算FC的控制台页面: https://fcnext.console.aliyun.com/
如图所示,点击免费开通并同意服务协议。
获取帮助
官方的帮助文档库有非常详尽的产品说明,如果你是初次体验的话请一定要看一看。
关于函数计算的优缺点等内容,文档内有非常详尽的介绍,请移步:
https://help.aliyun.com/product/50980.html
创建服务
开通函数计算产品后我们就可以愉快的开始创建函数的旅程了。
那么首先,请先看一看管理控制台的左侧边导航栏,里面提供了该产品的详细功能。
其中,公测的应用部分可以帮助我们快速的创建应用。例如,我们可以直接使用博客网盘等官方已经开发好的快速攻略和资源来构建我们自己需要的服务。
我们点选服务及函数。(通常我们还会选择地域,但现在好像是全球地域)
关于服务:
服务是函数计算资源管理的单位。创建函数前必须先创建服务,同一个服务下的所有函数共享一些相同的设置,例如服务授权、日志配置。
从业务场景出发,一个应用可以拆分为多个服务。从资源使用维度出发,一个服务可以由多个函数组成。例如一个数据处理服务,分为数据准备和数据处理两部分。数据准备函数资源需求小,可以选择小规格实例。数据处理函数资源需求大,可以选择大规格实例。
框选部分是必填项,填写完成后点击确定就可以完成服务创建并自动跳转到服务页面。
创建函数
创建名为yq的函数。
配置创建函数时如图配置即可:
值得注意的是,请求处理程序,是设置请求处理程序,函数计算的运行时会加载并调用您的请求处理程序处理请求。本文代码中的函数是index.py文件中的Get_Url函数,所以我们填写index.Get_Url即可。
其他内容配置与本文无关,函数的详尽设置请见:https://help.aliyun.com/document_detail/73338.html
配置触发器设置定时触发
本次设置异步调用的定时触发器,
CRON_TZ=Asia/Shanghai 0 0 14 * * *表示每天下午两点执行函数。
需要注意的是,当运行环境为 Python 时,请求处理程序的格式为 [文件名].[函数名]。如果请求处理程序值为 index.handler,那么在函数被触发时,将执行 index.py 文件中的 handler 函数。
本示例的环境信息配置如下:
函数代码
在函数代码tab,下方会加载在线的IDE,我们在其中放入已编写好的代码
具体代码如下:
该函数通过调用疫情API接口,解析json内容,处于便捷考量最后直接推送到第三方消息推送服务上。
import requests,random,json
url = "https://c.m.163.com/ug/api/wuhan/app/data/list-total" server_key = '' #填入你的对应推送服务SendKEY coolpush_key = '' qmsg_key = '' def UserAgent(): #随机获取请求头 user_agent_list = ['Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1464.0 Safari/537.36', 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.16 Safari/537.36', 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.3319.102 Safari/537.36', 'Mozilla/5.0 (X11; CrOS i686 3912.101.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36', 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36', 'Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36', 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:17.0) Gecko/20100101 Firefox/17.0.6', 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1468.0 Safari/537.36', 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2224.3 Safari/537.36', 'Mozilla/5.0 (X11; CrOS i686 3912.101.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36'] UserAgent={'User-Agent': random.choice(user_agent_list)} return UserAgent
def Get_Url(arg1,arg2): url_json = requests.get(url=url,headers=UserAgent()).json()
today_confirm = str(url_json['data']['chinaTotal']['today']['confirm'])#全国累计确诊较昨日新增 today_input =str(url_json['data']['chinaTotal']['today']['input'])#全国较昨日新增境外输入 today_storeConfirm = str(url_json['data']['chinaTotal']['today']['storeConfirm'])#全国现有确诊较昨日 today_dead =str(url_json['data']['chinaTotal']['today']['dead'])#累计死亡较昨日新增 today_heal = str(url_json['data']['chinaTotal']['today']['heal'])#累计治愈较昨日新增 today_incrNoSymptom = str(url_json['data']['chinaTotal']['extData']['incrNoSymptom'])#无症状感染者较昨日
total_confirm = str(url_json['data']['chinaTotal']['total']['confirm']) # 全国累计确诊 total_input = str(url_json['data']['chinaTotal']['total']['input']) # 境外输入 total_dead = str(url_json['data']['chinaTotal']['total']['dead']) # 累计死亡 total_heal = str(url_json['data']['chinaTotal']['total']['heal']) # 累计治愈 total_storeConfirm = str(url_json['data']['chinaTotal']['total']['confirm'] - url_json['data']['chinaTotal']['total']['dead'] - url_json['data']['chinaTotal']['total']['heal']) # 全国现有确诊 total_noSymptom = str(url_json['data']['chinaTotal']['extData']['noSymptom'])#无症状感染者
lastUpdateTime = url_json['data']['lastUpdateTime']#截止时间
data ='-' * 8 +'全国疫情数据实时统计' + '-' * 8 + '\n统计截至时间:'+ lastUpdateTime +'\n' + '-' * 32 + '\n' + \ ' 累计确诊:' + total_confirm + ' ; ' + '较昨日:' + today_confirm + \ '\n 现有确诊:' + total_storeConfirm + ' ; ' + '较昨日:' + today_storeConfirm + \ '\n 累计死亡:' + total_dead + ' ; ' + '较昨日:' + today_dead + \ '\n 累计治愈:' + total_heal + ' ; ' + '较昨日:' + today_heal + \ '\n 境外输入:' + total_input + ' ; ' + '较昨日:' + today_input + \ '\n 无症状感染者:' + total_noSymptom + ' ; ' + '较昨日:' + today_incrNoSymptom print(data) select_robots(2,data) #0为Qmsg推送,1为酷推推送,2为server酱推送。默认为0
def select_robots(i,data): if i == 0: HtmlPuch_Qmsg(data) elif i == 1: HtmlPuch_coolpush(data) elif i == 2: HtmlPuch_server(data) else: print('选择错误!')
def HtmlPuch_server(data): #server酱推送 url_key = "https://sc.ftqq.com/" + server_key + ".send" push_data = {'text':"全国疫情数据实时统计",'desp':data} html = requests.post(url_key,headers=UserAgent(),data=push_data)
def HtmlPuch_coolpush(data): #酷推推送 url_key = "https://push.xuthus.cc/send/" + coolpush_key push_data = {'c':data} html = requests.get(url=url_key,params=push_data,headers=UserAgent())
def HtmlPuch_Qmsg(data): #Qmsg推送 url_key = "https://qmsg.zendee.cn/send/" + qmsg_key push_data = {'msg':data} html = requests.get(url=url_key,params=push_data,headers=UserAgent())
|
日志功能
启用“日志功能”可以查看调用记录和日志详情,这可以方便优化我们的服务并进一步改进。
当然,这么好的功能是收费的,不过有时这个功能是非常必要的。
测试函数
单击测试函数tab,可以查看当前函数执行情况,包括返回结果、日志输出、请求信息、执行摘要等内容。
实机测试
具体呈现方式受你选择的消息推送服务限制,当然你也可以自己搭建消息推送服务,可以实现下列主流的消息推送服务渠道:服务号、企业微信、Bark iOS、企业微信群机器人、钉钉群机器人、飞书群机器人等。
感谢阅读
使用阿里云函数计算FC,我们可以省去环境配置和其他众多非业务环节。
并且,阿里云函数计算FC每月提供40万G免费计算资源和一百万次调用额度,这使得业务开发兼顾了成本和效率这两大难题,为企业提供降本增效新动能。
私人定制
本文使用的API是https://c.m.163.com/ug/api/wuhan/app/data/list-total,结合代码,你可以改动解析部分,将疫情推送的消息改为你所在的地区。
上海地区的json化数据样本如下:
{ "today": { "confirm": 7, "suspect": null, "heal": 26, "dead": 0, "severe": null, "storeConfirm": -19 }, "total": { "confirm": 63493, "suspect": 0, "heal": 62780, "dead": 595, "severe": 0, "input": 0 }, "extData": {}, "name": "上海", "id": "310000", "lastUpdateTime": "2022-07-20 08:11:32", "children": [{ "today": { "confirm": 1, "suspect": null, "heal": null, "dead": null, "severe": null, "storeConfirm": null }, "total": { "confirm": 17154, "suspect": 0, "heal": 15902, "dead": 1, "severe": 0 }, "extData": {}, "name": "浦东", "id": "C202006211504253", "lastUpdateTime": "2022-07-20 00:00:07", "children": [] }, { "today": { "confirm": 0, "suspect": null, "heal": null, "dead": null, "severe": null, "storeConfirm": null }, "total": { "confirm": 6618, "suspect": 0, "heal": 5218, "dead": 0, "severe": 0 }, "extData": {}, "name": "黄浦", "id": "310101", "lastUpdateTime": "2022-07-20 00:00:07", "children": [] }, { "today": { "confirm": 0, "suspect": null, "heal": null, "dead": null, "severe": null, "storeConfirm": null }, "total": { "confirm": 5432, "suspect": 0, "heal": 4713, "dead": 0, "severe": 0 }, "extData": {}, "name": "闵行", "id": "310112", "lastUpdateTime": "2022-07-20 00:00:07", "children": [] }, { "today": { "confirm": 2, "suspect": null, "heal": null, "dead": null, "severe": null, "storeConfirm": null }, "total": { "confirm": 4837, "suspect": 0, "heal": 4764, "dead": 0, "severe": 0 }, "extData": {}, "name": "境外输入", "id": "C202006211504251", "lastUpdateTime": "2022-07-20 08:11:33", "children": [] }, { "today": { "confirm": 0, "suspect": null, "heal": null, "dead": null, "severe": null, "storeConfirm": null }, "total": { "confirm": 4702, "suspect": 0, "heal": 3516, "dead": 1, "severe": 0 }, "extData": {}, "name": "徐汇", "id": "310104", "lastUpdateTime": "2022-07-20 00:00:07", "children": [] }, { "today": { "confirm": 0, "suspect": null, "heal": null, "dead": null, "severe": null, "storeConfirm": null }, "total": { "confirm": 3700, "suspect": 0, "heal": 2632, "dead": 0, "severe": 0 }, "extData": {}, "name": "虹口", "id": "310109", "lastUpdateTime": "2022-07-20 00:00:08", "children": [] }, { "today": { "confirm": 3, "suspect": null, "heal": null, "dead": null, "severe": null, "storeConfirm": null }, "total": { "confirm": 3275, "suspect": 0, "heal": 2416, "dead": 1, "severe": 0 }, "extData": {}, "name": "静安", "id": "310106", "lastUpdateTime": "2022-07-20 08:11:33", "children": [] }, { "today": { "confirm": 2, "suspect": null, "heal": null, "dead": null, "severe": null, "storeConfirm": null }, "total": { "confirm": 3132, "suspect": 0, "heal": 2460, "dead": 1, "severe": 0 }, "extData": {}, "name": "宝山", "id": "310113", "lastUpdateTime": "2022-07-20 08:11:33", "children": [] }, { "today": { "confirm": 0, "suspect": null, "heal": null, "dead": null, "severe": null, "storeConfirm": null }, "total": { "confirm": 2978, "suspect": 0, "heal": 2894, "dead": 0, "severe": 0 }, "extData": {}, "name": "松江", "id": "310117", "lastUpdateTime": "2022-07-20 00:00:08", "children": [] }, { "today": { "confirm": 0, "suspect": null, "heal": null, "dead": null, "severe": null, "storeConfirm": null }, "total": { "confirm": 2636, "suspect": 0, "heal": 2358, "dead": 2, "severe": 0 }, "extData": {}, "name": "嘉定", "id": "310114", "lastUpdateTime": "2022-07-20 00:00:07", "children": [] }, { "today": { "confirm": 0, "suspect": null, "heal": null, "dead": null, "severe": null, "storeConfirm": null }, "total": { "confirm": 2414, "suspect": 0, "heal": 1876, "dead": 0, "severe": 0 }, "extData": {}, "name": "长宁", "id": "310105", "lastUpdateTime": "2022-07-20 00:00:07", "children": [] }, { "today": { "confirm": 0, "suspect": null, "heal": null, "dead": null, "severe": null, "storeConfirm": null }, "total": { "confirm": 2397, "suspect": 0, "heal": 1194, "dead": 0, "severe": 0 }, "extData": {}, "name": "杨浦", "id": "310110", "lastUpdateTime": "2022-07-20 00:00:07", "children": [] }, { "today": { "confirm": 0, "suspect": null, "heal": null, "dead": null, "severe": null, "storeConfirm": null }, "total": { "confirm": 1790, "suspect": 0, "heal": 1291, "dead": 0, "severe": 0 }, "extData": {}, "name": "普陀", "id": "310107", "lastUpdateTime": "2022-07-20 00:00:07", "children": [] }, { "today": { "confirm": 0, "suspect": null, "heal": null, "dead": null, "severe": null, "storeConfirm": null }, "total": { "confirm": 1325, "suspect": 0, "heal": 1253, "dead": 0, "severe": 0 }, "extData": {}, "name": "青浦", "id": "310118", "lastUpdateTime": "2022-07-20 00:00:08", "children": [] }, { "today": { "confirm": 0, "suspect": null, "heal": null, "dead": null, "severe": null, "storeConfirm": null }, "total": { "confirm": 396, "suspect": 0, "heal": 225, "dead": 0, "severe": 0 }, "extData": {}, "name": "崇明", "id": "310130", "lastUpdateTime": "2022-07-20 00:00:07", "children": [] }, { "today": { "confirm": 0, "suspect": null, "heal": null, "dead": null, "severe": null, "storeConfirm": null }, "total": { "confirm": 339, "suspect": 0, "heal": 328, "dead": 0, "severe": 0 }, "extData": {}, "name": "金山", "id": "310116", "lastUpdateTime": "2022-07-20 00:00:08", "children": [] }, { "today": { "confirm": 0, "suspect": null, "heal": null, "dead": null, "severe": null, "storeConfirm": null }, "total": { "confirm": 242, "suspect": 0, "heal": 153, "dead": 0, "severe": 0 }, "extData": {}, "name": "奉贤", "id": "310120", "lastUpdateTime": "2022-07-20 00:00:07", "children": [] }, { "today": { "confirm": 0, "suspect": null, "heal": null, "dead": null, "severe": null, "storeConfirm": null }, "total": { "confirm": 113, "suspect": 0, "heal": 112, "dead": 1, "severe": 0 }, "extData": {}, "name": "外地来沪", "id": "310100", "lastUpdateTime": "2022-07-20 00:00:07", "children": [] }, { "today": { "confirm": 0, "suspect": null, "heal": null, "dead": null, "severe": null, "storeConfirm": null }, "total": { "confirm": 12, "suspect": 0, "heal": 9473, "dead": 588, "severe": 0 }, "extData": {}, "name": "未明确地区", "id": "923100", "lastUpdateTime": "2022-07-20 08:11:32", "children": [] }, { "today": { "confirm": 0, "suspect": null, "heal": null, "dead": null, "severe": null, "storeConfirm": null }, "total": { "confirm": 1, "suspect": 0, "heal": 2, "dead": 0, "severe": 0 }, "extData": {}, "name": "境外来沪", "id": "C202006300803441", "lastUpdateTime": "2022-07-20 00:00:06", "children": [] }, { "today": { "confirm": 0, "suspect": null, "heal": null, "dead": null, "severe": null, "storeConfirm": null }, "total": { "confirm": 0, "suspect": 0, "heal": 2, "dead": 0, "severe": 0 }, "extData": {}, "name": "待确认", "id": "C202101211446591", "lastUpdateTime": "2021-02-22 00:00:36", "children": [] }, { "today": { "confirm": 0, "suspect": null, "heal": 0, "dead": 0, "severe": null, "storeConfirm": null }, "total": { "confirm": 0, "suspect": 0, "heal": 0, "dead": 0, "severe": 0 }, "extData": {}, "name": "境外输入人员", "id": "9512312", "lastUpdateTime": "2020-08-14 13:15:23", "children": [] }, { "today": { "confirm": 0, "suspect": null, "heal": 0, "dead": 0, "severe": null, "storeConfirm": null }, "total": { "confirm": 0, "suspect": 0, "heal": 0, "dead": 0, "severe": 0 }, "extData": {}, "name": "浦东新区", "id": "310115", "lastUpdateTime": "2020-08-14 13:15:23", "children": [] }, { "today": { "confirm": 0, "suspect": null, "heal": 0, "dead": 0, "severe": null, "storeConfirm": null }, "total": { "confirm": 0, "suspect": 0, "heal": 0, "dead": 0, "severe": 0 }, "extData": {}, "name": "闸北", "id": "310108", "lastUpdateTime": "2020-08-14 13:15:23", "children": [] }] }