Implement forums redirector and default archive urls (since it's unlikely these will be changed). Also add redirectors for the index/main pages since those don't get handled by default.
This commit is contained in:
parent
c37cf4fc44
commit
f65361e06b
@ -1,39 +1,90 @@
|
||||
import argparse
|
||||
import gzip
|
||||
import urllib.request
|
||||
import json
|
||||
|
||||
from .archive_generator import ArchiveLinker, DEXES
|
||||
from .archive_generator import ArchiveLinker, DEXES, FORUM_THREAD_INDEX
|
||||
|
||||
from flask import Flask, redirect, request
|
||||
app = Flask(__name__)
|
||||
|
||||
DEFAULT_ARCHIVES_DOMAIN = "https://archives.glitchcity.info/"
|
||||
DEFAULT_FORUMS_ARCHIVE = f"{DEFAULT_ARCHIVES_DOMAIN}forums"
|
||||
DEFAULT_WIKI_ARCHIVE = f"{DEFAULT_ARCHIVES_DOMAIN}wiki"
|
||||
|
||||
## Wiki redirector
|
||||
@app.route("/wiki/")
|
||||
def redirect_wiki_main ():
|
||||
return redirect_wiki("Main Page")
|
||||
|
||||
@app.route("/wiki/<path:path>")
|
||||
def redirect_wiki (path):
|
||||
return redirect(make_wiki_url(path))
|
||||
|
||||
def make_wiki_url (path):
|
||||
url = app.args.wiki_archive
|
||||
|
||||
if path.endswith("/"):
|
||||
path = path[:-1]
|
||||
|
||||
return app.args.wiki_archive + app.wiki_linker.translate_page_title(path)
|
||||
|
||||
## Forum redirector
|
||||
@app.route('/forums/')
|
||||
def redirect_forums_index ():
|
||||
return redirect_forums("")
|
||||
|
||||
@app.route('/forums/<path:path>')
|
||||
def redirect_forums (path):
|
||||
return redirect(make_forum_url(request))
|
||||
|
||||
def make_forum_url (request):
|
||||
thread_id = request.args.get("topic", None)
|
||||
board_id = request.args.get("board", None)
|
||||
post_id = None
|
||||
|
||||
if thread_id:
|
||||
if "." in thread_id:
|
||||
(thread_id, post_id) = thread_id.split(".")
|
||||
post_id = post_id[len("msg"):]
|
||||
|
||||
if not board_id:
|
||||
board_id = app.thread_index[thread_id]['parent']
|
||||
|
||||
try:
|
||||
if "." in board_id:
|
||||
board_id = board_id.split(".")[0]
|
||||
except TypeError: pass
|
||||
|
||||
url = app.args.forums_archive
|
||||
|
||||
if board_id:
|
||||
url = url + f"board-{board_id}"
|
||||
|
||||
if thread_id:
|
||||
url = url + f"/thread-{thread_id}"
|
||||
|
||||
if not url.endswith("/"):
|
||||
url = url + "/"
|
||||
|
||||
return url + app.wiki_linker.translate_page_title(path)
|
||||
return url
|
||||
|
||||
## Forum redirector
|
||||
@app.route('/forums/<path:path>')
|
||||
def redirect_forums (path):
|
||||
return redirect(make_forum_url(path))
|
||||
|
||||
def make_forum_url (request):
|
||||
return str(request)
|
||||
def read_thread_index (forums_archive):
|
||||
with urllib.request.urlopen(f"{forums_archive}{FORUM_THREAD_INDEX}") as gzipped_in:
|
||||
data = gzipped_in.read()
|
||||
return json.loads(gzip.decompress(data).decode())
|
||||
|
||||
def main ():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--wiki-archive", help="URL to wiki archive")
|
||||
parser.add_argument("--forums-archive", help="URL to forums archive")
|
||||
parser.add_argument("--wiki-archive", help="URL to wiki archive", default=DEFAULT_WIKI_ARCHIVE)
|
||||
parser.add_argument("--forums-archive", help="URL to forums archive", default=DEFAULT_FORUMS_ARCHIVE)
|
||||
args = parser.parse_args()
|
||||
|
||||
app.args = parser.parse_args()
|
||||
if not args.wiki_archive.endswith("/"):
|
||||
args.wiki_archive = args.wiki_archive + "/"
|
||||
|
||||
if not args.forums_archive.endswith("/"):
|
||||
args.forums_archive = args.forums_archive + "/"
|
||||
|
||||
app.args = args
|
||||
app.thread_index = read_thread_index(args.forums_archive)
|
||||
app.wiki_linker = ArchiveLinker(directory_names=DEXES)
|
||||
app.run()
|
Loading…
x
Reference in New Issue
Block a user