Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fetch Redirects via API #18

Merged
merged 1 commit into from
Sep 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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