Skip to content

Commit

Permalink
Merge pull request #40 from seowings/main
Browse files Browse the repository at this point in the history
Fix for Rediction Plugin
  • Loading branch information
seowings authored Oct 6, 2023
2 parents da757a0 + 56e9a1b commit fc0a36a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 17 deletions.
33 changes: 33 additions & 0 deletions src/staticwordpress/core/errors.py
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
<LICENSE_BLOCK>
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.
</LICENSE_BLOCK>
"""


# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# EXCEPTIONS
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


class SerpWingsResponseNotValid(Exception):
pass
45 changes: 28 additions & 17 deletions src/staticwordpress/core/redirects.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@
import json
import requests
import hashlib
import logging

# +++++++++++++++++++++++++++++++++++++++++++++++++++++
# INTERNAL IMPORTS
# +++++++++++++++++++++++++++++++++++++++++++++++++++++

from .constants import HOST, REDIRECTS
from .errors import SerpWingsResponseNotValid

# +++++++++++++++++++++++++++++++++++++++++++++++++++++
# IMPLEMENATIONS
Expand Down Expand Up @@ -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(
Expand All @@ -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_="/*",
Expand Down

0 comments on commit fc0a36a

Please sign in to comment.