diff --git a/sru/almasru.py b/sru/almasru.py index 9bdadfa2756cd558ddd529ff42cdb4ea1fcc09dc..abd56e50645780dc391d12d6db4f22ebb384acd2 100644 --- a/sru/almasru.py +++ b/sru/almasru.py @@ -18,7 +18,7 @@ information: Possible formats are documented here: https://developers.exlibrisgroup.com/alma/integrations/SRU/ -The Library of Congrass has general information on SRU: +The Library of Congress has general information on SRU: http://www.loc.gov/standards/sru/ """ @@ -108,14 +108,7 @@ class RecordRetriever: first_page = self._get_first_page(alma_query) num_recs = self._get_num_records(first_page) - xml_list = [] - - for startrecord in range(1, int(num_recs), 10): - sru_url = self._create_url(alma_query, startrecord) - sru_xml = self._response_to_xml(sru_url) - self._raise_error_sru(sru_xml) - xml_list.extend(self._extract_marc_xml(sru_xml)) - + xml_list = self._get_xml_list(alma_query, num_recs) return xml_list def _get_first_page(self, alma_query): @@ -131,45 +124,52 @@ class RecordRetriever: num_recs = int(num_recs_element.text) return num_recs - def _create_url(self, alma_query: str, startrecord: int) -> str: + def _get_xml_list(self, alma_query: str, num_recs: int): + xml_list = [] + for xml in self._gen_xml(alma_query, num_recs): + xml_list.extend(self._extract_marc_xml(xml)) + return xml_list + def _gen_xml(self, alma_query: str, num_recs: int): + print(num_recs) + for startrecord in range(1, num_recs+1, 10): + print(startrecord) + sru_url = self._create_url(alma_query, startrecord) + sru_xml = self._response_to_xml(sru_url) + self._raise_error_sru(sru_xml) + yield sru_xml + + def _create_url(self, alma_query: str, startrecord: int) -> str: self.url_query['startRecord'] = str(startrecord) self.url_query['maximumRecords'] = '10' self.url_query['query'] = alma_query url_query_string = parse.urlencode(self.url_query) sru_url = self.url_base + url_query_string - return sru_url @staticmethod def _response_to_xml(url: str) -> Element: - response = request.urlopen(url).read() xml = fromstring(response) - return xml @staticmethod def _raise_error_unique(num_recs: int): - if num_recs > 1: raise NotUnique('More than one record found while exactly one was expected.') @staticmethod def _raise_error_no_match(num_recs_element: Element): - if (num_recs_element is not None and num_recs_element.text == '0'): raise NoRecord('No matching records found.') @staticmethod def _raise_error_too_many_recs(num_recs_element: Element): - if int(num_recs_element.text) > 10000: raise TooManyRecords('SRU searches are limited to 10.000 records.') def _raise_error_sru(self, sru_xml: Element): - if sru_xml.find("srw:diagnostics", self.namespaces): xpath = "srw:diagnostics/diag:diagnostic/diag:message" sru_message = sru_xml.find(xpath, self.namespaces).text