File size: 1,302 Bytes
24a410c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()