Skip to content

Instantly share code, notes, and snippets.

@MartinMReed
Last active November 19, 2024 14:43
Show Gist options
  • Save MartinMReed/ae81f1aea0d5a9ce363658cf675f0ba3 to your computer and use it in GitHub Desktop.
Save MartinMReed/ae81f1aea0d5a9ce363658cf675f0ba3 to your computer and use it in GitHub Desktop.
Maze Generator Scraper
"""
Scrape mazes generated by https://mazegenerator.net and save to pdf
"""
import os
import re
import time
import traceback
from math import floor
import requests
maze_no = 1
level_min = 8
level_max = 32
used_rec_spec = []
def execute():
cookie = None
initial_load_r = requests.get('https://mazegenerator.net')
for shape in [1, 2, 3, 4]:
shape_load_r = requests.post(
url='https://mazegenerator.net',
headers={
'Cookie': cookie,
},
data={
'__EVENTTARGET': 'ShapeDropDownList',
'__EVENTARGUMENT': scrape_input('__EVENTARGUMENT', initial_load_r),
'__LASTFOCUS': scrape_input('__LASTFOCUS', initial_load_r),
'__VIEWSTATE': scrape_input('__VIEWSTATE', initial_load_r),
'__VIEWSTATEGENERATOR': scrape_input('__VIEWSTATEGENERATOR', initial_load_r),
'__EVENTVALIDATION': scrape_input('__EVENTVALIDATION', initial_load_r),
'ShapeDropDownList': shape,
}
)
shape_load_r.raise_for_status()
if cookie is None and 'Set-Cookie' in shape_load_r.headers:
cookie = shape_load_r.headers['Set-Cookie'].split(';')[0].strip()
for size in range(level_min, level_max + 1, 1):
iter_shape_params = iter_shape_params_1
if shape == 2: iter_shape_params = iter_shape_params_2
if shape == 3: iter_shape_params = iter_shape_params_3
if shape == 4: iter_shape_params = iter_shape_params_4
for filename, shape_params in iter_shape_params(size):
form_post_r = requests.post(
url='https://mazegenerator.net',
headers={
'Cookie': cookie,
},
data={
'__EVENTTARGET': scrape_input('__EVENTTARGET', shape_load_r),
'__EVENTARGUMENT': scrape_input('__EVENTARGUMENT', shape_load_r),
'__LASTFOCUS': scrape_input('__LASTFOCUS', shape_load_r),
'__VIEWSTATE': scrape_input('__VIEWSTATE', shape_load_r),
'__VIEWSTATEGENERATOR': scrape_input('__VIEWSTATEGENERATOR', shape_load_r),
'__EVENTVALIDATION': scrape_input('__EVENTVALIDATION', shape_load_r),
'ShapeDropDownList': shape,
**shape_params,
'AlgorithmParameter1TextBox': 50,
'AlgorithmParameter2TextBox': 100,
'GenerateButton': 'Generate',
}
)
form_post_r.raise_for_status()
if cookie is None and 'Set-Cookie' in form_post_r.headers:
cookie = form_post_r.headers['Set-Cookie'].split(';')[0].strip()
pdf_get_r = requests.get(
url='https://mazegenerator.net/PdfGenerator.ashx',
params={
'Tag': re.search(rf'ImageGenerator.ashx\?Tag=(\d+)&', form_post_r.text).group(1),
'MazeType': 1,
'PageSize': 10,
},
headers={
'Cookie': cookie,
}
)
pdf_get_r.raise_for_status()
os.makedirs('mazes/', exist_ok=True)
with open(f'mazes/{filename}', 'wb') as f:
f.write(pdf_get_r.content)
print(filename)
time.sleep(1)
def iter_shape_params_1(size):
global used_rec_spec
for wx, hx in [(0.75, 1.00), (1.00, 0.75), (1.00, 1.00)]:
width = min(max(floor(size * wx), 8), 200)
height = min(max(floor(size * hx), 8), 200)
if (width, height) in used_rec_spec: continue
if wx != hx and abs(width - height) < 4:
continue
used_rec_spec.append((width, height))
for tesselation in [1, 2, 3]:
yield f'maze-{next_maze_no():04}-1-{width:03}x{height:03}-{tesselation}.pdf', {
'S1TesselationDropDownList': tesselation,
'S1WidthTextBox': width,
'S1HeightTextBox': height,
'S1InnerWidthTextBox': 0,
'S1InnerHeightTextBox': 0,
'S1StartsAtDropDownList': 1,
}
def iter_shape_params_2(size):
yield f'maze-{next_maze_no():04}-2-{size:03}.pdf', {
'S2OuterDiameterTextBox': size,
'S2InnerDiameterTextBox': 3,
'S2HorizontalBiasCheckBox': 'on',
'S2StartsAtDropDownList': 1,
}
def iter_shape_params_3(size):
yield f'maze-{next_maze_no():04}-3-{size:03}.pdf', {
'S3SideLengthTextBox': size,
'S3InnerSideLengthTextBox': 0,
'S3StartsAtDropDownList': 1,
}
def iter_shape_params_4(size):
for tesselation in [2, 3]:
yield f'maze-{next_maze_no():04}-4-{size:03}-{tesselation}.pdf', {
'S4TesselationDropDownList': tesselation,
'S4SideLengthTextBox': size,
'S4InnerSideLengthTextBox': 0,
'S4StartsAtDropDownList': 1,
}
def scrape_input(id, response):
match = re.search(rf'id="{id}" value="([^"]*)"', response.text)
return match.group(1) if match else ""
def next_maze_no():
global maze_no
try:
return maze_no
finally:
maze_no += 1
if __name__ == '__main__':
try:
execute()
except Exception as e:
traceback.print_exc()
raise
# OUTPUTS:
# maze-0001-1-008x008-1.pdf
# maze-0002-1-008x008-2.pdf
# maze-0003-1-008x008-3.pdf
# maze-0004-1-009x009-1.pdf
# maze-0005-1-009x009-2.pdf
# maze-0006-1-009x009-3.pdf
# maze-0007-1-010x010-1.pdf
# maze-0008-1-010x010-2.pdf
# maze-0009-1-010x010-3.pdf
# maze-0010-1-011x011-1.pdf
# maze-0011-1-011x011-2.pdf
# maze-0012-1-011x011-3.pdf
# maze-0013-1-012x012-1.pdf
# maze-0014-1-012x012-2.pdf
# maze-0015-1-012x012-3.pdf
# maze-0016-1-009x013-1.pdf
# maze-0017-1-009x013-2.pdf
# maze-0018-1-009x013-3.pdf
# maze-0019-1-013x009-1.pdf
# maze-0020-1-013x009-2.pdf
# maze-0021-1-013x009-3.pdf
# maze-0022-1-013x013-1.pdf
# maze-0023-1-013x013-2.pdf
# maze-0024-1-013x013-3.pdf
# maze-0025-1-010x014-1.pdf
# maze-0026-1-010x014-2.pdf
# maze-0027-1-010x014-3.pdf
# maze-0028-1-014x010-1.pdf
# maze-0029-1-014x010-2.pdf
# maze-0030-1-014x010-3.pdf
# maze-0031-1-014x014-1.pdf
# maze-0032-1-014x014-2.pdf
# maze-0033-1-014x014-3.pdf
# maze-0034-1-011x015-1.pdf
# maze-0035-1-011x015-2.pdf
# maze-0036-1-011x015-3.pdf
# maze-0037-1-015x011-1.pdf
# maze-0038-1-015x011-2.pdf
# maze-0039-1-015x011-3.pdf
# maze-0040-1-015x015-1.pdf
# maze-0041-1-015x015-2.pdf
# maze-0042-1-015x015-3.pdf
# maze-0043-1-012x016-1.pdf
# maze-0044-1-012x016-2.pdf
# maze-0045-1-012x016-3.pdf
# maze-0046-1-016x012-1.pdf
# maze-0047-1-016x012-2.pdf
# maze-0048-1-016x012-3.pdf
# maze-0049-1-016x016-1.pdf
# maze-0050-1-016x016-2.pdf
# maze-0051-1-016x016-3.pdf
# maze-0052-1-012x017-1.pdf
# maze-0053-1-012x017-2.pdf
# maze-0054-1-012x017-3.pdf
# maze-0055-1-017x012-1.pdf
# maze-0056-1-017x012-2.pdf
# maze-0057-1-017x012-3.pdf
# maze-0058-1-017x017-1.pdf
# maze-0059-1-017x017-2.pdf
# maze-0060-1-017x017-3.pdf
# maze-0061-1-013x018-1.pdf
# maze-0062-1-013x018-2.pdf
# maze-0063-1-013x018-3.pdf
# maze-0064-1-018x013-1.pdf
# maze-0065-1-018x013-2.pdf
# maze-0066-1-018x013-3.pdf
# maze-0067-1-018x018-1.pdf
# maze-0068-1-018x018-2.pdf
# maze-0069-1-018x018-3.pdf
# maze-0070-1-014x019-1.pdf
# maze-0071-1-014x019-2.pdf
# maze-0072-1-014x019-3.pdf
# maze-0073-1-019x014-1.pdf
# maze-0074-1-019x014-2.pdf
# maze-0075-1-019x014-3.pdf
# maze-0076-1-019x019-1.pdf
# maze-0077-1-019x019-2.pdf
# maze-0078-1-019x019-3.pdf
# maze-0079-1-015x020-1.pdf
# maze-0080-1-015x020-2.pdf
# maze-0081-1-015x020-3.pdf
# maze-0082-1-020x015-1.pdf
# maze-0083-1-020x015-2.pdf
# maze-0084-1-020x015-3.pdf
# maze-0085-1-020x020-1.pdf
# maze-0086-1-020x020-2.pdf
# maze-0087-1-020x020-3.pdf
# maze-0088-1-015x021-1.pdf
# maze-0089-1-015x021-2.pdf
# maze-0090-1-015x021-3.pdf
# maze-0091-1-021x015-1.pdf
# maze-0092-1-021x015-2.pdf
# maze-0093-1-021x015-3.pdf
# maze-0094-1-021x021-1.pdf
# maze-0095-1-021x021-2.pdf
# maze-0096-1-021x021-3.pdf
# maze-0097-1-016x022-1.pdf
# maze-0098-1-016x022-2.pdf
# maze-0099-1-016x022-3.pdf
# maze-0100-1-022x016-1.pdf
# maze-0101-1-022x016-2.pdf
# maze-0102-1-022x016-3.pdf
# maze-0103-1-022x022-1.pdf
# maze-0104-1-022x022-2.pdf
# maze-0105-1-022x022-3.pdf
# maze-0106-1-017x023-1.pdf
# maze-0107-1-017x023-2.pdf
# maze-0108-1-017x023-3.pdf
# maze-0109-1-023x017-1.pdf
# maze-0110-1-023x017-2.pdf
# maze-0111-1-023x017-3.pdf
# maze-0112-1-023x023-1.pdf
# maze-0113-1-023x023-2.pdf
# maze-0114-1-023x023-3.pdf
# maze-0115-1-018x024-1.pdf
# maze-0116-1-018x024-2.pdf
# maze-0117-1-018x024-3.pdf
# maze-0118-1-024x018-1.pdf
# maze-0119-1-024x018-2.pdf
# maze-0120-1-024x018-3.pdf
# maze-0121-1-024x024-1.pdf
# maze-0122-1-024x024-2.pdf
# maze-0123-1-024x024-3.pdf
# maze-0124-1-018x025-1.pdf
# maze-0125-1-018x025-2.pdf
# maze-0126-1-018x025-3.pdf
# maze-0127-1-025x018-1.pdf
# maze-0128-1-025x018-2.pdf
# maze-0129-1-025x018-3.pdf
# maze-0130-1-025x025-1.pdf
# maze-0131-1-025x025-2.pdf
# maze-0132-1-025x025-3.pdf
# maze-0133-1-019x026-1.pdf
# maze-0134-1-019x026-2.pdf
# maze-0135-1-019x026-3.pdf
# maze-0136-1-026x019-1.pdf
# maze-0137-1-026x019-2.pdf
# maze-0138-1-026x019-3.pdf
# maze-0139-1-026x026-1.pdf
# maze-0140-1-026x026-2.pdf
# maze-0141-1-026x026-3.pdf
# maze-0142-1-020x027-1.pdf
# maze-0143-1-020x027-2.pdf
# maze-0144-1-020x027-3.pdf
# maze-0145-1-027x020-1.pdf
# maze-0146-1-027x020-2.pdf
# maze-0147-1-027x020-3.pdf
# maze-0148-1-027x027-1.pdf
# maze-0149-1-027x027-2.pdf
# maze-0150-1-027x027-3.pdf
# maze-0151-1-021x028-1.pdf
# maze-0152-1-021x028-2.pdf
# maze-0153-1-021x028-3.pdf
# maze-0154-1-028x021-1.pdf
# maze-0155-1-028x021-2.pdf
# maze-0156-1-028x021-3.pdf
# maze-0157-1-028x028-1.pdf
# maze-0158-1-028x028-2.pdf
# maze-0159-1-028x028-3.pdf
# maze-0160-1-021x029-1.pdf
# maze-0161-1-021x029-2.pdf
# maze-0162-1-021x029-3.pdf
# maze-0163-1-029x021-1.pdf
# maze-0164-1-029x021-2.pdf
# maze-0165-1-029x021-3.pdf
# maze-0166-1-029x029-1.pdf
# maze-0167-1-029x029-2.pdf
# maze-0168-1-029x029-3.pdf
# maze-0169-1-022x030-1.pdf
# maze-0170-1-022x030-2.pdf
# maze-0171-1-022x030-3.pdf
# maze-0172-1-030x022-1.pdf
# maze-0173-1-030x022-2.pdf
# maze-0174-1-030x022-3.pdf
# maze-0175-1-030x030-1.pdf
# maze-0176-1-030x030-2.pdf
# maze-0177-1-030x030-3.pdf
# maze-0178-1-023x031-1.pdf
# maze-0179-1-023x031-2.pdf
# maze-0180-1-023x031-3.pdf
# maze-0181-1-031x023-1.pdf
# maze-0182-1-031x023-2.pdf
# maze-0183-1-031x023-3.pdf
# maze-0184-1-031x031-1.pdf
# maze-0185-1-031x031-2.pdf
# maze-0186-1-031x031-3.pdf
# maze-0187-1-024x032-1.pdf
# maze-0188-1-024x032-2.pdf
# maze-0189-1-024x032-3.pdf
# maze-0190-1-032x024-1.pdf
# maze-0191-1-032x024-2.pdf
# maze-0192-1-032x024-3.pdf
# maze-0193-1-032x032-1.pdf
# maze-0194-1-032x032-2.pdf
# maze-0195-1-032x032-3.pdf
# maze-0196-2-008.pdf
# maze-0197-2-009.pdf
# maze-0198-2-010.pdf
# maze-0199-2-011.pdf
# maze-0200-2-012.pdf
# maze-0201-2-013.pdf
# maze-0202-2-014.pdf
# maze-0203-2-015.pdf
# maze-0204-2-016.pdf
# maze-0205-2-017.pdf
# maze-0206-2-018.pdf
# maze-0207-2-019.pdf
# maze-0208-2-020.pdf
# maze-0209-2-021.pdf
# maze-0210-2-022.pdf
# maze-0211-2-023.pdf
# maze-0212-2-024.pdf
# maze-0213-2-025.pdf
# maze-0214-2-026.pdf
# maze-0215-2-027.pdf
# maze-0216-2-028.pdf
# maze-0217-2-029.pdf
# maze-0218-2-030.pdf
# maze-0219-2-031.pdf
# maze-0220-2-032.pdf
# maze-0221-3-008.pdf
# maze-0222-3-009.pdf
# maze-0223-3-010.pdf
# maze-0224-3-011.pdf
# maze-0225-3-012.pdf
# maze-0226-3-013.pdf
# maze-0227-3-014.pdf
# maze-0228-3-015.pdf
# maze-0229-3-016.pdf
# maze-0230-3-017.pdf
# maze-0231-3-018.pdf
# maze-0232-3-019.pdf
# maze-0233-3-020.pdf
# maze-0234-3-021.pdf
# maze-0235-3-022.pdf
# maze-0236-3-023.pdf
# maze-0237-3-024.pdf
# maze-0238-3-025.pdf
# maze-0239-3-026.pdf
# maze-0240-3-027.pdf
# maze-0241-3-028.pdf
# maze-0242-3-029.pdf
# maze-0243-3-030.pdf
# maze-0244-3-031.pdf
# maze-0245-3-032.pdf
# maze-0246-4-008-2.pdf
# maze-0247-4-008-3.pdf
# maze-0248-4-009-2.pdf
# maze-0249-4-009-3.pdf
# maze-0250-4-010-2.pdf
# maze-0251-4-010-3.pdf
# maze-0252-4-011-2.pdf
# maze-0253-4-011-3.pdf
# maze-0254-4-012-2.pdf
# maze-0255-4-012-3.pdf
# maze-0256-4-013-2.pdf
# maze-0257-4-013-3.pdf
# maze-0258-4-014-2.pdf
# maze-0259-4-014-3.pdf
# maze-0260-4-015-2.pdf
# maze-0261-4-015-3.pdf
# maze-0262-4-016-2.pdf
# maze-0263-4-016-3.pdf
# maze-0264-4-017-2.pdf
# maze-0265-4-017-3.pdf
# maze-0266-4-018-2.pdf
# maze-0267-4-018-3.pdf
# maze-0268-4-019-2.pdf
# maze-0269-4-019-3.pdf
# maze-0270-4-020-2.pdf
# maze-0271-4-020-3.pdf
# maze-0272-4-021-2.pdf
# maze-0273-4-021-3.pdf
# maze-0274-4-022-2.pdf
# maze-0275-4-022-3.pdf
# maze-0276-4-023-2.pdf
# maze-0277-4-023-3.pdf
# maze-0278-4-024-2.pdf
# maze-0279-4-024-3.pdf
# maze-0280-4-025-2.pdf
# maze-0281-4-025-3.pdf
# maze-0282-4-026-2.pdf
# maze-0283-4-026-3.pdf
# maze-0284-4-027-2.pdf
# maze-0285-4-027-3.pdf
# maze-0286-4-028-2.pdf
# maze-0287-4-028-3.pdf
# maze-0288-4-029-2.pdf
# maze-0289-4-029-3.pdf
# maze-0290-4-030-2.pdf
# maze-0291-4-030-3.pdf
# maze-0292-4-031-2.pdf
# maze-0293-4-031-3.pdf
# maze-0294-4-032-2.pdf
# maze-0295-4-032-3.pdf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment