File size: 4,023 Bytes
b4960bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests
from bs4 import BeautifulSoup
import os
import logging
import re
from urllib.parse import urlparse, parse_qs

# Set up logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

def download_image(url, card_name, card_type):
    folder = f"downloaded_images/{card_type}"
    if not os.path.exists(folder):
        os.makedirs(folder)
    image_name = f"{card_name}.png"
    image_path = os.path.join(folder, image_name)
    try:
        with requests.get(url, stream=True) as r:
            r.raise_for_status()
            with open(image_path, 'wb') as f:
                for chunk in r.iter_content(chunk_size=8192):
                    f.write(chunk)
        logging.info(f"Image downloaded: {image_path}")
    except requests.RequestException as e:
        logging.error(f"Error downloading {url}: {e}")

def get_image_urls_and_names(url):
    headers = {'User-Agent': 'Mozilla/5.0'}
    images_info = []
    card_type = parse_qs(urlparse(url).query).get('card_type', ['unknown'])[0]  # Extract card type from URL
    try:
        response = requests.get(url, headers=headers)
        response.raise_for_status()
        soup = BeautifulSoup(response.text, 'html.parser')
        
        images = soup.find_all('img')
        for img in images:
            if 'src' in img.attrs:
                src = img['src']
                # Assuming the card name can be extracted from the image src or alt attribute
                card_name_match = re.search(r'/([^/]+?)(-\d+x\d+)?\.\w+$', src)
                if card_name_match:
                    card_name = card_name_match.group(1).replace('b2-', '').replace('-308x420', '').replace('_', ' ')
                    images_info.append((src, card_name, card_type))
                
        logging.info(f"Found {len(images_info)} images for card type {card_type}.")
        return images_info
    except requests.RequestException as e:
        logging.error(f"Request error: {e}")
    except Exception as e:
        logging.error(f"An error occurred: {e}")

urls =  [
        "https://foursouls.com/card-search/?card_type=character", 
        "https://foursouls.com/card-search/page/2/?card_type=character", 
        "https://foursouls.com/card-search/?card_type=eternal", 
        "https://foursouls.com/card-search/page/2/?card_type=eternal", 
        "https://foursouls.com/card-search/?card_type=treasure",
        "https://foursouls.com/card-search/page/2/?card_type=treasure",
        "https://foursouls.com/card-search/page/3/?card_type=treasure",
        "https://foursouls.com/card-search/page/4/?card_type=treasure",
        "https://foursouls.com/card-search/page/5/?card_type=treasure",
        "https://foursouls.com/card-search/page/6/?card_type=treasure",
        "https://foursouls.com/card-search/?card_type=loot",
        "https://foursouls.com/card-search/page/2/?card_type=loot",
        "https://foursouls.com/card-search/page/3/?card_type=loot",
        "https://foursouls.com/card-search/?card_type=monster",
        "https://foursouls.com/card-search/page/2/?card_type=monster",
        "https://foursouls.com/card-search/page/3/?card_type=monster",
        "https://foursouls.com/card-search/page/4/?card_type=monster",
        "https://foursouls.com/card-search/page/5/?card_type=monster",
        "https://foursouls.com/card-search/page/6/?card_type=monster",
        "https://foursouls.com/card-search/page/7/?card_type=monster",
        "https://foursouls.com/card-search/?card_type=bsoul",
        "https://foursouls.com/card-search/?card_type=room",
        "https://foursouls.com/card-search/page/2/?card_type=room"
        ]

all_images_info = []

for url in urls:
    images_info = get_image_urls_and_names(url)
    if images_info:
        all_images_info.extend(images_info)

if all_images_info:
    for image_url, card_name, card_type in all_images_info:
        download_image(image_url, card_name, card_type)
else:
    logging.info("No image URLs found or an error occurred.")