diff --git a/requirements.txt b/requirements.txt index 6ca9b0b..72af20f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,5 @@ psycopg2-binary python-dotenv flask-session requests -flask \ No newline at end of file +flask +markdown \ No newline at end of file diff --git a/src/routes/dynamic_routes.py b/src/routes/dynamic_routes.py index 5fbc3eb..d82d3a9 100644 --- a/src/routes/dynamic_routes.py +++ b/src/routes/dynamic_routes.py @@ -1,7 +1,7 @@ # Imports from flask import Blueprint, render_template, abort from os import getenv as env -import logging, os, re +import logging, os, re, markdown # Create blueprint bp = Blueprint( @@ -16,6 +16,7 @@ log = logging.getLogger(__name__) # Get all files in folder def ListFiles(path): + path = os.path.join(bp.template_folder, 'pages', path)[3:] files = [] for root, dirs, files_in_dir in os.walk(path): for file in files_in_dir: @@ -27,14 +28,27 @@ def ListFiles(path): # Catch-all route for generic pages @bp.route('/') def catch_all(filename): - try: - return render_template(f'pages/{filename if re.match(r'^.+\.[a-zA-Z0-9]+$', filename) else filename + '.html'}') + if os.path.exists(os.path.join(bp.template_folder, 'pages', filename)[3:]): + return render_template(f'pages/{filename}') - except Exception as e: - os_path = os.path.join(bp.template_folder, 'pages', filename)[3:] - if os.path.isdir(os_path): - if not filename.endswith('/'): filename += '/' - return render_template('bases/directory.html', directory=filename, pages=ListFiles(os_path)) + elif os.path.exists(os.path.join(bp.template_folder, 'pages', filename + '.html')[3:]): + return render_template(f'pages/{filename}.html') - # If it is a file, return a 404 error - abort(404, f"Template '{filename}' not found: {e}") \ No newline at end of file + elif os.path.exists(os.path.join(bp.template_folder, 'pages', filename + '.md')[3:]): + print("yay") + print(markdown.markdownFromFile("../templates/pages/test.md")) + return render_template( + f'bases/md.html', + title = filename.split("/")[-1], + markdown = markdown.markdownFromFile(os.path.join(bp.template_folder, 'pages', filename + '.md')) + ) + + elif os.path.isdir(os.path.join(bp.template_folder, 'pages', filename)[3:]): + return render_template( + 'bases/directory.html', + directory=filename + "/" if not filename.endswith('/') else filename, + pages=ListFiles(filename) + ) + + else: + abort(404, f"'{filename}' not found") \ No newline at end of file diff --git a/src/routes/error_handlers.py b/src/routes/error_handlers.py index 22e19f2..683d69b 100644 --- a/src/routes/error_handlers.py +++ b/src/routes/error_handlers.py @@ -1,5 +1,6 @@ # Imports from flask import Blueprint, render_template +from werkzeug.exceptions import HTTPException from os import getenv as env import logging @@ -17,23 +18,35 @@ log = logging.getLogger(__name__) # Route for 500 error @bp.route('/500') @bp.app_errorhandler(500) -def internal_server_error(error=None): - if error is not None: - log.error("Internal server error: %s", error) - return render_template('errors/500.html'), 500 +def internal_server_error(error:HTTPException=None): + return render_template('errors/500.html', error=error), 500 # Route for 404 error @bp.route('/404') @bp.app_errorhandler(404) -def not_found(error=None): - if error is not None: - log.warning("Page not found: %s", error) - return render_template('errors/404.html'), 404 if error is not None else 200 +def not_found(error:HTTPException=None): + return render_template('errors/404.html', error=error), 404 # Route for 400 error @bp.route('/400') @bp.app_errorhandler(400) -def bad_request(error=None): - if error is not None: - log.warning("Bad request: %s", error) - return render_template('errors/400.html', error=error), 400 \ No newline at end of file +def bad_request(error:HTTPException=None): + return render_template('errors/400.html', error=error), 400 + +# Route for all other errors +@bp.route('/error') +@bp.app_errorhandler(Exception) +def unauthorized(error:HTTPException=None): + if isinstance(error, HTTPException): + return render_template( + 'errors/error.html', + code = error.code, + description = error.description, + name = error.name + ), error.code + return render_template( + 'errors/error.html', + code=418, + description="meow :3", + name="I'm a teapot" + ), 418 \ No newline at end of file diff --git a/src/routes/lastfm.py b/src/routes/lastfm.py deleted file mode 100644 index 7c7710c..0000000 --- a/src/routes/lastfm.py +++ /dev/null @@ -1,26 +0,0 @@ -from flask import Blueprint, jsonify -from os import getenv as env -import logging, requests - -# Create blueprint -bp = Blueprint( - 'lastfm', - __name__, - template_folder=env('TEMPLATE_FOLDER', default='../templates'), - static_folder=env('STATIC_FOLDER', default='../static') -) - -# Create logger -log = logging.getLogger(__name__) - -# lastfm info -@bp.route('/info') -def lastfm_info(): - url = f"http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user={env('LASTFM_USER')}&api_key={env('LASTFM_API_KEY')}&format=json&limit=1" - response = requests.get(url).json() - data = { - 'artist': response['recenttracks']['track'][0]['artist']['#text'], - 'track': response['recenttracks']['track'][0]['name'], - 'image': response['recenttracks']['track'][0]['image'][3]['#text'] - } - return jsonify(data) \ No newline at end of file diff --git a/src/wsgi.py b/src/wsgi.py index 1e90942..3c01824 100644 --- a/src/wsgi.py +++ b/src/wsgi.py @@ -8,7 +8,6 @@ import logging import src.routes.error_handlers import src.routes.dynamic_routes -import src.routes.lastfm # Load env load_dotenv() @@ -43,7 +42,6 @@ Session(app) # Load routes app.register_blueprint(src.routes.error_handlers.bp, url_prefix='/error') -app.register_blueprint(src.routes.lastfm.bp, url_prefix='/lastfm') app.register_blueprint(src.routes.dynamic_routes.bp, url_prefix='/') # Generic routes diff --git a/static/content/fonts/avali-scratch.otf.woff2 b/static/content/fonts/avali-scratch.otf.woff2 new file mode 100644 index 0000000..ded650d Binary files /dev/null and b/static/content/fonts/avali-scratch.otf.woff2 differ diff --git a/static/css/bases/base.css b/static/css/bases/base.css index 9bb5eac..b35ef34 100644 --- a/static/css/bases/base.css +++ b/static/css/bases/base.css @@ -5,6 +5,12 @@ font-weight:normal; font-style:normal; } +@font-face { + font-family:"Scratch"; + src:url("/static/content/fonts/avali-scratch.otf.woff2") format("woff2"); + font-weight:normal; + font-style:normal; +} :root { --primary-color: #5cdd8b; @@ -17,6 +23,7 @@ --font-family: "Space Mono", "serif"; --title-font: 'Roboto Mono', sans-serif; --irken-font: 'Irken'; + --scratch-font: 'Scratch'; } body { @@ -244,6 +251,10 @@ main section a { font-family: var(--irken-font); } +.scratch { + font-family: var(--scratch-font); +} + #alt-nav { display: none; backdrop-filter: blur(2px) brightness(0.6); diff --git a/static/js/base.js b/static/js/base.js index 35f4e9c..c8a246f 100644 --- a/static/js/base.js +++ b/static/js/base.js @@ -72,41 +72,58 @@ typing(); // HIDDEN STUFF (shh don't tell anyone >:3) -let last5Chars = ""; +let last15Chars = ""; document.addEventListener('keydown', function(event) { - last5Chars += event.key; - if (last5Chars == "furry") { + last15Chars += event.key; + if (last15Chars.includes("furry")) { console.log("owo, whats this?"); document.getElementById('furry').style.display = 'block'; + last15Chars = ""; } - if (last5Chars == "irken") { + if (last15Chars.includes("irken")) { console.log("doom doom doom!"); document.querySelector(":root").style.setProperty('--font-family', 'Irken'); document.querySelector(":root").style.setProperty('--title-font', '1.5em'); + last15Chars = ""; } - while (last5Chars.length >= 5) { - last5Chars = last5Chars.slice(1); + if (last15Chars.includes("scratch")) { + console.log("space chicken"); + document.querySelector(":root").style.setProperty('--font-family', 'Scratch'); + document.querySelector(":root").style.setProperty('--title-font', '1em'); + last15Chars = ""; + } + while (last15Chars.length >= 15) { + last15Chars = last15Chars.slice(1); } }); // Spotify API (now lastfm) function getSpotify() { - fetch('/lastfm/info').then(response => { + fetch('https://api.alfieking.dev/spotify/nowplaying/xz02oolstlvwxqu1pfcua9exz').then(response => { return response.json(); }).then(data => { - document.getElementById('spotify').style.backgroundImage = "url(" + data.image + ")"; - document.getElementById('spotify-title').innerHTML = data.track; - document.getElementById('spotify-artist').innerHTML = data.artist; + if (data.item == null) { + document.getElementById('spotify').style.backgroundImage = "none"; + document.getElementById('spotify-title').innerHTML = "Spotify is not playing anything"; + document.getElementById('spotify-artist').innerHTML = ":("; + document.getElementById('spotify-link').href = "https://open.spotify.com/"; + return; + } + document.getElementById('spotify').style.backgroundImage = "url(" + data.item.album.images[0].url + ")"; + document.getElementById('spotify-title').innerHTML = data.item.name; + document.getElementById('spotify-artist').innerHTML = data.item.artists[0].name; + document.getElementById('spotify-link').href = data.item.external_urls.spotify; }); } if (document.getElementById('spotify')) { getSpotify(); - setInterval(getSpotify, 60000); + setInterval(getSpotify, 15000); } + // load buttons function loadButtons() { diff --git a/templates/bases/base.html b/templates/bases/base.html index e5e1041..d01da80 100644 --- a/templates/bases/base.html +++ b/templates/bases/base.html @@ -41,7 +41,7 @@
-
heya, try typing "furry" and "irken" into this page!
+
heya, try typing "furry", "irken" or scratch into this page!

BUTTONS

diff --git a/templates/bases/md.html b/templates/bases/md.html new file mode 100644 index 0000000..9030e27 --- /dev/null +++ b/templates/bases/md.html @@ -0,0 +1,10 @@ +{% extends "bases/base.html" %} + +{% block title %}{{ title }} - Alfie's basement{% endblock %} +{% block description %}server backend survivor{% endblock %} + +{% block content %} +
+ {{ markdown }} +
+{% endblock %} \ No newline at end of file diff --git a/templates/errors/404.html b/templates/errors/404.html index 0215a8d..5a78f84 100644 --- a/templates/errors/404.html +++ b/templates/errors/404.html @@ -10,4 +10,10 @@ It seems like the thing you are looking for does not exist or rm -rf itself out of exsistance.

+
+

Actual error

+

+ {{ error }} +

+
{% endblock %} \ No newline at end of file diff --git a/templates/errors/500.html b/templates/errors/500.html index eec13f3..58afff4 100644 --- a/templates/errors/500.html +++ b/templates/errors/500.html @@ -14,4 +14,10 @@ Oopsie Woopsie! Uwu We made a fucky wucky!! A wittle fucko boingo! The code monkeys at our headquarters are working VEWY HAWD to fix this!

+
+

Actual error

+

+ {{ error }} +

+
{% endblock %} \ No newline at end of file diff --git a/templates/errors/error.html b/templates/errors/error.html new file mode 100644 index 0000000..c5ea5f8 --- /dev/null +++ b/templates/errors/error.html @@ -0,0 +1,16 @@ +{% extends "bases/base.html" %} + +{% block title %}{{ code }} - {{ name }}{% endblock %} +{% block description %}The page you are looking for does not exist.{% endblock %} + +{% block content %} +
+ +
+
+

Actual error

+

+ {{ description }} +

+
+{% endblock %} \ No newline at end of file diff --git a/templates/pages/test.md b/templates/pages/test.md new file mode 100644 index 0000000..17e2883 --- /dev/null +++ b/templates/pages/test.md @@ -0,0 +1,2 @@ +# hello +this is a test \ No newline at end of file