-
Notifications
You must be signed in to change notification settings - Fork 0
/
codechef.py
154 lines (130 loc) · 4.42 KB
/
codechef.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import re
from robobrowser import RoboBrowser
import sys
from bs4 import BeautifulSoup
import requests
import platform
import time
"""
This program takes commandline argument as
$python codechef.py c|p [path_to_Filename_with_extension]
c : Contest
p : Practice
"""
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
User = {
'username' : 'user_name',
'password' : 'password'
}
#add your suitable language and code from codechef submit drop down menu
langList = {
'cpp' : '44',
'py' : '4',
'c' : '11',
'java' : '10'
}
delay = 30 # delay to show the verdict once submitted. Adjust as required
def login():
browser = RoboBrowser(parser="html.parser")
browser.open("https://www.codechef.com")
login_form = browser.get_forms()[0]
if login_form == None:
print "Some Error Occurred"
exit(0)
login_form['name'] = User['username']
login_form['pass'] = User['password']
browser.submit_form(login_form)
#authentication yet to be implemented
return browser
def login_Until():
br = login()
while True:
#print br
if br.url == "https://www.codechef.com/node" or br.url == "https://www.codechef.com/":
print bcolors.OKGREEN + "Successfully Logged In" + bcolors.ENDC
break
elif br.url == "https://www.codechef.com/session/limit":
sess_form = br.get_form(id='session-limit-page')
if sess_form == None :
continue
opList = sess_form['sid'].options
sess_form['sid'].value = opList[0]
br.submit_form(sess_form)
else:
#print br.url
br = login()
return br
def get_Problem_Code(prog):
"""Gets Problem code if the file name and the problem code is similar,
for example, if the file name is CHEFCODE.cpp and the problem code is CHEFCODE
"""
os = platform.system()
char =''
if os == 'Windows':
char = '\\'
elif os == 'Linux':
char ='/'
s = prog
while True:
i = s.find(char)
if i == -1:
break
s = s[i+1:]
return s[:s.find('.')]
if __name__ == '__main__':
if len(sys.argv) != 3 :
print bcolors.FAIL + bcolors.BOLD + "Command Error: $python codechef.py p|c <path_to_source_file>" + bcolors.ENDC
exit(0)
browser = login_Until()
prob_type = sys.argv[1]
prog = sys.argv[2]
#prob_code = get_Problem_Code(prog) #use this if you submit your source file as problem code
prog_lang = prog[prog.find('.')+1:]
prob_code = raw_input('Problem Code: ')
if prob_type == 'p':
sub_url = 'https://www.codechef.com/submit/'+prob_code
res_url = url = 'https://www.codechef.com/status/'+prob_code+','+User["username"]
elif prob_type == 'c':
contest = raw_input('Enter Contest Code: ')
sub_url = 'https://www.codechef.com/'+contest+'/submit/'+prob_code
res_url = url = 'https://www.codechef.com/'+contest+'/status/'+prob_code+','+User["username"];
else:
print bcolors.FAIL + bcolors.BOLD + "Command Error: $python codechef.py p|c <path_to_source_file>" + bcolors.ENDC
exit(0)
browser.open(sub_url)
subForm = browser.get_form(id='problem-submission')
if subForm == None:
print bcolors.FAIL +"Some Error Occured! Retry. [Authentication Problem] " + bcolors.ENDC
exit(0)
subForm['files[sourcefile]'].value = open(prog,'r')
subForm['language'].value = langList[prog_lang]
browser.submit_form(subForm)
print bcolors.OKBLUE + "Succesfully Submitted..." + bcolors.ENDC
print bcolors.OKBLUE + "Waiting for Verdict..." + bcolors.ENDC
time.sleep(delay)
f = requests.get(res_url)
soup = BeautifulSoup(f.text,"html.parser")
t = soup.find_all("table",{"class" : "dataTable"})
area = t[0].tbody.tr
if area.img == None:
print bcolors.FAIL + "Error! Please Check Verdict on site : " + res_url + bcolors.ENDC
exit(0)
res_img = area.img['src']
if res_img.endswith('tick-icon.gif'):
print bcolors.OKGREEN + bcolors.BOLD + "Accepted" + bcolors.ENDC
elif res_img.endswith('alert-icon.gif'):
print bcolors.WARNING + bcolors.BOLD + "Compilation Error" + bcolors.ENDC
elif res_img.endswith('cross-icon.gif'):
print bcolors.FAIL + bcolors.BOLD + "Wrong Answer" + bcolors.ENDC
elif res_img.endswith('runtime-error.png'):
print bcolors.FAIL + bcolors.BOLD + "Runtime Error" + bcolors.ENDC
elif res_img.endswith('clock_error.png'):
print bcolors.WARNING + bcolors.BOLD + "Time Limit Exceeded" + bcolors.ENDC