File size: 12,048 Bytes
5ab87e0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
from __future__ import annotations
from config import CFG, _SESS, _RND
import logging
import re
from bs4 import BeautifulSoup
import functools
import random
import requests
import time
import trafilatura
from web_helpers import retry, fetch_blocked_site
_REDDIT_UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
@retry
def _reddit_json_api(url: str) -> str | None:
api_url = re.sub(r"/comments/([a-z0-9]{6,8}).*", r"/comments/\1.json", url)
try:
headers = {"User-Agent": _REDDIT_UA, "Accept": "application/json"}
r = _SESS.get(
api_url,
params={"limit": 5, "depth": 2, "raw_json": 1},
headers=headers,
timeout=(CFG.connect_to, CFG.read_to),
)
if "blocked" in r.text.lower() or r.status_code != 200:
return None
data = r.json()
post_data = data[0]["data"]["children"][0]["data"]
title = post_data.get("title", "")
selftext = post_data.get("selftext", "")
author = post_data.get("author", "")
comments = []
if len(data) > 1:
for comment in data[1]["data"]["children"][:50]:
if comment["kind"] == "t1":
c_author = comment["data"].get("author", "")
c_body = comment["data"].get("body", "")
if c_body:
comments.append(f"u/{c_author}: {c_body}")
result = f"Title: {title}\nPosted by: u/{author}\n\n"
if selftext:
result += f"{selftext}\n\n"
if comments:
result += "Top comments:\n\n" + "\n\n".join(comments)
return result.strip()
except Exception:
return None
import urllib.parse as _u
_ID_RE = re.compile(r"([a-z0-9]{6,8})", re.I)
def _extract_post_id(url: str) -> str | None:
"""
Heuristics to find the 6β8βchar baseβ36 Reddit ID in *any* post URL:
β’ shortβlink redd.it/<id>
β’ /r/sub/abc123/β¦ (old style)
β’ /comments/<id>/β¦ (APIβfriendly)
"""
# 1) shortβlink host
u = _u.urlparse(url)
if u.netloc in {"redd.it", "www.redd.it"}:
return u.path.lstrip("/").split("/")[0] or None
# 2) /comments/<id>/ pattern (works already)
m = re.search(r"/comments/([a-z0-9]{6,8})", url, re.I)
if m:
return m.group(1)
# 3) generic β/r/<sub>/<id>/β or trailing ββ¦/<id>β
parts = [p for p in u.path.split("/") if p]
for p in parts[::-1]: # search from rightβmost
if _ID_RE.fullmatch(p):
return p
return None
# ----------------------------------------------------------------------
# Reddit OAuth helper β appβonly token (readβonly)
# ----------------------------------------------------------------------
import base64
import threading
_TOKEN_LOCK = threading.Lock()
_REDDIT_TOKEN_CACHE: dict[str, float | str] = {"token": None, "expires": 0.0}
def get_reddit_token(client_id: str, client_secret: str) -> str | None:
"""
Return a cached bearer token obtained via Reddit's clientβcredentials flow.
Returns None on error so callers can fall back to other scraping paths.
"""
now = time.time()
# Fast path β cached and still valid
if (_tok := _REDDIT_TOKEN_CACHE["token"]) and now < _REDDIT_TOKEN_CACHE["expires"] - 30:
return _tok # 30βsec grace
with _TOKEN_LOCK: # only one thread refreshes
# Reβcheck after acquiring the lock
if (_tok := _REDDIT_TOKEN_CACHE["token"]) and now < _REDDIT_TOKEN_CACHE["expires"] - 30:
return _tok
try:
auth = requests.auth.HTTPBasicAuth(client_id, client_secret)
headers = {"User-Agent": _REDDIT_UA}
data = {"grant_type": "client_credentials"} # appβonly, readβonly
r = requests.post(
"https://www.reddit.com/api/v1/access_token",
auth=auth,
data=data,
headers=headers,
timeout=10,
)
r.raise_for_status()
payload = r.json()
token = payload["access_token"]
ttl = int(payload.get("expires_in", 3600))
_REDDIT_TOKEN_CACHE.update({"token": token, "expires": now + ttl})
return token
except Exception as e:
logging.warning("Reddit token fetch failed: %s", e)
return None
@retry
def reddit_official_api(url: str, client_id: str, client_secret: str) -> str | None:
"""
β’ Works for *any* Reddit permalink or shortβlink.
β’ If the URL is a subreddit root (/r/<sub>) it still fetches 3 hot posts + top comment (unchanged).
"""
token = get_reddit_token(client_id, client_secret)
if not token:
return None
headers = {
"Authorization": f"bearer {token}",
"User-Agent": _REDDIT_UA,
}
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 1. Try to treat it as a *post* link by extracting an ID
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
post_id = _extract_post_id(url)
if post_id:
try:
r = requests.get(
f"https://oauth.reddit.com/comments/{post_id}",
headers=headers,
params={"limit": 5, "depth": 2, "raw_json": 1},
timeout=10,
)
r.raise_for_status()
data = r.json()
post = data[0]["data"]["children"][0]["data"]
title = post.get("title", "")
body = post.get("selftext", "")
author = post.get("author", "")
comments = []
if len(data) > 1:
for c in data[1]["data"]["children"][:50]:
if c["kind"] == "t1":
c_auth = c["data"].get("author", "")
c_body = c["data"].get("body", "")
if c_body:
comments.append(f"u/{c_auth}: {c_body}")
out = f"Title: {title}\nPosted by: u/{author}\n\n"
if body:
out += f"{body}\n\n"
if comments:
out += "Top comments:\n\n" + "\n\n".join(comments)
return out.strip()
except Exception as e:
logging.debug("Official API post fetch failed (%s); will try other strategies", e)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 2. If not a post (or the fetch above failed) treat as *subreddit*
# root and list 3 hot posts, each with top comment (unchanged).
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
m_sub = re.search(r"reddit\.com/r/([^/?#]+)", url)
if not m_sub:
return None # allow caller to fall back
subreddit = m_sub.group(1)
try:
r = requests.get(
f"https://oauth.reddit.com/r/{subreddit}/hot",
headers=headers,
params={"limit": 3, "raw_json": 1},
timeout=10,
)
r.raise_for_status()
posts = r.json()["data"]["children"]
out_blocks = []
for p in posts:
pd = p["data"]
pid = pd["id"]
title = pd.get("title", "")
auth = pd.get("author", "")
link = pd.get("permalink", "")
# fetch one top comment
top_comment = ""
try:
c = requests.get(
f"https://oauth.reddit.com/comments/{pid}",
headers=headers,
params={"limit": 1, "depth": 1, "raw_json": 1},
timeout=10,
).json()
if len(c) > 1:
for cmt in c[1]["data"]["children"]:
if cmt["kind"] == "t1":
cauth = cmt["data"].get("author", "")
cbody = cmt["data"].get("body", "")
top_comment = f"u/{cauth}: {cbody}"
break
except Exception:
pass
block = f"Title: {title}\nPosted by: u/{auth}\nLink: https://www.reddit.com{link}\n"
if top_comment:
block += f"Top comment:\n{top_comment}"
out_blocks.append(block)
return "\n\n---\n\n".join(out_blocks)
except Exception as e:
logging.debug("Official API subreddit fetch failed: %s", e)
return None
@retry
def _reddit_old_version(url: str) -> str | None:
old_url = url.replace("www.reddit.com", "old.reddit.com")
try:
r = _SESS.get(old_url, headers={"User-Agent": _REDDIT_UA}, timeout=(CFG.connect_to, CFG.read_to))
if r.status_code != 200:
return None
soup = BeautifulSoup(r.text, "lxml")
title = soup.select_one(".title").text.strip() if soup.select_one(".title") else ""
author = soup.select_one(".author").text.strip() if soup.select_one(".author") else ""
post_body = soup.select_one(".usertext-body")
post_text = post_body.get_text(strip=True) if post_body else ""
comments = []
for comment in soup.select(".comment")[:50]:
c_author = comment.select_one(".author")
c_body = comment.select_one(".usertext-body")
if c_author and c_body:
comments.append(f"u/{c_author.text}: {c_body.get_text(strip=True)}")
result = f"Title: {title}\nPosted by: u/{author}\n\n"
if post_text:
result += f"{post_text}\n\n"
if comments:
result += "Top comments:\n\n" + "\n\n".join(comments)
return result.strip()
except Exception:
print("old reddit failed")
return None
@retry
def _pushshift_fallback(url: str) -> str | None:
m = re.search(r"/comments/([a-z0-9]{6,8})", url)
if not m:
return None
link_id = m.group(1)
try:
pst = _SESS.get(
"https://api.pushshift.io/reddit/submission/search/",
params={"ids": link_id, "size": 1},
timeout=10,
).json()["data"]
post_txt = pst[0]["selftext"] if pst else ""
com = _SESS.get(
"https://api.pushshift.io/reddit/comment/search/",
params={"link_id": link_id, "sort": "desc", "size": 3},
timeout=10,
).json()["data"]
top_txt = "\n\n".join(c["body"] for c in com)
txt = (post_txt + "\n\n" + top_txt).strip()
return txt or None
except Exception:
return None
def fetch_reddit(url: str) -> str:
txt = _reddit_old_version(url)
if txt:
return "[Retrieved from Reddit]" + txt[:CFG.text_cap]
if CFG.reddit_client_id and CFG.reddit_client_secret:
# print("AAAA")
txt = reddit_official_api(url, CFG.reddit_client_id, CFG.reddit_client_secret)
if txt:
return "[Retrieved from Reddit]" + txt[:CFG.text_cap]
txt = _reddit_json_api(url)
if txt:
return "[Retrieved from Reddit]" + txt[:CFG.text_cap]
txt = _pushshift_fallback(url)
if txt:
return "[Retrieved from Reddit]" + txt[:CFG.text_cap]
return fetch_blocked_site(url)[:CFG.text_cap]
|