-
Notifications
You must be signed in to change notification settings - Fork 9
/
app.py
177 lines (146 loc) · 4.41 KB
/
app.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
'''
Import Modules
'''
from email import message
from flask import Flask, render_template, request,jsonify,redirect,url_for,session
import subprocess
import datetime
import os
import requests
from bs4 import BeautifulSoup
from flask_socketio import SocketIO,emit,join_room,leave_room
from flask_session import Session
from flask_cors import CORS, cross_origin
def generate_txt(url):
'''
Function to webscrape Data from given url
'''
res = requests.get(url)
soup = BeautifulSoup(res.content, 'html.parser')
parts = url.split('/')
num, lev = parts[-2], parts[-1]
ip_op = []
for i in soup.find_all("pre"):
if '<br/>' in str(i):
data = str(i).split('<br/>')
data[0] = data[0].split('<pre>')[1]
data = data[:-1]
ip_op.append(data)
else:
data = str(i).split('\n')
data = data[1:-1]
ip_op.append(data)
input, output = [], []
for i in range(2):
if i % 2 == 0:
input.append("\n".join(ip_op[i]))
else:
output.append("\n".join(ip_op[i]))
input_name, output_name = "input" + num + lev + ".txt", "output" + num + lev + ".txt"
textfile = open(input_name, "w")
for element in input:
textfile.write(element + "\n")
textfile.close()
textfile = open(output_name, "w")
for element in output:
textfile.write(element + "\n")
textfile.close()
return input_name, output_name
def createFile(code,ext):
'''
Function to Create File
'''
fname=datetime.datetime.now().strftime("%m%d%Y%H%M%S")
fname=fname.replace(" ","_")
fname=fname.replace(":","_")
fname=fname+ext
file1 = open(fname, 'w')
file1.write(code)
file1.close()
return fname
def runFile(l,inp):
'''
Run the given file
'''
with open(inp, 'rb', 0) as a:
output = subprocess.run(l,stdin=a,stdout=subprocess.PIPE,stderr=subprocess.PIPE,text=True)
os.remove(inp)
return output.stdout,output.stderr
'''
Initiate the app
'''
app = Flask(__name__)
app.debug = True
app.config['SECRET_KEY'] = 'Innovate.Accelerate.Elevate.'
app.config['SESSION_TYPE'] = 'filesystem'
Session(app)
socketio = SocketIO(app, manage_session=False)
'''
Processing Requests
'''
@app.route('/', methods=['GET','POST'])
def index():
return render_template('index.html')
@app.route('/ide',methods=['GET','POST'])
def ide():
if(request.method=='POST'):
username = request.form['username']
room = request.form['room']
#Store the data in the session
session['username'] = username
session['room'] = room
return render_template('ide.html',session=session)
else:
if(session.get('username') is not None):
return render_template('ide.html',session=session)
else:
return redirect(url_for('index'))
@socketio.on('join',namespace='/ide')
def join(message):
room = session.get('room')
join_room(room)
@socketio.on('text',namespace='/ide')
def text(message):
room = session.get('room')
emit('message',{'msg': message['msg']},room=room)
@socketio.on('left', namespace='/ide')
def left(message):
room = session.get('room')
username = session.get('username')
leave_room(room)
session.clear()
@app.route('/compile/',methods = ['POST'])
def compile():
if request.method == 'POST':
#Fetch Parameters
code = request.json['code']
lang = request.json['lang']
custom = request.json['custom']
url = request.json['url']
#To Display Results
status = 'Compiled'
if url!='':
#Generate Files
iptxt,optxt=generate_txt(url)
testcase = iptxt
file = open(optxt,mode='r')
expop = file.read()
file.close()
os.remove(optxt)
else:
testcase = createFile(custom,".txt")
if lang=="python":
fname=createFile(code,".py")
output,err = runFile(['python3',fname],testcase)
elif lang=="javascript":
fname=createFile(code,".js")
output,err = runFile(['node',fname],testcase)
os.remove(fname)
if url!='':
if output==expop:
status = "Passed"
else:
status = "Failed"
return jsonify({'out':output,'err':err,'status':status})
if __name__ == '__main__':
app.run()