Skip to content

Instantly share code, notes, and snippets.

@NBonaparte
Last active June 28, 2020 20:31
Show Gist options
  • Save NBonaparte/2f3fa01c410087ca71d97276dc7cbfcc to your computer and use it in GitHub Desktop.
Save NBonaparte/2f3fa01c410087ca71d97276dc7cbfcc to your computer and use it in GitHub Desktop.
generates new page from eu4 achievements wiki page with unlocked achievements hidden
# NOTE: Game Details setting on Steam must be public
# Replace key and steamID values with your own api key (from https://steamcommunity.com/dev/apikey) and Steam ID.
from datetime import datetime
import requests
from bs4 import BeautifulSoup
# get currently unlocked achievements
key = "0"
steamID = 0
appID = 236850
achUrl = "https://api.steampowered.com/ISteamUserStats/GetPlayerAchievements/v1"
apiKeys = {"steamid": str(steamID), "appid": str(appID), "key": key, "l": "en"}
achievements = requests.get(achUrl, params=apiKeys).json()["playerstats"]["achievements"]
achieved = [i["name"] for i in achievements if i["achieved"]]
print(f"completed {len(achieved)}/{len(achievements)} achievements")
baseUrl = "https://eu4.paradoxwikis.com"
wikiUrl = baseUrl + "/Achievements"
wikiHtml = requests.get(wikiUrl).text
soup = BeautifulSoup(wikiHtml, "html.parser")
# modify all src and hrefs to the wiki url
def has_src(tag):
return tag.has_attr("src")
def has_href(tag):
return tag.has_attr("href")
for tag in soup.find_all(has_src):
src = tag["src"]
if "http" not in src and "cloudfront" not in src:
tag["src"] = baseUrl + src
for tag in soup.find_all(has_href):
href = tag["href"]
if "http" not in href:
tag["href"] = baseUrl + href
diff_tot = {}
diff_left = {}
# remove already unlocked achievements from the html
table = soup.find("table")
for row in table.find_all("tr"):
items = row.find_all("td")
if items:
diff = next(items[-1].strings).strip()
diff_tot[diff] = diff_tot.get(diff, 0) + 1
name = items[0]
if any(i in name.strings for i in achieved):
# remove row from the table
row.decompose()
else:
diff_left[diff] = diff_left.get(diff, 0) + 1
print("Achievements left (by difficulty):")
for di, n in diff_left.items():
tot = diff_tot[di]
print(f"{di}: {n}/{tot}")
with open("eu4_ach.html", "w") as f:
f.write(soup.prettify())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment