Skip to content

Commit

Permalink
Fix errors and update logging info
Browse files Browse the repository at this point in the history
- Create an exception when no records in CKAN
- Update exception when a CKAN metadata is wrongly converted 
- Add info about the PYCSW Endpoint
  • Loading branch information
mjanez authored Apr 12, 2023
1 parent 3b58539 commit 95500a7
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions ckan2pycsw/ckan2pycsw.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@


URL = os.environ["CKAN_URL"]
PYCSW_URL = os.environ["PYCSW_URL"]
APP_DIR = os.environ["APP_DIR"]
CKAN_API = "api/3/action/package_search"
PYCSW_CKAN_SCHEMA = os.environ["PYCSW_CKAN_SCHEMA"]
Expand All @@ -37,17 +38,20 @@
}

def get_datasets(base_url):
if not base_url.endswith('/'):
base_url += '/'
package_search = urljoin(base_url, CKAN_API)
res = requests.get(package_search, params={"rows": 0})
end = res.json()["result"]["count"]
rows = 10
for start in range(0, end, rows):
res = requests.get(package_search, params={"start": start, "rows": rows})
for dataset in res.json()["result"]["results"]:
if dataset["type"] == "dataset":
yield dataset
try:
if not base_url.endswith('/'):
base_url += '/'
package_search = urljoin(base_url, CKAN_API)
res = requests.get(package_search, params={"rows": 0})
end = res.json()["result"]["count"]
rows = 10
for start in range(0, end, rows):
res = requests.get(package_search, params={"start": start, "rows": rows})
for dataset in res.json()["result"]["results"]:
if dataset["type"] == "dataset":
yield dataset
except Exception as e:
logging.error(f"{log_module}:ckan2pycsw | No metadata in CKAN: {base_url} | Error: {e}")

def main():
log_file(APP_DIR + "/log")
Expand Down Expand Up @@ -102,9 +106,11 @@ def main():
record = metadata.parse_record(context, xml_string, repo)[0]
repo.insert(record, "local", util.get_today_and_now())
except Exception as e:
logging.error(f"{log_module}:ckan2pycsw | Complete metadata in CKAN for: {dataset['name']} [DCAT Type: {d_dcat_type.capitalize()}] Error: {e}")
logging.error(f"{log_module}:ckan2pycsw | Fail when transform record from CKAN for: {dataset['name']} [DCAT Type: {d_dcat_type.capitalize()}] Error: {e}")
continue

logging.info(f"{log_module}:ckan2pycsw | Create a CSW Endpoint at: {PYCSW_URL}")

# Export records to Folder
pycsw.core.admin.export_records(
context,
Expand Down

0 comments on commit 95500a7

Please sign in to comment.