Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement EMS fetcher #378

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions metadata_fetcher/fetchers/ems_fetcher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import json
from xml.etree import ElementTree
from .Fetcher import Fetcher


class EmsFetcher(Fetcher):
def __init__(self, params):
super(EmsFetcher, self).__init__(params)

# If `next_url` is a param, we know that this is not
# the fetch of the first page, so skip setting those
# attributes
if "next_url" in params:
for key in params:
setattr(self, key, params[key])
return

self.base_url = params.get("harvest_data").get("url")
self.original_url = self.get_current_url()
self.next_url = self.original_url
self.docs_total = 123

def get_current_url(self):
query_params = f"/search/*/objects/xml?filter=approved%3Atrue&page={self.write_page}"
return f"{self.base_url}{query_params}"

def build_fetch_request(self):
request = {"url": self.next_url}

print(
f"[{self.collection_id}]: Fetching page {self.write_page} "
f"at {request.get('url')}")

return request

def get_text_from_response(self, response):
return response.content

def check_page(self, http_resp):
"""
TODO: review other fetchers, do what they do
"""
hits = 345

print(
f"[{self.collection_id}]: Fetched page {self.write_page} "
f"at {http_resp.url} with {hits} hits"
)

return True

def increment(self, http_resp):
super(EmsFetcher, self).increment(http_resp)
tree = ElementTree.fromstring(http_resp.content.encode('utf-8'))
self.docs_total = len(tree.findall("objects/object"))
self.next_url = self.get_current_url() if self.docs_total > 0 else None

def json(self):
current_state = {
"harvest_type": self.harvest_type,
"collection_id": self.collection_id,
"next_url": self.next_url,
"write_page": self.write_page,
"base_url": self.base_url
}

if not self.next_url:
current_state.update({"finished": True})

return json.dumps(current_state)
3 changes: 3 additions & 0 deletions metadata_fetcher/lambda_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@


def import_fetcher(harvest_type):
if harvest_type == "emuseum":
harvest_type = "ems"

fetcher_module = importlib.import_module(
f"fetchers.{harvest_type}_fetcher", package="metadata_fetcher")
fetcher_module_words = harvest_type.split('_')
Expand Down