Skip to content

Commit 48f43fe

Browse files
committed
Simplify logic
1 parent e7444cd commit 48f43fe

2 files changed

Lines changed: 19 additions & 15 deletions

File tree

src/hdx/api/utilities/url_utils.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ def follow_url(
6767
# Is it a CKAN resource? (Assumes the v.3 API for now)
6868
result = CKAN_URL.match(url)
6969
if result:
70-
urls = _get_ckan_urls(result.group(2), result.group(3))
71-
if urls:
72-
return urls[0]
70+
result = _get_ckan_urls(result.group(2), result.group(3))
71+
if result:
72+
return result
7373

7474
# Is it a Google Drive "open" URL?
7575
result = GOOGLE_DRIVE_URL.match(url)
@@ -106,7 +106,7 @@ def follow_url(
106106
return url
107107

108108

109-
def _get_ckan_urls(dataset_id: str, resource_id: str) -> list[str]:
109+
def _get_ckan_urls(dataset_id: str, resource_id: str) -> str | None:
110110
"""Look up a CKAN download URL starting from a dataset or resource page
111111
112112
If the link is to a dataset page, try the first resource. If it's
@@ -120,16 +120,16 @@ def _get_ckan_urls(dataset_id: str, resource_id: str) -> list[str]:
120120
121121
Returns:
122122
The direct-download URL for the CKAN dataset
123-
124123
"""
125-
result_urls = []
126124
if resource_id:
127125
resource = hdx.data.resource.Resource.read_from_hdx(resource_id)
128126
if resource:
129-
result_urls.append(resource["url"])
130-
else:
131-
dataset = hdx.data.dataset.Dataset.read_from_hdx(dataset_id)
132-
for resource in dataset.get_resources():
133-
if url := resource.get("url"):
134-
result_urls.append(url)
135-
return result_urls
127+
return resource["url"]
128+
129+
dataset = hdx.data.dataset.Dataset.read_from_hdx(dataset_id)
130+
if not dataset:
131+
return None
132+
for resource in dataset.get_resources():
133+
if url := resource.get("url"):
134+
return url
135+
return None

src/hdx/data/dataset.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,13 @@ def save_to_json(
215215
"""
216216
dataset_dict = self.get_dataset_dict()
217217
if follow_urls:
218-
session = hdx.api.utilities.url_utils.get_ckan_ready_session(self.configuration)
218+
session = hdx.api.utilities.url_utils.get_ckan_ready_session(
219+
self.configuration
220+
)
219221
for resource in dataset_dict.get("resources", tuple()):
220-
resource["url"] = hdx.api.utilities.url_utils.follow_url(resource["url"], session=session)
222+
resource["url"] = hdx.api.utilities.url_utils.follow_url(
223+
resource["url"], session=session
224+
)
221225
save_json(dataset_dict, path)
222226

223227
@staticmethod

0 commit comments

Comments
 (0)