|
1 | 1 | import requests |
2 | 2 | import re |
| 3 | +import json |
3 | 4 | from pathlib import Path |
4 | 5 |
|
5 | 6 | # Image URLs collected from the Smogon Sprite Project spreadsheet |
|
8 | 9 | # Smogon thread: |
9 | 10 | # https://www.smogon.com/forums/threads/smogon-sprite-project.3647722/ |
10 | 11 | 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/", |
15 | 20 | ] |
16 | 21 |
|
17 | 22 | # Set a User-Agent to prevent Smogon from blocking the request |
18 | 23 | HEADERS = { |
19 | 24 | "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" |
20 | 25 | } |
21 | 26 |
|
| 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 | + |
22 | 48 |
|
23 | 49 | def download_sprites(): |
24 | 50 | script_dir = Path(__file__).resolve().parent |
25 | 51 | download_dir = script_dir / "downloads" |
26 | 52 | download_dir.mkdir(exist_ok=True) |
27 | 53 |
|
| 54 | + # Load and invert the name map |
| 55 | + name_to_id = load_forms_map(script_dir) |
| 56 | + |
28 | 57 | print(f"📁 Directory ready: {download_dir.absolute()}") |
29 | 58 | print(f"🚀 Starting download of {len(urls)} files...") |
30 | 59 |
|
31 | 60 | for url in urls: |
32 | 61 | try: |
| 62 | + filename = None |
| 63 | + |
33 | 64 | # 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 --- |
38 | 85 | 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 |
41 | 101 |
|
42 | 102 | save_path = download_dir / filename |
43 | 103 |
|
|
0 commit comments