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

added 2 api keys

Browse files
Files changed (1) hide show
  1. utils.py +25 -5
utils.py CHANGED
@@ -19,16 +19,31 @@ def capture_output():
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,
@@ -52,11 +67,16 @@ def _execute_with_onecompiler(code: str, stdin: str, language: str, filename: st
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":
 
19
  finally:
20
  sys.stdout, sys.stderr = old_out, old_err
21
 
 
22
  def _execute_with_onecompiler(code: str, stdin: str, language: str, filename: str) -> Tuple[str, str, str]:
23
+ keys = [os.environ["ONECOMPILER_API_KEY"], os.environ["ONECOMPILER_API_KEY1"]]
24
+ first_key = random.choice(keys)
25
+
26
+ result = _call_onecompiler_api(first_key, code, stdin, language, filename)
27
+
28
+ if _is_quota_or_invalid(result):
29
+ # Fallback: try all keys one-by-one until one works
30
+ for key in keys:
31
+ if key == first_key:
32
+ continue # Already tried this one
33
+ result = _call_onecompiler_api(key, code, stdin, language, filename)
34
+ if not _is_quota_or_invalid(result):
35
+ return result
36
+
37
+ return result # Either success, or last failed response
38
+
39
+
40
+ def _call_onecompiler_api(key: str, code: str, stdin: str, language: str, filename: str) -> Tuple[str, str, str]:
41
  url = "https://onecompiler-apis.p.rapidapi.com/api/v1/run"
42
  headers = {
43
  "Content-Type": "application/json",
44
  "x-rapidapi-host": "onecompiler-apis.p.rapidapi.com",
45
+ "x-rapidapi-key": key
46
  }
 
 
47
  payload = {
48
  "language": language.lower(),
49
  "stdin": stdin,
 
67
  data.get("stderr", "") or "",
68
  data.get("exception", "")
69
  )
 
70
  except Exception as e:
71
  return "", "", str(e)
72
 
73
 
74
+ def _is_quota_or_invalid(result: Tuple[str, str, str]) -> bool:
75
+ _, _, error = result
76
+ if not error:
77
+ return False
78
+ return any(token in error.lower() for token in ["quota", "e002", "e003", "invalid", "exhausted"])
79
+
80
  def execute_code(code: str, stdin: str = "", language: str = "cpp") -> Tuple[str, str, str]:
81
  try:
82
  if language == "Python":