Skip to content
This repository has been archived by the owner on Oct 28, 2024. It is now read-only.

Commit

Permalink
Add map URL and contacts and stats
Browse files Browse the repository at this point in the history
  • Loading branch information
ahosgood committed Jan 18, 2024
1 parent d6cb3ab commit 55274b2
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 6 deletions.
4 changes: 4 additions & 0 deletions app/lib/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class GetAPI(BaseAPI):
results_per_page: int = 20
params: dict = {}

def __init__(self):
super().__init__()
self.params = {}

def add_parameter(self, key: str, value: str | int) -> None:
self.params[key] = value

Expand Down
6 changes: 5 additions & 1 deletion app/records/routes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from app.records import router
from app.schemas import Filter
from app.sources.rosetta import RosettaRecordDetails, RosettaRecordsSearch
from app.sources.rosetta import (
RosettaRecordDetails,
RosettaRecordsSearch,
RosettaRecordsSearchStats,
)

from .schemas import Record, RecordArchive, RecordCreator, RecordSearchResults

Expand Down
1 change: 1 addition & 0 deletions app/records/schemas/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ class RecordSearchResults(APISearchResponse):
model_config = ConfigDict(arbitrary_types_allowed=True)

results: list[RecordSearchResult] = []
results_stats: dict = {}
6 changes: 5 additions & 1 deletion app/sources/rosetta/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
from .api import RosettaRecordDetails, RosettaRecordsSearch
from .api import (
RosettaRecordDetails,
RosettaRecordsSearch,
RosettaRecordsSearchStats,
)
38 changes: 35 additions & 3 deletions app/sources/rosetta/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
RecordSearchResults,
)
from config import Config
from pydash import objects

from .lib import RosettaResponseParser, RosettaSourceParser

Expand All @@ -34,9 +35,15 @@ def get_result(
self.add_parameter("from", offset)
url = self.build_url()
raw_results = self.execute(url)
return self.parse_results(raw_results, page, url)
results = self.parse_results(raw_results, page, url)
stats_api = RosettaRecordsSearchStats(self.params["q"])
results_stats = stats_api.get_result()
results.results_stats = results_stats
return results.toJSON() if results.page_in_range() else {}

def parse_results(self, raw_results, page, source_url) -> dict:
def parse_results(
self, raw_results, page, source_url
) -> RecordSearchResults:
response = RecordSearchResults()
response.source_url = source_url
for r in raw_results["metadata"]:
Expand Down Expand Up @@ -69,7 +76,32 @@ def parse_results(self, raw_results, page, source_url) -> dict:
)
response.results_per_page = self.results_per_page
response.page = page
return response.toJSON() if response.page_in_range() else {}
return response


class RosettaRecordsSearchStats(RosettaRecords):
def __init__(self, query_string):
super().__init__()
self.api_path = "/search"
self.add_parameter("q", query_string)

def get_result(self) -> dict:
stats = {
"tna": None,
"digitised": None,
"nonTna": None,
"creator": None,
"archive": None,
}
for group in stats:
self.add_parameter("filter", f"group:({group})")
url = self.build_url()
print(url)
raw_results = self.execute(url)
print(url)
results_count = objects.get(raw_results, "stats.total")
stats[group] = results_count
return stats


class RosettaRecordDetails(RosettaRecords):
Expand Down
18 changes: 17 additions & 1 deletion app/sources/rosetta/lib/response_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,11 +340,18 @@ def contact_info(self) -> dict:
),
None,
):
ephemera = (
ephemera.replace("mapURL", "mapurl")
.replace("jobTitle", "jobtitle")
.replace("firstName", "firstname")
.replace("lastName", "lastname")
)
document = PyQuery(ephemera)
return {
"address_line_1": document("addressline1")
.text()
.replace("<br /><br />", ", "),
.replace("<br /><br />", ", ")
or None,
"town": document("addresstown").text() or None,
"postcode": document("postcode").text() or None,
"country": document("addresscountry").text() or None,
Expand All @@ -353,6 +360,15 @@ def contact_info(self) -> dict:
"phone": document("telephone").text() or None,
"fax": document("fax").text() or None,
"email": document("email").text() or None,
"contacts": [
{
"first_name": contact.find("firstname").text
or None,
"last_name": contact.find("lastname").text or None,
"job_title": contact.find("jobtitle").text or None,
}
for contact in document("contact")
],
}
return {}

Expand Down

0 comments on commit 55274b2

Please sign in to comment.