Spaces:
Building
Building
from flask import Flask, jsonify | |
from generator import run # Import the generator script as a module | |
import asyncio | |
import time | |
from concurrent.futures import ThreadPoolExecutor | |
app = Flask(__name__) | |
# Create a ThreadPoolExecutor | |
executor = ThreadPoolExecutor(1) | |
latest_token = None | |
auth = None # Define auth as a global variable | |
def update_token_periodically(): | |
global latest_token, auth | |
while True: | |
event_loop = asyncio.new_event_loop() | |
asyncio.set_event_loop(event_loop) | |
success = False | |
while not success: | |
try: | |
latest_token, auth = event_loop.run_until_complete(run()) | |
if latest_token and auth: | |
success = True | |
print(f"cookie: {latest_token}, auth: {auth}") | |
else: | |
print("Token generation failed. Retrying...") | |
except Exception as e: | |
print(f"An error occurred: {e}. Retrying...") | |
if not success: | |
time.sleep(5) # Short delay before retrying | |
event_loop.close() | |
# Wait the established amount of time before generating new tokens | |
time.sleep(50 * 60) | |
def cookie(): | |
global latest_token, auth | |
if latest_token and auth: | |
return jsonify({"cookie": latest_token, "auth": auth}) | |
else: | |
return "Token not found.", 500 | |
if __name__ == '__main__': | |
# Start the background thread | |
executor.submit(update_token_periodically) | |
app.run(host='0.0.0.0', port=8080) | |