From 437ad585ac539e5bb427e90848c549ff415d6ed9 Mon Sep 17 00:00:00 2001 From: redxef Date: Tue, 14 Nov 2023 15:40:53 +0100 Subject: [PATCH] Add nonce support. --- adapters/wordpress.py | 11 +++++++++++ requirements.txt | 1 + 2 files changed, 12 insertions(+) diff --git a/adapters/wordpress.py b/adapters/wordpress.py index 8278d82..1ebb001 100644 --- a/adapters/wordpress.py +++ b/adapters/wordpress.py @@ -8,6 +8,7 @@ import datetime import logging from schema import Use, Schema +from bs4 import BeautifulSoup from . import utils from .abc import Sink, Adapter @@ -151,7 +152,16 @@ class Wordpress(Sink, Adapter): ) return final_dict + def get_nonce(self): + r = self.session.get( + f'{self.base_url}/wp-admin/admin.php?page=wpbs-calendars&subpage=edit-calendar&calendar_id=1', + ) + soup = BeautifulSoup(r.text, 'html.parser') + nonce = soup.find_all('input', {'id': 'wpbs_token'})[0] + return nonce['value'] + def post_events(self, events, start: datetime.datetime | None=None, until: datetime.timedelta | None=None): + metadata = self.calendar_metadata.to_dict() data = self._generate_data(events, start=start, until=until) update_request = self.session.post( @@ -161,6 +171,7 @@ class Wordpress(Sink, Adapter): 'action': 'wpbs_save_calendar_data', 'form_data': urllib.parse.urlencode(metadata), 'calendar_data': json.dumps(data), + 'wpbs_token': self.get_nonce(), }, ) r = 'wpbs_message=calendar_update_success' in update_request.text diff --git a/requirements.txt b/requirements.txt index b5d8814..d1ba0f5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,4 @@ google-api-python-client google-auth-httplib2 google-auth-oauthlib click +bs4