树莓派每日定时全自动上报疫情健康表单

功能介绍

最近公司又要每天更新健康数据打卡,虽然填的东西不多,但每天都得做还是有点烦人的。为了既配合疫情、又免于每天打卡,于是晚上花了会儿时间做了个小脚本,用来实现:

  1. python requests请求信息,实现自动上报表单
  2. 树莓派设置定点服务,实现每日定点执行python代码
  3. 将上报成功与否的信息推送到手机

最终效果如下:
在这里插入图片描述

另外其实还用pythonista写了个iphone上的桌面小插件,可以一键提交信息,后面有空再把教程写出来吧,效果如下,点击自己or其他人,再选择上班or休息,最好点上报即可自动完成上报。
在这里插入图片描述

下面是正文:

1. python requests请求信息,实现自动上报表单

以下代码隐去了url部分,主要看思路。

import requests
from urllib.parse import urlencode
import json
import datetime
import time

# login函数为用empno和username获取sessionID
def login(empno="", username=""):
    url = "https://xxxx/THealth/InfoReport/app/doLogin"
    headers = {
        "Accept": "application/json",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36",
    }
    data = {
        "empno": empno,
        "username": username
    }
    response = requests.post(url=url, headers=headers, data=data)
    if response.status_code == 200:
        return response.headers['Set-Cookie'][0:43]
    else:
        return False

# get_record函数为查询当前系统中已经录入的数据,确认后面是否需要上报
def get_record(jsessionid=""):
    url = "https://xxxx/THealth/InfoReport/app/queryReportList"
    headers = {
        "Accept": "application/json",
        "Cookie": jsessionid,
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36",
    }
    response = requests.get(url=url, headers=headers)
    text = ''
    newest = ''
    if response.status_code == 200:
        record_list = json.loads(response.content)['data']

        if len(record_list) == 1:
            newest = record_list[0]['recordDate'][5:10]
            text +=  '   ' + record_list[0]['recordDate'] + ' ' + record_list[0]['recordLocation'] + ' ' + record_list[0][
                'hRiskFlag'] + '&' + record_list[0]['hRiskTogetherFlag']
        elif len(record_list) >= 2:
            newest = record_list[0]['recordDate'][5:10]
            text += '   ' + record_list[0]['recordDate'] + ' ' + record_list[0]['recordLocation'] + ' ' + record_list[0][
                'hRiskFlag'] + '&' + record_list[0]['hRiskTogetherFlag'] + '\n'
            text += '   ' + record_list[1]['recordDate'] + ' ' + record_list[1]['recordLocation'] + ' ' + record_list[1][
                'hRiskFlag'] + '&' + record_list[1]['hRiskTogetherFlag']

        return text, newest

    else:
        return False

# update用于上报处理后的表单
def update(jsessionid="", data=""):
    url = "https://xxxx/THealth/InfoReport/report/addReport"
    headers = {
        "Accept": "application/json",
        "Content-Type": "application/x-www-form-urlencoded",
        "Cookie": jsessionid,
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36",
    }
    response = requests.post(url=url, headers=headers, data=data)

# 将json文件转化为url编码,其中有个单双引号的问题,即replace("%27", "%22")
def update_check(jsessionid="", user={}):
    data = urlencode(user['data']).replace("%27", "%22")
    update(jsessionid, data)

# 将自动上报的状况自动推送到手机上,失败、成功等
def push(content='打卡失败'):
    url = 'https://api.day.app/xxxxxxxx/每日健康/'+content
    requests.get(url)

# 读取本地同目录下的json文件
with open('user.json', 'r', encoding='utf-8') as f:
    a = json.loads(f.read())
    user_list = a["user_list"]
    vacation_list = a["vacation_list"]

# 获取当天和第二天日期,如02-07
this_day = datetime.datetime.strftime(datetime.datetime.now(), '%m-%d')
next_day = datetime.datetime.strftime(datetime.datetime.now()+datetime.timedelta(days=1), '%m-%d')

#循环给每个人上报
for user in user_list:
    vacation = vacation_list[user['username']]
    # 根据每个人提前录入的的休假计划,判断第二天是否休息
    if next_day in vacation:
        user["data"]["empInfo"]["isNextDayWork"] = "否"
        user["data"]["empInfo"]["isNextDayEat"] = "否"
	# 获取sessionID
    jsessionid = login(empno=user["empno"], username=user["username"])
    text, newest = get_record(jsessionid)
    if this_day == newest:
        print(user["username"] + '已上报,近两日打卡时间/地点/是否被高风险标记:')
        print(text)
        push(user["username"]+newest+'已上报')
    else:
        update_check(jsessionid=jsessionid, user=user)
        text, newest = get_record(jsessionid)
        if this_day == newest:
            print(user["username"] + '上报成功,近两日打卡时间/地点/是否被高风险标记:')
            print(text)
            push(user["username"] + newest + '上报成功')
        else:
            print(user["username"] + '上报失败,近两日打卡时间/地点/是否被高风险标记:')
            print(text)
            push(user["username"] + newest + '上报失败')
    time.sleep(0.2)

应该只需要修改里面的empno和username,contact

{
    "vacation_list": {
        "username": ["02-11","02-12","02-13","02-14","02-15","02-16","02-17","02-20","02-21","02-27","03-06","03-07","03-13","03-14","03-20","03-21","03-27","03-28"]
    },
    "user_list":
    [
        {
            "empno": "empno", "username": "username", "data":
            {
                "empInfo": {"recordId": "", "contact": "xxxxxxx", "temperature": 36.7,
                            "birthPlace": {"province": "xx省", "city": "xx市", "county": "xx市", "detailAddr": ""},
                            "workPlace": {"province": "xx省", "city": "xx市", "county": "xx市"},
                            "lifePlace": {"province": "xx省", "city": "xx市", "county": "xx市", "detailAddr": "xxxxx"},
                            "commuteType": "私家车", "isNextDayWork": "是", "isNextDayEat": "是",
                            "nextDayEatTime": ["中餐"], "diagnosisFlag": "否", "isReturn": "否",
                            "togetherLifeHasLeave": "否", "togetherLifeHasDisease": "否", "remark": ""},
                "location": {"anchor": {"latitude": "", "longitude": "", "addr": ""},
                             "modify": {"province": "xx省", "city": "xx市", "county": "xx市", "detailAddr": "xxxxxx"},
                             "channel": {}},
                "reportInfo": {"vehicle": {"type": "自驾", "frequency": ""},
                               "isDisease": {"type": "否", "measures": "", "hospital": "", "verdict": "", "name": ""},
                               "isHRisk": {"type": "否", "together": "否"}, "isObserv": {"type": "否", "quarantine": {
                        "startDate": "NaN-aN-aN", "endDate": "NaN-aN-aN"}}, "hasTest": {"type": "否", "record": {}}}
            }
        }
	]
}

2. 树莓派设置定点服务,实现每日定点执行python代码

这里引用文章 树莓派学习笔记——crontab定时运行脚本

  1. 查看已有的定时任务
 pi@raspberrypi:~ $ crontab -l
  1. 编辑定时任务
 pi@raspberrypi:~ $ crontab -e
  1. 添加行
30 7 * * * python3 绝对地址.py >> 绝对地址.log 2>&1
# 上面一行是按照下面一行的规则填写的,意思是每天的7点半自动执行对应地址的py文件,并保存log日志
# m h  dom mon dow   command
# m: 表示分钟,范围059
# h:表示小时,范围023
# dom:表示日,范围131
# mon:表示月,范围112
# dow:表示星期,范围0707都是星期日
# command:表示命令
  1. 如用nano编辑,则ctrl+o保存并ctrl+x退出

完工!

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 1024 设计师:白松林 返回首页