bingxuan9112

testdata_modify_t.py

Aug 23rd, 2020
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.00 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. upload_input = True # if you want to update the testdata files
  10. upload_output = False
  11. input_suffix = '.in'
  12. output_suffix = '.out'
  13. input_prefix = ''
  14. output_prefix = ''
  15. filename_format = '%s%01d%s' # % (prefix, id, suffix)
  16. time_limit = '1000'
  17. memory_limit = '65536'
  18. output_limit = '262144'
  19.  
  20. # end config
  21.  
  22. session = requests.Session()
  23.  
  24. if upload_input or upload_output: print("You're going to replace testdatas!")
  25.  
  26. def login():
  27.     TIOJusername = input('Username: ')
  28.     TIOJpassword = getpass('Password: ')
  29.     print('logging in...')
  30.     global session
  31.     rel = session.get('https://tioj.ck.tp.edu.tw/users/sign_in')
  32.     soup = BeautifulSoup(rel.text, "html.parser")
  33.     inputs = soup.find('form').find_all('input')
  34.     rel = session.post('https://tioj.ck.tp.edu.tw/users/sign_in', data = {
  35.         inputs[0].attrs['name']: inputs[0].attrs['value'],
  36.         inputs[1].attrs['name']: inputs[1].attrs['value'],
  37.         'user[username]': TIOJusername,
  38.         'user[password]': TIOJpassword,
  39.         'user[remember_me]': '1',
  40.         'commit': 'Sign in'
  41.     })
  42.  
  43. login()
  44. print('Successful log in')
  45. problem_id = input('Problem ID: ')
  46. num_start = int(input('Testdata start number: '))
  47. num_end = int(input('Testdata end number: '))
  48.  
  49. url = 'https://tioj.ck.tp.edu.tw/problems/%s/testdata' % problem_id
  50.  
  51. rel = session.get(url)
  52. soup = BeautifulSoup(rel.text, "html.parser")
  53. inputs = soup.find_all('a')
  54.  
  55. lst = []
  56. for t in inputs:
  57.     st = str(t)
  58.     if st.find("btn btn-info btn-xs") != -1:
  59.         c = st.find("testdata") + 9
  60.         d = st.find("/", c)
  61.         val = int(st[c:d])
  62.         if not val in lst: lst.append(val)
  63. lst = lst[num_start:num_end+1]
  64.  
  65. if len(lst) == 0: print('Error')
  66.  
  67. c = num_start
  68. for i in lst:
  69.     print('processing %d(%d)...' % (c, i))
  70.     now_url = 'https://tioj.ck.tp.edu.tw/problems/%s/testdata/%d/edit' % (problem_id, i)
  71.     post_url = now_url[:-5]
  72.  
  73.     rel = session.get(now_url)
  74.     soup = BeautifulSoup(rel.text, "html.parser")
  75.     inputs = soup.find('form').find_all('input')
  76.  
  77.     data = {
  78.         inputs[0].attrs['name']: inputs[0].attrs['value'],
  79.         inputs[1].attrs['name']: inputs[1].attrs['value'],
  80.         inputs[2].attrs['name']: inputs[2].attrs['value'],
  81.         'testdatum[limit_attributes][time]': time_limit,
  82.         'testdatum[limit_attributes][memory]': memory_limit,
  83.         'testdatum[limit_attributes][output]': output_limit,
  84.         'testdatum[limit_attributes][id]': str(i),
  85.         'testdatum[problem_id]': problem_id,
  86.         'commit': 'Update Testdatum'
  87.     }
  88.  
  89.     files = {}
  90.     if upload_input: files['testdatum[test_input]'] = open(filename_format % (input_prefix, c, input_suffix), 'rb')
  91.     if upload_output: files['testdatum[test_output]'] = open(filename_format % (output_prefix, c, output_suffix), 'rb')
  92.     rel = session.post(post_url, data = data, files = files)
  93.  
  94.     print('Modify %d(%d)!!' % (c, i))
  95.     c += 1
  96.  
Add Comment
Please, Sign In to add comment