vsj0702 commited on
Commit
8a0322c
·
verified ·
1 Parent(s): 68625cd

replacing jdoodle with onecompiler for java js and c#

Browse files
Files changed (1) hide show
  1. utils.py +30 -38
utils.py CHANGED
@@ -19,59 +19,60 @@ def capture_output():
19
  finally:
20
  sys.stdout, sys.stderr = old_out, old_err
21
 
22
- def _execute_with_jdoodle(code: str, stdin: str, language: str, version_index="0"):
23
- url = "https://api.jdoodle.com/v1/execute"
 
 
 
 
 
 
 
 
24
  payload = {
25
- "clientId": os.environ["JDOODLE_CLIENT_ID"],
26
- "clientSecret": os.environ["JDOODLE_CLIENT_SECRET"],
27
- "script": code,
28
- "stdin": stdin,
29
  "language": language.lower(),
30
- "versionIndex": version_index
 
 
 
 
 
 
31
  }
32
 
33
  try:
34
- response = requests.post(url, json=payload, timeout=10)
35
  data = response.json()
36
- output = data.get("output", "").strip()
37
- error = data.get("error", "")
38
- statusCode = data.get("statusCode")
39
 
40
- if statusCode != 200:
41
- return "", error or output, f"JDoodle Error: {statusCode}"
42
- return output, "", None
 
 
 
 
 
 
43
  except Exception as e:
44
  return "", "", str(e)
45
 
46
 
47
  def execute_code(code: str, stdin: str = "", language: str = "cpp") -> Tuple[str, str, str]:
48
- """
49
- Execute code in the specified language and return (stdout, stderr, exception)
50
-
51
- Currently supports: python (default). Other languages scaffolded.
52
- """
53
  try:
54
  if language == "Python":
55
  return _execute_python(code, stdin)
56
-
57
  elif language == "C":
58
  return _execute_c(code, stdin)
59
-
60
  elif language == "C++":
61
  return _execute_cpp(code, stdin)
62
-
63
  elif language == "Java":
64
- return _execute_java(code, stdin)
65
-
66
  elif language == "JavaScript":
67
- return _execute_javascript(code, stdin)
68
-
69
  elif language == "C#":
70
- return _execute_csharp(code, stdin)
71
-
72
  else:
73
  return "", f"Unsupported language: {language}", None
74
-
75
  except Exception as e:
76
  return "", "", str(e)
77
 
@@ -128,15 +129,6 @@ def _execute_cpp(code: str, stdin: str):
128
  return _run_subprocess([binary_path], stdin)
129
 
130
 
131
- def _execute_java(code: str, stdin: str):
132
- return _execute_with_jdoodle(code, stdin, language="java", version_index="4")
133
-
134
- def _execute_javascript(code: str, stdin: str):
135
- return _execute_with_jdoodle(code, stdin, language="nodejs", version_index="4")
136
-
137
- def _execute_csharp(code: str, stdin: str):
138
- return _execute_with_jdoodle(code, stdin, language="csharp", version_index="4")
139
-
140
  def export_session(code: str, output: str, error: str) -> dict:
141
  return {
142
  "timestamp": datetime.now().isoformat(),
 
19
  finally:
20
  sys.stdout, sys.stderr = old_out, old_err
21
 
22
+
23
+ def _execute_with_onecompiler(code: str, stdin: str, language: str, filename: str) -> Tuple[str, str, str]:
24
+ url = "https://onecompiler-apis.p.rapidapi.com/api/v1/run"
25
+ headers = {
26
+ "Content-Type": "application/json",
27
+ "x-rapidapi-host": "onecompiler-apis.p.rapidapi.com",
28
+ "x-rapidapi-key": os.environ["ONECOMPILER_API_KEY"] # Secure access
29
+ }
30
+
31
+
32
  payload = {
 
 
 
 
33
  "language": language.lower(),
34
+ "stdin": stdin,
35
+ "files": [
36
+ {
37
+ "name": filename,
38
+ "content": code
39
+ }
40
+ ]
41
  }
42
 
43
  try:
44
+ response = requests.post(url, json=payload, headers=headers, timeout=10)
45
  data = response.json()
 
 
 
46
 
47
+ if data.get("status") == "failed":
48
+ return "", "", f"OneCompiler Error: {data.get('error')}"
49
+
50
+ return (
51
+ data.get("stdout", "").strip(),
52
+ data.get("stderr", "") or "",
53
+ data.get("exception", "")
54
+ )
55
+
56
  except Exception as e:
57
  return "", "", str(e)
58
 
59
 
60
  def execute_code(code: str, stdin: str = "", language: str = "cpp") -> Tuple[str, str, str]:
 
 
 
 
 
61
  try:
62
  if language == "Python":
63
  return _execute_python(code, stdin)
 
64
  elif language == "C":
65
  return _execute_c(code, stdin)
 
66
  elif language == "C++":
67
  return _execute_cpp(code, stdin)
 
68
  elif language == "Java":
69
+ return _execute_with_onecompiler(code, stdin, language="java", filename="Main.java")
 
70
  elif language == "JavaScript":
71
+ return _execute_with_onecompiler(code, stdin, language="javascript", filename="script.js")
 
72
  elif language == "C#":
73
+ return _execute_with_onecompiler(code, stdin, language="csharp", filename="Program.cs")
 
74
  else:
75
  return "", f"Unsupported language: {language}", None
 
76
  except Exception as e:
77
  return "", "", str(e)
78
 
 
129
  return _run_subprocess([binary_path], stdin)
130
 
131
 
 
 
 
 
 
 
 
 
 
132
  def export_session(code: str, output: str, error: str) -> dict:
133
  return {
134
  "timestamp": datetime.now().isoformat(),