-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSpriteDownloader.py
69 lines (57 loc) · 3.11 KB
/
SpriteDownloader.py
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
import logging
import requests
import os
import traceback
from pathlib import Path
from PIL import Image
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException, TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
#This script is for downloading missing Pokemon sprite files from Showdown for use by Babiri
class SpriteDownloader:
base_download_location = os.environ['spriteFolderLocation'] # set 'spriteFolderLocation' in environment variables to the sprites folder of the Babiri front end
sprite_folder_url = "https://play.pokemonshowdown.com/sprites/gen5/"
def __init__(self):
logging.basicConfig(level=logging.INFO, format="%(asctime)s | %(levelname)s | %(message)s", datefmt='%Y-%m-%d %H:%M:%S')
self.driver_path = "S:\Program Files\ChromeDriver\chromedriver.exe"
self.options = webdriver.ChromeOptions()
self.options.add_argument("--headless")
def get_missing_sprites(self):
driver = webdriver.Chrome(options=self.options, service=Service(ChromeDriverManager().install()))
logging.info("Loading page...")
driver.get(self.sprite_folder_url)
# get all <a> where the href is the download url for each sprite
# if that sprite is not in download location
# then use requests to download sprite to download location
try:
png_download_links = WebDriverWait(driver, 10).until(
EC.presence_of_all_elements_located((By.XPATH, "//a[contains(@href, 'png')]"))
)
logging.info(f"Number of download links: {len(png_download_links)}")
for png_link in png_download_links:
download_location = self.base_download_location + f"{png_link.text}"
path = Path(download_location)
if not path.is_file() and "totem" not in png_link.text and "vivillon" not in png_link.text and "furfrou" not in png_link.text and "gmax" not in png_link.text:
logging.info(f"{png_link.text} was not found in project")
sprite_data = requests.get(str(png_link.get_attribute("href"))).content
with open(f'{download_location}', 'wb') as handler:
handler.write(sprite_data)
logging.info(f"{png_link.text} saved.")
image = Image.open(download_location)
resized_image = image.resize((96, 96))
resized_image.save(download_location)
logging.info(f"{png_link.text} was resized.")
except TimeoutException:
logging.error("Timeout Exception occurred, could not find sprite links.")
except:
logging.error("Unexpected error when trying to open the login page")
traceback.print_exc()
driver.quit()
def main():
SpriteDownloader().get_missing_sprites()
if __name__ == '__main__':
main()