Files
BSN-Chatsystem/parse_reddit.py
T

81 lines
3.4 KiB
Python

import re, json, sys
from datetime import datetime, timezone
# Current time: June 4, 2026 ~08:00 UTC
# 72 hours prior: June 1, 2026 ~08:00 UTC
CUTOFF_MS = 1780272000000 # June 1, 2026 08:00 UTC in ms
files = {
'top': '/home/hermes/workspace/reddit_top.html',
'hot': '/home/hermes/workspace/reddit_hot.html',
'new': '/home/hermes/workspace/reddit_new.html',
}
all_posts = {}
for source, path in files.items():
with open(path) as f:
content = f.read()
# Extract data attributes from divs containing data-permalink
# Pattern: find blocks with data-permalink and nearby data-*
permalinks = re.findall(r'data-permalink="([^"]*)"', content)
scores = re.findall(r'data-score="([^"]*)"', content)
comments = re.findall(r'data-comments-count="([^"]*)"', content)
timestamps = re.findall(r'data-timestamp="([^"]*)"', content)
authors = re.findall(r'data-author="([^"]*)"', content)
# Title extraction: find <a class="title" or similar near permalink
# Let's use a different approach - extract by matching data- attributes group by group
# Use regex to find blocks that contain data-permalink
blocks = re.findall(r'(<div[^>]*data-permalink="([^"]*)"[^>]*>.*?</div>)', content, re.DOTALL)
# Simpler: iterate and match by position
print(f"Source {source}: {len(permalinks)} permalinks, {len(scores)} scores, {len(comments)} comments, {len(timestamps)} timestamps, {len(authors)} authors")
# Extract titles - look for <a> tags with class containing "title" that are near the permalink
# Better: find <p class="title"><a ...>Title</a></p> patterns
titles = re.findall(r'<p class="title">\s*<a[^>]*>(.*?)</a>', content, re.DOTALL)
title_hrefs = re.findall(r'<p class="title">\s*<a[^>]*href="([^"]*)"', content)
print(f" titles: {len(titles)}, hrefs: {len(title_hrefs)}")
for i, p in enumerate(permalinks):
if i < len(timestamps):
ts = int(timestamps[i])
if ts >= CUTOFF_MS:
post = {
'permalink': p,
'source': source,
'timestamp': ts,
'score': int(scores[i]) if i < len(scores) else 0,
'comments': int(comments[i]) if i < len(comments) else 0,
'author': authors[i] if i < len(authors) else 'unknown',
}
# Find matching title
for j, href in enumerate(title_hrefs):
if p in href or href.endswith(p.rstrip('/')):
if j < len(titles):
post['title'] = titles[j].strip()
break
if 'title' not in post:
post['title'] = 'Unknown Title'
all_posts[p] = post
# Deduplicate by permalink (keep highest score version)
print(f"\nTotal posts in last 72h: {len(all_posts)}")
# Sort by score descending
sorted_posts = sorted(all_posts.values(), key=lambda x: x['score'], reverse=True)
# Print summary
for i, p in enumerate(sorted_posts):
dt = datetime.fromtimestamp(p['timestamp']/1000, tz=timezone.utc)
age = (datetime.now(timezone.utc) - dt).total_seconds() / 3600
print(f"{i+1}. [{p['source'].upper()}] Score:{p['score']} Comments:{p['comments']} Age:{age:.0f}h")
print(f" Title: {p['title'][:100]}")
print(f" Author: u/{p['author']}")
print(f" Link: https://old.reddit.com{p['permalink']}")
print()