server.py 2.27 KB
Newer Older
Robin Sonnabend's avatar
Robin Sonnabend committed
1 2 3 4 5 6 7 8 9
from flask import Flask, request, session, redirect, url_for, abort, render_template, Response, Markup
from flask_wtf import FlaskForm
from wtforms import StringField, TextAreaField, SelectField
import requests
import bs4

import os
import urllib

Robin Sonnabend's avatar
Robin Sonnabend committed
10
import config
Robin Sonnabend's avatar
Robin Sonnabend committed
11

Robin Sonnabend's avatar
Robin Sonnabend committed
12 13
app = Flask(__name__, static_folder="argumenthelperstatic")
app.config.from_object(config)
Robin Sonnabend's avatar
Robin Sonnabend committed
14

Robin Sonnabend's avatar
Robin Sonnabend committed
15
wikis = config.WIKIS
Robin Sonnabend's avatar
Robin Sonnabend committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
wiki_choices = list(zip(wikis.keys(), wikis.keys()))


class RequestForm(FlaskForm):
    page = StringField("Page")
    wiki = SelectField("Wiki", choices=wiki_choices)
    text = TextAreaField("Inhalt")


def fix_link(href, wiki_url):
    parts = urllib.parse.urlparse(href)
    if not parts.netloc:
        parts = list(parts)
        parts[1] = urllib.parse.urlparse(wiki_url).netloc
    return urllib.parse.urlunparse(parts)


def fix_links(soup, wiki_url):
    for tag in soup.find_all(["a", "link"], href=True):
        tag["href"] = fix_link(tag["href"], wiki_url)
    for tag in soup.find_all(["script", "img"], src=True):
        tag["src"] = fix_link(tag["src"], wiki_url)



def insert_content(soup, new_content):
    content_element = soup.find("div", class_="mw-parser-output")
    first_paragraph = content_element.find("p", class_=None)
    first_paragraph.append(new_content)


@app.route("/", methods=["GET", "POST"])
def index():
    form = RequestForm()
    if form.validate_on_submit():
        page_title = form.page.data
        wiki = form.wiki.data
        session["wiki"] = wiki
        print(session["wiki"])
        new_content = form.text.data
        original_url = wikis[wiki] + page_title
        response = requests.get(original_url)
        if response.status_code == 404:
            form.page.errors.append("Page not found")
            return render_template("index.html", form=form)
        content = response.text
        soup = bs4.BeautifulSoup(content, "html.parser")
        fix_links(soup, wikis[wiki])
        insert_content(soup, new_content)
        return str(soup)
    return render_template("index.html", form=form)


@app.route("/<path:path>")
def catch_all(path):
    parts = list(urllib.parse.urlparse(request.url))
    base_url = wikis.get(session.get("wiki", "Wikipedia English"))
    
    parts[1] = urllib.parse.urlparse(base_url).netloc
    return redirect(urllib.parse.urlunparse(parts))