| 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 |
import re |
| 26 |
import sys |
| 27 |
import urllib.parse |
| 28 |
import urllib.request |
| 29 |
from pathlib import Path |
| 30 |
|
| 31 |
DEST = Path(__file__).resolve().parent / "downloads" |
| 32 |
|
| 33 |
UA = {"User-Agent": "Mozilla/5.0 (x16-emulator test reference fetcher)"} |
| 34 |
|
| 35 |
PDFS = { |
| 36 |
|
| 37 |
"w65c816s.pdf": "https://www.westerndesigncenter.com/wdc/documentation/w65c816s.pdf", |
| 38 |
"w65c02s.pdf": "https://www.westerndesigncenter.com/wdc/documentation/w65c02s.pdf", |
| 39 |
|
| 40 |
|
| 41 |
"w65c22s.pdf": "https://www.westerndesigncenter.com/wdc/documentation/w65c22s.pdf", |
| 42 |
} |
| 43 |
|
| 44 |
PAGES = { |
| 45 |
"6502org-65c816-opcodes.txt": "http://6502.org/tutorials/65c816opcodes.html", |
| 46 |
"6502org-65c02-opcodes.txt": "http://6502.org/tutorials/65c02opcodes.html", |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
DOCS_COMMIT = "79a8303bc33cda4ec3e85f3239f9b3d4c8c76260" |
| 59 |
|
| 60 |
DOCS = { |
| 61 |
"x16-memory-map.md": "X16 Reference - 08 - Memory Map.md", |
| 62 |
"x16-hardware.md": "X16 Reference - 14 - Hardware.md", |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
def fetch(url, path): |
| 67 |
if path.exists(): |
| 68 |
print(f" have {path.name}") |
| 69 |
return True |
| 70 |
print(f" get {path.name}") |
| 71 |
try: |
| 72 |
req = urllib.request.Request(url, headers=UA) |
| 73 |
with urllib.request.urlopen(req, timeout=120) as r: |
| 74 |
path.write_bytes(r.read()) |
| 75 |
return True |
| 76 |
except Exception as exc: |
| 77 |
print(f" FAIL {path.name}: {exc}") |
| 78 |
return False |
| 79 |
|
| 80 |
|
| 81 |
def html_to_text(path): |
| 82 |
raw = path.read_bytes().decode("utf-8", "replace") |
| 83 |
raw = re.sub(r"(?is)<(script|style).*?</\1>", "", raw) |
| 84 |
raw = re.sub(r"(?i)<br\s*/?>", "\n", raw) |
| 85 |
raw = re.sub(r"(?i)</(p|div|tr|h\d|li)>", "\n", raw) |
| 86 |
text = re.sub(r"(?s)<[^>]+>", "", raw) |
| 87 |
for a, b in (("<", "<"), (">", ">"), ("&", "&"), (" ", " ")): |
| 88 |
text = text.replace(a, b) |
| 89 |
return re.sub(r"\n{3,}", "\n\n", text) |
| 90 |
|
| 91 |
|
| 92 |
def pdf_to_text(pdf, out): |
| 93 |
try: |
| 94 |
import pypdf |
| 95 |
except ImportError: |
| 96 |
print(f" skip {out.name} (pip install pypdf to enable)") |
| 97 |
return |
| 98 |
reader = pypdf.PdfReader(str(pdf)) |
| 99 |
with out.open("w", encoding="utf-8") as fh: |
| 100 |
for i, page in enumerate(reader.pages, 1): |
| 101 |
|
| 102 |
|
| 103 |
fh.write(f"\n===== page {i} =====\n") |
| 104 |
fh.write(page.extract_text() or "") |
| 105 |
print(f" made {out.name} ({len(reader.pages)} pages)") |
| 106 |
|
| 107 |
|
| 108 |
def main(): |
| 109 |
DEST.mkdir(parents=True, exist_ok=True) |
| 110 |
ok = True |
| 111 |
|
| 112 |
print("WDC datasheets:") |
| 113 |
for name, url in PDFS.items(): |
| 114 |
path = DEST / name |
| 115 |
if fetch(url, path): |
| 116 |
pdf_to_text(path, path.with_suffix(".txt")) |
| 117 |
else: |
| 118 |
ok = False |
| 119 |
|
| 120 |
print("6502.org tutorials:") |
| 121 |
for name, url in PAGES.items(): |
| 122 |
raw = DEST / (name + ".html") |
| 123 |
if fetch(url, raw): |
| 124 |
(DEST / name).write_text(html_to_text(raw), encoding="utf-8") |
| 125 |
raw.unlink() |
| 126 |
print(f" made {name}") |
| 127 |
else: |
| 128 |
ok = False |
| 129 |
|
| 130 |
print("X16 documentation:") |
| 131 |
base = f"https://raw.githubusercontent.com/X16Community/x16-docs/{DOCS_COMMIT}" |
| 132 |
for name, path in DOCS.items(): |
| 133 |
quoted = urllib.parse.quote(path) |
| 134 |
if not fetch(f"{base}/{quoted}", DEST / name): |
| 135 |
ok = False |
| 136 |
|
| 137 |
print(f"\nreferences in {DEST}") |
| 138 |
return 0 if ok else 1 |
| 139 |
|
| 140 |
|
| 141 |
if __name__ == "__main__": |
| 142 |
sys.exit(main()) |