fuckyourday/app.py

51 lines
1.7 KiB
Python
Raw Normal View History

2024-03-24 18:56:52 -05:00
import os
import random
from flask import Flask, send_from_directory, session
2024-03-24 18:56:52 -05:00
app = Flask(__name__)
# Implement count to load specific page on Nth load
# Using session['requests']
app.secret_key = '420-69'
2024-03-26 20:36:43 -05:00
2024-03-24 18:56:52 -05:00
# 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
website_dir = None
def current_website_dir():
session.pop('website_dir', None)
if session['requests'] % 5 == 0:
2024-03-27 20:27:59 -05:00
session['website_dir'] = (os.path.join(ROOT_DIR, 'Marvel'))
elif session['requests'] % 6 == 0:
2024-03-27 20:27:59 -05:00
session['website_dir'] = (os.path.join(ROOT_DIR, 'Escape'))
session.pop('requests', None)
else:
global WEBSITE_DIRS
try:
2024-03-27 20:27:59 -05:00
WEBSITE_DIRS.remove(os.path.join(ROOT_DIR, 'Marvel'))
WEBSITE_DIRS.remove(os.path.join(ROOT_DIR, 'Escape'))
except (ValueError, IndexError) as e:
pass
session['website_dir'] = random.choice(WEBSITE_DIRS)
2024-03-24 18:56:52 -05:00
# Make static files available
@app.route('/<path:filename>', methods=['GET'])
def static_proxy(filename):
return send_from_directory(session['website_dir'], filename)
2024-03-24 18:56:52 -05:00
# Serve site index.html
@app.route('/', methods=['GET'])
def index():
if 'requests' in session:
session['requests'] += 1
else:
session['requests'] = 1
2024-03-24 18:56:52 -05:00
current_website_dir()
return send_from_directory(session['website_dir'], 'index.html')
2024-03-24 18:56:52 -05:00
if __name__ == "__main__":
app.run()