Datasets:
Upload main.py
Browse files- Cards File (Normal)/main.py +90 -0
Cards File (Normal)/main.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
from bs4 import BeautifulSoup
|
| 3 |
+
import os
|
| 4 |
+
import logging
|
| 5 |
+
import re
|
| 6 |
+
from urllib.parse import urlparse, parse_qs
|
| 7 |
+
|
| 8 |
+
# Set up logging
|
| 9 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 10 |
+
|
| 11 |
+
def download_image(url, card_name, card_type):
|
| 12 |
+
folder = f"downloaded_images/{card_type}"
|
| 13 |
+
if not os.path.exists(folder):
|
| 14 |
+
os.makedirs(folder)
|
| 15 |
+
image_name = f"{card_name}.png"
|
| 16 |
+
image_path = os.path.join(folder, image_name)
|
| 17 |
+
try:
|
| 18 |
+
with requests.get(url, stream=True) as r:
|
| 19 |
+
r.raise_for_status()
|
| 20 |
+
with open(image_path, 'wb') as f:
|
| 21 |
+
for chunk in r.iter_content(chunk_size=8192):
|
| 22 |
+
f.write(chunk)
|
| 23 |
+
logging.info(f"Image downloaded: {image_path}")
|
| 24 |
+
except requests.RequestException as e:
|
| 25 |
+
logging.error(f"Error downloading {url}: {e}")
|
| 26 |
+
|
| 27 |
+
def get_image_urls_and_names(url):
|
| 28 |
+
headers = {'User-Agent': 'Mozilla/5.0'}
|
| 29 |
+
images_info = []
|
| 30 |
+
card_type = parse_qs(urlparse(url).query).get('card_type', ['unknown'])[0] # Extract card type from URL
|
| 31 |
+
try:
|
| 32 |
+
response = requests.get(url, headers=headers)
|
| 33 |
+
response.raise_for_status()
|
| 34 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 35 |
+
|
| 36 |
+
images = soup.find_all('img')
|
| 37 |
+
for img in images:
|
| 38 |
+
if 'src' in img.attrs:
|
| 39 |
+
src = img['src']
|
| 40 |
+
# Assuming the card name can be extracted from the image src or alt attribute
|
| 41 |
+
card_name_match = re.search(r'/([^/]+?)(-\d+x\d+)?\.\w+$', src)
|
| 42 |
+
if card_name_match:
|
| 43 |
+
card_name = card_name_match.group(1).replace('b2-', '').replace('-308x420', '').replace('_', ' ')
|
| 44 |
+
images_info.append((src, card_name, card_type))
|
| 45 |
+
|
| 46 |
+
logging.info(f"Found {len(images_info)} images for card type {card_type}.")
|
| 47 |
+
return images_info
|
| 48 |
+
except requests.RequestException as e:
|
| 49 |
+
logging.error(f"Request error: {e}")
|
| 50 |
+
except Exception as e:
|
| 51 |
+
logging.error(f"An error occurred: {e}")
|
| 52 |
+
|
| 53 |
+
urls = [
|
| 54 |
+
"https://foursouls.com/card-search/?card_type=character",
|
| 55 |
+
"https://foursouls.com/card-search/page/2/?card_type=character",
|
| 56 |
+
"https://foursouls.com/card-search/?card_type=eternal",
|
| 57 |
+
"https://foursouls.com/card-search/page/2/?card_type=eternal",
|
| 58 |
+
"https://foursouls.com/card-search/?card_type=treasure",
|
| 59 |
+
"https://foursouls.com/card-search/page/2/?card_type=treasure",
|
| 60 |
+
"https://foursouls.com/card-search/page/3/?card_type=treasure",
|
| 61 |
+
"https://foursouls.com/card-search/page/4/?card_type=treasure",
|
| 62 |
+
"https://foursouls.com/card-search/page/5/?card_type=treasure",
|
| 63 |
+
"https://foursouls.com/card-search/page/6/?card_type=treasure",
|
| 64 |
+
"https://foursouls.com/card-search/?card_type=loot",
|
| 65 |
+
"https://foursouls.com/card-search/page/2/?card_type=loot",
|
| 66 |
+
"https://foursouls.com/card-search/page/3/?card_type=loot",
|
| 67 |
+
"https://foursouls.com/card-search/?card_type=monster",
|
| 68 |
+
"https://foursouls.com/card-search/page/2/?card_type=monster",
|
| 69 |
+
"https://foursouls.com/card-search/page/3/?card_type=monster",
|
| 70 |
+
"https://foursouls.com/card-search/page/4/?card_type=monster",
|
| 71 |
+
"https://foursouls.com/card-search/page/5/?card_type=monster",
|
| 72 |
+
"https://foursouls.com/card-search/page/6/?card_type=monster",
|
| 73 |
+
"https://foursouls.com/card-search/page/7/?card_type=monster",
|
| 74 |
+
"https://foursouls.com/card-search/?card_type=bsoul",
|
| 75 |
+
"https://foursouls.com/card-search/?card_type=room",
|
| 76 |
+
"https://foursouls.com/card-search/page/2/?card_type=room"
|
| 77 |
+
]
|
| 78 |
+
|
| 79 |
+
all_images_info = []
|
| 80 |
+
|
| 81 |
+
for url in urls:
|
| 82 |
+
images_info = get_image_urls_and_names(url)
|
| 83 |
+
if images_info:
|
| 84 |
+
all_images_info.extend(images_info)
|
| 85 |
+
|
| 86 |
+
if all_images_info:
|
| 87 |
+
for image_url, card_name, card_type in all_images_info:
|
| 88 |
+
download_image(image_url, card_name, card_type)
|
| 89 |
+
else:
|
| 90 |
+
logging.info("No image URLs found or an error occurred.")
|