From d08a4d6a2eb620d874a313ccdf0bbe58e15d2b65 Mon Sep 17 00:00:00 2001 From: Faisal Shahzad <84210709+seowings@users.noreply.github.com> Date: Fri, 6 Oct 2023 23:19:27 +0200 Subject: [PATCH] fix for redirection plugin error #39 --- src/staticwordpress/core/errors.py | 33 ++++++++++++++++++++ src/staticwordpress/core/redirects.py | 45 +++++++++++++++++---------- 2 files changed, 61 insertions(+), 17 deletions(-) create mode 100644 src/staticwordpress/core/errors.py diff --git a/src/staticwordpress/core/errors.py b/src/staticwordpress/core/errors.py new file mode 100644 index 0000000..254479e --- /dev/null +++ b/src/staticwordpress/core/errors.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- + +""" +STATIC WORDPRESS: WordPress as Static Site Generator +A Python Package for Converting WordPress Installation to a Static Website +https://github.com/serpwings/staticwordpress + + src\staticwordpress\core\errors.py + + Copyright (C) 2020-2023 Faisal Shahzad + + +The contents of this file are subject to version 3 of the +GNU General Public License (GPL-3.0). You may not use this file except in +compliance with the License. You may obtain a copy of the License at +https://www.gnu.org/licenses/gpl-3.0.txt +https://github.com/serpwings/staticwordpress/blob/master/LICENSE + + +Software distributed under the License is distributed on an "AS IS" basis, +WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the +specific language governing rights and limitations under the License. + +""" + + +# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +# EXCEPTIONS +# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + + +class SerpWingsResponseNotValid(Exception): + pass diff --git a/src/staticwordpress/core/redirects.py b/src/staticwordpress/core/redirects.py index 109e653..c083136 100644 --- a/src/staticwordpress/core/redirects.py +++ b/src/staticwordpress/core/redirects.py @@ -30,12 +30,14 @@ import json import requests import hashlib +import logging # +++++++++++++++++++++++++++++++++++++++++++++++++++++ # INTERNAL IMPORTS # +++++++++++++++++++++++++++++++++++++++++++++++++++++ from .constants import HOST, REDIRECTS +from .errors import SerpWingsResponseNotValid # +++++++++++++++++++++++++++++++++++++++++++++++++++++ # IMPLEMENATIONS @@ -83,8 +85,8 @@ def consolidate(self) -> None: pass def add_redirects(self, redirects_list_: list) -> None: - for redirect in redirects_list_: - self.add_redirect(redirect) + for redirect_ in redirects_list_: + self.add_redirect(redirect_) def save(self, output_file_, host_: HOST) -> None: with open( @@ -98,25 +100,34 @@ def save(self, output_file_, host_: HOST) -> None: else: f.write(redirect.as_line(True)) - def get_from_plugin(self, redirects_api_path: str, wp_auth_token_: str): - red_response = requests.get( - redirects_api_path, headers={"Authorization": "Basic " + wp_auth_token_} - ) + def get_from_plugin(self, redirects_api_path: str, wp_auth_token_: str) -> None: + try: + wp_api_response = requests.get( + redirects_api_path, headers={"Authorization": "Basic " + wp_auth_token_} + ) - redirects_dict = json.loads(red_response.content) - for red in redirects_dict["items"]: - self.add_redirect( - redirect_=Redirect( - from_=red["url"], - to_=red["action_data"]["url"], - status=red["action_code"], - query_=None, - force_=True, - source_=REDIRECTS.REDIRECTION.value, + if wp_api_response.status_code >= 400: + raise SerpWingsResponseNotValid + + redirects_as_dict = json.loads(wp_api_response.content) + + for redirect_ in redirects_as_dict["items"]: + self.add_redirect( + redirect_=Redirect( + from_=redirect_["url"], + to_=redirect_["action_data"]["url"], + status=redirect_["action_code"], + query_=None, + force_=True, + source_=REDIRECTS.REDIRECTION.value, + ) ) + except SerpWingsResponseNotValid: + logging.info( + f"Redirects are not valid. Make sure that redirection plug is properly configured." ) - def add_search(self, search_page: str): + def add_search(self, search_page: str) -> None: self.add_redirect( Redirect( from_="/*",