由于每次蹲不到肯德基,于是写了这个脚本

代码如下:

import time
import json
import requests
from dingtalkchatbot.chatbot import DingtalkChatbot

# 设置DingTalk机器人参数
ROBOT_WEBHOOK = "dd"
ROBOT_SECRET = "xxx"
robot = DingtalkChatbot(webhook=ROBOT_WEBHOOK, secret=ROBOT_SECRET)

# 设置微信参数
corpid = 'xxxx'
appsecret = 'bs-xxx-x'
agentid = 1000006

def send_wechat(msg):
    # 获取access token
    token_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + appsecret
    req = requests.get(token_url)
    accesstoken = req.json()['access_token']

    # 发送消息
    msgsend_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + accesstoken

    touser = '@all'

    params = {
        "touser": touser,
        "msgtype": "text",
        "agentid": agentid,
        "text": {
            "content": str(msg)
        },
        "safe": 0
    }

    req = requests.post(msgsend_url, data=json.dumps(params))
    print(req)

def kfc(number_day):
    # 日期计算
    timeStamp = int(time.mktime(time.localtime(time.time()))) + 86400 * int(number_day)
    otherStyleTime = time.strftime("%Y-%m-%d", time.localtime(timeStamp))
    url = 'https://www.meican.com/preorder/api/v2.1/restaurants/list?tabUniqueId=1f7bb0e8-d5fc-408a-8cec-8dea306edf14&targetTime={}+16:00&client_id=Xqr8w0Uk4ciodqfPwjhav5rdxTaYepD&client_secret=vD11O6xI9bG3kqYRu9OyPAHkRGxLh4E'.format(otherStyleTime)

    headers={
     "GET /preorder/api/v2.1/restaurants/list?tabUniqueId=1f7bb0e8-d5fc-408a-8cec-8dea306edf14&targetTime={}+16": "00&client_id=Xqr8w0Uk4ciodqfPwjhav5rdxTaYepD&client_secret=vD11O6xI9bG3kqYRu9OyPAHkRGxLh4E HTTP/1.1".format(otherStyleTime),
     "Host": "www.meican.com",
     "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv99.0) Gecko/20100101 Firefox/99.0",
     "Accept": "application/json, text/plain, */*",
     "Accept-Language": "zh",
     "Accept-Encoding": "gzip, deflate, br",
     "Cache-Control": "no-cache",
     "Pragma": "no-cache",
     "Expires": "0",
     "Connection": "keep-alive",
     "Referer": "https://www.meican.com/?date={}&key=1f7bb0e8-d5fc-408a-8cec-8dea306edf14X1653984000000".format(tStamp),
     "Cookie": "cna=xxx; machineId=4bdad021-23fc-4d48-a0c9-xx; guestId=90ae2033-f68e-4afa-9470-xx; remember=xx-2012851; PLAY_FLASH=\"inner_authorized=true\"",
     "Sec-Fetch-Dest": "empty",
     "Sec-Fetch-Mode": "cors",
     "Sec-Fetch-Site": "same-origin"
    }

    result_data = requests.get(url, timeout=10, headers=headers)
    result = result_data.text
    jsonfile = json.loads(result)

    canting_time = jsonfile['targetTime']

    for i in jsonfile.get('restaurantList'):
        str_m = '麦当劳'
        str_k = '肯德基'

        if str_m in i['name'] or str_k in i['name']:
            availableDishCount = i['availableDishCount']
            name = i['name']

            if i['availableDishCount'] > 0:
                message = '肯德基来了!抓紧时间\n{}\n开饭时间:{}\n产品余量:{}份'.format(name, canting_time, availableDishCount)
                ding_message = '> - 肯德基来了!抓紧时间\n> - {}\n> - 开饭时间:{}\n> - 产品余量:{}\n'.format(name, canting_time, availableDishCount)
                send_wechat(message)
                robot.send_markdown(title='WTF,高能预警!', text=ding_message + '> - ![](http://pic.962.net/up/2019-2/15511502034033430.gif)\n', is_at_all=True)

if __name__ == '__main__':
    for index in range(1, 5):
        kfc(index)

检查周一到无有无肯德基麦当劳上架咯,有就发。没救不发,现在吃腻了,不想维护了。

才知道脚本已经有bug,报错了如下信息:

Traceback (most recent call last):
File "C:/python/kfc.py", line 8, in 
KfcError: KFC Crazy Thursday WhoEver Gives me 50 CNY, I Will Thank Him.

有缘人可以修复下bug,继续维护一下~

版权声明:如无特殊说明,文章均为本站原创,转载请注明出处

本文链接:http://kkxl95.cn/article/lNohO96f7LwyekVnfzwH/