-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrape_realclimate.py
More file actions
423 lines (352 loc) · 11.5 KB
/
Copy pathscrape_realclimate.py
File metadata and controls
423 lines (352 loc) · 11.5 KB
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#!/usr/bin/env python3
"""
scrape_realclimate.py
---------------------
Scrape RealClimate comments for an exact author match and save the results as
JSON and Markdown.
Why this script exists
----------------------
The WordPress comments endpoint at RealClimate appears to ignore the
`author_name` query parameter, so server-side filtering can silently return
comments from unrelated authors. This scraper avoids that bug by collecting
candidate comments and filtering them client-side using exact normalized author
names.
Usage
-----
Fast search-based mode (recommended first):
python3 scrape_realclimate.py \
--author "Paul Pukite (@whut)" \
--search-term whut \
--search-term pukite
Exhaustive full scan (slower, but more robust if aliases changed over time):
python3 scrape_realclimate.py \
--scan-mode full-scan \
--author "Paul Pukite (@whut)"
Dependencies
------------
pip install requests
"""
from __future__ import annotations
import argparse
import json
import time
from html import unescape
from pathlib import Path
import requests
BASE_URL = "https://www.realclimate.org"
API_BASE = f"{BASE_URL}/wp-json/wp/v2"
COMMENTS_ENDPOINT = f"{API_BASE}/comments"
POSTS_ENDPOINT = f"{API_BASE}/posts"
PER_PAGE = 100
REQUEST_TIMEOUT = 30
COMMENT_FIELDS = "id,date,link,post,content,author_name,author_url"
DEFAULT_AUTHOR_ALIASES = [
"Paul Pukite (@whut)",
"Paul Pukite",
"whut",
]
DEFAULT_SEARCH_TERMS = [
"whut",
"pukite",
]
def normalize_text(value: str) -> str:
return " ".join(value.casefold().split())
def build_session() -> requests.Session:
session = requests.Session()
session.headers.update(
{
"User-Agent": (
"Mozilla/5.0 (compatible; RealClimate-exact-author-scraper/1.0; "
"+https://pukpr.github.io)"
)
}
)
return session
def fetch_comments_page(
session: requests.Session,
*,
page: int,
delay: float,
search: str | None = None,
order: str = "asc",
) -> tuple[list[dict], int]:
params = {
"per_page": PER_PAGE,
"page": page,
"orderby": "date",
"order": order,
"_fields": COMMENT_FIELDS,
}
if search:
params["search"] = search
try:
resp = session.get(COMMENTS_ENDPOINT, params=params, timeout=REQUEST_TIMEOUT)
if resp.status_code == 400:
return [], 0
resp.raise_for_status()
total_pages = int(resp.headers.get("X-WP-TotalPages", "0") or 0)
time.sleep(delay)
return resp.json(), total_pages
except requests.RequestException as exc:
label = f"search={search!r}" if search else "full scan"
print(f" [WARNING] Failed page {page} for {label}: {exc}")
return [], 0
def fetch_post_url(session: requests.Session, post_id: int, delay: float) -> str:
if post_id == 0:
return BASE_URL
try:
resp = session.get(
f"{POSTS_ENDPOINT}/{post_id}",
params={"_fields": "link"},
timeout=REQUEST_TIMEOUT,
)
resp.raise_for_status()
data = resp.json()
time.sleep(delay)
return data.get("link", BASE_URL)
except requests.RequestException as exc:
print(f" [WARNING] Could not resolve post {post_id}: {exc}")
return BASE_URL
def author_matches(author_name: str, author_aliases: set[str]) -> bool:
return normalize_text(author_name) in author_aliases
def collect_search_candidates(
session: requests.Session,
*,
search_terms: list[str],
delay: float,
max_pages: int | None,
) -> list[dict]:
candidates: list[dict] = []
seen_ids: set[int] = set()
for term in search_terms:
print(f"\nSearching comments for term: {term!r}")
page = 1
total_pages = None
while True:
if max_pages is not None and page > max_pages:
break
results, page_count = fetch_comments_page(
session,
page=page,
delay=delay,
search=term,
order="asc",
)
if total_pages is None:
total_pages = page_count
if not results:
print(f" page {page}: no results / end")
break
new_count = 0
for comment in results:
comment_id = comment.get("id", 0)
if comment_id not in seen_ids:
seen_ids.add(comment_id)
candidates.append(comment)
new_count += 1
total_label = total_pages if total_pages else "?"
print(
f" page {page}/{total_label}: got {len(results)} "
f"-> {new_count} new candidate(s)"
)
if total_pages and page >= total_pages:
break
page += 1
return candidates
def collect_full_scan_candidates(
session: requests.Session,
*,
delay: float,
max_pages: int | None,
) -> list[dict]:
candidates: list[dict] = []
page = 1
total_pages = None
print("\nRunning full comment scan (newest first) ...")
while True:
if max_pages is not None and page > max_pages:
break
results, page_count = fetch_comments_page(
session,
page=page,
delay=delay,
order="desc",
)
if total_pages is None:
total_pages = page_count
if not results:
print(f" page {page}: no results / end")
break
candidates.extend(results)
total_label = total_pages if total_pages else "?"
print(f" page {page}/{total_label}: got {len(results)} comment(s)")
if total_pages and page >= total_pages:
break
page += 1
return candidates
def filter_comments_by_author(
comments: list[dict], author_aliases: list[str]
) -> list[dict]:
alias_set = {normalize_text(alias) for alias in author_aliases}
filtered = [
comment
for comment in comments
if author_matches(comment.get("author_name", ""), alias_set)
]
filtered.sort(key=lambda comment: comment.get("date", ""))
return filtered
def enrich_with_post_url(
session: requests.Session, comments: list[dict], delay: float
) -> list[dict]:
unique_post_ids = {comment.get("post", 0) for comment in comments}
print(f"\nResolving URLs for {len(unique_post_ids)} unique post(s) ...")
post_url_cache: dict[int, str] = {}
for post_id in sorted(unique_post_ids):
if post_id not in post_url_cache:
post_url_cache[post_id] = fetch_post_url(session, post_id, delay)
for comment in comments:
post_id = comment.get("post", 0)
comment["post_url"] = comment.get("link") or post_url_cache.get(post_id, BASE_URL)
return comments
def plain_text(html: str) -> str:
import re
text = re.sub(r"<[^>]+>", "", html)
return unescape(text).strip()
def save_json(comments: list[dict], path: Path) -> None:
with path.open("w", encoding="utf-8") as handle:
json.dump(comments, handle, indent=2, ensure_ascii=False)
print(f"Saved JSON -> {path}")
def save_markdown(comments: list[dict], path: Path) -> None:
lines = [
"# RealClimate.org — Exact-author comments",
"",
f"*{len(comments)} comment(s) found.*",
"",
"*Sections are keyed by the original RealClimate comment ID so other docs can link to them reliably.*",
"",
"---",
"",
]
for index, comment in enumerate(comments, start=1):
comment_id = comment.get("id", 0)
author_name = comment.get("author_name", "unknown author")
date = comment.get("date", "unknown date")
url = comment.get("post_url", BASE_URL)
body = plain_text(comment.get("content", {}).get("rendered", ""))
lines.extend(
[
f'<a id="comment-{comment_id}"></a>',
"",
f"## Comment {comment_id}",
"",
f"**Archive index:** {index} ",
f"**Author:** {author_name} ",
f"**Date:** {date} ",
f"**URL:** <{url}>",
"",
body,
"",
"---",
"",
]
)
with path.open("w", encoding="utf-8") as handle:
handle.write("\n".join(lines))
print(f"Saved Markdown -> {path}")
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Scrape RealClimate comments by exact author match.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"--output-dir",
default="output",
help="Directory for output files.",
)
parser.add_argument(
"--delay",
type=float,
default=0.5,
help="Seconds to pause between HTTP requests.",
)
parser.add_argument(
"--author",
action="append",
dest="authors",
help=(
"Exact author alias to match after normalization. "
"Repeat as needed. If omitted, built-in Paul/whut aliases are used."
),
)
parser.add_argument(
"--search-term",
action="append",
dest="search_terms",
help=(
"Search term used to gather candidate comments in search mode. "
"Repeat as needed."
),
)
parser.add_argument(
"--scan-mode",
choices=["search", "full-scan"],
default="search",
help="Whether to gather candidates via search terms or by scanning all comments.",
)
parser.add_argument(
"--max-pages",
type=int,
default=None,
help="Optional page limit for debugging or partial runs.",
)
parser.add_argument(
"--json-name",
default="realclimate_exact_author_comments.json",
help="Output JSON filename.",
)
parser.add_argument(
"--markdown-name",
default="realclimate_exact_author_comments.md",
help="Output Markdown filename.",
)
return parser.parse_args()
def main() -> None:
args = parse_args()
output_dir = Path(args.output_dir)
output_dir.mkdir(parents=True, exist_ok=True)
author_aliases = args.authors or DEFAULT_AUTHOR_ALIASES
search_terms = args.search_terms or DEFAULT_SEARCH_TERMS
print("Author aliases:")
for alias in author_aliases:
print(f" - {alias}")
session = build_session()
if args.scan_mode == "search":
candidates = collect_search_candidates(
session,
search_terms=search_terms,
delay=args.delay,
max_pages=args.max_pages,
)
else:
candidates = collect_full_scan_candidates(
session,
delay=args.delay,
max_pages=args.max_pages,
)
print(f"\nCollected {len(candidates)} candidate comment(s).")
comments = filter_comments_by_author(candidates, author_aliases)
print(f"Matched {len(comments)} exact-author comment(s).")
if not comments:
print(
"\nNo exact-author comments matched. "
"Try additional --author aliases or --scan-mode full-scan."
)
return
comments = enrich_with_post_url(session, comments, args.delay)
json_path = output_dir / args.json_name
markdown_path = output_dir / args.markdown_name
save_json(comments, json_path)
save_markdown(comments, markdown_path)
print("\nDone.")
if __name__ == "__main__":
main()