maze-generator / maze_generator.py
utarn's picture
Initial commit of maze generator app
f48fe4d
raw
history blame
5.57 kB
#!/usr/bin/env python3
import requests
import re
from urllib.parse import urljoin
import os
def get_maze_generator_page():
"""Get the maze generator page to extract form data and session info"""
url = "https://www.mazegenerator.net/"
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
response = requests.get(url, headers=headers)
return response.text, response.cookies
def extract_form_data(html_content):
"""Extract form data and hidden fields from the page"""
# Look for form action URL
form_action_match = re.search(r'<form[^>]*action=["\']([^"\']*)["\']', html_content, re.IGNORECASE)
form_action = form_action_match.group(1) if form_action_match else ""
# Look for hidden inputs
hidden_inputs = re.findall(r'<input[^>]*type=["\']hidden["\'][^>]*name=["\']([^"\']*)["\'][^>]*value=["\']([^"\']*)["\']', html_content, re.IGNORECASE)
form_data = dict(hidden_inputs)
# Look for viewstate and other common ASP.NET fields
viewstate_match = re.search(r'name=["\']__VIEWSTATE["\'][^>]*value=["\']([^"\']*)["\']', html_content)
if viewstate_match:
form_data['__VIEWSTATE'] = viewstate_match.group(1)
viewstategen_match = re.search(r'name=["\']__VIEWSTATEGENERATOR["\'][^>]*value=["\']([^"\']*)["\']', html_content)
if viewstategen_match:
form_data['__VIEWSTATEGENERATOR'] = viewstategen_match.group(1)
eventvalidation_match = re.search(r'name=["\']__EVENTVALIDATION["\'][^>]*value=["\']([^"\']*)["\']', html_content)
if eventvalidation_match:
form_data['__EVENTVALIDATION'] = eventvalidation_match.group(1)
return form_action, form_data
def generate_maze(width=25, height=35):
"""Generate a maze with specified dimensions"""
# Get the initial page
html_content, cookies = get_maze_generator_page()
# Extract form data
form_action, form_data = extract_form_data(html_content)
# Add maze parameters with correct field names from the HTML
form_data.update({
'ShapeDropDownList': '1', # Rectangular
'S1TesselationDropDownList': '1', # Orthogonal (Square cells)
'S1WidthTextBox': str(width),
'S1HeightTextBox': str(height),
'S1InnerWidthTextBox': '0',
'S1InnerHeightTextBox': '0',
'S1StartsAtDropDownList': '1', # Top
'AlgorithmParameter1TextBox': '50', # E parameter
'AlgorithmParameter2TextBox': '100', # R parameter
'GenerateButton': 'Generate'
})
# Make the request to generate the maze
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
'Content-Type': 'application/x-www-form-urlencoded',
'Referer': 'https://www.mazegenerator.net/'
}
# Determine the full URL for the form submission
if form_action:
submit_url = urljoin('https://www.mazegenerator.net/', form_action)
else:
submit_url = 'https://www.mazegenerator.net/'
response = requests.post(submit_url, data=form_data, cookies=cookies, headers=headers)
return response.text, response.cookies
def download_pdf(html_content, cookies, filename="maze.pdf"):
"""Download the maze as PDF by submitting the download form"""
# Extract form data for the download request
form_action, form_data = extract_form_data(html_content)
# Add download parameters
form_data.update({
'FileFormatSelectorList': '1', # PDF (A4 size)
'DownloadFileButton': 'Download'
})
# Make the request to download the PDF
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
'Content-Type': 'application/x-www-form-urlencoded',
'Referer': 'https://www.mazegenerator.net/'
}
# Determine the full URL for the form submission
if form_action:
submit_url = urljoin('https://www.mazegenerator.net/', form_action)
else:
submit_url = 'https://www.mazegenerator.net/'
response = requests.post(submit_url, data=form_data, cookies=cookies, headers=headers)
# Check if the response is a PDF file
content_type = response.headers.get('content-type', '').lower()
if 'pdf' in content_type:
with open(filename, 'wb') as f:
f.write(response.content)
print(f"PDF downloaded successfully as {filename}")
return True
else:
print(f"Response is not a PDF. Content-Type: {content_type}")
# Save the response for debugging
with open("download_response.html", "w", encoding="utf-8") as f:
f.write(response.text)
print("Download response saved to download_response.html for inspection")
return False
if __name__ == "__main__":
print("Generating maze with width=25, height=35...")
# Generate the maze
maze_html, maze_cookies = generate_maze(25, 35)
# Try to download PDF
success = download_pdf(maze_html, maze_cookies, "maze_25x35.pdf")
if not success:
print("Let's try to find alternative download methods...")
# Save the HTML response for inspection
with open("maze_response.html", "w", encoding="utf-8") as f:
f.write(maze_html)
print("Response saved to maze_response.html for inspection")