still refactoring

This commit is contained in:
Eric Lay 2024-03-27 22:39:22 -05:00
parent 897f577706
commit 6cf1558efb
1 changed files with 18 additions and 23 deletions

41
app.py
View File

@ -3,46 +3,41 @@ import random
from flask import Flask, send_from_directory, session from flask import Flask, send_from_directory, session
app = Flask(__name__) app = Flask(__name__)
app.secret_key = '420-69-LOL' # For using client side session cookies
# Implement count to load specific page on Nth load
# Using session['requests']
app.secret_key = '420-69'
# Script must run from root dir containing all websites dirs
# OR change the ROOT_DIR path below :)
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
WEBSITE_DIRS = [name for name in os.listdir(ROOT_DIR) if not name.startswith('.') and os.path.isdir(os.path.join(ROOT_DIR, name))]
# Site choosing logic # Site choosing logic
def current_website_dir(): def current_website_dir():
session.pop('website_dir', None) # Script must run from root dir containing all websites dirs
match session['requests']: # OR change the ROOT_DIR path :)
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
WEBSITE_DIRS = [name for name in os.listdir(ROOT_DIR) if not name.startswith('.') and os.path.isdir(os.path.join(ROOT_DIR, name))]
session.pop('website_dir', None) # Clear website_dir if exist
match session['requests']: #Match Nth page request
case 5: case 5:
session['website_dir'] = (os.path.join(ROOT_DIR, 'Marvel')) session['website_dir'] = (os.path.join(ROOT_DIR, 'Marvel'))
case 6: case 6:
session['website_dir'] = (os.path.join(ROOT_DIR, 'Escape')) session['website_dir'] = (os.path.join(ROOT_DIR, 'Escape'))
session.pop('requests', None) session.pop('requests', None) # Reset request count
case _: case _:
try: for dir in WEBSITE_DIRS:
WEBSITE_DIRS.remove(os.path.join(ROOT_DIR, 'Escape')) match dir: # Remove request specific pages
WEBSITE_DIRS.remove(os.path.join(ROOT_DIR, 'Marvel')) case 'Escape':
except (ValueError, IndexError) as e: WEBSITE_DIRS.remove(dir)
pass case 'Marvel':
WEBSITE_DIRS.remove(dir)
session['website_dir'] = random.choice(WEBSITE_DIRS) session['website_dir'] = random.choice(WEBSITE_DIRS)
# Make static files available @app.route('/<path:filename>', methods=['GET']) # Make static files available
@app.route('/<path:filename>', methods=['GET'])
def static_proxy(filename): def static_proxy(filename):
return send_from_directory(session['website_dir'], filename) return send_from_directory(session['website_dir'], filename)
# Serve site index.html @app.route('/', methods=['GET']) # Serve site index.html
@app.route('/', methods=['GET'])
def index(): def index():
if 'requests' in session: if 'requests' in session: # init requests count
session['requests'] += 1 session['requests'] += 1
else: else:
session['requests'] = 1 session['requests'] = 1
current_website_dir() current_website_dir() # Choose website dir
return send_from_directory(session['website_dir'], 'index.html') return send_from_directory(session['website_dir'], 'index.html')
if __name__ == "__main__": if __name__ == "__main__":