Skip to content

Commit

Permalink
Updates 2024-08-28 - Added more functionalty with search methods
Browse files Browse the repository at this point in the history
  • Loading branch information
CHRISCARLON committed Aug 28, 2024
1 parent 6634fa6 commit 7b710e4
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 13 deletions.
27 changes: 20 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,38 @@

>[!IMPORTANT]
> THIS IS A WORK IN PROGRESS
> Using London's Datastore to test functionality
# Purpose

**The aim of this project is simple, create a basic python library to explore and interact with the UK's open data catalogues**.

## Examples of UK Open Data Catalogues

1. [Bristol Open Data](https://opendata.bristol.gov.uk)
2. [London Datastore](https://data.london.gov.uk)
3. [Data Mill North](https://datamillnorth.org)
4. [Gov Open Data](https://www.data.gov.uk)
| Catalogue Name | Website | Catalogue API Endpoint |
|----------------|---------|-------------------|
| Bristol Open Data | https://opendata.bristol.gov.uk | TBC |
| London Datastore | https://data.london.gov.uk | CKAN: https://data.london.gov.uk/api/3/ |
| Data Mill North | https://datamillnorth.org | TBC |
| Gov Open Data | https://www.data.gov.uk | TBC |

## Basic usage examples:

```python
# Example usage
# Example usage 1
if __name__ == "__main__":
with CatSession("data.london.gov.uk") as session:
explore = CatExplorer(session)
v = explore.package_search_json(search_query="census")
pprint(v)
census_package = explore.package_search_json(search_query="census")
pprint(census_package)
```

```python
# Example usage 2
if __name__ == "__main__":
with CatSession("data.london.gov.uk") as session:
explore = CatExplorer(session)
packlage_list = explore.package_list_json()
boundary_info = explore.package_show_info_json('2011-boundary-files')
pprint(show_info)
```
2 changes: 1 addition & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[pytest]
pythonpath = .
pythonpath = src/
addopts = -v
1 change: 1 addition & 0 deletions src/api_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class CkanApiPaths:
BASE_PATH = "/api/3/action/{}"
PACKAGE_LIST = BASE_PATH.format("package_list")
PACKAGE_SEARCH = BASE_PATH.format("package_search")
PACKAGE_INFO = BASE_PATH.format("package_show")
# Add more paths as needed...


Expand Down
28 changes: 23 additions & 5 deletions src/herding_cats.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from enum import Enum
from urllib.parse import urlencode

from .api_endpoints import CkanApiPaths
from .cats_errors import CatExplorerError, CatSessionError
from api_endpoints import CkanApiPaths
from cats_errors import CatExplorerError, CatSessionError

class CatSession:
def __init__(self, domain: str) -> None:
Expand Down Expand Up @@ -93,7 +93,24 @@ def package_search_json(self, search_query: Optional[str]=None):
params["q"] = search_query

url = f"{base_url}?{urlencode(params)}" if params else base_url
print(url)

try:
response = self.cat_session.session.get(url)
response.raise_for_status()
data = response.json()
return data['result']
except requests.RequestException as e:
logger.error(f"Failed to search datasets: {e}")
raise CatExplorerError(f"Failed to search datasets: {str(e)}")

def package_show_info_json(self, package_name:str):
base_url = self.cat_session.base_url + CkanApiPaths.PACKAGE_INFO

params = {}
if package_name:
params["id"] = package_name

url = f"{base_url}?{urlencode(params)}" if params else base_url

try:
response = self.cat_session.session.get(url)
Expand All @@ -119,5 +136,6 @@ def get_package_count(self) -> int:
if __name__ == "__main__":
with CatSession("data.london.gov.uk") as session:
explore = CatExplorer(session)
v = explore.package_search_json(search_query="census")
pprint(v)
show_list = explore.package_list_json()
show_info = explore.package_show_info_json('2011-boundary-files')
pprint(show_info)

0 comments on commit 7b710e4

Please sign in to comment.