Skip to content

Commit ba13092

Browse files
committed
Port to run as import process
Remove main methods Port from lxml to xml Rename for clarity
1 parent 7521277 commit ba13092

2 files changed

Lines changed: 18 additions & 60 deletions

File tree

coding_systems/icd10/scrape.py

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
import traceback
55
from collections import defaultdict
66
from pathlib import Path
7+
from tempfile import TemporaryDirectory
78
from urllib import parse
89

910
import requests as rq
1011
from bs4 import BeautifulSoup, element
1112

1213

1314
BASE_URL = "https://classbrowser.nhs.uk/"
14-
BASE_PATH = Path("icdscrape")
15-
1615
errors = {}
1716

1817

@@ -50,7 +49,15 @@ def get_block_urls(menu_json_path):
5049
)
5150

5251

53-
def get_menu_chapters(menu):
52+
def get_menu_chapters(menu, html_dir):
53+
def fetch_html(url, force_download=False):
54+
htmls_dir = html_dir / "html_cache"
55+
htmls_dir.mkdir(parents=True, exist_ok=True)
56+
file_path = htmls_dir / os.path.split(parse.urlparse(url).path)[-1]
57+
if force_download or not file_path.exists():
58+
download_stream(url, file_path)
59+
return file_path
60+
5461
def split_text(text):
5562
return text.split(": ")
5663

@@ -134,15 +141,6 @@ def get_blocks(chapter):
134141
)
135142

136143

137-
def fetch_html(url, force_download=False):
138-
htmls_dir = BASE_PATH / "html_cache"
139-
htmls_dir.mkdir(parents=True, exist_ok=True)
140-
file_path = htmls_dir / os.path.split(parse.urlparse(url).path)[-1]
141-
if force_download or not file_path.exists():
142-
download_stream(url, file_path)
143-
return file_path
144-
145-
146144
def label_text(elem):
147145
label = " ".join(
148146
"".join(
@@ -399,18 +397,6 @@ def get_html_body(html_path):
399397

400398
def scrape():
401399
menu = fetch_menu_json()
402-
chapters = {k: v for k, v in get_menu_chapters(menu)}
403-
404-
json.dump(
405-
chapters, (BASE_PATH / "chapters.json").open("w"), indent=4, ensure_ascii=False
406-
)
407-
json.dump(
408-
errors, (BASE_PATH / "errors.json").open("w"), indent=4, ensure_ascii=False
409-
)
410-
411-
if errors:
412-
print("Errors found, check errors.json")
413-
414-
415-
if __name__ == "__main__":
416-
scrape()
400+
with TemporaryDirectory() as temp:
401+
chapters = {k: v for k, v in get_menu_chapters(menu, Path(temp))}
402+
return chapters
Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
import argparse
2-
import json
31
import re
2+
import xml.etree.ElementTree as etree
43
from collections import defaultdict
54
from dataclasses import dataclass, field
65
from enum import Enum, EnumMeta, auto
76
from itertools import count
87
from pathlib import Path
98

10-
from lxml import etree
11-
129

1310
JSON_PATH = Path("icdscrape/chapters.json")
1411
CLAML_PATH = Path("icdscrape/chapters.xml")
@@ -416,11 +413,8 @@ def append_subclasses():
416413
_class.subclasses.append(SubClass(code=child.code))
417414

418415

419-
def main(json_path, claml_path):
420-
chapters = json.load(json_path.open())
421-
claml = etree.fromstring(
422-
CLAML_HEADER, parser=etree.XMLParser(remove_blank_text=True)
423-
)
416+
def convert_chapters_to_claml(chapters, claml_path):
417+
claml = etree.fromstring(CLAML_HEADER)
424418
claml.append(ClassKind.to_element())
425419
claml.append(UsageKind.to_element())
426420
claml.append(RubricKind.to_element())
@@ -433,27 +427,5 @@ def main(json_path, claml_path):
433427
for _class in classes_to_write:
434428
claml.append(_class.to_element())
435429
xml = etree.ElementTree(claml)
436-
xml.write(claml_path, pretty_print=True, xml_declaration=True, encoding="UTF-8")
437-
438-
439-
if __name__ == "__main__":
440-
parser = argparse.ArgumentParser(
441-
prog="json_to_claml",
442-
description="converts scraped chapters.json to ClaML format",
443-
)
444-
parser.add_argument(
445-
"json_path",
446-
nargs="?",
447-
default=JSON_PATH,
448-
help="Path to input json file",
449-
type=Path,
450-
)
451-
parser.add_argument(
452-
"claml_path",
453-
nargs="?",
454-
default=CLAML_PATH,
455-
help="Output path for ClaML file",
456-
type=Path,
457-
)
458-
args = parser.parse_args()
459-
main(args.json_path, args.claml_path)
430+
etree.indent(xml, space="\t", level=0)
431+
xml.write(claml_path, xml_declaration=True, encoding="UTF-8")

0 commit comments

Comments
 (0)