-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautomate.py
83 lines (69 loc) · 1.8 KB
/
automate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import json
import urllib.request
from bs4 import BeautifulSoup
from pywinauto import Application
import os
import sys
if(len(sys.argv)==1):
app = Application(backend='uia')
app.connect(title_re=".*Chrome.*")
dlg = app.top_window()
url = dlg.child_window(title="Address and search bar", control_type="Edit").get_value()
url = "https://" + url
filename = "solution.cpp__tests"
elif(len(sys.argv)==2):
app = Application(backend='uia')
app.connect(title_re=".*Chrome.*")
dlg = app.top_window()
url = dlg.child_window(title="Address and search bar", control_type="Edit").get_value()
url = "https://" + url
filename = sys.argv[1] + "__tests"
else:
url = sys.argv[1]
filename = sys.argv[2] + ".cpp__tests"
if( url.find('https://codeforces.com/') == -1):
with open("solution.cpp__tests", "w") as outfile:
outfile.write('Please open a problem page')
exit()
else:
try:
page = urllib.request.urlopen(url)
except:
print(url)
exit()
soup = BeautifulSoup(page, features = "html.parser")
x = soup.body.find_all('div', attrs={'class' : 'input'})
y = soup.body.find_all('div', attrs={'class' : 'output'})
res = ""
out = ""
for elements in x:
for br in elements.find_all("br"):
br.replace_with("\n")
res += elements.text
for elements in y:
out += elements.text
if 'Input\n' in res:
res = res.split('Input\n')
else:
res = res.split('Input')
if 'Output\n' in out:
out = out.split('Output\n')
else:
out = out.split('Output')
res.remove("")
out.remove("")
#res = [elements.strip() for elements in res]
out = [elements.strip() for elements in out]
correct = []
for elements in out:
correct.append([elements])
final = []
sz = len(res)
for i in range(sz):
dic = {
"correct_answers" : correct[i],
"test" : res[i]
}
final.append(dic)
with open(filename, "w") as outfile:
outfile.write(json.dumps(final))