bingxuan9112

testdata_upload_t.py

Aug 25th, 2020 (edited)
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.26 KB | None | 0 0
  1. import requests
  2. import random
  3. import string
  4. from bs4 import BeautifulSoup
  5. from getpass import *
  6.  
  7. # config
  8.  
  9. # the testdata will append at the end of the testdata list
  10. input_suffix = '.in'
  11. output_suffix = '.out'
  12. input_prefix = ''
  13. output_prefix = ''
  14. filename_format = '%s%02d%s' # % (prefix, id, suffix)
  15. time_limit = '2500'
  16. memory_limit = '262144'
  17.  
  18. # end config
  19.  
  20. session = requests.Session()
  21.  
  22. def login():
  23.     TIOJusername = input('Username: ')
  24.     TIOJpassword = getpass('Password: ')
  25.     print('logging in...')
  26.     global session
  27.     rel = session.get('https://tioj.ck.tp.edu.tw/users/sign_in')
  28.     soup = BeautifulSoup(rel.text, "html.parser")
  29.     inputs = soup.find('form').find_all('input')
  30.     rel = session.post('https://tioj.ck.tp.edu.tw/users/sign_in', data = {
  31.         inputs[0].attrs['name']: inputs[0].attrs['value'],
  32.         inputs[1].attrs['name']: inputs[1].attrs['value'],
  33.         'user[username]': TIOJusername,
  34.         'user[password]': TIOJpassword,
  35.         'user[remember_me]': '1',
  36.         'commit': 'Sign in'
  37.     })
  38.  
  39. login()
  40. print('Successful log in')
  41. problem_id = input('Problem ID: ')
  42. num_start = int(input('Testdata start number: '))
  43. num_end = int(input('Testdata end number: '))
  44.  
  45. sign_up_get_url = 'https://tioj.ck.tp.edu.tw/problems/%s/testdata/new' % problem_id
  46. sign_up_post_url = 'https://tioj.ck.tp.edu.tw/problems/%s/testdata' % problem_id
  47.  
  48. for i in range(num_start, num_end + 1):
  49.     print('processing %d...' % i)
  50.     rel = session.get(sign_up_get_url)
  51.     soup = BeautifulSoup(rel.text, "html.parser")
  52.     inputs = soup.find('form').find_all('input')
  53.  
  54.     rel = session.post(sign_up_post_url, data = {
  55.         inputs[0].attrs['name']: inputs[0].attrs['value'],
  56.         inputs[1].attrs['name']: inputs[1].attrs['value'],
  57.         'testdatum[limit_attributes][time]': time_limit,
  58.         'testdatum[limit_attributes][memory]': memory_limit,
  59.         'testdatum[limit_attributes][output]': '65536',
  60.         'testdatum[problem_id]': problem_id,
  61.         'commit': 'Create Testdatum'
  62.     }, files = {
  63.         'testdatum[test_input]': open(filename_format % (input_prefix, i, input_suffix), 'rb'),
  64.         'testdatum[test_output]': open(filename_format % (output_prefix, i, output_suffix), 'rb')
  65.     })
  66.     print('Create %d.' % i)
  67.  
Add Comment
Please, Sign In to add comment