SorahISA

judgeProtocol.py

Jun 7th, 2021 (edited)
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.45 KB | None | 0 0
  1. import getpass, re, json
  2. from requests import Session
  3. from hashlib import sha512
  4. from urllib.parse import urlencode
  5. from time import time
  6.  
  7. def login(s, handle, password):
  8.     res = s.get('https://codeforces.com/enter')
  9.     csrf_token = re.findall('<meta name="X-Csrf-Token" content="(.{32})"/>', res.text)[0]
  10.     data = {
  11.         'csrf_token': csrf_token,
  12.         'action': 'enter',
  13.         'ftaa': '',
  14.         'bfaa': '',
  15.         'handleOrEmail': handle,
  16.         'password': password
  17.     }
  18.     res = s.post('https://codeforces.com/enter', data = data)
  19.     assert 'Logout' in res.text
  20.  
  21. def get_submission_detail(s, ID):
  22.     res = s.get('https://codeforces.com')
  23.     csrf_token = re.findall('<meta name="X-Csrf-Token" content="(.{32})"/>', res.text)[0]
  24.     data = {
  25.         'csrf_token': csrf_token,
  26.         'submissionId': ID,
  27.     }
  28.     res = s.post('https://codeforces.com/group/owo/data/judgeProtocol', data = data) # CHANGE IT
  29.     return res.text
  30.  
  31. def logout(s):
  32.     res = s.get('https://codeforces.com')
  33.     link = re.findall('<a href="(/.{32}/logout)">Logout</a>', res.text)[0]
  34.     res = s.get('https://codeforces.com' + link)
  35.     assert 'Logout' not in res.text
  36.  
  37. def get_submission_ids(s, contestId):
  38.     key = 'owo' # CHANGE IT
  39.     secret = 'owo' # CHANGE IT
  40.     # contestId = 316970
  41.     data = urlencode({
  42.         'apiKey': key,
  43.         'contestId': contestId,
  44.         # 'count': 100,
  45.         # 'from': 1,
  46.         'time': int(time())
  47.     })
  48.     methods = 'contest.status'
  49.     apiSig = sha512(f'123456/{methods}?{data}#{secret}'.encode()).hexdigest()
  50.     res = s.get(f'https://codeforces.com/api/{methods}?{data}', params = {'apiSig': '123456' + apiSig})
  51.     api_json = json.loads(res.text)
  52.     submission_ids = []
  53.     for submission_info in api_json['result']:
  54.         if (submission_info['author']['participantType'] == 'MANAGER'):
  55.             continue
  56.         if (submission_info['relativeTimeSeconds'] == 2147483647):
  57.             continue
  58.         # if (submission_info['points'] == 0.0):
  59.             # continue
  60.         handle = submission_info['author']['members'][0]['handle']
  61.         participant_type = submission_info['author']['participantType']
  62.         problem_index = submission_info['problem']['index']
  63.         submission_time = submission_info['relativeTimeSeconds']
  64.         submission_id = submission_info['id']
  65.         submission_ids.append([handle, participant_type, problem_index, submission_time, submission_id])
  66.     return submission_ids
  67.  
  68. def process_submission(submission):
  69.     start = 0
  70.     place = submission.find('Group')
  71.     subtasks = []
  72.     while (place >= 0):
  73.         start = place+1
  74.         place = submission.find(':', start)
  75.         for i in range(place+2, place+10):
  76.             if (submission[i] == ' '):
  77.                 subtasks.append(float(submission[place+2 : i]))
  78.                 break
  79.         place = submission.find('Group', start)
  80.     return subtasks
  81.  
  82. if __name__ == '__main__':
  83.     # handle = input('enter handle: ')
  84.     handle = 'owo' # CHANGE IT
  85.     password = getpass.getpass('enter password: ')
  86.     contestId = int(input('contestId: '))
  87.    
  88.     s = Session()
  89.     login(s, handle, password)
  90.     submission_ids = get_submission_ids(s, contestId);
  91.     data = [[x[0], x[1], x[2], x[3], process_submission(get_submission_detail(s, x[4]))] for x in submission_ids]
  92.     file = open(str(contestId) + '.txt', 'w')
  93.     for submission in data:
  94.         file.write(str(submission) + '\n')
  95.     logout(s)
  96.  
Add Comment
Please, Sign In to add comment