Logic to convert index into useable post

This commit is contained in:
Jarian Cottingham 2025-10-14 14:49:56 -05:00
parent 79ecd8db81
commit f8f269b0b4
5 changed files with 29 additions and 4 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

View File

@ -20,7 +20,7 @@ def get_initial_posts():
# Use archive parser to fetch posts
posts = archive_parser.get_posts(count=10, start_index=0)
return jsonify(posts)
except Exception as e:
except Exception:
return jsonify({"error": "Failed to fetch posts"}), 500
@ -35,7 +35,7 @@ def get_more_posts():
posts = archive_parser.get_posts(count=10, start_index=count)
return jsonify(posts)
except Exception as e:
except Exception:
return jsonify({"error": "Failed to fetch more posts"}), 500
@ -46,7 +46,7 @@ def get_total_posts():
# Get total from archive parser
total = archive_parser.get_total_posts()
return jsonify({"total": total})
except Exception:
except Exception as e:
return jsonify({"error": "Failed to fetch total count"}), 500

View File

@ -2,6 +2,7 @@ import os
import subprocess
import json
from typing import List, Dict, Any
import uuid
class ArchiveParser:
@ -44,7 +45,9 @@ class ArchiveParser:
dir_path = os.path.join(self.archive_dir, directory)
posts.extend(self._extract_posts_from_directory(dir_path))
return posts
# Convert extracted posts to final format with proper IDs
formatted_posts = [self._create_post(post) for post in posts]
return formatted_posts
except subprocess.CalledProcessError as e:
print(f"Error executing command: {e}")
@ -110,3 +113,25 @@ class ArchiveParser:
except Exception as e:
print(f"Error getting total posts: {e}")
return 0
def _create_post(self, index) -> Dict[str, Any]:
"""Convert an index entry to a properly formatted post with ID and default image."""
try:
post = dict()
# Ensure we have all required fields with defaults
post["id"] = str(uuid.uuid4()) # Generate a unique ID for each post
post["title"] = index.get("title", "Untitled Post")
post["url"] = index.get("url", "#")
# Set default image to reddit_logo.webp if no image is provided
post["image"] = (
"/Reddit_Logo.webp" # Default to the logo in website directory
)
return post
except Exception as e:
print(f"Error converting index to post: {e}")
# Return a minimal post if there's an error
raise e

View File

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB