Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ from selenium.webdriver.chrome.options import Options
|
|
| 4 |
from selenium.webdriver.common.by import By
|
| 5 |
from bs4 import BeautifulSoup
|
| 6 |
import time
|
|
|
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
| 9 |
|
|
@@ -13,6 +14,12 @@ def get_hidden_code(url):
|
|
| 13 |
chrome_options.add_argument("--headless=new")
|
| 14 |
chrome_options.add_argument("--disable-gpu")
|
| 15 |
chrome_options.add_argument("--no-sandbox")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
driver = webdriver.Chrome(options=chrome_options)
|
| 17 |
|
| 18 |
token_found = []
|
|
@@ -21,7 +28,7 @@ def get_hidden_code(url):
|
|
| 21 |
driver.get(url)
|
| 22 |
time.sleep(3) # wait for page load
|
| 23 |
|
| 24 |
-
#
|
| 25 |
try:
|
| 26 |
start_button = driver.find_element(By.XPATH, "//button[contains(text(), 'Start Challenge')]")
|
| 27 |
start_button.click()
|
|
@@ -29,21 +36,21 @@ def get_hidden_code(url):
|
|
| 29 |
except Exception as e:
|
| 30 |
print("⚠️ Could not click Start Challenge button:", e)
|
| 31 |
|
| 32 |
-
# Get updated HTML after
|
| 33 |
html = driver.page_source
|
| 34 |
soup = BeautifulSoup(html, "html.parser")
|
| 35 |
|
| 36 |
-
#
|
| 37 |
for tag in soup.find_all(True):
|
| 38 |
if tag.has_attr("style") and "display:none" in tag["style"]:
|
| 39 |
token_found.append(tag.get_text(strip=True))
|
| 40 |
|
| 41 |
-
#
|
| 42 |
for hidden in soup.find_all("input", {"type": "hidden"}):
|
| 43 |
if hidden.get("value"):
|
| 44 |
token_found.append(hidden["value"])
|
| 45 |
|
| 46 |
-
#
|
| 47 |
if "hidden code" in html.lower():
|
| 48 |
start = html.lower().find("hidden code")
|
| 49 |
token_found.append(html[start:start+100])
|
|
@@ -68,6 +75,13 @@ def mission():
|
|
| 68 |
|
| 69 |
tokens = get_hidden_code(url)
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
return jsonify({
|
| 72 |
"url_received": url,
|
| 73 |
"questions_received": questions,
|
|
@@ -77,6 +91,5 @@ def mission():
|
|
| 77 |
|
| 78 |
if __name__ == "__main__":
|
| 79 |
import os
|
| 80 |
-
port = int(os.environ.get("PORT", 7860))
|
| 81 |
app.run(host="0.0.0.0", port=port, debug=False)
|
| 82 |
-
|
|
|
|
| 4 |
from selenium.webdriver.common.by import By
|
| 5 |
from bs4 import BeautifulSoup
|
| 6 |
import time
|
| 7 |
+
import tempfile
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
| 10 |
|
|
|
|
| 14 |
chrome_options.add_argument("--headless=new")
|
| 15 |
chrome_options.add_argument("--disable-gpu")
|
| 16 |
chrome_options.add_argument("--no-sandbox")
|
| 17 |
+
chrome_options.add_argument("--disable-dev-shm-usage")
|
| 18 |
+
|
| 19 |
+
# Force Selenium to use /tmp (writable in Hugging Face)
|
| 20 |
+
chrome_options.add_argument(f"--user-data-dir={tempfile.mkdtemp()}")
|
| 21 |
+
chrome_options.add_argument(f"--disk-cache-dir={tempfile.mkdtemp()}")
|
| 22 |
+
|
| 23 |
driver = webdriver.Chrome(options=chrome_options)
|
| 24 |
|
| 25 |
token_found = []
|
|
|
|
| 28 |
driver.get(url)
|
| 29 |
time.sleep(3) # wait for page load
|
| 30 |
|
| 31 |
+
# Try clicking Start Challenge button
|
| 32 |
try:
|
| 33 |
start_button = driver.find_element(By.XPATH, "//button[contains(text(), 'Start Challenge')]")
|
| 34 |
start_button.click()
|
|
|
|
| 36 |
except Exception as e:
|
| 37 |
print("⚠️ Could not click Start Challenge button:", e)
|
| 38 |
|
| 39 |
+
# Get updated HTML after click
|
| 40 |
html = driver.page_source
|
| 41 |
soup = BeautifulSoup(html, "html.parser")
|
| 42 |
|
| 43 |
+
# Find hidden elements
|
| 44 |
for tag in soup.find_all(True):
|
| 45 |
if tag.has_attr("style") and "display:none" in tag["style"]:
|
| 46 |
token_found.append(tag.get_text(strip=True))
|
| 47 |
|
| 48 |
+
# Find hidden input fields
|
| 49 |
for hidden in soup.find_all("input", {"type": "hidden"}):
|
| 50 |
if hidden.get("value"):
|
| 51 |
token_found.append(hidden["value"])
|
| 52 |
|
| 53 |
+
# Look for "Hidden Code" text
|
| 54 |
if "hidden code" in html.lower():
|
| 55 |
start = html.lower().find("hidden code")
|
| 56 |
token_found.append(html[start:start+100])
|
|
|
|
| 75 |
|
| 76 |
tokens = get_hidden_code(url)
|
| 77 |
|
| 78 |
+
# Print in logs for debugging
|
| 79 |
+
print("\n===== Mission Request =====")
|
| 80 |
+
print("URL received:", url)
|
| 81 |
+
print("Questions received:", questions)
|
| 82 |
+
print("Token(s) found:", tokens)
|
| 83 |
+
print("===========================\n")
|
| 84 |
+
|
| 85 |
return jsonify({
|
| 86 |
"url_received": url,
|
| 87 |
"questions_received": questions,
|
|
|
|
| 91 |
|
| 92 |
if __name__ == "__main__":
|
| 93 |
import os
|
| 94 |
+
port = int(os.environ.get("PORT", 7860))
|
| 95 |
app.run(host="0.0.0.0", port=port, debug=False)
|
|
|