#!/usr/bin/env python

from flask import Flask, render_template, request, redirect
import requests
import html
import re
from bs4 import BeautifulSoup


def scrape(url):
    data = requests.get(url)
    
    our_path = re.sub(r".*://.*/", "/", request.url)
    path = re.sub(r".*://.*/", "/", data.url)
    print()
    print(our_path)
    print(path)
    print()
    if our_path != path:
        return f"REDIRECT {path}"
    ret = []
    soup = BeautifulSoup(data.text, "html.parser")
    for div in soup.find_all("div"):
        defid = div.get('data-defid')
        if defid != None:
            definition = soup.find(attrs={"data-defid": [defid]})
            word = definition.select("div div h1 a, div div h2 a")[0].text
            meaning = definition.find(attrs={"class" : ["break-words meaning mb-4"]}).decode_contents()
            example = definition.find(attrs={"class" : ["break-words example italic mb-4"]}).decode_contents()
            contributor = definition.find(attrs={"class" : ["contributor font-bold"]})
            ret.append([defid, word, meaning, example, contributor])
    pages = soup.find(attrs={"class" : ["pagination text-xl text-center"]})
    if pages == None:
        pages = ""
    return (ret, pages)

def render(data):
    return render_template('index.html', data=data)

app = Flask(__name__, template_folder="templates", static_folder="static")

@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def catch_all(path):
    scraped = scrape(f"https://urbandictionary.com/{re.sub(r'.*://.*/', '/', request.url)}")
    if type(scraped) == str and scraped.startswith("REDIRECT"):
        return redirect(scraped.replace("REDIRECT ", ""), 302)
    scraped = (scraped[0], str(scraped[1]).replace("»", "»").replace("›", "›").replace("«", "«").replace("‹", "‹"))
    return render(scraped)

if __name__ == '__main__':
    app.run(port=8000)