Files
BSN-Chatsystem/parse_reddit_v3.py
T

115 lines
4.3 KiB
Python

import re, json
from datetime import datetime, timezone
CUTOFF_MS = 1780272000000 # June 1, 2026 08:00 UTC
NOW_UTC = datetime.now(timezone.utc)
files = {
'top': '/home/hermes/workspace/reddit_top.html',
'hot': '/home/hermes/workspace/reddit_hot.html',
'new': '/home/hermes/workspace/reddit_new.html',
}
all_posts = {} # keyed by thing ID (t3_xxx)
for source, path in files.items():
with open(path) as f:
content = f.read()
# Find all 'thing' divs
things = re.findall(r'<div class=" thing id-t3_([a-z0-9]+)[^"]*"(.*?)<div class="clearleft"></div></div>', content, re.DOTALL)
print(f"Source {source}: {len(things)} things found")
for thing_id, thing_html in things:
# Extract data attributes
permalink_m = re.search(r'data-permalink="([^"]*)"', thing_html)
score_m = re.search(r'data-score="(\d+)"', thing_html)
comments_m = re.search(r'data-comments-count="(\d+)"', thing_html)
timestamp_m = re.search(r'data-timestamp="(\d+)"', thing_html)
author_m = re.search(r'data-author="([^"]*)"', thing_html)
if not permalink_m:
continue
permalink = permalink_m.group(1)
score = int(score_m.group(1)) if score_m else 0
comments = int(comments_m.group(1)) if comments_m else 0
timestamp = int(timestamp_m.group(1)) if timestamp_m else 0
author = author_m.group(1) if author_m else 'unknown'
if timestamp < CUTOFF_MS:
continue
# Extract title - look for <p class="title"> ... </p> within this thing
title_block = re.search(r'<p class="title">(.*?)</p>', thing_html, re.DOTALL)
title = "Unknown Title"
flair = ""
if title_block:
title_html = title_block.group(1)
# Extract flair label
flair_m = re.search(r'linkflairlabel[^>]*title="([^"]*)"', title_html)
if not flair_m:
flair_m = re.search(r'flairrichtext[^>]*title="([^"]*)"', title_html)
if flair_m:
flair = flair_m.group(1)
# Extract actual title text from <a> tag
title_text_m = re.search(r'<a[^>]*>(.*?)</a>', title_html)
if title_text_m:
title = title_text_m.group(1).strip()
key = thing_id
if key in all_posts:
if score > all_posts[key]['score']:
all_posts[key] = {
'id': thing_id,
'title': title,
'flair': flair,
'permalink': permalink,
'source': source,
'score': score,
'comments': comments,
'timestamp': timestamp,
'author': author,
}
else:
all_posts[key] = {
'id': thing_id,
'title': title,
'flair': flair,
'permalink': permalink,
'source': source,
'score': score,
'comments': comments,
'timestamp': timestamp,
'author': author,
}
print(f"\nTotal unique posts in last 72h: {len(all_posts)}")
# Exclude stickied AutoModerator posts, COMC posts
excluded_flairs = {'COMC', 'Daily Game Recs', 'Midweek Mingle'}
filtered = {k: v for k, v in all_posts.items() if v['flair'] not in excluded_flairs and v['author'] != 'AutoModerator'}
# Also filter out obvious "look at my collection" posts by title
def is_collection_post(title):
low = title.lower()
return any(x in low for x in ['[comc]', 'my collection', 'my game room', 'game room'])
filtered = {k: v for k, v in filtered.items() if not is_collection_post(v['title'])}
print(f"After filtering out stickied/COMC: {len(filtered)}")
sorted_posts = sorted(filtered.values(), key=lambda x: x['score'], reverse=True)
for i, p in enumerate(sorted_posts[:30]):
dt = datetime.fromtimestamp(p['timestamp']/1000, tz=timezone.utc)
age_h = (NOW_UTC - dt).total_seconds() / 3600
flair_str = f" [{p['flair']}]" if p['flair'] else ""
print(f"{i+1}. [{p['source'].upper()}{flair_str}] Score:{p['score']} 💬{p['comments']} | {age_h:.0f}h ago | u/{p['author']}")
print(f" {p['title']}")
print(f" https://old.reddit.com{p['permalink']}")
print()