Adding the subreddit into the title
This commit is contained in:
parent
ce8b3c716d
commit
640600e4ab
@ -1,12 +1,13 @@
|
|||||||
|
import json
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
import json
|
import urllib.parse
|
||||||
|
import uuid
|
||||||
|
from io import BytesIO
|
||||||
|
from typing import Any, Dict, List
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
from io import BytesIO
|
|
||||||
from typing import List, Dict, Any
|
|
||||||
import uuid
|
|
||||||
import urllib.parse
|
|
||||||
|
|
||||||
|
|
||||||
class ArchiveParser:
|
class ArchiveParser:
|
||||||
@ -156,6 +157,36 @@ class ArchiveParser:
|
|||||||
|
|
||||||
return ""
|
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]:
|
def _create_post(self, index) -> Dict[str, Any]:
|
||||||
"""Convert an index entry to a properly formatted post with ID and default image."""
|
"""Convert an index entry to a properly formatted post with ID and default image."""
|
||||||
try:
|
try:
|
||||||
@ -171,7 +202,12 @@ class ArchiveParser:
|
|||||||
base_url = index.get("base_url", "")
|
base_url = index.get("base_url", "")
|
||||||
real_title = self._extract_real_title_from_url(base_url)
|
real_title = self._extract_real_title_from_url(base_url)
|
||||||
if real_title:
|
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
|
# Set default image to reddit_logo.webp if no image is provided
|
||||||
post["image"] = (
|
post["image"] = (
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user