Skip to content

Instantly share code, notes, and snippets.

@Konano
Created December 4, 2020 06:31
Show Gist options
  • Save Konano/b37b1349211599dfd322511bb5cedb40 to your computer and use it in GitHub Desktop.
Save Konano/b37b1349211599dfd322511bb5cedb40 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
# @Author: NanoApe
# @Date: 2020-12-04 11:40:53
# @Last Modified by: NanoApe
# @Last Modified time: 2020-12-04 14:26:30
import requests, random, re
import http.cookiejar as HC
user = 2017000000
pwd = 'password'
session = requests.session()
session.cookies = HC.LWPCookieJar(filename='50_cookies')
try:
session.cookies.load(ignore_discard=True)
except:
pass
def login():
session.post('http://50.tsinghua.edu.cn/j_spring_security_check', data={'un': user, 'pw': pwd, 'x': random.randint(1,100), 'y': random.randint(1,30)}, timeout=(5,10))
session.cookies.save(ignore_discard=True)
def getArea():
r = session.get('http://50.tsinghua.edu.cn/gymsite/gymBookViewAction.do?ms=viewBook_site&fromPage=index', timeout=(5,10))
return re.findall(r'<a href="javascript:submitView\(\'(\d*)\',\'(\d*)\'\)" class=".*" sportsId="\d*" gymnasium_id="\d*" item_id="\d*">&nbsp;(.*)&nbsp;</a>', r.text)
def getFree(gym_id, item_id, date): # find areas which can be booked
r = session.get(f'http://50.tsinghua.edu.cn/gymsite/cacheAction.do?ms=viewBook&gymnasium_id={gym_id}&item_id={item_id}&time_date={date}&userType=', timeout=(5,10))
book = re.findall(r'markStatusColor\(\'(\d*)\'\,\'([^\']*)\'\,\'(\d*)',r.text)
field = re.findall(r'resourceArray.push\({id:\'(\d*)\',time_session:\'([\d:-]*)\',field_name:\'(.*)\',overlaySize:\'(\d*)\',can_net_book:\'(\d*)\'}\);', r.text)
cost = re.findall(r'addCost\(\'(\d*)\',\'(\d*).0\'\);', r.text)
return [list(x)+[c[1]] for x in field if x[0] not in [y[0] for y in book] and x[-1] == '1' for c in cost if c[0] == x[0]]
def book(area_name, date, pay_online, phone):
assert pay_online == True or pay_online == False
pay_way = 1 if pay_online else 0
area = getArea()
area = [x for x in area if x[2] == area_name]
assert len(area) == 1
gym_id, item_id = area[0][0], area[0][1]
data = getFree(gym_id, item_id, date)
for x in data:
if x[-1] == '0': # book when cost is 0
resource_id = x[0]
cost = x[-1]
r = session.post('http://50.tsinghua.edu.cn/gymbook/gymbook/gymBookAction.do?ms=saveGymBook',
data={'bookData.totalCost': cost, 'bookData.book_person_zjh': '', 'bookData.book_person_name': '', 'bookData.book_person_phone': phone,
'gymnasium_idForCache': gym_id, 'item_idForCache': item_id, 'time_dateForCache': date, 'userTypeNumForCache': 1, 'putongRes': 'putongRes', 'selectedPayWay': pay_way, 'allFieldTime': f'{resource_id}#{date}'}, timeout=(5,10))
print(r.text)
return
login()
book('气膜馆乒乓球场', '2020-12-01', True, 13800000000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment