Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.67 KB | None | 0 0
  1. from enum import Enum
  2. import hashlib
  3. import re
  4. import requests
  5. import sys
  6. import time
  7.  
  8. ############################################################
  9. # Configs
  10. username = 'username'
  11. password = 'pwd'
  12. # The partner name as seen from the "Unregistered" column of registration table.
  13. # E.g. 'first_name lastname 1234' -- The whitespaces really do exist.
  14. partner = 'partner_first partner_last id'
  15. # 4/24/2018 8PM = 287
  16. # 4/24/2018 6:30PM = 286
  17. # Probably have to keep incrementing this by 2 every week.
  18. # If you exceed the latest play date, they will just register from max play_date.
  19. play_date = '287'
  20. # How often to try registering.
  21. delay_secs = 5
  22. ######################################################################
  23.  
  24. class Status(Enum):
  25.   SUCCESS = 0
  26.   TEMPORARY_ERROR = 1
  27.   PERMANENT_ERROR = 2
  28.  
  29. def main():
  30.   while True:
  31.     result = register(play_date, username, password, partner)
  32.     if result == Status.SUCCESS:
  33.       break
  34.     time.sleep(delay_secs)
  35.  
  36. def register(play_date, username, password, partner):
  37.   # 1. Get the initial session ids to be passed in future requests.
  38.   initial_resp = requests.get("http://www.seattlebadmintonclub.com/Login.aspx")
  39.   view_state = extract_hidden_form_value(initial_resp.text, '__VIEWSTATE')
  40.   view_state_generator = extract_hidden_form_value(initial_resp.text, '__VIEWSTATEGENERATOR')
  41.   event_validation = extract_hidden_form_value(initial_resp.text, '__EVENTVALIDATION')
  42.   previous_page = extract_hidden_form_value(initial_resp.text, '__PREVIOUSPAGE')
  43.   asp_net_session_id = initial_resp.cookies.get('ASP.NET_SessionId', '')
  44.   forms_auth = initial_resp.cookies.get('.ASPXFORMSAUTH', '')
  45.  
  46.   #2. Login request.
  47.   login_body = {
  48.   '__ASYNCPOST': 'true',
  49.   '__EVENTARGUMENT': '',
  50.   '__EVENTTARGET': '',
  51.   '__EVENTVALIDATION': event_validation,
  52.   '__PREVIOUSPAGE': previous_page,
  53.   '__VIEWSTATE': view_state,
  54.   '__VIEWSTATEGENERATOR': view_state_generator,
  55.   'ctl00$ScriptManager1': 'ctl00$bodyContentPlaceHolder$UpdatePanel1|ctl00$bodyContentPlaceHolder$Login1$LoginButton',
  56.   'ctl00$bodyContentPlaceHolder$Login1$LoginButton': 'Log In',
  57.   'ctl00$bodyContentPlaceHolder$Login1$Password': password,
  58.   'ctl00$bodyContentPlaceHolder$Login1$UserName': username
  59.   }
  60.  
  61.   login_headers = {
  62.   'Accept': '*/*',
  63.   'Accept-Encoding': 'gzip, deflate',
  64.   'Accept-Language': 'en-US,en;q=0.9,id;q=0.8',
  65.   'Cache-Control': 'no-cache',
  66.   'Connection': 'keep-alive',
  67.   'Content-Length': '1084',
  68.   'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
  69.   'Host': 'www.seattlebadmintonclub.com',
  70.   'Origin': 'http://www.seattlebadmintonclub.com',
  71.   'Referer': 'http://www.seattlebadmintonclub.com/Login.aspx',
  72.   'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36',
  73.   'X-MicrosoftAjax': 'Delta=true'
  74.   }
  75.  
  76.   jar = requests.cookies.RequestsCookieJar()
  77.   jar.set('ASP.NET_SessionId', asp_net_session_id)
  78.  
  79.   login_resp = requests.post("http://www.seattlebadmintonclub.com/Login.aspx",
  80.     data=login_body, headers=login_headers, cookies=jar)
  81.   login_view_state = extract_hidden_form_value(login_resp.text, '__VIEWSTATE')
  82.   login_view_state_generator = extract_hidden_form_value(login_resp.text, '__VIEWSTATEGENERATOR')
  83.   login_event_validation = extract_hidden_form_value(login_resp.text, '__EVENTVALIDATION')
  84.   login_session_id = login_resp.cookies.get('ASP.NET_SessionId', '')
  85.   login_forms_auth = login_resp.cookies.get('.ASPXFORMSAUTH', '')
  86.   jar.set('.ASPXFORMSAUTH', login_forms_auth)
  87.  
  88.   if len(login_forms_auth) > 0:
  89.     print ('Login successful!')
  90.   else:
  91.     print ('Login failed. Response: ' + login_resp.text[:20])
  92.     return Status.PERMANENT_ERROR
  93.  
  94.   #3. Load league radio buttons. Doesn't look like this can get bypassed. Need the new event validation ids.
  95.   load_radio_headers = {
  96.   'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
  97.   'Accept-Encoding': 'gzip, deflate',
  98.   'Accept-Language': 'en-US,en;q=0.9,id;q=0.8',
  99.   'Connection': 'keep-alive',
  100.   'Host': 'www.seattlebadmintonclub.com',
  101.   'Referer': 'http://www.seattlebadmintonclub.com/Login.aspx',
  102.   'Upgrade-Insecure-Requests': '1',
  103.   'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
  104.   }
  105.  
  106.   load_radio_resp = requests.get(
  107.     "http://www.seattlebadmintonclub.com/Security.aspx",
  108.     headers=load_radio_headers, cookies=jar)
  109.   load_radio_view_state = extract_hidden_form_value(load_radio_resp.text, '__VIEWSTATE')
  110.   load_radio_view_state_generator = extract_hidden_form_value(load_radio_resp.text, '__VIEWSTATEGENERATOR')
  111.   load_radio_event_validation = extract_hidden_form_value(load_radio_resp.text, '__EVENTVALIDATION')
  112.  
  113.   # Need to register POST twice. They chain the event validations.
  114.   # It's only after this dance that the next GET Register2.aspx will show the ladder
  115.   # Without this dance, it will GET Register2 will still show the radios.
  116.   choose_radio_1_headers = {
  117.   'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
  118.   'Accept-Encoding': 'gzip, deflate',
  119.   'Accept-Language': 'en-US,en;q=0.9,id;q=0.8',
  120.   'Connection': 'keep-alive',
  121.   'Host': 'www.seattlebadmintonclub.com',
  122.   'Referer': 'http://www.seattlebadmintonclub.com/Security.aspx',
  123.   'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36',
  124.   'X-MicrosoftAjax': 'Delta=true'
  125.   }
  126.  
  127.   choose_radio1_body = {
  128.     'ctl00$ScriptManager1': 'ctl00$bodyContentPlaceHolder$UpdatePanel1|ctl00$bodyContentPlaceHolder$leagueRadioButtonList$2',
  129.   '__EVENTTARGET': 'ctl00$bodyContentPlaceHolder$leagueRadioButtonList$2',
  130.   '__EVENTARGUMENT': '',
  131.   '__LASTFOCUS': '',
  132.   '__VIEWSTATE': load_radio_view_state,
  133.   '__VIEWSTATEGENERATOR': load_radio_view_state_generator,
  134.   '__EVENTVALIDATION': load_radio_event_validation,
  135.   'ctl00$bodyContentPlaceHolder$leagueRadioButtonList': '7',
  136.   '__ASYNCPOST': 'true',
  137.   }
  138.  
  139.   choose_radio1_resp = requests.post(
  140.     "http://www.seattlebadmintonclub.com/Security.aspx",
  141.     data=choose_radio1_body, headers=choose_radio_1_headers, cookies=jar)
  142.  
  143.   choose_radio1_view_state = extract_post_resp(choose_radio1_resp.text, '__VIEWSTATE')
  144.   choose_radio1_view_state_generator = extract_post_resp(choose_radio1_resp.text, '__VIEWSTATEGENERATOR')
  145.   choose_radio1_event_validation = extract_post_resp(choose_radio1_resp.text, '__EVENTVALIDATION')
  146.  
  147.   #4. 2nd radio choosing.
  148.   choose_radio2_body = {
  149.   'ctl00$ScriptManager1': 'ctl00$bodyContentPlaceHolder$UpdatePanel1|ctl00$bodyContentPlaceHolder$LoginButton',
  150.   'ctl00$bodyContentPlaceHolder$leagueRadioButtonList': '7',
  151.   '__EVENTTARGET': '',
  152.   '__EVENTARGUMENT': '',
  153.   '__EVENTVALIDATION': choose_radio1_event_validation,
  154.   '__LASTFOCUS': '',
  155.   '__VIEWSTATE': choose_radio1_view_state,
  156.   '__VIEWSTATEGENERATOR': choose_radio1_view_state_generator,
  157.   '__ASYNCPOST': 'true',
  158.   'ctl00$bodyContentPlaceHolder$LoginButton': 'Enter',
  159.   }
  160.  
  161.   choose_radio2_resp = requests.post(
  162.     "http://www.seattlebadmintonclub.com/Security.aspx",
  163.     data=choose_radio2_body, headers=choose_radio_1_headers, cookies=jar)
  164.  
  165.   # 5. Load registration page. Player list should show up.
  166.   load_reg_headers = {
  167.   'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
  168.   'Accept-Encoding': 'gzip, deflate',
  169.   'Accept-Language': 'en-US,en;q=0.9,id;q=0.8',
  170.   'Connection': 'keep-alive',
  171.   'Host': 'www.seattlebadmintonclub.com',
  172.   'Referer': 'http://www.seattlebadmintonclub.com/Security.aspx',
  173.   'Upgrade-Insecure-Requests': '1',
  174.   'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
  175.   }
  176.  
  177.   load_reg_resp = requests.get(
  178.     "http://www.seattlebadmintonclub.com/Register2.aspx",
  179.     headers=load_reg_headers, cookies=jar)
  180.  
  181.   load_reg_view_state = extract_hidden_form_value(load_reg_resp.text, '__VIEWSTATE')
  182.   load_reg_view_state_generator = extract_hidden_form_value(load_reg_resp.text, '__VIEWSTATEGENERATOR')
  183.   load_reg_event_validation = extract_hidden_form_value(load_reg_resp.text, '__EVENTVALIDATION')
  184.  
  185.   # 6 (Final). Make the actual registration request.
  186.   register_body = {
  187.   '__ASYNCPOST': 'true',
  188.   '__EVENTARGUMENT': '',
  189.   '__EVENTTARGET': '',
  190.   '__EVENTVALIDATION': load_reg_event_validation,
  191.   '__LASTFOCUS': '',
  192.   '__VIEWSTATE': load_reg_view_state,
  193.   '__VIEWSTATEGENERATOR': load_reg_view_state_generator,
  194.   'ctl00$ScriptManager1': 'ctl00$bodyContentPlaceHolder$UpdatePanel1|ctl00$bodyContentPlaceHolder$registerTB',
  195.   'ctl00$bodyContentPlaceHolder$ddlistPlayDate': play_date,
  196.   'ctl00$bodyContentPlaceHolder$listUnselected': partner,
  197.   'ctl00$bodyContentPlaceHolder$registerTB': 'Register'
  198.   }
  199.  
  200.   register_headers = {
  201.   'Accept': '*/*',
  202.   'Accept-Encoding': 'gzip, deflate',
  203.   'Accept-Language': 'en-US,en;q=0.9,id;q=0.8',
  204.   'Cache-Control': 'no-cache',
  205.   'Connection': 'keep-alive',
  206.   'Content-Length': '51043',
  207.   'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
  208.   'Host': 'www.seattlebadmintonclub.com',
  209.   'Origin': 'http://www.seattlebadmintonclub.com',
  210.   'Referer': 'http://www.seattlebadmintonclub.com/Register2.aspx',
  211.   'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36',
  212.   'X-MicrosoftAjax': 'Delta=true'
  213.   }
  214.  
  215.   register_resp = requests.get("http://www.seattlebadmintonclub.com/Register2.aspx",
  216.     data=register_body, headers=register_headers, cookies=jar)
  217.   #print (register_resp.text)
  218.   if 'Maximum number of teams' in register_resp.text:
  219.     # Too lazy to regex to check if spot already taken.
  220.     # If you run script early, then this should only mean next week's timeslot is not open yet.
  221.     print ('All spots taken. Either registered too late, or spot not open yet')
  222.     return Status.TEMPORARY_ERROR
  223.   else:
  224.     # Too lazy to check for other error cases.
  225.     return Status.SUCCESS
  226.  
  227.  
  228. # Get the value corresponding to a hidden form field in html
  229. def extract_hidden_form_value(html_dump, hidden_field_name):
  230.   search_result = re.search('id="' + hidden_field_name + '" value="(.*?)"', html_dump)
  231.   return '' if search_result is None else search_result.group(1)
  232.  
  233. def extract_post_resp(resp_dump, field_name):
  234.   search_result = re.search('\|' + field_name + '\|(.*?)\|', resp_dump)
  235.   return '' if search_result is None else search_result.group(1)
  236.  
  237. # Summarize key-val strings. Values are summarized for ez comparison.
  238. def print_summary(dict):
  239.   for k in dict:
  240.     v = dict[k]
  241.     hash = hashlib.md5(v.encode('utf-8')).hexdigest()
  242.     print ("  ", k, (v if (len(v) < 20) else '({})({}){}...{}'.format(len(v), hash, v[:5], v[-5:])))
  243.  
  244. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement