From b94653cea3c9656a24fccf2c60c0400da35e69f7 Mon Sep 17 00:00:00 2001
From: vuillaut <thomas.vuillaume@gmail.com>
Date: Fri, 10 Sep 2021 16:19:21 +0200
Subject: [PATCH] building upon pyzenodo3 to search the OSSR and get records

---
 eossr/api/__init__.py       | 83 +++++++++++++++++++++++++++++++++++++
 eossr/api/tests/test_api.py |  0
 2 files changed, 83 insertions(+)
 create mode 100644 eossr/api/tests/test_api.py

diff --git a/eossr/api/__init__.py b/eossr/api/__init__.py
index e69de29b..0d3cf212 100644
--- a/eossr/api/__init__.py
+++ b/eossr/api/__init__.py
@@ -0,0 +1,83 @@
+from pyzenodo3 import Record, Zenodo
+import requests
+
+__all__ = [
+    'Ossr',
+    'pprint_record',
+    'get_all_ossr_records'
+]
+
+escape_community = 'escape2020'
+
+
+class Ossr(Zenodo):
+
+    def __init__(self):
+        super().__init__()
+
+    def search(self, search: str, **kwargs) -> list[Record]:
+        """
+        Search the ESCAPE OSSR
+
+        :param search: string
+        :param kwargs: Zenodo query arguments.
+        For an exhaustive list, see the query arguments at https://developers.zenodo.org/#list36
+        Common arguments are:
+            - size: int
+                Number of results to return
+            - all_versions: int
+                Show (1) or hide (0) all versions of records
+            - type: string
+                Records of the specified type (Publication, Poster, Presentation, Software, ...)
+            - keywords: string
+                Records with the specified keywords
+
+        :return:
+        list of `pyzenodo3.Record`
+        """
+        search = search.replace("/", " ")  # zenodo can't handle '/' in search query
+
+        # if another community is specified, a logical OR is apply be zenodo API,
+        # thus potentially finding entries that are not part of escap2020
+        # ruling out that possibility at the moment
+        kwargs['communities'] = escape_community
+        params = {
+            'q': search,
+            **kwargs
+        }
+
+        recs = self._get_records(params)
+
+        if not recs:
+            raise LookupError(f"No records found for search {search}")
+
+        return recs
+
+
+
+def pprint_record(record: Record):
+    """
+    Pretty print of a record
+
+    :param record: `pyzenodo3.Record`
+    """
+    entry_id = record.data['id']
+    metadata = record.data['metadata']
+    print(f"=== Record #{entry_id} : {metadata['title']} ===")
+    print(metadata['description'])
+    print('\n')
+
+
+
+def get_all_ossr_records():
+    """
+    Retrieve all OSSR entries.
+    Potentially long when the number of entries in the OSSR increases.
+
+    :return: list[pyzenodo3.Record]
+    """
+    ossr = Ossr()
+    r = requests.get(ossr.base_url+'/records', params={'communities': escape_community})
+    number_of_ossr_entries = r.json()['aggregations']['access_right']['buckets'][0]['doc_count']
+
+    return ossr.search('', size=number_of_ossr_entries)
diff --git a/eossr/api/tests/test_api.py b/eossr/api/tests/test_api.py
new file mode 100644
index 00000000..e69de29b
-- 
GitLab