From 4bf2018f2024451231ebb43da48cb7c04fe77df6 Mon Sep 17 00:00:00 2001 From: Eugeniu Plamadeala Date: Mon, 11 Apr 2022 00:57:54 -0700 Subject: [PATCH] [paypal] Update parsing because Paypal changed website. * another attribute parsed for CSRF token * transactions list and transactions detail schemas needed minor updates --- finance_dl/paypal.py | 48 +++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/finance_dl/paypal.py b/finance_dl/paypal.py index 1f3ae11..d4aead6 100644 --- a/finance_dl/paypal.py +++ b/finance_dl/paypal.py @@ -68,24 +68,30 @@ 'properties': { 'data': { 'type': 'object', - 'required': ['activity'], + 'required': ['data'], 'properties': { - 'activity': { + 'data': { 'type': 'object', - 'required': ['transactions'], + 'required': ['activity'], 'properties': { - 'transactions': { - 'type': 'array', - 'items': { - 'type': 'object', - 'required': ['id'], - 'properties': { - 'id': { - 'type': 'string', - 'pattern': r'^[A-Za-z0-9\-]+$', - }, + 'activity': { + 'type': 'object', + 'required': ['transactions'], + 'properties': { + 'transactions': { + 'type': 'array', + 'items': { + 'type': 'object', + 'required': ['id'], + 'properties': { + 'id': { + 'type': 'string', + 'pattern': r'^[A-Za-z0-9\-]+$', + }, + }, + } }, - } + }, }, }, }, @@ -102,9 +108,9 @@ 'properties': { 'data': { 'type': 'object', - 'required': ['details'], + 'required': ['amount'], 'properties': { - 'details': { + 'amount': { 'type': 'object', }, }, @@ -168,9 +174,9 @@ def get_csrf_token(self): logging.info('Getting CSRF token') self.driver.get('https://www.paypal.com/myaccount/transactions/') # Get CSRF token - body_element, = self.wait_and_locate((By.XPATH, - '//body[@data-token!=""]')) - self.csrf_token = body_element.get_attribute('data-token') + body_element, = self.wait_and_locate((By.ID, "__react_data__")) + attribute_object = json.loads(body_element.get_attribute("data")) + self.csrf_token = attribute_object["_csrf"] return self.csrf_token def get_transaction_list(self): @@ -188,7 +194,7 @@ def get_transaction_list(self): resp.raise_for_status() j = resp.json() jsonschema.validate(j, transaction_list_schema) - return j['data']['activity']['transactions'] + return j['data']['data']['activity']['transactions'] def save_transactions(self): transaction_list = self.get_transaction_list() @@ -243,7 +249,7 @@ def save_transactions(self): jsonschema.validate(j, transaction_details_schema) with atomic_write(json_path, mode='wb', overwrite=True) as f: f.write( - json.dumps(j['data']['details'], indent=' ').encode()) + json.dumps(j['data'], indent=' ', sort_keys=True).encode()) def run(self): if not os.path.exists(self.output_directory):