Compare commits
2 Commits
ef3f3dd60c
...
f65361e06b
Author | SHA1 | Date | |
---|---|---|---|
f65361e06b | |||
c37cf4fc44 |
@ -2,6 +2,8 @@ import os
|
||||
import logging
|
||||
import shutil
|
||||
import math
|
||||
import json
|
||||
import gzip
|
||||
|
||||
from itertools import chain
|
||||
from traceback import print_exc
|
||||
@ -24,6 +26,8 @@ DEX_TYPES = [
|
||||
]
|
||||
DEXES = list(chain.from_iterable([[f"{dex_type}{language}" for dex_type in DEX_TYPES] for language in DEX_LANGUAGES]))
|
||||
|
||||
FORUM_THREAD_INDEX = "thread_index.json.gz"
|
||||
|
||||
class ArchiveLinker(Linker):
|
||||
def __init__ (self, directory_names=[]):
|
||||
super().__init__()
|
||||
@ -158,10 +162,15 @@ class ArchiveGenerator():
|
||||
"categories": forum.get_board_tree()
|
||||
})
|
||||
|
||||
threads = []
|
||||
for board in forum.get_boards():
|
||||
self.generate_forum_board(forum, board, out_dir)
|
||||
forum_threads = forum.get_threads_in_board(board)
|
||||
threads = threads + forum_threads
|
||||
self.generate_forum_board(forum, board, forum_threads, out_dir)
|
||||
|
||||
def generate_forum_board (self, forum, board, out_dir):
|
||||
self.generate_thread_index(threads, os.path.join(out_dir, FORUM_THREAD_INDEX))
|
||||
|
||||
def generate_forum_board (self, forum, board, threads, out_dir):
|
||||
board_out_dir = os.path.join(out_dir, "board-{}".format(board.id))
|
||||
logger.info("Archiving board %s to %s", board.name, board_out_dir)
|
||||
try:
|
||||
@ -169,7 +178,7 @@ class ArchiveGenerator():
|
||||
except FileExistsError: pass
|
||||
|
||||
renderer = TemplateRenderer(self.template_dir, board_out_dir)
|
||||
threads = [prepare_thread(thread) for thread in forum.get_threads_in_board(board)]
|
||||
threads = [prepare_thread(thread) for thread in threads]
|
||||
renderer.render_template_to_file("threads", "index.html", {
|
||||
"title": " - {}".format(board.name),
|
||||
"base": "../",
|
||||
@ -214,6 +223,13 @@ class ArchiveGenerator():
|
||||
})
|
||||
page = page + 1
|
||||
|
||||
def generate_thread_index (self,threads, out_path):
|
||||
# with open(out_path, "wb") as out:
|
||||
# pickle.dump({thread.id: {"parent": thread.parent} for thread in threads}, out, protocol=4)
|
||||
threads = {thread.id: {"parent": thread.parent} for thread in threads}
|
||||
with gzip.open(out_path, "w") as out:
|
||||
out.write(json.dumps(threads).encode())
|
||||
|
||||
class TemplateRenderer():
|
||||
def __init__ (self, template_dir, out_dir):
|
||||
self.template_dir = template_dir
|
||||
|
@ -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