|
6 | 6 | """ |
7 | 7 |
|
8 | 8 | import json |
| 9 | +import requests |
9 | 10 | import sys |
10 | 11 | from pathlib import Path |
11 | | -from urllib.request import Request, urlopen |
| 12 | +from urllib.parse import urlparse |
12 | 13 |
|
13 | 14 | from utils import MIN_REDDIT_POINTS, TOP_REDDIT_LIMIT, USER_AGENT, create_slug |
14 | 15 |
|
|
35 | 36 |
|
36 | 37 | # SUBREDDITS = ["r/dotnet", "r/csharp"] |
37 | 38 |
|
| 39 | +REDDIT_COOKIES = json.loads(Path(__file__).parent.joinpath("reddit_cookies.json").read_text()) |
| 40 | + |
| 41 | +def create_cookie_jar(): |
| 42 | + parsed = urlparse("https://www.reddit.com") |
| 43 | + jar = requests.cookies.RequestsCookieJar() |
| 44 | + for name, value in REDDIT_COOKIES.items(): |
| 45 | + jar.set(name, value, domain=parsed.hostname, path="/") |
| 46 | + return jar |
38 | 47 |
|
39 | 48 | def fetch_subreddit_posts(subreddit: str, limit: int = 50) -> list[dict]: |
40 | 49 | """Fetch top posts from a subreddit using Reddit's JSON API.""" |
41 | 50 | url = f"https://www.reddit.com/{subreddit}/hot.json?limit={limit}" |
42 | | - req = Request(url, headers={"User-Agent": USER_AGENT}) |
43 | | - with urlopen(req, timeout=10) as resp: |
44 | | - data = json.loads(resp.read().decode("utf-8")) |
| 51 | + resp = requests.get(url, headers={"user-agent": USER_AGENT}, |
| 52 | + cookies=create_cookie_jar(), timeout=30, allow_redirects=True) |
| 53 | + resp.raise_for_status() |
| 54 | + data = resp.json() |
45 | 55 |
|
46 | 56 | posts = [] |
47 | 57 | for child in data.get("data", {}).get("children", []): |
|
0 commit comments