File size: 6,020 Bytes
6c223ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python
"""Fetch public-domain esoteric/occult/masonic books from Project Gutenberg
via the Gutendex API, strip the boilerplate, and package as JSONL.

Only books with copyright == false (or null) are taken — genuinely public
domain. Uploaded to the HF dataset repo so build_corpus.py consumes them.

Usage:
    python scripts/gutenberg_to_jsonl.py                       # default esoteric topics
    python scripts/gutenberg_to_jsonl.py --topics magic alchemy
"""
import argparse, os, sys, json, re, time, io
import urllib.request
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

HF_REPO = "thefinalboss/fractus-datasets"

# Topics that surface the wanted corpus. Queried against Gutendex.
ESOTERIC_TOPICS = [
    "occultism", "magic", "alchemy", "mysticism", "rosicrucian",
    "hermeticism", "tarot", "theosophy", "kabbalah", "masonry",
    "freemasonry", "gnosticism", "witchcraft", "demonology", "astrology",
    "divination", "spiritualism", "esoteric", "ritual", "symbolism",
]

UA = {"User-Agent": "fractus-dataset/1.0"}


def http_get(url, timeout=120, retries=2):
    last = None
    for _ in range(retries + 1):
        try:
            req = urllib.request.Request(url, headers=UA)
            with urllib.request.urlopen(req, timeout=timeout) as r:
                return r.read()
        except Exception as e:
            last = e
            time.sleep(2)
    raise last


def query_topic(topic, max_pages=4):
    """Yield book dicts for a topic (paginated). Only public-domain."""
    url = f"https://gutendex.com/books/?topic={topic}"
    pages = 0
    seen = set()
    while url and pages < max_pages:
        try:
            data = json.loads(http_get(url, timeout=60).decode("utf-8"))
        except Exception as e:
            print(f"    gutendex page failed: {e}", flush=True)
            break
        for b in data.get("results", []):
            if b["id"] in seen:
                continue
            seen.add(b["id"])
            # copyright: False or None => public domain. True => skip.
            if b.get("copyright") is True:
                continue
            yield b
        url = data.get("next")
        pages += 1
        time.sleep(1)  # be polite to the API


def pick_text_url(formats):
    """Prefer utf-8 plain text; fall back to us-ascii."""
    for key in ("text/plain; charset=utf-8", "text/plain; charset=us-ascii",
                "text/plain"):
        if key in formats:
            return formats[key]
    return None


# Project Gutenberg boilerplate markers (handle older + newer phrasings).
START_RE = re.compile(r"\*\*\*\s*START OF (?:THE|THIS)?\s*PROJECT GUTENBERG.*?\*\*\*",
                      re.IGNORECASE | re.DOTALL)
END_RE = re.compile(r"\*\*\*\s*END OF (?:THE|THIS)?\s*PROJECT GUTENBERG.*?\*\*\*",
                    re.IGNORECASE | re.DOTALL)


def strip_boilerplate(text):
    """Cut to the text between the Gutenberg START/END markers."""
    s = START_RE.search(text)
    e = END_RE.search(text)
    if s and e and e.start() > s.end():
        return text[s.end():e.start()].strip()
    return text.strip()


def main():
    ap = argparse.ArgumentParser(description="Fetch public-domain books from Gutenberg")
    ap.add_argument("--topics", nargs="*", default=ESOTERIC_TOPICS)
    ap.add_argument("--out", default="data/_gutenberg/esoteric.jsonl")
    ap.add_argument("--max-per-topic", type=int, default=60)
    ap.add_argument("--min-chars", type=int, default=5000,
                    help="skip tiny fragments (likely indexes)")
    ap.add_argument("--no-upload", action="store_true")
    args = ap.parse_args()

    os.makedirs(os.path.dirname(args.out) or ".", exist_ok=True)
    print(f"=== Gutenberg fetch: topics={args.topics} ===", flush=True)

    # Collect unique books across all topics.
    books = {}
    for topic in args.topics:
        n = 0
        for b in query_topic(topic):
            if b["id"] not in books:
                books[b["id"]] = b
                n += 1
            if n >= args.max_per_topic:
                break
        print(f"  {topic:<14} -> {len(books)} unique total", flush=True)
    print(f"\n{len(books)} unique public-domain books to fetch.\n", flush=True)

    n_ok, n_skip, total_chars = 0, 0, 0
    with open(args.out, "w", encoding="utf-8") as out:
        for bid, b in books.items():
            url = pick_text_url(b.get("formats", {}))
            if not url:
                n_skip += 1
                continue
            try:
                raw = http_get(url, timeout=180)
                text = raw.decode("utf-8", errors="ignore")
            except Exception as e:
                print(f"  skip #{bid}: {str(e)[:60]}", flush=True)
                n_skip += 1
                continue
            text = strip_boilerplate(text)
            if len(text) < args.min_chars:
                n_skip += 1
                continue
            authors = ", ".join(a.get("name", "?") for a in b.get("authors", []))
            out.write(json.dumps({
                "text": text,
                "title": b.get("title", "?"),
                "author": authors,
                "source": f"gutenberg/{bid}",
            }, ensure_ascii=False) + "\n")
            total_chars += len(text)
            n_ok += 1
            if n_ok % 5 == 0:
                print(f"  {n_ok}/{len(books)} fetched "
                      f"(~{total_chars//4:,} tokens so far)", flush=True)
            time.sleep(0.5)  # politeness

    print(f"\nFetched {n_ok} books ({n_skip} skipped), "
          f"{total_chars:,} chars (~{total_chars//4:,} tokens)", flush=True)

    if not args.no_upload and n_ok:
        from huggingface_hub import HfApi
        api = HfApi()
        api.upload_file(
            path_or_fileobj=args.out,
            path_in_repo="gutenberg/esoteric.jsonl",
            repo_id=HF_REPO, repo_type="dataset")
        print(f"  → uploaded to {HF_REPO}/gutenberg/esoteric.jsonl", flush=True)


if __name__ == "__main__":
    main()