diff options
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | app.py | 14 | ||||
| -rw-r--r-- | database.py | 2 | ||||
| -rw-r--r-- | parser.py | 123 | ||||
| -rw-r--r-- | services.py | 9 | ||||
| -rw-r--r-- | templates/services.html | 10 | 
6 files changed, 154 insertions, 6 deletions
| @@ -1,4 +1,6 @@ +!README.md  edaweb.conf +*.md  # Byte-compiled / optimized / DLL files  __pycache__/ @@ -1,4 +1,5 @@  import configparser +import webbrowser  import database  import services  import flask @@ -10,7 +11,7 @@ CONFIG.read("edaweb.conf")  def get_template_items(title, db):      return {          "links": db.get_header_links(), -        "image": db.get_image("telegrampic"), +        "image": db.get_image("twitterpic"),          "title": title,          "articles": db.get_header_articles()      } @@ -44,5 +45,16 @@ def serve_services():              pihole = services.get_pihole_stats()          ) +@app.route("/preview") +def preview(): +    import os +    if "PREVIEW" in os.environ: +        with database.Database() as db: +            return flask.render_template_string(os.environ["PREVIEW"], **get_template_items(os.environ["PREVIEW_TITLE"], db)) +    else: +        return "page for internal use only" + +     +  if __name__ == "__main__":      app.run(host = "0.0.0.0", debug = True) diff --git a/database.py b/database.py index 05e8a7e..93f7538 100644 --- a/database.py +++ b/database.py @@ -30,4 +30,4 @@ class Database:  if __name__ == "__main__":      with Database() as db: -        print(db.get_header_articles())
\ No newline at end of file +        print(db.get_image("headerImage"))
\ No newline at end of file diff --git a/parser.py b/parser.py new file mode 100644 index 0000000..8677342 --- /dev/null +++ b/parser.py @@ -0,0 +1,123 @@ +import argparse +from urllib.parse import urlparse +import webbrowser +import app +import re +import os + +HEADER_INCREMENTER = 1 +IMAGE_TYPES = [".png", ".jpg"] + +def parse_file(path): +    with open(path, "r") as f: +        unformatted = f.read() + +    formatted = parse_headers(unformatted) +    formatted = parse_asteriscs(formatted) +    formatted = parse_links(formatted) +    formatted = add_linebreaks(formatted) + +    return '{% extends "template.html" %}\n{% block content %}\n' + formatted + '\n{% endblock %}' + +def parse_headers(test_str): +    regex = r"^#{1,5}\s\w.*$" +    matches = re.finditer(regex, test_str, re.MULTILINE) +    offset = 0 + +    for match in matches: +        # work out if its h2, h3 etc. from the number of #s +        headerNo = len(match.group().split(" ")[0]) + HEADER_INCREMENTER +         +        replacement = "<h%i>%s</h%i>" % (headerNo, " ".join(match.group().split(" ")[1:]), headerNo) + +        #don't use .replace() in the unlikely case the the regex hit appears in a block +        test_str = test_str[:match.start()+offset] + replacement + test_str[match.end()+offset:] +        #replacing the hits fucks up the indexes, accommodate for this +        offset += (len(replacement) - (match.end() - match.start())) + +    return test_str + +def parse_asteriscs(test_str): +    regex = r"(?<!\\)\*{1,3}.*?\*{1,3}" +    matches = re.finditer(regex, test_str, re.MULTILINE) +    offset = 0 + +    for match in matches: +        if len(re.findall(r"\*{1,3}.*?\\\*{1,3}", match.group())) == 0:     #need to find a way of doing this with regexes +            if match.group().startswith(re.findall(r"\w\*{1,3}", match.group())[0][1:]):    #this too +                if match.group().startswith("***"): +                    replacement = "<b><i>%s</i></b>" % (match.group()[3:-3]) +                elif match.group().startswith("**"): +                    replacement = "<b>%s</b>" % (match.group()[2:-2]) +                else: +                    replacement = "<i>%s</i>" % (match.group()[1:-1]) +         +                test_str = test_str[:match.start()+offset] + replacement + test_str[match.end()+offset:] +                offset += (len(replacement) - (match.end() - match.start())) + +    return test_str + +def parse_links(test_str): +    regex = r"(?<!\\)\[.*?\]\(http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+\)" +    matches = re.finditer(regex, test_str, re.MULTILINE) +    offset = 0 + +    for match in matches: +        s = match.group().split("(") +        label = s[0][1:-1] +        url = s[1][:-1] + +        if os.path.splitext(urlparse(url).path)[1] in IMAGE_TYPES: +            replacement = "<img alt='%s' src=%s>" % (label, url) +        else: +            replacement = "<a href=%s>%s</a>" % (url, label) + +        test_str = test_str[:match.start()+offset] + replacement + test_str[match.end()+offset:] +        offset += (len(replacement) - (match.end() - match.start())) + +    return test_str + +def add_linebreaks(test_str): +    return re.sub(r"^$", "<br><br>", test_str, 0, re.MULTILINE) + +def preview_markdown(path, title): +    def startBrowser(): +        webbrowser.get("firefox").open("http://localhost:5000/preview") +        del os.environ["PREVIEW"] +        del os.environ["PREVIEW_TITLE"] + +    os.environ["PREVIEW"] = parse_file(path) +    os.environ["PREVIEW_TITLE"] = title + +    import threading +    threading.Timer(1.25, startBrowser ).start() +     +    app.app.run(host = "0.0.0.0", debug = True) + +def main(): +    p = argparse.ArgumentParser() +    p.add_argument( +        "-m", "--markdown", +        help = "Path to a markdown file", +        required = True, +        type = str +    ) +    p.add_argument( +        "-t", "--title", +        help = "Article title", +        required = True, +        type = str +    ) +    p.add_argument( +        "-p", "--preview", +        help = "Preview markdown rendering", +        action='store_true' +    ) +    args = vars(p.parse_args()) +    if args["preview"]: +        preview_markdown(args["markdown"], args["title"]) +    else: +        print(parse_file(args["markdown"])) + +if __name__ == "__main__": +    main()
\ No newline at end of file diff --git a/services.py b/services.py index dd70b33..b592427 100644 --- a/services.py +++ b/services.py @@ -1,5 +1,6 @@ -import qbittorrent  import multiprocessing +import qbittorrent +import datetime  import docker  import clutch  import pihole @@ -104,9 +105,11 @@ def get_pihole_stats():          "queries": ph.total_queries,          "clients": ph.unique_clients,          "percentage": ph.ads_percentage, -        "blocked": ph.blocked +        "blocked": ph.blocked, +        "domains": ph.domain_count, +        "last_updated": str(datetime.datetime.fromtimestamp(ph.gravity_last_updated["absolute"]))      }  if __name__ == "__main__": -    print(get_qbit_stats())
\ No newline at end of file +    print(get_pihole_stats())
\ No newline at end of file diff --git a/templates/services.html b/templates/services.html index 321a7a0..9ea1d3f 100644 --- a/templates/services.html +++ b/templates/services.html @@ -95,9 +95,17 @@                      <td>{{pihole["percentage"]}}%</td>                  </tr>                  <tr> -                    <td>blocked</td> +                    <td>blocked requests</td>                      <td>{{pihole["blocked"]}}</td>                  </tr> +                <tr> +                    <td>domains in blocklist</td> +                    <td>{{pihole["domains"]}}</td> +                </tr> +                <tr> +                    <td>last updated</td> +                    <td>{{pihole["last_updated"]}}</td> +                </tr>              </table>          {% endif %}      </section> | 
