Skip to content

Commit

Permalink
Merge pull request #18 from seowings/redirects
Browse files Browse the repository at this point in the history
fetch Redirects via API with fix for #17
  • Loading branch information
seowings authored Sep 6, 2023
2 parents 6b0af11 + 0b74282 commit da5618c
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import base64
import json
import re
from urllib import parse

# +++++++++++++++++++++++++++++++++++++++++++++++++++++
# IMPORTS (3rd Party)
Expand All @@ -52,7 +53,6 @@
import requests
from requests.structures import CaseInsensitiveDict


# +++++++++++++++++++++++++++++++++++++++++++++++++++++
# Local Imports
# +++++++++++++++++++++++++++++++++++++++++++++++++++++
Expand Down Expand Up @@ -104,6 +104,7 @@ def __init__(self, config_=None):
self.redirect_page = Path(
self.output_folder, self.config["pages"]["redirect"]
)
self.all_redirects = self.config["pages"]["all-redirects"]
self.robots_txt_page = Path(
self.output_folder, self.config["pages"]["robots"]
)
Expand Down Expand Up @@ -412,6 +413,18 @@ def create_redirect_toml_file(self):
else:
helpers.log_to_console("WARNING", "No Redirect File found")

for redirect in self.all_redirects:
rules.append(
[
f"[[redirects]]\n",
f'from = "{redirect["from"]}"\n',
f'to = "{redirect["to"]}"\n',
f'status = {redirect["status"]}\n',
"force = true\n",
"\n",
]
)

netlify_toml_file = Path(self.output_folder, "netlify.toml")

with open(netlify_toml_file, "w", encoding="utf-8") as f:
Expand Down Expand Up @@ -460,6 +473,28 @@ def create_redirect_toml_file(self):
except:
pass

# redirects
all_redirects = []
try:
redirects_source = src_url + "/wp-json/redirection/v1/redirect"
red_response = requests.get(
redirects_source, headers={"Authorization": "Basic " + wp_token}
)

redirects_dict = json.loads(red_response.content)

all_redirects = [
{
"from": red["url"],
"to": red["action_data"]["url"],
"status": red["action_code"],
"force": True,
}
for red in redirects_dict["items"]
]
except:
pass

if wordpress_simply_static_zip_url:
configurations = {
"root": "",
Expand All @@ -473,6 +508,7 @@ def create_redirect_toml_file(self):
"redirect": page_redirects,
"robots": page_robots,
"search": page_search,
"all-redirects": all_redirects,
},
"search": {
"title": "true",
Expand Down

0 comments on commit da5618c

Please sign in to comment.