Skip to content

Commit 1d31161

Browse files
committed
Update reddit_top.py
1 parent 2734821 commit 1d31161

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

scripts/reddit_top.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import json
99
import requests
1010
import sys
11+
from concurrent.futures import ThreadPoolExecutor, as_completed
1112
from pathlib import Path
1213
from urllib.parse import urlparse
1314

@@ -32,6 +33,15 @@
3233
"r/programming",
3334
"r/technology",
3435
"r/webdev",
36+
"r/linux",
37+
"r/mac",
38+
"r/apple",
39+
"r/windows",
40+
"r/cybersecurity",
41+
"/r/ArtificialInteligence",
42+
"/r/technews",
43+
"r/gadgets",
44+
"r/hardware",
3545
]
3646

3747
# SUBREDDITS = ["r/dotnet", "r/csharp"]
@@ -85,12 +95,14 @@ def fetch_subreddit_posts(subreddit: str, limit: int = 50) -> list[dict]:
8595
def fetch_reddit_top(limit: int = TOP_REDDIT_LIMIT) -> list[dict]:
8696
"""Fetch and aggregate top posts across multiple subreddits."""
8797
all_posts = []
88-
for subreddit in SUBREDDITS:
89-
try:
90-
posts = fetch_subreddit_posts(subreddit)
91-
all_posts.extend(posts)
92-
except Exception as e:
93-
print(f"Warning: failed to fetch {subreddit}: {e}", file=sys.stderr)
98+
with ThreadPoolExecutor(max_workers=len(SUBREDDITS)) as executor:
99+
futures = {executor.submit(fetch_subreddit_posts, subreddit): subreddit for subreddit in SUBREDDITS}
100+
for future in as_completed(futures):
101+
subreddit = futures[future]
102+
try:
103+
all_posts.extend(future.result())
104+
except Exception as e:
105+
print(f"Warning: failed to fetch {subreddit}: {e}", file=sys.stderr)
94106

95107
# Deduplicate by id
96108
seen = set()

0 commit comments

Comments
 (0)