From 059c790987a7399bd8ffa4e34c68af796cee8003 Mon Sep 17 00:00:00 2001 From: Herr Erde Date: Fri, 28 Jun 2024 16:26:47 +0200 Subject: [PATCH] fix name outputs --- script/fetch_profile.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/script/fetch_profile.py b/script/fetch_profile.py index b31abbf..5e1ad2b 100644 --- a/script/fetch_profile.py +++ b/script/fetch_profile.py @@ -4,6 +4,8 @@ url = "https://subwaysurf.fandom.com/wiki/Player_Profile" +filename = "upload/playerprofile_links.json" + def fetch_data(url): try: @@ -32,7 +34,15 @@ def fetch_profile(html): if img_tag: img_src = img_tag.get("data-src", "") or img_tag.get("src", "") img_src = img_src.split(".png")[0] + ".png" - profile_name = item.find("div", class_="lightbox-caption").text.strip() + + # Fetch the title from the anchor tag within the lightbox-caption div + lightbox_caption_div = item.find("div", class_="lightbox-caption") + if lightbox_caption_div: + anchor_tag = lightbox_caption_div.find("a") + profile_name = ( + anchor_tag.get("title", "").strip() if anchor_tag else "" + ) + profiles.append({"name": profile_name, "img_url": img_src}) return profiles @@ -51,24 +61,27 @@ def fetch_frame(html): raise ValueError("Gallery div with id 'gallery-2' not found") items = gallery_div.find_all("div", class_="wikia-gallery-item") - profiles = [] + frames = [] for item in items: img_tag = item.find("div", class_="gallery-image-wrapper").find("img") if img_tag: img_src = img_tag.get("data-src", "") or img_tag.get("src", "") img_src = img_src.split(".png")[0] + ".png" - profile_name = item.find("div", class_="lightbox-caption").text.strip() - profiles.append({"name": profile_name, "img_url": img_src}) + frame_name = item.find("div", class_="lightbox-caption").text.strip() + frame_name = frame_name.split(" (from ")[ + 0 + ] # Remove " (from " and everything after it + frames.append({"name": frame_name, "img_url": img_src}) - return profiles + return frames except Exception as e: print("Error fetching frame:", e) return [] -def save_json(portraits, frames, filename="upload/playerprofile_links.json"): +def save_json(portraits, frames): data = {"Portraits": portraits, "Frames": frames} with open(filename, "w") as f: