From 5f5377feb43bd78469709fdbfc35e0ec903ad1ca Mon Sep 17 00:00:00 2001 From: csae8092 Date: Thu, 5 May 2022 09:49:38 +0200 Subject: [PATCH] added new class for dumping persons --- freud_api_crawler/entities.py | 46 +++++++++++++++++++++++++++++++++ tests/test_freud_api_crawler.py | 6 +++++ 2 files changed, 52 insertions(+) create mode 100644 freud_api_crawler/entities.py diff --git a/freud_api_crawler/entities.py b/freud_api_crawler/entities.py new file mode 100644 index 0000000..e0398c1 --- /dev/null +++ b/freud_api_crawler/entities.py @@ -0,0 +1,46 @@ +import requests +import freud_api_crawler.freud_api_crawler as frd +from freud_api_crawler.string_utils import always_https + + +class FrdPerson(frd.FrdClient): + + """class to interact with person endpoint""" + + def __init__(self, **kwargs): + super().__init__(**kwargs) + self.person_ep = f"{self.endpoint}taxonomy_term/personen" + + def yield_persons(self): + """ iterates through person endpoint and yields dict with some data """ + counter = 1 + url = self.person_ep + next_page = True + while next_page: + print(url) + response = None + result = None + x = None + response = requests.get( + url, + cookies=self.cookie, + allow_redirects=True + ) + result = response.json() + links = result['links'] + if links.get('next', False): + orig_url = links['next']['href'] + url = always_https(orig_url) + else: + next_page = False + for x in result['data']: + item = {} + item['id'] = f"frd_person_{counter:05}" + item['drupal_hash'] = x['id'] + for y in [ + 'drupal_internal__tid', + 'name', + ]: + item[y] = x['attributes'][y] + counter += 1 + yield(item) diff --git a/tests/test_freud_api_crawler.py b/tests/test_freud_api_crawler.py index 7a4552e..e33f372 100644 --- a/tests/test_freud_api_crawler.py +++ b/tests/test_freud_api_crawler.py @@ -10,6 +10,7 @@ from freud_api_crawler import freud_api_crawler as frd from freud_api_crawler import string_utils from freud_api_crawler import tei_utils +from freud_api_crawler.entities import FrdPerson WERK_ID = "9d035a03-28d7-4013-adaf-63337d78ece4" @@ -55,6 +56,11 @@ def setUp(self): def tearDown(self): """Tear down test fixtures, if any.""" + def test_001__persons(self): + persons = FrdPerson(auth_items=frd.AUTH_ITEMS) + first_person = persons.yield_persons().__next__() + self.assertTrue(first_person['id'][-1] == '1') + def test_002_endpoints_with_auth(self): """Test of endpoints-method""" frd_obj = frd.FrdClient(auth_items=frd.AUTH_ITEMS)