File size: 10,604 Bytes
25c60c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb70d66
 
 
25c60c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb70d66
 
 
 
 
 
25c60c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
scrape_plans.py — Fetch the RocketReviews.com plans index and scrape each
detail page, saving structured JSON to source/plans/.

Output
------
    source/plans/index.jsonl        one record per plan (raw index fields)
    source/plans/detail/{slug}.json full parsed detail per plan

Usage
-----
    python scripts/plans/01_scrape.py
    python scripts/plans/01_scrape.py --delay 1.0 --limit 10
    python scripts/plans/01_scrape.py --force   # re-scrape existing files
"""

from __future__ import annotations

import argparse
import json
import logging
import re
import sys
import time
from datetime import datetime, timezone
from pathlib import Path
from typing import Optional

import requests
from bs4 import BeautifulSoup
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry

# ---------------------------------------------------------------------------
# Config
# ---------------------------------------------------------------------------

BASE_URL = "https://www.rocketreviews.com"
INDEX_URL = f"{BASE_URL}/rocketry-plans.html"
USER_AGENT = "RocketReviews-Dataset/1.0"
DEFAULT_DELAY = 1.0

ROOT = Path(__file__).parent.parent.parent
SOURCE_DIR = ROOT / "source" / "plans"
DETAIL_DIR = SOURCE_DIR / "detail"

# ---------------------------------------------------------------------------
# Logging
# ---------------------------------------------------------------------------

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s %(levelname)s %(message)s",
    handlers=[logging.StreamHandler(sys.stdout)],
)
log = logging.getLogger(__name__)

# ---------------------------------------------------------------------------
# HTTP session
# ---------------------------------------------------------------------------


def _build_session() -> requests.Session:
    s = requests.Session()
    s.headers["User-Agent"] = USER_AGENT
    retry = Retry(
        total=3,
        backoff_factor=2.0,
        status_forcelist=[429, 500, 502, 503, 504],
        allowed_methods=["GET"],
    )
    s.mount("https://", HTTPAdapter(max_retries=retry))
    s.mount("http://", HTTPAdapter(max_retries=retry))
    return s


class RateLimiter:
    def __init__(self, delay: float) -> None:
        self.delay = delay
        self._last: float = 0.0

    def wait(self) -> None:
        elapsed = time.monotonic() - self._last
        if elapsed < self.delay:
            time.sleep(self.delay - elapsed)
        self._last = time.monotonic()


# ---------------------------------------------------------------------------
# Parsing helpers
# ---------------------------------------------------------------------------


def _slug_from_path(path: str) -> str:
    """
    Extract the slug from a url path.
    e.g. '/1940-exploratory-planetary-cargo-ferry-180703114912.html' ->
         '1940-exploratory-planetary-cargo-ferry'
    """
    name = path.lstrip("/").removesuffix(".html")
    # Remove trailing -NNNNNNNNNNNN timestamp (12-digit numeric suffix)
    name = re.sub(r"-\d{10,}$", "", name)
    return name


def _parse_index(html: str) -> list[dict]:
    """
    Parse the rocketry-plans.html static page and return one record
    per plan from the main data table.
    """
    soup = BeautifulSoup(html, "lxml")
    records = []

    # The plans table is typically the last large table on the page
    tables = soup.find_all("table")
    table = tables[-1] if tables else None
    
    if not table:
        log.warning("Could not find the plans table on the index page.")
        return records

    # Skip the header row
    for row in table.find_all("tr")[1:]:
        cells = row.find_all(["td", "th"])
        if len(cells) < 5:
            continue
            
        # 1: Source, 2: Title (link to detail), 3: Style, 4: Site (external link)
        source = cells[1].get_text(strip=True) or None
        
        title_a = cells[2].find("a")
        title_trunc = title_a.get_text(strip=True) if title_a else cells[2].get_text(strip=True)
        
        detail_path = title_a["href"] if title_a and title_a.has_attr("href") else None
        
        # Skip if there's no detail page link to use as an identifier
        if not detail_path:
            continue
            
        detail_url = detail_path if detail_path.startswith("http") else f"{BASE_URL}{detail_path}"
        slug = _slug_from_path(detail_path)
        
        style = cells[3].get_text(strip=True) or None
        
        site_a = cells[4].find("a")
        site_name = site_a.get_text(strip=True) if site_a else cells[4].get_text(strip=True)
        external_url = site_a["href"] if site_a and site_a.has_attr("href") else None
        
        # In case it's just plain text without a link, normalize empty string to None
        if not site_name:
            site_name = None

        records.append({
            "slug": slug,
            "title_truncated": title_trunc,
            "source": source,
            "style": style,
            "site": {
                "name": site_name,
                "url": external_url
            },
            "url": detail_url
        })

    return records


def _parse_detail(html: str, index_rec: dict) -> dict:
    """Merge index-level fields with full title from the detail page."""
    soup = BeautifulSoup(html, "lxml")
    
    # Try to extract the full title from the h1 on the detail page
    h1 = soup.find("h1")
    full_title = None
    if h1:
        # Example: "Rocketry Plans/Instructions - Model Rocket News 1940 Exploratory Planetary Cargo Ferry"
        raw_h1 = h1.get_text(strip=True)
        full_title = re.sub(r"^Rocketry Plans/Instructions\s*-\s*", "", raw_h1, flags=re.I).strip()
        
    # If the h1 wasn't found or was weird, fall back to what we had
    if not full_title:
        full_title = index_rec.get("title_truncated")
        
    # Exclude title_truncated from the final output and replace with full title
    out_rec = {k: v for k, v in index_rec.items() if k != "title_truncated"}
        
    return {
        **out_rec,
        "title": full_title,
        "scraped_at": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
    }


# ---------------------------------------------------------------------------
# Fetch helpers
# ---------------------------------------------------------------------------


def fetch_index(session: requests.Session) -> list[dict]:
    log.info("Fetching plans index from %s", INDEX_URL)
    resp = session.get(INDEX_URL, timeout=30)
    resp.raise_for_status()
    records = _parse_index(resp.text)
    log.info("Index returned %d records.", len(records))
    return records


def scrape_detail(
    session: requests.Session,
    rate: RateLimiter,
    index_rec: dict,
    force: bool = False,
) -> Optional[dict]:
    slug = index_rec["slug"]
    shard = slug[0].lower() if slug else "_"
    shard_dir = DETAIL_DIR / shard
    dest = shard_dir / f"{slug}.json"

    if dest.exists() and not force:
        log.debug("Already scraped %s, skipping.", slug)
        return None

    url = index_rec["url"]
    rate.wait()

    try:
        resp = session.get(url, timeout=30)
        resp.raise_for_status()
    except requests.RequestException as exc:
        log.warning("Failed to fetch plan %s: %s", slug, exc)
        return None

    return _parse_detail(resp.text, index_rec)


# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------


def main() -> None:
    parser = argparse.ArgumentParser(description="Scrape RocketReviews.com plans.")
    parser.add_argument(
        "--delay",
        type=float,
        default=DEFAULT_DELAY,
        help=f"Seconds between requests (default: {DEFAULT_DELAY})",
    )
    parser.add_argument(
        "--limit",
        type=int,
        default=None,
        help="Stop after scraping this many detail pages (useful for testing)",
    )
    parser.add_argument(
        "--force",
        action="store_true",
        help="Re-scrape plans that already have a saved detail file",
    )
    args = parser.parse_args()

    SOURCE_DIR.mkdir(parents=True, exist_ok=True)
    DETAIL_DIR.mkdir(parents=True, exist_ok=True)

    session = _build_session()
    rate = RateLimiter(args.delay)

    # ------------------------------------------------------------------
    # Step 1: fetch index and write index.jsonl
    # ------------------------------------------------------------------
    records = fetch_index(session)

    scraped_at = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
    index_path = SOURCE_DIR / "index.jsonl"
    with index_path.open("w", encoding="utf-8") as f:
        for rec in records:
            # We want to output full 'title' to index.jsonl if possible, 
            # but since we only have truncated title right now, we'll write that as 'title'
            out_rec = {k: v for k, v in rec.items() if k != "title_truncated"}
            out_rec["title"] = rec["title_truncated"]
            f.write(json.dumps({**out_rec, "scraped_at": scraped_at}) + "\n")
    log.info("Wrote %d index records to %s", len(records), index_path)

    # ------------------------------------------------------------------
    # Step 2: scrape each detail page
    # ------------------------------------------------------------------
    if args.limit:
        records = records[: args.limit]

    ok = skipped = failed = 0
    total = len(records)

    for i, rec in enumerate(records, 1):
        result = scrape_detail(session, rate, rec, force=args.force)

        if result is None:
            skipped += 1
            continue

        slug = rec["slug"]
        shard = slug[0].lower() if slug else "_"
        shard_dir = DETAIL_DIR / shard
        shard_dir.mkdir(parents=True, exist_ok=True)
        dest = shard_dir / f"{slug}.json"
        
        try:
            dest.write_text(
                json.dumps(result, indent=2, ensure_ascii=False), encoding="utf-8"
            )
            ok += 1
            log.debug("Saved %s", dest.name)
        except OSError as exc:
            log.warning("Could not write %s: %s", dest, exc)
            failed += 1

        if i % 25 == 0 or i == total:
            log.info(
                "Progress: %d/%d — ok=%d  skipped=%d  failed=%d",
                i,
                total,
                ok,
                skipped,
                failed,
            )

    log.info("Done — ok=%d  skipped=%d  failed=%d", ok, skipped, failed)


if __name__ == "__main__":
    main()