File tree Expand file tree Collapse file tree 1 file changed +18
-6
lines changed
Expand file tree Collapse file tree 1 file changed +18
-6
lines changed Original file line number Diff line number Diff line change 88import json
99import requests
1010import sys
11+ from concurrent .futures import ThreadPoolExecutor , as_completed
1112from pathlib import Path
1213from urllib .parse import urlparse
1314
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]:
8595def 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 ()
You can’t perform that action at this time.
0 commit comments