简述: 本文转载自While True: learn(),然后自己做了一部分小修改(增加了邮件提醒功能)。

本文初发于 “曾晨de小站” zengchen233.cn,同步转载于此。

配置函数调用

首先登陆阿里云,找到控制台-函数计算FC-创建服务,进入之后选择创建函数:

image-20220202161050787

然后配置基本设置:

image-20220202161319990

编写&上传代码

原作者写的打卡脚本:

# -*- coding: utf-8 -*-
import logging
import datetime
import json
import os
import requests
 
# Made by BATTLEHAWK
# https://battlehawk233.cn/
logger=logging.getLogger()
url = r"https://mps.zocedu.com/corona/submitHealthCheck/submit"
url_info = r"https://mps.zocedu.com/corona/submitHealthCheck/getCurrentInfo"
defaultjson = {
    "data": {
        "checkPlace": "",
        "contactMethod": "",
        "teacher": "",
        "temperature": "36.5",
        "isCohabitFever": "否",
        "isLeavePalce": "否",
        "beenPlace": "",
        "isContactNcov": "否",
        "livingPlace": "",
        "livingPlaceDetail": "",
        "name1": "",
        "relation1": "",
        "phone1": "",
        "name2": "",
        "relation2": "",
        "phone2": "",
        "remark": "",
        "extraInfo": "[]",
        "healthStatus": "z",
        "emergencyContactMethod": "[]",
        "checkPlacePoint": "124,37",
        "checkPlaceDetail": "",
        "checkPlaceCountry": "",
        "checkPlaceProvince": "",
        "checkPlaceCity": "",
        "checkPlaceArea": "",
    },
    "other": {
        "openid": ""
    }
}
openid = ""
headers = {
    "Content-Type": "application/x-www-form-urlencoded",
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36 MicroMessenger/7.0.9.501 NetType/WIFI MiniProgramEnv/Windows WindowsWechat"
}
data = {}
jsonfile = "config.json" 


def getSessionID():
    url = "https://mps.zocedu.com/corona/submitHealthCheck"
    res = requests.get(url, {
        "openId": openid,
        "latitude": "",
        "longitude": ""
    })
    sessionid = res.cookies.get("JSESSIONID")
    return sessionid
 
 
# 加载Json配置文件
def loadJson():
    global data, openid
    f = open(jsonfile, "r")
    obj = json.load(f)
    f.close()
    data = obj["data"]
    openid = obj["other"]["openid"]
 
 
# 打卡函数
def checkIn():
    cookies = {
        "JSESSIONID": getSessionID()
    }
    res = requests.post(url, data=data, headers=headers, cookies=cookies)
    if res.text == "":
        logger.info("校趣多打卡成功!当前时间:" + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
    else:
        logger.error("校趣多打卡失败!请检查配置文件是否填写正确!")
 
 
# 创建配置文件
def createConfigFile():
    global defaultjson
    f = open(jsonfile, "w")
    json.dump(defaultjson, f, ensure_ascii=False, indent=2)
    f.close()
 
def handler(event, context):
    if not os.path.exists(jsonfile):
        createConfigFile()
        logger.error("未检测到配置文件,请填写config.json后运行本打卡脚本!")
        exit(0)
    else:
        loadJson()
        checkIn()

详细说明:

  • 打卡点 checkPlace 格式:XX省-XX市-XX区
  • 联系方式 contactMethod 格式:电话号码
  • 居住地 livingPlace 格式:XX省-XX市-XX区
  • 详细住址 livingPlaceDetail
  • 打卡省份 checkPlaceProvince
  • 打卡城市 checkPlaceCity
  • 打卡县市区 checkPlaceArea

以上这些是必须要填写的,另外还有一个不能忽视:

"other": {
        "openid": ""
    }

这里是一定不能忘记的,要不然就会一直报错,这里需要用到抓包工具,我推荐一个抓包工具:Fiddler

获取openid

首先需要在电脑上登陆微信,找到校趣多的小程序:

image-20220202181253494

用电脑打卡一次,去找到路径:

image-20220202181541444

获取到自己openid以后就填写到代码当中去,这里最好先手动生成一下config.json,因为阿里云FC那个里面不知道是怎么回事,无法通过代码自动生成config.json

设置定时触发

image-20220203115927372

这里可以设置定时触发器,我设置的是上海时间每天早上六点自动打卡:CRON_TZ=Asia/Shanghai 0 0 6 * * *

修改版本

这里对原先的代码进行了一些修改,增添了以下功能:

  • 邮件发送

代码如下:

# -*- coding: utf-8 -*-
import logging
import datetime
import json
import os
import pytz
import smtplib
import requests
from email.mime.text import MIMEText
from email.header import Header

logger = logging.getLogger()

# 邮箱参数
sender = ''  # 发送邮箱
pwd = ''  # 邮箱smtp密码
server_host = ''  # smtp地址
receiver = ''  # 接收者

# post地址
url = r"https://mps.zocedu.com/corona/submitHealthCheck/submit"
url_info = r"https://mps.zocedu.com/corona/submitHealthCheck/getCurrentInfo"

# 生成json文件
defaultjson = {
    "data": {
        "checkPlace": "",
        "contactMethod": "",
        "teacher": "",
        "temperature": "36.2",
        "isCohabitFever": "否",
        "isLeavePalce": "否",
        "beenPlace": "",
        "isContactNcov": "否",
        "livingPlace": "",
        "livingPlaceDetail": "",
        "name1": "",
        "relation1": "",
        "phone1": "",
        "name2": "",
        "relation2": "",
        "phone2": "",
        "remark": "",
        "extraInfo": "[]",
        "healthStatus": "z",
        "emergencyContactMethod": "[]",
        "checkPlacePoint": "124,37",
        "checkPlaceDetail": "",
        "checkPlaceCountry": "",
        "checkPlaceProvince": "",
        "checkPlaceCity": "",
        "checkPlaceArea": "",
    },
    "other": {
        "openid": ""
    }
}
openid = ""
headers = {
    "Content-Type": "application/x-www-form-urlencoded",
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36 MicroMessenger/7.0.9.501 NetType/WIFI MiniProgramEnv/Windows WindowsWechat"
}
data = {}
jsonfile = "config.json"


# 获取JSESSIONID
def getSessionID():
    url = "https://mps.zocedu.com/corona/submitHealthCheck"
    res = requests.get(url, {
        "openId": openid,
        "latitude": "",
        "longitude": ""
    })
    sessionid = res.cookies.get("JSESSIONID")
    return sessionid


# 加载Json配置文件
def loadJson():
    global data, openid
    f = open(jsonfile, "r")
    obj = json.load(f)
    f.close()
    data = obj["data"]
    openid = obj["other"]["openid"]


# 打卡函数
def checkIn():
    cookies = {
        "JSESSIONID": getSessionID()
    }
    res = requests.post(url, data=data, headers=headers, cookies=cookies)
    if res.text == "":
        logger.info("校趣多打卡成功!当前时间:" + datetime.datetime.now(pytz.timezone('PRC')).strftime("%Y-%m-%d %H:%M:%S"))
        send_email()
    else:
        logger.error("校趣多打卡失败!请检查配置文件是否填写正确!")
        send_error_email()


# 创建配置文件
def createConfigFile():
    global defaultjson
    f = open(jsonfile, "w")
    json.dump(defaultjson, f, ensure_ascii=False, indent=2)
    f.close()


def send_email():
    global server
    # 邮件内容
    subject = '健康打卡已经完成!'
    time = datetime.datetime.now(pytz.timezone('PRC')).strftime("%Y-%m-%d %H:%M")
    sentence = '当前时间为:' + time + ',当天健康打卡已经完成!'
    message = MIMEText(sentence, 'plain', 'utf-8')
    message['Subject'] = Header(subject, 'utf-8')
    message['From'] = sender

    # 发送
    try:
        server = smtplib.SMTP_SSL(server_host)
        server.connect(server_host, 465)
        server.login(sender, pwd)
        server.sendmail(sender, receiver, message.as_string())
        print("邮件发送成功")
    except smtplib.SMTPException:
        print("Error: 无法发送邮件")
    finally:
        server.close()


def send_error_email():
    global server
    # 邮件内容
    subject = '好像出错啦!'
    time = datetime.datetime.now(pytz.timezone('PRC')).strftime("%Y-%m-%d %H:%M")
    sentence = '当前时间为:' + time + ',当天未完成健康打卡,请手动打卡,错误信息见控制台。'
    message = MIMEText(sentence, 'plain', 'utf-8')
    message['Subject'] = Header(subject, 'utf-8')
    message['From'] = sender

    # 发送
    try:
        server = smtplib.SMTP_SSL(server_host)
        server.connect(server_host, 465)
        server.login(sender, pwd)
        server.sendmail(sender, receiver, message.as_string())
        print("邮件发送成功")
    except smtplib.SMTPException:
        print("Error: 无法发送邮件")
    finally:
        server.close()


def handler(event, context):
    if not os.path.exists(jsonfile):
        createConfigFile()
        logger.error("未检测到配置文件,请填写config.json后运行本打卡脚本!")
        exit(0)
    else:
        loadJson()
        checkIn()

结果截图

image-20220202182901321


image-20220202182916790


我把代码放Github上了,欢迎大家forkpr,谢谢大家观看!

Readme Card