repl.it
Python

No description

fork
loading
Files
  • main.py
  • Packager files
  • poetry.lock
  • pyproject.toml
  • requirements.txt
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
from flask import Flask, request
import requests
app = Flask('app')

class Embed:
    def __init__(self):
        self.html = '<!DOCTYPE html><html><head><meta charset="utf-8"><title></title>{}</head><body>Link to this in Discord!</body></html>'
        self.meta = ""
    def addMeta(self, property, content):
        self.meta += '<meta name="{}" property="{}" content="{}">'.format(property.replace('"', "'"), property.replace('"', "'"), content.replace('"', "'"))
    def doc(self):
        return self.html.format(self.meta)

@app.route('/')
@app.route('/help')
def help():
    commands = {
        "help": "This help message", 
        "validate?url=URL": "Validate a webpage"
    }
    desc = "⚙️ Available:\n"
    for cmd in commands.keys():
        desc += ("&nbsp;" * 3) + "{} - {}\n".format(cmd, commands[cmd])
    e = Embed()
    e.addMeta("theme-color", "#000000")
    e.addMeta("og:title", "Welcome to Embeds")
    e.addMeta("og:description", desc)
    return e.doc()
    
@app.route('/validate')
def validate():
    e = Embed()
    e.addMeta("theme-color", "#000000")
    resp = requests.get("https://validator.w3.org/nu/?doc={}&out=json".format(request.args['url'])).json()
    e.addMeta("og:title", request.args['url'])
    desc = "{} message(s).".format(len(resp['messages']))
    for message in resp['messages']:
        icon = "⚠️"
        if message['type'] == 'info':
            icon = "ℹ️"
        elif message['type'] == 'error':
            icon = "❌"
        desc += "\n" + ("&nbsp;" * 3) + '{} Line {}: {}'.format(icon, message['lastLine'], message['message'])
    e.addMeta("og:description", desc)
    e.addMeta("og:url", "https://validator.w3.org/nu/?doc={}".format(request.args['url']))
    return e.doc()

app.run(host='0.0.0.0', port=8080)
?