diff --git a/tools/getVanillaCountries.py b/tools/getVanillaCountries.py index 2cc601c..9e4c4ab 100644 --- a/tools/getVanillaCountries.py +++ b/tools/getVanillaCountries.py @@ -12,59 +12,78 @@ HOI_SOURCE = "https://hoi4.paradoxwikis.com/" +# Helper functions + +def getCountryDetails(row) -> tuple[str, str]: + country_name = row.find_all("td")[0].text.strip() + country_code = row.find_all("td")[1].text.strip() + return country_name, country_code + +def appendCountryToFile(country_name: str, country_code: str, filename: str) -> None: + # Write the country to the file + with open(f"{filename}.py", "a") as countriesFile: + # it must be in the format "ITA" : ("Italy", "ita"), + countriesFile.write(f'"{country_code}": ("{country_name}", "{country_code.lower()}"),\n') + + +def getSoup(url: str, error_message: str) -> BeautifulSoup: + response = requests.get(url) + if response.status_code != 200: + print(error_message) + return None + + return BeautifulSoup(response.text, 'html.parser') + def getCountries(table: list[str], filename: str): + """Donwloads the flags of the countries and creates a file with the countries and their codes in a python dictionary format""" for row in table.find_all("tr"): - if row.find_all("td"): - country_name = row.find_all("td")[0].text.strip() - country_code = row.find_all("td")[1].text.strip() - - print(f"Country: {country_name}Code: {country_code}") + if not row.find_all("td"): continue - try: - # Write the country to the file - with open(f"{filename}.py", "a") as countriesFile: - # it must be in the format "ITA" : ("Italy", "ita"), - countriesFile.write(f'"{country_code}": ("{country_name}", "{country_code.lower()}"),\n') + cName, cCode = getCountryDetails(row) + print(f"Country: {cName}Code: {cCode}") + try: + # Write the country to the file + appendCountryToFile(cName, cCode, filename) - # Download the flag of the country - # the flag is in another webpage - image_webpage = HOI_SOURCE + row.find_all("td")[0].find("a")["href"][1:] + # Download the flag of the country + # the flag is in another webpage + image_webpage = HOI_SOURCE + row.find_all("td")[0].find("a")["href"][1:] - image_webpage_response = requests.get(image_webpage) - if image_webpage_response.status_code != 200: - print(f"Failed to get flag WEBPAGE for {country_name}") - continue - - image_webpage_soup = BeautifulSoup(image_webpage_response.text, 'html.parser') + image_webpage_response = requests.get(image_webpage) + if image_webpage_response.status_code != 200: + print(f"Failed to get flag WEBPAGE for {cName}") + continue + + image_webpage_soup = BeautifulSoup(image_webpage_response.text, 'html.parser') - # image = image_webpage_soup.select_one("div.eu4box:nth-child(2) > a:nth-child(2) > img") - image_location = HOI_SOURCE + image_webpage_soup.select_one("div.mw-parser-output:nth-child(4) > div:nth-child(2) > a")["href"][1:] + # image = image_webpage_soup.select_one("div.eu4box:nth-child(2) > a:nth-child(2) > img") + image_location = HOI_SOURCE + image_webpage_soup.select_one("div.mw-parser-output:nth-child(4) > div:nth-child(2) > a")["href"][1:] - image_response = requests.get(image_location) - if image_response.status_code != 200: - print(f"Failed to get flag IMAGE for {country_name}") - continue + image_response = requests.get(image_location) + if image_response.status_code != 200: + print(f"Failed to get flag IMAGE for {cName}") + continue - image_soup = BeautifulSoup(image_response.text, 'html.parser') + image_soup = BeautifulSoup(image_response.text, 'html.parser') - image = image_soup.select_one("#file > a > img") + image = image_soup.select_one("#file > a > img") - if not image: - print(f"Failed to get flag IMAGE for {country_name}") - continue + if not image: + print(f"Failed to get flag IMAGE for {cName}") + continue - flag_url = HOI_SOURCE + image["src"] + flag_url = HOI_SOURCE + image["src"] - flag_response = requests.get(flag_url) - if flag_response.status_code == 200: - with open(f"flags/{country_code}.png", "wb") as flag_file: - flag_file.write(flag_response.content) - print(f"Flag for {country_name} downloaded.") - else: - print(f"Failed to download flag for {country_name}") - except Exception as e: - print(f"Failed to get flag for {country_name} - {e}") + flag_response = requests.get(flag_url) + if flag_response.status_code == 200: + with open(f"flags/{cCode}.png", "wb") as flag_file: + flag_file.write(flag_response.content) + print(f"Flag for {cName} downloaded.") + else: + print(f"Failed to download flag for {cName}") + except Exception as e: + print(f"Failed to get flag for {cName} - {e}") def main():