Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,21 +4,18 @@ from selenium.webdriver.chrome.options import Options
|
|
| 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 |
|
| 11 |
def get_hidden_code(url):
|
| 12 |
-
# Setup Selenium headless Chrome
|
| 13 |
chrome_options = Options()
|
| 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 |
-
|
| 20 |
-
chrome_options.add_argument(
|
| 21 |
-
chrome_options.add_argument(
|
| 22 |
|
| 23 |
driver = webdriver.Chrome(options=chrome_options)
|
| 24 |
|
|
@@ -26,44 +23,39 @@ def get_hidden_code(url):
|
|
| 26 |
|
| 27 |
try:
|
| 28 |
driver.get(url)
|
| 29 |
-
time.sleep(3)
|
| 30 |
|
| 31 |
-
# Try clicking
|
| 32 |
try:
|
| 33 |
start_button = driver.find_element(By.XPATH, "//button[contains(text(), 'Start Challenge')]")
|
| 34 |
start_button.click()
|
| 35 |
-
time.sleep(3)
|
| 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 |
-
#
|
| 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])
|
| 57 |
|
| 58 |
driver.quit()
|
| 59 |
-
|
| 60 |
return list(set(token_found)) or ["No hidden code found"]
|
| 61 |
|
| 62 |
except Exception as e:
|
| 63 |
driver.quit()
|
| 64 |
return [f"Error: {str(e)}"]
|
| 65 |
|
| 66 |
-
|
| 67 |
@app.route("/mission", methods=["POST"])
|
| 68 |
def mission():
|
| 69 |
data = request.json
|
|
@@ -75,7 +67,6 @@ def mission():
|
|
| 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)
|
|
@@ -88,7 +79,6 @@ def mission():
|
|
| 88 |
"token_found": tokens
|
| 89 |
})
|
| 90 |
|
| 91 |
-
|
| 92 |
if __name__ == "__main__":
|
| 93 |
import os
|
| 94 |
port = int(os.environ.get("PORT", 7860))
|
|
|
|
| 4 |
from selenium.webdriver.common.by import By
|
| 5 |
from bs4 import BeautifulSoup
|
| 6 |
import time
|
|
|
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
| 9 |
|
| 10 |
def get_hidden_code(url):
|
|
|
|
| 11 |
chrome_options = Options()
|
| 12 |
chrome_options.add_argument("--headless=new")
|
|
|
|
| 13 |
chrome_options.add_argument("--no-sandbox")
|
| 14 |
chrome_options.add_argument("--disable-dev-shm-usage")
|
| 15 |
+
chrome_options.add_argument("--disable-gpu")
|
| 16 |
+
chrome_options.add_argument("--disable-software-rasterizer")
|
| 17 |
+
chrome_options.add_argument("--incognito")
|
| 18 |
+
chrome_options.add_argument("--remote-debugging-port=9222")
|
| 19 |
|
| 20 |
driver = webdriver.Chrome(options=chrome_options)
|
| 21 |
|
|
|
|
| 23 |
|
| 24 |
try:
|
| 25 |
driver.get(url)
|
| 26 |
+
time.sleep(3)
|
| 27 |
|
| 28 |
+
# Try clicking the button
|
| 29 |
try:
|
| 30 |
start_button = driver.find_element(By.XPATH, "//button[contains(text(), 'Start Challenge')]")
|
| 31 |
start_button.click()
|
| 32 |
+
time.sleep(3)
|
| 33 |
except Exception as e:
|
| 34 |
print("⚠️ Could not click Start Challenge button:", e)
|
| 35 |
|
|
|
|
| 36 |
html = driver.page_source
|
| 37 |
soup = BeautifulSoup(html, "html.parser")
|
| 38 |
|
| 39 |
+
# Scrape hidden elements
|
| 40 |
for tag in soup.find_all(True):
|
| 41 |
if tag.has_attr("style") and "display:none" in tag["style"]:
|
| 42 |
token_found.append(tag.get_text(strip=True))
|
| 43 |
|
|
|
|
| 44 |
for hidden in soup.find_all("input", {"type": "hidden"}):
|
| 45 |
if hidden.get("value"):
|
| 46 |
token_found.append(hidden["value"])
|
| 47 |
|
|
|
|
| 48 |
if "hidden code" in html.lower():
|
| 49 |
start = html.lower().find("hidden code")
|
| 50 |
token_found.append(html[start:start+100])
|
| 51 |
|
| 52 |
driver.quit()
|
|
|
|
| 53 |
return list(set(token_found)) or ["No hidden code found"]
|
| 54 |
|
| 55 |
except Exception as e:
|
| 56 |
driver.quit()
|
| 57 |
return [f"Error: {str(e)}"]
|
| 58 |
|
|
|
|
| 59 |
@app.route("/mission", methods=["POST"])
|
| 60 |
def mission():
|
| 61 |
data = request.json
|
|
|
|
| 67 |
|
| 68 |
tokens = get_hidden_code(url)
|
| 69 |
|
|
|
|
| 70 |
print("\n===== Mission Request =====")
|
| 71 |
print("URL received:", url)
|
| 72 |
print("Questions received:", questions)
|
|
|
|
| 79 |
"token_found": tokens
|
| 80 |
})
|
| 81 |
|
|
|
|
| 82 |
if __name__ == "__main__":
|
| 83 |
import os
|
| 84 |
port = int(os.environ.get("PORT", 7860))
|