Update app.py

Total rework to requests count using Flask session
This commit is contained in:
Eric Lay 2024-03-27 18:17:11 -05:00 committed by GitHub
parent 888b082eb6
commit 279e492193
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 12 additions and 10 deletions

22
app.py
View File

@ -1,13 +1,12 @@
import os import os
import uuid
import random import random
from flask import Flask, send_from_directory, request from flask import Flask, send_from_directory, session
app = Flask(__name__) app = Flask(__name__)
# Implement count to load specific page on Nth # Implement count to load specific page on Nth load
request_count = 0 # Using session['requests']
unique_requests = set() app.secret_key = '420-69'
# Script must run from root dir containing all websites dirs # Script must run from root dir containing all websites dirs
# OR change the ROOT_DIR path below :) # OR change the ROOT_DIR path below :)
@ -17,13 +16,12 @@ WEBSITE_DIRS = [name for name in os.listdir(ROOT_DIR) if not name.startswith('.'
# Site choosing logic # Site choosing logic
website_dir = None website_dir = None
def current_website_dir(): def current_website_dir():
global request_count if session['requests'] % 5 == 0:
request_count += 1
if request_count % 5 == 0:
global website_dir global website_dir
website_dir = str(ROOT_DIR+"/Marvel") website_dir = str(ROOT_DIR+"/Marvel")
elif request_count % 6 == 0: elif session['requests'] % 6 == 0:
website_dir = str(ROOT_DIR+"/Escape") website_dir = str(ROOT_DIR+"/Escape")
session.pop('requests', None)
else: else:
global WEBSITE_DIRS global WEBSITE_DIRS
try: try:
@ -41,8 +39,12 @@ def static_proxy(filename):
# Serve site index.html # Serve site index.html
@app.route('/', methods=['GET']) @app.route('/', methods=['GET'])
def index(): def index():
if 'requests' in session:
session['requests'] += 1
else:
session['requests'] = 1
current_website_dir() current_website_dir()
return send_from_directory(website_dir, 'index.html') return send_from_directory(website_dir, 'index.html')
if __name__ == "__main__": if __name__ == "__main__":
app.run() app.run(debug=True)