转载 | 基于阿里云Serverless函数计算开发的疫情数据统计推送机器人

简介: 本文选自函数计算征集令优秀征文!

一、Serverless函数计算

什么是Serverless?

在《Serverless Architectures》中对 Serverless 是这样子定义的:

 

Serverless was first used to describe applications that significantly or fully incorporate third-party, cloud-hosted applications and services, to manage server-side logic and state. These are typically “rich client” applications—think single-page web apps, or mobile apps—that use the vast ecosystem of cloud-accessible databases (e.g., Parse, Firebase), authentication services(e.g., Auth0, AWS Cognito), and so on. These types of services have been previously described as “(Mobile) Backend as a service", and I use “BaaS” as shorthand in the rest of this article. Serverless can also mean applications where server-side logic is still written by the application developer, but, unlike traditional architectures, it’s run in stateless compute containers that are event-triggered, ephemeral (may only last for one invocation), and fully managed by a third party. One way to think of this is “Functions as a Service” or “FaaS”.(Note: The original source for this name—a tweet by @marak—isno longer publicly available.) AWS Lambda is one of the most popular implementations of a Functions-as-a-Service platform at present, but there are many others, too.

 

这样的描述我相信有很多小伙伴不明白,我们可以这样子来理解Serverless:

它的中文直译就是【无服务器】

 

目前对于 Serverless 有几种解读方法:

  • 在某些场景可以解读为一种软件系统架构方法,通常称为 Serverless 架构
  • 而在另一些情况下,又可以代表一种产品形态,称为 Serverless 产品

 

可以理解为Severless=FAAS+BAAS  即函数即服务 (Function as a Service)+后端即服务 (Backend as a Service)

 

阿里云函数计算

阿里云函数计算是事件驱动的全托管计算服务。使用函数计算,您无需采购与管理服务器等基础设施,只需编写并上传代码。函数计算为您准备好计算资源,弹性地、可靠地运行任务,并提供日志查询、性能监控和报警等功能。

 

借助函数计算,您可以快速构建任何类型的应用和服务,并且只需为任务实际消耗的资源付费。

1657873769501.jpg

 

阿里云也为开发者朋友们提供了每月免费额度!

image.png

image.png

 

二、成果介绍

 疫情数据统计推送基于Python和阿里云Serverless函数计算开发。实现了使用Python爬取获得疫情数据并进行整理,使用函数计算配合定时触发器,每天定时推送全国疫情数据到企业微信。

 

三、背景意义

 疫情防控常态化,在全球疫情不断加速蔓延态势下在短期内完全结束是不可能的,很有可能较长时期处于疫情防控的状态,这要求我们时刻保持警惕,及时了解疫情情况。疫情数据统计推送项目,顺应了此背景。企业员工每天打开手机微信就可以收到一条简约的推送,了解当日的疫情情况。

 

四、优势和不足

 优势:相对各大媒体每日推送的疫情情况相比,此疫情数据统计推送更加简介,可以更快的获取到有效信息。使用了阿里云函数FC开发,维护方便,无需关注服务器等基础设施,可以根据企业微信推送的需求量自动扩缩容,而且成本极低。使用定时触发器,每天定时的触发程序,发送数据推送,无需人为干预。

 不足:文字单调,将在后期推出数据可视化版本。

 

五、作品展示

项目代码:

import requests,random,json
 
url = "https://c.m.163.com/ug/api/wuhan/app/data/list-total"


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(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 ='-' * 6 +'全国疫情数据实时统计' + '-' * 5 + '\n统计截至时间:'+ lastUpdateTime +'\n' + '-' * 27 + '\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)
    HtmlPuch_server(data)

 
 
def HtmlPuch_server(data):
    url_wx = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=3b4bd7fa-4063-477f-bbc6-0fe767c52fdf"
    headers = {"Content-Type": "text/plain"}
    push_data ={
                "msgtype": "text",
                "text": {
                    "content":data
                    }
                }
    html = requests.post(url_wx,headers=headers,json=push_data)
    print(html.text)

 

使用阿里云函数计算FC服务:

image.png

image.png

 

image.png

 

使用定时触发器:

image.png

 

最终效果:

411e3f8cbb590c0fcf15f5dc128c2d7.jpg

 

六、总结

通过Serverless我们不再需要关注务器等基础设施,只需编写并上传代码,只要为任务实际消耗的资源付费,每月的免费额度可以满足开发者的基本使用。现在函数计算FC为开发者提供一站式 Serverless 应用管理,从一键创建应用到快速体验。

image.png

 原文链接:http://click.aliyun.com/m/1000350416/


本文为阿里云原创内容,未经允许不得转载。

posted @ 2022-07-27 16:56  阿里云云栖号  阅读(198)  评论(0编辑  收藏  举报