欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

闯越自动签到demo版补充说明

程序员文章站 2022-07-09 21:10:35
demo代码:https://www.cnblogs.com/canmeng/p/11000548.html 定位出错是由于cookie 我重新登录账号过,cookies的值就变了 当时没注意cookie的值变化,然而定位参数回复仍然回复成功 至于获取cookie可以参考https://www.cn ......

demo代码:

定位出错是由于cookie 我重新登录账号过,cookies的值就变了

当时没注意cookie的值变化,然而定位参数回复仍然回复成功

{"state":1,"meg":"成功","result":{}}

至于获取cookie可以参考

保存文件名为cookie.txt 

格式化为

key=value;key=value;key=value;

提交写入日志后,cookies添加了一个类似这样子的值

wlog_intercontent=2019-06-25%0d%0a%e6%97%a0%e4%ba%ba%e5%80%bc%e5%ae%88%0d%0a%e6%af%8f%e6%97%a5%e6%97%a5%e5%bf%97;wlog_internstateid=3;wlog_logimg=;wlog_newlogimg=;wlog_posaddress=;wlog_poslong=;wlog_poslati=;

根据上面的情况,改进代码如下
#! /usr/bin/python3
# -*- coding:utf-8 -*-
# time : 2019/5/19  20:53
# file : requestforch.py
# by 卤蛋
from urllib.parse import quote
import datetime
import requests
import calendar
import sys
import time
import random
import urllib3
urllib3.disable_warnings(urllib3.exceptions.insecurerequestwarning)

request = requests.session()

d = datetime.datetime.now()

stateid = 3

index_html = __file__.replace("requestforch.py", "") + "index.html"

useragent_list = [r"mozilla/5.0 (linux; android 8.1.0; redmi 6 build/o11019; wv) applewebkit/537.36 (khtml, like gecko) version/4.0 chrome/71.0.3578.99 mobile safari/537.36",
                  r"mozilla/5.0 (linux; android 4.1.1; nexus 7 build/jro03d) applewebkit/535.19 (khtml, like gecko) chrome/18.0.1025.166 safari/535.19",
                  r"mozilla/5.0 (linux; u; android 4.0.4; en-gb; gt-i9300 build/imm76d) applewebkit/534.30 (khtml, like gecko) version/4.0 mobile safari/534.30",
                  r"mozilla/5.0 (linux; u; android 6.2; en-gb; gt-p1000 build/froyo) applewebkit/533.1 (khtml, like gecko) version/4.0 mobile safari/533.1"]

with open(__file__.replace("requestforch.py", "") + r'cookie.txt', 'r') as file:
    cookie = file.read()
cookies = {}  # 初始化cookies字典变量
for line in cookie.split(';'):  # 按照字符:进行划分读取
    # 其设置为1就会把字符串拆分成2份
    name, value = line.strip().split('=', 1)
    cookies[name] = value  # 为字典cookies添加内容

random_num = random.randint(0, len(useragent_list)-1)


def useragent():
    #return useragent_list[random_num]
    return useragent_list[0]


def locationstateadd(userid):
    headers = {
        "content-type": "application/x-www-form-urlencoded",
        "host": "sqg.cydgsx.com",
        "connection": "keep-alive",
        "accept-encoding": "gzip",
        "user-agent": "okhttp/3.10.0",
    }
    data = {"params": str({"state": stateid, "user_id": userid})}
    response = request.get(
        url="https://sqg.cydgsx.com/locationstateadd",
        headers=headers,
        data=data,
        timeout=60,
        verify=false)
    return response.json()


def studentlocationadd(userid):
    headers = {
        "content-type": "application/x-www-form-urlencoded",
        "host": "sqg.cydgsx.com",
        "connection": "keep-alive",
        "accept-encoding": "gzip",
        "user-agent": r"okhttp/3.10.0",
    }
    data = {"params": '{"address":"中国广东省广州市海珠区","latitude":"23.09610289210092","longitude":"113.33666163412784","user_id":%s}' % userid}
    response = request.post(
        url="https://sqg.cydgsx.com/studentlocationadd",
        data=data,
        headers=headers,
        timeout=60,
        verify=false)
    print("定位:",response.text)
    for key, value in response.json().items():
        if value == "成功" and key == "meg":
            return true
    return false


def index(url="https://sqg.cydgsx.com/m/s/log/index",cookie_dict={}):
    headers = {
        "host": "sqg.cydgsx.com",
        "connection": "keep-alive",
        "upgrade-insecure-requests": "1",
        "user-agent": useragent(),
        "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
        "referer": url,
        "accept-encoding": "gzip, deflate",
        "accept-language": "zh-cn,en-us;q=0.9",
        "x-requested-with": "com.gcykj.boss",
    }
    temp_cookies = cookies.copy()
    temp_cookies.update(cookie_dict)
    response = request.get(
        url=url,
        headers=headers,
        cookies=temp_cookies,
        timeout=60,
        verify=false)
    html = response.text.replace('href="', 'href="https://sqg.cydgsx.com').replace("'get','", "'get','https://sqg.cydgsx.com").replace(r'src="', 'src="https://sqg.cydgsx.com')
    with open(index_html, "w", encoding='utf-8') as file:
        file.write(html)
    print(locationstateadd(cookies["loginuser_id"].split("&")[0]))  # 等待服务器添加定位
    return studentlocationadd(cookies["loginuser_id"].split("&")[0])  # 添加定位


def day(string=d.strftime('%y-%m-%d')):

    if d.day == getmonthfirstdayandlastday()[1].day:
        month(d.strftime('%y-%m-%d') + "\n无人值守\n" + "每月小结")
    elif d.weekday() == 6:
        week(d.strftime('%y-%m-%d') + "\n无人值守\n" + "每周周报")
    headers = {
        "host": "sqg.cydgsx.com",
        "user-agent": useragent(),
        "referer": "https://sqg.cydgsx.com/m/s/log/wlog",
        "accept-encoding": "gzip, deflate",
        "accept-language": "zh-cn,en-us;q=0.9",
        "accept": "application/json",
        "origin": "https://sqg.cydgsx.com",
        "x-requested-with": "xmlhttprequest",
        "connection": "keep-alive",
        "content-type": "application/x-www-form-urlencoded"
    }

    data = {"internstateid": stateid,
            "intercontent": quote(string, 'utf-8'),
            "logimg": "",
            "newlogimg": "",
            "posaddress": "",
            "poslong": "",
            "poslati": ""}
    response = request.post(
        url="https://sqg.cydgsx.com/m/s/log/savewritelog",
        data=data,
        headers=headers,
        cookies=cookies,
        timeout=60,
        verify=false)
    print("cookies: ",response.cookies.items())
    for key, value in response.json().items():
        if value == "成功" and key == "meg":
            print("每日小结: ",response.json())
            return true
    return false


def week(string=d.strftime('%y-%m-%d')):
    if d.weekday() != 6:
        return false
    else:
        headers = {
            "host": "sqg.cydgsx.com",
            "connection": "keep-alive",
            "accept": "application/json",
            "origin": "https://sqg.cydgsx.com",
            "x-requested-with": "xmlhttprequest",
            "user-agent": useragent(),
            "content-type": "application/x-www-form-urlencoded",
            "referer": "https://sqg.cydgsx.com/m/s/log/wweeksmy?date={}".format(d.strftime('%y-%m-%d')),
            "accept-encoding": "gzip, deflate",
            "accept-language": "zh-cn,en-us;q=0.9",
        }
        data = {
            "summaryinfo": string,
            "logimg": "",
            "newlogimg": "",
            "smydate": quote(d.strftime('%y/%m/%d'),
                             'utf-8').replace("2f05%",
                                              "2f5%") + "+" + quote("0:00:00", 'utf-8'),
            "summarytype": "周小结",
        }
        response = request.post(
            url="https://sqg.cydgsx.com/m/s/log/savesmyjson",
            data=data,
            headers=headers,
            cookies=cookies,
            timeout=60,
            verify=false)

        for key, value in response.json().items():
            if value == "成功" and key == "meg":
                print("每周小结: ",response.json())
                return true
    return false


def month(string=d.strftime('%y-%m-%d')):
    if d.day != getmonthfirstdayandlastday()[1].day:
        return false
    else:
        headers = {
            "host": "sqg.cydgsx.com",
            "connection": "keep-alive",
            "accept": "application/json",
            "origin": "https://sqg.cydgsx.com",
            "x-requested-with": "xmlhttprequest",
            "user-agent": useragent(),
            "content-type": "application/x-www-form-urlencoded",
            "referer": "https://sqg.cydgsx.com/m/s/log/wmonthsmy?date={}".format(d.strftime('%y-%m-%d')),
            "accept-encoding": "gzip, deflate",
            "accept-language": "zh-cn,en-us;q=0.9",
        }

        data = {
            "summaryinfo": string,
            "logimg": "",
            "newlogimg": "",
            "smydate": quote(d.strftime('%y/%m/%d'),
                             'utf-8').replace("%2f0", "%2f") + "+" + quote("22:32:00", 'utf-8'),
            "summarytype": "月小结",
        }
        response = request.post(
            url="https://sqg.cydgsx.com/m/s/log/savesmyjson",
            data=data,
            headers=headers,
            cookies=cookies,
            timeout=60,
            verify=false)

        for key, value in response.json().items():
            if value == "成功" and key == "meg":
                print("每月小结: ",response.json())
                return true
    return false


def getmonthfirstdayandlastday(year=none, month=none):
    if year:
        year = int(year)
    else:
        year = datetime.date.today().year

    if month:
        month = int(month)
    else:
        month = datetime.date.today().month

    firstdayweekday, monthrange = calendar.monthrange(year, month)

    return [
        datetime.date(
            year=year,
            month=month,
            day=1),
        datetime.date(
            year=year,
            month=month,
            day=monthrange)]


def main(data=""):
    def setlog(data):
        if sys.argv[1] == "day":
            return day(data + "每日日志")
        elif sys.argv[1] == "week":
            return week(data + "每周周报")
        elif sys.argv[1] == "month":
            return month(data + "每月小结")
    if len(sys.argv) > 2:
        return setlog("".join(sys.argv[2:]))
    elif len(sys.argv) > 1:
        return setlog(data)
    else:
        return day(data + "每日日志")


if __name__ == '__main__':
    print("##"*20)
    # index("https://sqg.cydgsx.com/m/s/home/index")
    if main(d.strftime('%y-%m-%d') + "\n无人值守\n"):
        print(d.strftime('%y-%m-%d %h:%m:%s') + "\t成功写入日志")
        temp_dict = {"wlog_intercontent":quote(d.strftime('%y-%m-%d') + "\n无人值守\n每日日志", 'utf-8')}
        temp_dict["wlog_internstateid"] = str(stateid)
        temp_dict["wlog_logimg"] = ""
        temp_dict["wlog_newlogimg"] = ""
        temp_dict["wlog_posaddress"] = ""
        temp_dict["wlog_poslong"] = ""
        temp_dict["wlog_poslati"] = ""
        print(index("https://sqg.cydgsx.com/m/s/log/index",temp_dict))
    else:
        print(d.strftime('%y-%m-%d %h:%m:%s') + "\n写入日志失败")
    print("==" * 20 + "\n闯越自动写日志系统(更新版)\tby:卤蛋 \n2019.05.20·06.25\n" + "==" * 20)
    print("##"*20)