From 640600e4ab225ba3059af216b5d9fd0400ea522d Mon Sep 17 00:00:00 2001 From: Jarian Cottingham Date: Sat, 8 Nov 2025 12:20:33 -0600 Subject: [PATCH] Adding the subreddit into the title --- src/server/parse_archive.py | 48 ++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/src/server/parse_archive.py b/src/server/parse_archive.py index 26d4a83..aca8343 100644 --- a/src/server/parse_archive.py +++ b/src/server/parse_archive.py @@ -1,12 +1,13 @@ +import json import os import subprocess -import json +import urllib.parse +import uuid +from io import BytesIO +from typing import Any, Dict, List + import requests from PIL import Image -from io import BytesIO -from typing import List, Dict, Any -import uuid -import urllib.parse class ArchiveParser: @@ -156,6 +157,36 @@ class ArchiveParser: return "" + def _extract_subreddit_from_url(self, base_url: str) -> str: + """Extract the subreddit name from base_url.""" + if not base_url or not isinstance(base_url, str): + return "" + + # Check if the URL contains reddit pattern + if "reddit.com/" in base_url: + try: + # Parse URL to extract path component + parsed_url = urllib.parse.urlparse(base_url) + path_parts = parsed_url.path.strip("/").split("/") + + # Find index of 'r' which marks the subreddit + r_index = -1 + for i, part in enumerate(path_parts): + if part == "r": + r_index = i + break + + # If we found 'r', the next part should be the subreddit name + if r_index != -1 and r_index + 1 < len(path_parts): + subreddit = path_parts[r_index + 1] + return subreddit + + except Exception as e: + print(f"Error extracting subreddit from URL: {e}") + return "" + + return "" + def _create_post(self, index) -> Dict[str, Any]: """Convert an index entry to a properly formatted post with ID and default image.""" try: @@ -171,7 +202,12 @@ class ArchiveParser: base_url = index.get("base_url", "") real_title = self._extract_real_title_from_url(base_url) if real_title: - post["title"] = real_title + # Extract subreddit name to format title properly + subreddit = self._extract_subreddit_from_url(base_url) + if subreddit: + post["title"] = f"{subreddit} - {real_title}" + else: + post["title"] = real_title # Set default image to reddit_logo.webp if no image is provided post["image"] = (