├── README.md └── submissions.py /README.md: -------------------------------------------------------------------------------- 1 | # Codeforces Submission Parser 2 | ## How to use 3 | you should have ***BeautifulSoup*** installed for python3 - to install it copy and past the following command to your terminal
4 | using apt package manager -> `sudo apt-get install python3-bs4`
5 | using pip dependencies manager - >`pip3 install beautifulsoup`
6 | to run the program use the following command
7 | `python3 submissions.py `
8 | -------------------------------------------------------------------------------- /submissions.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | import urllib.request 3 | import os 4 | import re 5 | 6 | 7 | try: 8 | from bs4 import BeautifulSoup 9 | except Exception as e: 10 | print(e, "\n------------------------------------------------" 11 | "\nYout should have BeautifulSoup installed\n" 12 | "You can use the following command to install it\n", 13 | "sudo apt-get install python3-bs4\n" 14 | "------------------------------------------------" 15 | ) 16 | raise e 17 | 18 | soup = None 19 | 20 | 21 | def parse_it(div_class, file_name): 22 | global soup 23 | element = "input" if file_name == "in" else "output" 24 | file_name = "." + file_name 25 | test_number = 0 26 | for div in soup.find_all("div", div_class): 27 | test_number += 1 28 | print(">> Parsing", element, "#", test_number) 29 | with open(str(test_number) + file_name, 'w') as out_file: 30 | out_file.writelines(div.contents[len(div.contents) - 2].pre.contents) 31 | 32 | return test_number 33 | 34 | 35 | def main(): 36 | problem_link = input(">> Enter Submission Link: ") 37 | print(">> Please wait while downloading the submission content") 38 | try: 39 | my_request = urllib.request.urlopen(problem_link) 40 | my_html = my_request.read() 41 | except Exception as __connection_error__: 42 | print(">> Something went wrong while reading the submission link!", __connection_error__) 43 | return -1 44 | 45 | print(">> Submission HTML Downloaded successfully") 46 | print(">> Extraction input and output from submission HTML") 47 | 48 | numbers_in_link = re.findall("([0-9]+)", problem_link) # numbers 49 | 50 | contest_number = numbers_in_link[0] 51 | submission_number = numbers_in_link[1] 52 | 53 | dir_name = str(contest_number) + '_' + str(submission_number) 54 | 55 | # create a directory named after the submission number and contest number 56 | os.system("mkdir " + dir_name) 57 | os.chdir("./" + str(dir_name)) 58 | os.system('mkdir test_cases') 59 | os.chdir("./test_cases") 60 | 61 | global soup 62 | soup = BeautifulSoup(my_html, "html.parser") 63 | 64 | number_of_testcases = parse_it("file input-view", "in") 65 | if parse_it("file answer-view", "out"): 66 | print(">> Parsed", number_of_testcases, "test case") 67 | print(">> DONE!", 68 | "\n>> Submission has been parsed Successfully to", dir_name, "!" 69 | ) 70 | os.chdir("..") 71 | with open("number_of_test_cases.txt", "w") as n_ts: 72 | n_ts.write(str(number_of_testcases)) 73 | else: 74 | print(">> Something went WRONG!") 75 | 76 | 77 | if __name__ == '__main__': 78 | main() 79 | --------------------------------------------------------------------------------