Skip to content

Commit 2eff62d

Browse files
committed
add support download animated
1 parent 46a3da8 commit 2eff62d

1 file changed

Lines changed: 70 additions & 10 deletions

File tree

scripts/smogon_download.py

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import requests
22
import re
3+
import json
34
from pathlib import Path
45

56
# Image URLs collected from the Smogon Sprite Project spreadsheet
@@ -8,36 +9,95 @@
89
# Smogon thread:
910
# https://www.smogon.com/forums/threads/smogon-sprite-project.3647722/
1011
urls = [
11-
"https://www.smogon.com/forums/attachments/964-png.603593/",
12-
"https://www.smogon.com/forums/attachments/964s-png.603594/",
13-
# "https://www.smogon.com/forums/attachments/964-png.536964/",
14-
# "https://www.smogon.com/forums/attachments/964s-png.536965/",
12+
"https://play.pokemonshowdown.com/sprites/gen5ani/comfey.gif",
13+
"https://play.pokemonshowdown.com/sprites/gen5ani-back/comfey.gif",
14+
"https://play.pokemonshowdown.com/sprites/gen5ani-shiny/comfey.gif",
15+
"https://play.pokemonshowdown.com/sprites/gen5ani-back-shiny/comfey.gif",
16+
"https://www.smogon.com/forums/attachments/762-gif.369401/",
17+
"https://www.smogon.com/forums/attachments/762b-gif.369402/",
18+
"https://www.smogon.com/forums/attachments/762s-gif.369403/",
19+
"https://www.smogon.com/forums/attachments/762sb-gif.369404/",
1520
]
1621

1722
# Set a User-Agent to prevent Smogon from blocking the request
1823
HEADERS = {
1924
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.31"
2025
}
2126

27+
# Mapping Showdown directories to your filename suffixes
28+
SHOWDOWN_SUFFIX_MAP = {
29+
"gen5ani": "",
30+
"gen5ani-back": "b",
31+
"gen5ani-shiny": "s",
32+
"gen5ani-back-shiny": "sb",
33+
}
34+
35+
36+
def load_forms_map(script_dir):
37+
"""Loads forms.json and inverts it to {name: id} for easy lookup."""
38+
forms_path = script_dir / "forms.json"
39+
if not forms_path.exists():
40+
print("⚠️ Warning: forms.json not found. Showdown downloads may fail.")
41+
return {}
42+
43+
with open(forms_path, "r") as f:
44+
data = json.load(f)
45+
# Invert {"764": "comfey"} -> {"comfey": "764"}
46+
return {v.lower(): k for k, v in data.items()}
47+
2248

2349
def download_sprites():
2450
script_dir = Path(__file__).resolve().parent
2551
download_dir = script_dir / "downloads"
2652
download_dir.mkdir(exist_ok=True)
2753

54+
# Load and invert the name map
55+
name_to_id = load_forms_map(script_dir)
56+
2857
print(f"📁 Directory ready: {download_dir.absolute()}")
2958
print(f"🚀 Starting download of {len(urls)} files...")
3059

3160
for url in urls:
3261
try:
62+
filename = None
63+
3364
# 1. Extract filename from URL
34-
# Example: 652sb-png.492504/ -> 652sb.png
35-
match = re.search(r"attachments/([\w-]+)-png\.\d+/?", url)
36-
if match:
37-
filename = f"{match.group(1)}.png"
65+
# --- Pokemon Showdown ---
66+
if "play.pokemonshowdown.com" in url:
67+
# Extract the directory and the name (e.g., gen5ani-back and comfey)
68+
# URL pattern: .../sprites/{directory}/{name}.gif
69+
parts = url.rstrip("/").split("/")
70+
directory = parts[-2]
71+
name_with_ext = parts[-1]
72+
name = name_with_ext.split(".")[0].lower()
73+
extension = name_with_ext.split(".")[-1]
74+
75+
pokemon_id = name_to_id.get(name)
76+
suffix = SHOWDOWN_SUFFIX_MAP.get(directory)
77+
78+
if pokemon_id is not None and suffix is not None:
79+
filename = f"{pokemon_id}{suffix}.{extension}"
80+
else:
81+
print(f"⚠️ Could not map Showdown URL: {url}")
82+
continue
83+
84+
# --- Smogon Forums ---
3885
else:
39-
# Fallback if regex fails
40-
filename = f"{Path(url).parts[-1].split('-')[0]}.png"
86+
# Capture the name and the extension type (e.g., 762-gif.369401 -> 762 and gif)
87+
match = re.search(r"attachments/([\w-]+)-(png|gif)\.\d+/?", url)
88+
if match:
89+
base_name = match.group(1)
90+
extension = match.group(2)
91+
filename = f"{base_name}.{extension}"
92+
else:
93+
# Handles cases where the regex might miss a specific format
94+
raw_part = Path(url).parts[-1].split(".")[0] # e.g., "762-gif"
95+
if "-" in raw_part:
96+
name, ext = raw_part.rsplit("-", 1)
97+
filename = f"{name}.{ext}"
98+
else:
99+
print(f"⚠️ Could not parse Smogon URL: {url}")
100+
continue
41101

42102
save_path = download_dir / filename
43103

0 commit comments

Comments
 (0)