Skip to content

Commit

Permalink
Doc: redirect pre-versioned URLs to /en/latest/
Browse files Browse the repository at this point in the history
  • Loading branch information
dbaston committed Jul 25, 2024
1 parent 614bb7f commit 4bbf9ce
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions doc/rtd/update_redirects.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3

import os
import time

import requests

Expand All @@ -22,12 +23,20 @@ def get_redirects():

def delete_redirect(redirect_id):
url = f"https://readthedocs.org/api/v3/projects/gdal/redirects/{redirect_id}"
requests.delete(url, headers=HEADERS)
res = requests.delete(url, headers=HEADERS)
print(res.status_code)
if res.status_code == 429:
time.sleep(3)
delete_redirect(redirect_id)


def create_redirect(data):
url = "https://readthedocs.org/api/v3/projects/gdal/redirects/"
requests.post(url, json=data, headers=HEADERS)
res = requests.post(url, json=data, headers=HEADERS)
print(res.status_code)
if res.status_code == 429:
time.sleep(3)
create_redirect(data)


existing_redirects = get_redirects()
Expand Down Expand Up @@ -117,6 +126,9 @@ def create_redirect(data):
{"from_url": f"/{program}.html", "to_url": f"/programs/{program}.html"}
)

# prepend /en/latest/ to pre-RTD URLs
redirects.append({"from_url": "/*", "to_url": "/en/latest/:splat", "type:": "exact"})

for redirect in existing_redirects:
print(f'Deleting redirect from {redirect["from_url"]} to {redirect["to_url"]}')
delete_redirect(redirect["pk"])
Expand All @@ -127,5 +139,5 @@ def create_redirect(data):
data["type"] = data.get("type", "page")
data["position"] = i

print(f'Creatng redirect from {redirect["from_url"]} to {redirect["to_url"]}')
print(f'Creating redirect from {redirect["from_url"]} to {redirect["to_url"]}')
create_redirect(data)

0 comments on commit 4bbf9ce

Please sign in to comment.