Skip to content

Commit 76a4d2e

Browse files
authored
Merge pull request #2943 from opensafely-core/Jongmassey/fix-bnf-dmd-mapping-scraper
Fix link html parsing for Sept 2025 release
2 parents b63a085 + b67c424 commit 76a4d2e

3 files changed

Lines changed: 13 additions & 2 deletions

File tree

mappings/bnfdmd/data_downloader.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ def get_releases(self):
4747
valid_from := datepat.search(inner_text.replace("\xa0", " "))
4848
):
4949
# sometimes "valid from" inner text is across adjacent elements
50-
inner_text += a_tag.next_sibling()[0].text
50+
if a_tag.next_sibling:
51+
inner_text += a_tag.next_sibling.text
52+
elif a_tag.previous_sibling:
53+
inner_text = a_tag.previous_sibling.text + inner_text
5154
valid_from = datetime.strptime(valid_from.group(), datefmt)
5255
matches.append((match.group("date"), url, filename, valid_from))
5356

mappings/bnfdmd/tests/conftest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ def mocked_responses_homepage():
3939
"https://www.nhsbsa.nhs.uk/prescription-data/understanding-our-data/bnf-snomed-mapping",
4040
body=f"""
4141
<p><a href="/sites/default/files/{MOCK_FILEPATH}">January 2024 (ZIP file: 16.76MB)</a></p>
42-
<p><a href="/sites/default/files/2023-01/BNF%20Snomed%20Mapping%20data%2020230116.zip">January 2023 (ZIP file: 16.76MB)</a></p>
42+
<p><a href="/sites/default/files/2023-01/BNF%20Snomed%20Mapping%20data%2020230116.zip">January</a> 2023 (ZIP file: 16.76MB)</p>
43+
<p>September<a href="/sites/default/files/2023-11/BNF%20Snomed%20Mapping%20data%2020231120.zip"> 2023 (ZIP file: 18.77MB)</a></p>
4344
""",
4445
status=200,
4546
)

mappings/bnfdmd/tests/test_data_downloader.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,10 @@ def test_get_latest_release_with_existing(mocked_responses_homepage, tmp_path):
1717
downloader = Downloader(tmp_path)
1818
with pytest.raises(ValueError, match="Latest release already exists"):
1919
downloader.download_latest_release()
20+
21+
22+
def test_historical_release_dates_parsed(mocked_responses_homepage, tmp_path):
23+
downloader = Downloader(tmp_path)
24+
releases = downloader.get_releases()
25+
26+
assert len(releases) == 3

0 commit comments

Comments
 (0)