Spaces:
Sleeping
Sleeping
File size: 5,570 Bytes
f48fe4d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
#!/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") |