Skip to content

Commit

Permalink
feat: Add script to get vanilla countries from the wiki and download …
Browse files Browse the repository at this point in the history
…their flags
  • Loading branch information
ThiaudioTT committed Jun 29, 2024
1 parent 68ec120 commit 5e90bcf
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@ __pycache__/
/build/
hoi4-presence*/
*.zip
.venv/
.venv/
flags/
releasedCountries.py
initialCountries.py
tools/flags/
tools/releasedCountries.py
tools/initialCountries.py
87 changes: 87 additions & 0 deletions tools/getVanillaCountries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
from bs4 import BeautifulSoup
import requests
import os

# this script is used to get the vanilla countries from the wiki
# It creates a file with the countries and their codes in a python dictionary format
# it also downloads the flags of the countries

# Todo: this was made in a hurry, LMAO. Refactor it!
# Hint: use threads to be faster

HOI_SOURCE = "https://hoi4.paradoxwikis.com/"

def getCountries(table: list[str], filename: str):
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}")

# 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')


# 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_response = requests.get(image_webpage)
if image_response.status_code != 200:
print(f"Failed to get flag WEBPAGE for {country_name}")
continue

image_soup = BeautifulSoup(image_response.text, 'html.parser')

image = image_soup.select_one("div.eu4box:nth-child(2) > a:nth-child(2) > img")
if not image:
print(f"Failed to get flag IMAGE for {country_name}")
continue

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}")


def main():
countriesWiki = "https://hoi4.paradoxwikis.com/Countries"
responseCountriesWiki = requests.get(countriesWiki)
if responseCountriesWiki.status_code != 200:
print("Failed to get countries from wiki.")
return
soup = BeautifulSoup(responseCountriesWiki.text, 'html.parser')

os.makedirs("flags", exist_ok=True)


# pick the table with the initial countries
initialCountriesTable = soup.select_one("table.wikitable:nth-child(16) > tbody")

# Download the images and create the dictionary with the countries
getCountries(initialCountriesTable, "initialCountries")

# todo: do this part
# # pick the table with the released countries
# releasedCountriesTable = soup.select_one("table.wikitable:nth-child(26) > tbody")

# # Download the images and create the dictionary with the countries
# getCountries(releasedCountriesTable, "releasedCountries")

print("Done.")






if __name__ == '__main__':
main()

0 comments on commit 5e90bcf

Please sign in to comment.