图书馆座位自动预约

图书馆座位自动预约

使用方法

  • python3 环境下
  • python test.py -u 登录用户名 -p 密码 -r 自习室号 -s 座位号
  • 座位号请填写三位数,不足三位前面用0补充
  • 自习室号码只有1和2
  • 把脚本放在vps上然后用crontab设置自动运行即可

函数说明

  • def login() 模拟登录,获取cookie
  • seat_get() 座位申请
  • send_mail() 发送邮件(未补充)

程序运行流程

  • 模拟登录,获取cookie
  • 获取座位的唯一标志码parameters
  • 提交座位申请

主程序

# -*- coding:utf-8 -*-

import requests
import argparse
import time
from bs4 import BeautifulSoup


def login(session, user, passwd):       # 自行处理了重定向
    _login_url = "http://202.112.150.5/Default.aspx"
    login_url = "http://202.112.150.5/Florms/FormSYS.aspx"

    head = {
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
        "Accept-Encoding": "gzip, deflate",
        "Accept-Language": "zh-CN,zh;q=0.8",
        "Cache-Control": "max-age=0",
        "Connection": "keep-alive",
        "Content-Length": "320",
        "Content-Type": "application/x-www-form-urlencoded",
        "Host": "202.112.150.5",
        "Origin": "http://202.112.150.5",
        "Referer": "http://202.112.150.5/Default.aspx",
        "Upgrade-Insecure-Requests": "1",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
                      "(KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36",

    }
    data = {
        "__VIEWSTATE": "/wEPDwUKMTc2NzMyNTQ1NGQYAQUeX19Db250cm9sc1JlcXVpc"
                       "mVQb3N0QmFja0tleV9fFgEFBWNtZE9LAU63sGdcRZousyc0v0TDACjoQc9H7B8FrclixY+IihI=",
        "__EVENTVALIDATION": "/wEWBAL+7ZzRCwKl1bKzCQK1qbSRCwLgiqiFDt27SO1Swv+TcdphG8+Hdd86C/KqdRGdDLyzxY/3rowI",
        "txtUserName": user,
        "txtPassword": passwd,
        "cmdOK.x": "37",
        "cmdOK.y": "11",
    }
    _session = session.post(_login_url, data, headers=head).text
    return session


def seat_get(session, room, _seat):
    if room == 1:
        room = "101001"
        seat = room + str(_seat)
    else:
        room = "101002"
        seat = room + str(_seat)
    print(seat)
    # time = "636085440000000000"
    get_url = "http://202.112.150.5/FunctionPages/SeatBespeak/SeatLayoutHandle.ashx"
    head = {
        "Accept": "text/html, */*",
        "Accept-Encoding": "gzip, deflate",
        "Accept-Language": "zh-CN,zh;q=0.8",
        "Connection": "keep-alive",
        "Content-Length": "45",
        "Content-Type": "application/x-www-form-urlencoded",
        "Host": "202.112.150.5",
        "Origin": "http://202.112.150.5",
        # "Referer": "http://202.112.150.5/FunctionPages/SeatBespeak/BespeakSeatLayout.aspx?roomId=101001&date=636085440000000000",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
                      "(KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36",
        "X-Requested-With": "XMLHttpRequest",
    }
    mouth = time.strftime('%m')
    if int(time.strftime('%d')) < 10:
        day = '0' + str(int(time.strftime('%d'))+1)
    else:
        day = str(int(time.strftime('%d'))+1)
    data = {
        "roomNum": room,
        "date": "2016/" + mouth + "/" + day + " 0:00:00",
    }
    soup = BeautifulSoup(session.post(get_url, data=data, headers=head).content.decode(), 'html.parser')
    try:
        parameters = soup.find(attrs={'id': seat}).get('onclick')[18:-2]
        if parameters == None:
            raise ValueError('invalid value')
    except ValueError as e:
        print("你的位子已经有人预约了")
    except Exception as e:
        print("请求错误,请联系作者")
        return
    _get_url = "http://202.112.150.5/FunctionPages/SeatBespeak/BespeakSubmitWindow.aspx?parameters=" + parameters
    head1 = {
        "Accept": "*/*",
        "Accept-Encoding": "gzip, deflate",
        "Accept-Language": "zh-CN,zh;q=0.8",
        "Connection": "keep-alive",
        "Content-Length": "3660",
        "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        "Host": "202.112.150.5",
        "Origin": "http://202.112.150.5",
        "Referer": _get_url,
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
                      "(KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36",
        "X-Requested-With": "XMLHttpRequest",
    }
    data1 = {
        "__EVENTTARGET": "ContentPanel1$btnBespeak",
        "__EVENTARGUMENT": "",
        "__VIEWSTATE": "/wEPDwULLTExNDEyODQ3MDVkGAEFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYHBQVGb3JtMgUURm9yb"
                       "TIkY3RsMDMkcmJsTW9kZWwFIUZvcm0yJGN0bDA0JERyb3BEb3duTGlzdF9GcmVlVGltZQUdRm9ybTIkY3RsMDUkRH"
                       "JvcERvd25MaXN0X1RpbWUFDUNvbnRlbnRQYW5lbDEFGENvbnRlbnRQYW5lbDEkYnRuQmVzcGVhawUWQ29udGVudFBh"
                       "bmVsMSRidG5DbG9zZdsYGf+DorexaTBUgJvhcO5hgSPt8LAEl2Zy6TsLpau8",
        "__EVENTVALIDATION": "/wEWAgLW1Lf9BgL+mI+WBkpvcE4C4rUAG0WtWF76DhiF5RRKR9MffDMa26CfuGpP",
        "roomOpenTime": "7:00",
        "Form2$ctl03$rblModel": "0",
        "Form2$ctl04$DropDownList_FreeTime": "7:10",
        "Form2$ctl05$DropDownList_Time": "10:00",
        "X_CHANGED": "false",
        "X_TARGET": "ContentPanel1_btnBespeak",
        "Form2_Collapsed": "false",
        "ContentPanel1_Collapsed": "false",
        "X_STATE": "eyJGb3JtMl9jdGwwMF9sYmxSb29tTmFtZSI6eyJUZXh0Ijoi56ys5LiA6Ieq5Lmg5a6kIn0sIkZvcm0yX2N0bDAxX2xibFNlYXRObyI6eyJUZXh0IjoiMjA3In0sIkZvcm0yX2N0bDAyX2xibGJlZ2luRGF0ZSI6eyJUZXh0IjoiMjAxNi85LzQifSwiRm9ybTJfY3RsMDNfcmJsTW9kZWwiOnsiSGlkZGVuIjp0cnVlfSwiRm9ybTJfY3RsMDRfRHJvcERvd25MaXN0X0ZyZWVUaW1lIjp7IkhpZGRlbiI6dHJ1ZSwi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A6MjAiLDFdLFsiMjA6MzAiLCIyMDozMCIsMV0sWyIyMDo0MCIsIjIwOjQwIiwxXSxbIjIwOjUwIiwiMjA6NTAiLDFdLFsiMjE6MDAiLCIyMTowMCIsMV0sWyIyMToxMCIsIjIxOjEwIiwxXSxbIjIxOjIwIiwiMjE6MjAiLDFdLFsiMjE6MzAiLCIyMTozMCIsMV0sWyIyMTo0MCIsIjIxOjQwIiwxXSxbIjIxOjUwIiwiMjE6NTAiLDFdXSwiU2VsZWN0ZWRWYWx1ZSI6Ijc6MTAifSwiRm9ybTJfY3RsMDVfRHJvcERvd25MaXN0X1RpbWUiOnsiSGlkZGVuIjp0cnVlLCJYX0l0ZW1zIjpbWyIxMDowMCIsIjEwOjAwIiwxXSxbIjEyOjAwIiwiMTI6MDAiLDFdXSwiU2VsZWN0ZWRWYWx1ZSI6IjEwOjAwIn0sIkZvcm0yX2N0bDA2X2xibEVuZERhdGUiOnsiVGV4dCI6IjY6NTnoh7M4OjQwIn19",
        "X_AJAX": "true",
    }
    _session = session.post(_get_url, data=data1, headers=head1).content.decode()
    print(_session)


def send_mail():
    pass

if __name__ == "__main__":
    '''
    四个参数:登录用户名、密码、自习室号码、座位号、提醒邮箱
    座位号请填写三位数,不足三位前面用0补充
    自习室号码只有1和2
    '''
    parse = argparse.ArgumentParser()
    parse.add_argument("-u", "--user", help="the user")
    parse.add_argument("-p", "--pwd", help="the passwd")
    parse.add_argument("-r", "--room", type=int, help="the room")
    parse.add_argument("-s", "--seat", type=int, help="the seat")
    # parse.add_argument("-m", "--mail", help="the mail")
    # 解析命令行
    results = parse.parse_args()
    user = results.user
    passwd = results.pwd
    room = results.room
    seat = results.seat
    session = requests.Session()
    _session = login(session, user, passwd)
    seat_get(_session, room, seat)
posted @ 2016-09-03 17:14  随意orz  阅读(6168)  评论(1编辑  收藏  举报