import logging logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO) # Flask application to serve the web pages from flask import Flask, render_template, request, redirect, url_for # Mediawiki client to interact with the Wiki import mwclient # BS to read the html table from the wiki from bs4 import BeautifulSoup # os and dotenv to store the mediawiki credentials in a safe place import os from dotenv import load_dotenv from pathlib import Path # load the mediawiki credentials from the shared folder dotenv_path = Path("/var/www/.mw-credentials") load_dotenv(dotenv_path=dotenv_path) # prefix to add /soupboat/padliography to all the routes # and to leave the @app.route() decorator more clean class PrefixMiddleware(object): def __init__(self, app, prefix=""): self.app = app self.prefix = prefix def __call__(self, environ, start_response): if environ["PATH_INFO"].startswith(self.prefix): environ["PATH_INFO"] = environ["PATH_INFO"][len(self.prefix) :] environ["SCRIPT_NAME"] = self.prefix return self.app(environ, start_response) else: start_response("404", [("Content-Type", "text/plain")]) return ["This url does not belong to the app.".encode()] # create flask application app = Flask(__name__) # Url prefix for the soupboat base_url = "/soupboat/padliography" # Page of the wiki with the pads padliography = 'Padliography2' # register the middleware to prefix all the requests with our base_url app.wsgi_app = PrefixMiddleware(app.wsgi_app, prefix=base_url) def add_pad(link, title, overview, categories, date): site = mwclient.Site('pzwiki.wdka.nl', path='/mw-mediadesign/') site.login( username=os.environ.get('MW_BOT'), password=os.environ.get('MW_KEY') ) page = site.pages[padliography] text = page.text() new_row = f'|-\n| {link} || {title} || {overview} || {categories} || {date}\n|-\n' + '|}' text = text.replace('|}', new_row) page.edit(text, f'New pad in the {padliography}: {title}') def get_pads(): site = mwclient.Site('pzwiki.wdka.nl', path='/mw-mediadesign/') site.login( username=os.environ.get('MW_BOT'), password=os.environ.get('MW_KEY') ) html = site.api('parse', prop='text', page=padliography) pads = BeautifulSoup(html['parse']['text']['*']).find("table", attrs={"class":"padliography"}) return pads @app.route('/') def home(): pads = get_pads() return render_template('index.html', pads=pads) @app.route('/add') def add(): link = request.args.get('link', None) title = request.args.get('title', None) overview = request.args.get('overview', '') categories = request.args.get('categories', '') date = request.args.get('date', None) if None not in (link, title, date): add_pad(link, title, overview, categories, date) redirect(url_for('home')) return render_template('index.html') app.run(port=3146)