Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from enum import Enum
- import hashlib
- import re
- import requests
- import sys
- import time
- ############################################################
- # Configs
- username = 'username'
- password = 'pwd'
- # The partner name as seen from the "Unregistered" column of registration table.
- # E.g. 'first_name lastname 1234' -- The whitespaces really do exist.
- partner = 'partner_first partner_last id'
- # 4/24/2018 8PM = 287
- # 4/24/2018 6:30PM = 286
- # Probably have to keep incrementing this by 2 every week.
- # If you exceed the latest play date, they will just register from max play_date.
- play_date = '287'
- # How often to try registering.
- delay_secs = 5
- ######################################################################
- class Status(Enum):
- SUCCESS = 0
- TEMPORARY_ERROR = 1
- PERMANENT_ERROR = 2
- def main():
- while True:
- result = register(play_date, username, password, partner)
- if result == Status.SUCCESS:
- break
- time.sleep(delay_secs)
- def register(play_date, username, password, partner):
- # 1. Get the initial session ids to be passed in future requests.
- initial_resp = requests.get("http://www.seattlebadmintonclub.com/Login.aspx")
- view_state = extract_hidden_form_value(initial_resp.text, '__VIEWSTATE')
- view_state_generator = extract_hidden_form_value(initial_resp.text, '__VIEWSTATEGENERATOR')
- event_validation = extract_hidden_form_value(initial_resp.text, '__EVENTVALIDATION')
- previous_page = extract_hidden_form_value(initial_resp.text, '__PREVIOUSPAGE')
- asp_net_session_id = initial_resp.cookies.get('ASP.NET_SessionId', '')
- forms_auth = initial_resp.cookies.get('.ASPXFORMSAUTH', '')
- #2. Login request.
- login_body = {
- '__ASYNCPOST': 'true',
- '__EVENTARGUMENT': '',
- '__EVENTTARGET': '',
- '__EVENTVALIDATION': event_validation,
- '__PREVIOUSPAGE': previous_page,
- '__VIEWSTATE': view_state,
- '__VIEWSTATEGENERATOR': view_state_generator,
- 'ctl00$ScriptManager1': 'ctl00$bodyContentPlaceHolder$UpdatePanel1|ctl00$bodyContentPlaceHolder$Login1$LoginButton',
- 'ctl00$bodyContentPlaceHolder$Login1$LoginButton': 'Log In',
- 'ctl00$bodyContentPlaceHolder$Login1$Password': password,
- 'ctl00$bodyContentPlaceHolder$Login1$UserName': username
- }
- login_headers = {
- 'Accept': '*/*',
- 'Accept-Encoding': 'gzip, deflate',
- 'Accept-Language': 'en-US,en;q=0.9,id;q=0.8',
- 'Cache-Control': 'no-cache',
- 'Connection': 'keep-alive',
- 'Content-Length': '1084',
- 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
- 'Host': 'www.seattlebadmintonclub.com',
- 'Origin': 'http://www.seattlebadmintonclub.com',
- 'Referer': 'http://www.seattlebadmintonclub.com/Login.aspx',
- 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36',
- 'X-MicrosoftAjax': 'Delta=true'
- }
- jar = requests.cookies.RequestsCookieJar()
- jar.set('ASP.NET_SessionId', asp_net_session_id)
- login_resp = requests.post("http://www.seattlebadmintonclub.com/Login.aspx",
- data=login_body, headers=login_headers, cookies=jar)
- login_view_state = extract_hidden_form_value(login_resp.text, '__VIEWSTATE')
- login_view_state_generator = extract_hidden_form_value(login_resp.text, '__VIEWSTATEGENERATOR')
- login_event_validation = extract_hidden_form_value(login_resp.text, '__EVENTVALIDATION')
- login_session_id = login_resp.cookies.get('ASP.NET_SessionId', '')
- login_forms_auth = login_resp.cookies.get('.ASPXFORMSAUTH', '')
- jar.set('.ASPXFORMSAUTH', login_forms_auth)
- if len(login_forms_auth) > 0:
- print ('Login successful!')
- else:
- print ('Login failed. Response: ' + login_resp.text[:20])
- return Status.PERMANENT_ERROR
- #3. Load league radio buttons. Doesn't look like this can get bypassed. Need the new event validation ids.
- load_radio_headers = {
- 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
- 'Accept-Encoding': 'gzip, deflate',
- 'Accept-Language': 'en-US,en;q=0.9,id;q=0.8',
- 'Connection': 'keep-alive',
- 'Host': 'www.seattlebadmintonclub.com',
- 'Referer': 'http://www.seattlebadmintonclub.com/Login.aspx',
- 'Upgrade-Insecure-Requests': '1',
- 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
- }
- load_radio_resp = requests.get(
- "http://www.seattlebadmintonclub.com/Security.aspx",
- headers=load_radio_headers, cookies=jar)
- load_radio_view_state = extract_hidden_form_value(load_radio_resp.text, '__VIEWSTATE')
- load_radio_view_state_generator = extract_hidden_form_value(load_radio_resp.text, '__VIEWSTATEGENERATOR')
- load_radio_event_validation = extract_hidden_form_value(load_radio_resp.text, '__EVENTVALIDATION')
- # Need to register POST twice. They chain the event validations.
- # It's only after this dance that the next GET Register2.aspx will show the ladder
- # Without this dance, it will GET Register2 will still show the radios.
- choose_radio_1_headers = {
- 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
- 'Accept-Encoding': 'gzip, deflate',
- 'Accept-Language': 'en-US,en;q=0.9,id;q=0.8',
- 'Connection': 'keep-alive',
- 'Host': 'www.seattlebadmintonclub.com',
- 'Referer': 'http://www.seattlebadmintonclub.com/Security.aspx',
- 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36',
- 'X-MicrosoftAjax': 'Delta=true'
- }
- choose_radio1_body = {
- 'ctl00$ScriptManager1': 'ctl00$bodyContentPlaceHolder$UpdatePanel1|ctl00$bodyContentPlaceHolder$leagueRadioButtonList$2',
- '__EVENTTARGET': 'ctl00$bodyContentPlaceHolder$leagueRadioButtonList$2',
- '__EVENTARGUMENT': '',
- '__LASTFOCUS': '',
- '__VIEWSTATE': load_radio_view_state,
- '__VIEWSTATEGENERATOR': load_radio_view_state_generator,
- '__EVENTVALIDATION': load_radio_event_validation,
- 'ctl00$bodyContentPlaceHolder$leagueRadioButtonList': '7',
- '__ASYNCPOST': 'true',
- }
- choose_radio1_resp = requests.post(
- "http://www.seattlebadmintonclub.com/Security.aspx",
- data=choose_radio1_body, headers=choose_radio_1_headers, cookies=jar)
- choose_radio1_view_state = extract_post_resp(choose_radio1_resp.text, '__VIEWSTATE')
- choose_radio1_view_state_generator = extract_post_resp(choose_radio1_resp.text, '__VIEWSTATEGENERATOR')
- choose_radio1_event_validation = extract_post_resp(choose_radio1_resp.text, '__EVENTVALIDATION')
- #4. 2nd radio choosing.
- choose_radio2_body = {
- 'ctl00$ScriptManager1': 'ctl00$bodyContentPlaceHolder$UpdatePanel1|ctl00$bodyContentPlaceHolder$LoginButton',
- 'ctl00$bodyContentPlaceHolder$leagueRadioButtonList': '7',
- '__EVENTTARGET': '',
- '__EVENTARGUMENT': '',
- '__EVENTVALIDATION': choose_radio1_event_validation,
- '__LASTFOCUS': '',
- '__VIEWSTATE': choose_radio1_view_state,
- '__VIEWSTATEGENERATOR': choose_radio1_view_state_generator,
- '__ASYNCPOST': 'true',
- 'ctl00$bodyContentPlaceHolder$LoginButton': 'Enter',
- }
- choose_radio2_resp = requests.post(
- "http://www.seattlebadmintonclub.com/Security.aspx",
- data=choose_radio2_body, headers=choose_radio_1_headers, cookies=jar)
- # 5. Load registration page. Player list should show up.
- load_reg_headers = {
- 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
- 'Accept-Encoding': 'gzip, deflate',
- 'Accept-Language': 'en-US,en;q=0.9,id;q=0.8',
- 'Connection': 'keep-alive',
- 'Host': 'www.seattlebadmintonclub.com',
- 'Referer': 'http://www.seattlebadmintonclub.com/Security.aspx',
- 'Upgrade-Insecure-Requests': '1',
- 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
- }
- load_reg_resp = requests.get(
- "http://www.seattlebadmintonclub.com/Register2.aspx",
- headers=load_reg_headers, cookies=jar)
- load_reg_view_state = extract_hidden_form_value(load_reg_resp.text, '__VIEWSTATE')
- load_reg_view_state_generator = extract_hidden_form_value(load_reg_resp.text, '__VIEWSTATEGENERATOR')
- load_reg_event_validation = extract_hidden_form_value(load_reg_resp.text, '__EVENTVALIDATION')
- # 6 (Final). Make the actual registration request.
- register_body = {
- '__ASYNCPOST': 'true',
- '__EVENTARGUMENT': '',
- '__EVENTTARGET': '',
- '__EVENTVALIDATION': load_reg_event_validation,
- '__LASTFOCUS': '',
- '__VIEWSTATE': load_reg_view_state,
- '__VIEWSTATEGENERATOR': load_reg_view_state_generator,
- 'ctl00$ScriptManager1': 'ctl00$bodyContentPlaceHolder$UpdatePanel1|ctl00$bodyContentPlaceHolder$registerTB',
- 'ctl00$bodyContentPlaceHolder$ddlistPlayDate': play_date,
- 'ctl00$bodyContentPlaceHolder$listUnselected': partner,
- 'ctl00$bodyContentPlaceHolder$registerTB': 'Register'
- }
- register_headers = {
- 'Accept': '*/*',
- 'Accept-Encoding': 'gzip, deflate',
- 'Accept-Language': 'en-US,en;q=0.9,id;q=0.8',
- 'Cache-Control': 'no-cache',
- 'Connection': 'keep-alive',
- 'Content-Length': '51043',
- 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
- 'Host': 'www.seattlebadmintonclub.com',
- 'Origin': 'http://www.seattlebadmintonclub.com',
- 'Referer': 'http://www.seattlebadmintonclub.com/Register2.aspx',
- 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36',
- 'X-MicrosoftAjax': 'Delta=true'
- }
- register_resp = requests.get("http://www.seattlebadmintonclub.com/Register2.aspx",
- data=register_body, headers=register_headers, cookies=jar)
- #print (register_resp.text)
- if 'Maximum number of teams' in register_resp.text:
- # Too lazy to regex to check if spot already taken.
- # If you run script early, then this should only mean next week's timeslot is not open yet.
- print ('All spots taken. Either registered too late, or spot not open yet')
- return Status.TEMPORARY_ERROR
- else:
- # Too lazy to check for other error cases.
- return Status.SUCCESS
- # Get the value corresponding to a hidden form field in html
- def extract_hidden_form_value(html_dump, hidden_field_name):
- search_result = re.search('id="' + hidden_field_name + '" value="(.*?)"', html_dump)
- return '' if search_result is None else search_result.group(1)
- def extract_post_resp(resp_dump, field_name):
- search_result = re.search('\|' + field_name + '\|(.*?)\|', resp_dump)
- return '' if search_result is None else search_result.group(1)
- # Summarize key-val strings. Values are summarized for ez comparison.
- def print_summary(dict):
- for k in dict:
- v = dict[k]
- hash = hashlib.md5(v.encode('utf-8')).hexdigest()
- print (" ", k, (v if (len(v) < 20) else '({})({}){}...{}'.format(len(v), hash, v[:5], v[-5:])))
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement