| import os | |
| from concurrent.futures import ThreadPoolExecutor | |
| import requests | |
| OUTPUT_DIR = "htmls" | |
| MAX_WORKERS = 10 | |
| def download_url(url, full_path): | |
| try: | |
| response = requests.get(url) | |
| response.raise_for_status() | |
| with open(full_path, 'wb') as f: | |
| f.write(response.content) | |
| except Exception as e: | |
| print(f"Failed to download {url}: {e}") | |
| def main(): | |
| if not os.path.isdir(OUTPUT_DIR): | |
| os.mkdir(OUTPUT_DIR) | |
| for grid_size in ['4x7', '4x6', '4x5', '4x4', '3x5', '3x4']: | |
| for difficulty in ['challenging', 'moderate', 'easy']: | |
| file_prefix = f"htmls/{difficulty}{grid_size}" | |
| if not os.path.isdir(file_prefix): | |
| os.mkdir(file_prefix) | |
| print(f"PROCESSING: {file_prefix}") | |
| with open(f'urls/{difficulty}{grid_size}.txt') as urls: | |
| with ThreadPoolExecutor(max_workers=MAX_WORKERS) as executor: | |
| for uu in urls: | |
| executor.submit(process_one_chunk, file_prefix, uu.strip()) | |
| def process_one_chunk(file_prefix, url): | |
| url_suffix = url.removeprefix("https://logic.puzzlebaron.com/") | |
| file_path = os.path.join(file_prefix, url_suffix) | |
| if not os.path.exists(file_path): | |
| download_url(url, file_path) | |
| main() | |