from flask import Flask, jsonify from flask_cors import CORS import threading import time import subprocess import sys import os import json app = Flask(__name__) CORS(app) # Enable CORS so the React frontend can fetch data def run_scrapers_periodically(): """Background thread to run scrapers every 60 minutes.""" while True: print(f"\n[{time.strftime('%Y-%m-%d %H:%M:%S')}] Starting scheduled background aggregation...") try: # Run aggregator.py subprocess.run([sys.executable, "aggregator.py"], check=True) # Run ai_training_scraper.py subprocess.run([sys.executable, "ai_training_scraper.py"], check=True) # Run upwork_scraper.py subprocess.run([sys.executable, "upwork_scraper.py"], check=True) print(f"[{time.strftime('%Y-%m-%d %H:%M:%S')}] All scrapers synced successfully.") except Exception as e: print(f"[{time.strftime('%Y-%m-%d %H:%M:%S')}] Aggregation error: {e}") # Wait for 60 minutes time.sleep(3600) @app.route("/") def health_check(): return jsonify({"status": "running", "platform": "Firstify Engine"}) @app.route("/api/startups") def get_startups(): try: with open("data.json", "r") as f: return jsonify(json.load(f)) except: return jsonify([]) @app.route("/api/training") def get_training(): try: with open("training_data.json", "r") as f: return jsonify(json.load(f)) except: return jsonify([]) @app.route("/api/upwork") def get_upwork(): try: with open("upwork_data.json", "r") as f: return jsonify(json.load(f)) except: return jsonify([]) if __name__ == "__main__": # Start the scraping thread threading.Thread(target=run_scrapers_periodically, daemon=True).start() # Start the Flask server # Hugging Face Spaces use port 7860 by default port = int(os.environ.get("PORT", 7860)) app.run(host="0.0.0.0", port=port)