-
Notifications
You must be signed in to change notification settings - Fork 4
/
runWithoutRequests.py
85 lines (70 loc) · 1.78 KB
/
runWithoutRequests.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
"""
:copyright: (c) 2014 by Shyam Sundar Choudhary.
:license: The MIT License , see LICENSE for more details.
"""
import language
#import requests
import json
import sys
import os.path
import urllib
import urllib2
# constants
RUN_URL = u'http://api.hackerearth.com/code/run/'
CLIENT_SECRET = '807834b1529d46b54b22f7427156bf95562db4df'
#taking file input
#supported languages:
#C C++ Clojure Haskell C# Java Objective-C Perl PHP Python ruby
#C, CPP, CPP11, CLOJURE, CSHARP, JAVA, JAVASCRIPT, HASKELL, PERL, PHP, PYTHON, RUBY
filename=sys.argv[1]
source = open(filename, 'r')
langua=filename.rsplit('.',1)[1]
#decide language
if langua in language.DetectedLanguage:
lang=language.DetectedLanguage[langua]
print "\nLanguage Detected : ",lang,"\n"
else:
print "This Is Not A Compitible Language.\n"
print "Extention Detected : ",langua.upper(),"\n"
sys.exit()
#taking input
if len(sys.argv)<=2:
print "No Input Provided."
inp=""
else:
print "provided input : "
input_file_name=sys.argv[2]
inp= open(input_file_name, 'r').read()
print inp
print
#data to be sent
data = {
'input' : inp,
'client_secret': CLIENT_SECRET,
'async': 0,
'source': source.read(),
'lang': lang,
'time_limit': 5,
'memory_limit': 262144,
}
req = urllib2.Request(RUN_URL, urllib.urlencode(data))
res = urllib2.urlopen(req)
# response in JSON format
payload = json.load(res)
#if requests
#r = requests.post(RUN_URL, data=data)
#closing source file
source.close()
#if requests then
#payload= r.json()
#print payload
run_status = payload.get('run_status')
o = run_status['output']
web=payload.get('web_link')
time=run_status['time_used']
memory=run_status['memory_used']
print "output : "
print "\n",o
print "Memory Used : ",memory,"\n"
print "Time Taken : ",time,"\n"
print "Web Link For Code : ",web,"\n"