| 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)
|
|
|
| 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:
|
|
|
| subprocess.run([sys.executable, "aggregator.py"], check=True)
|
|
|
| subprocess.run([sys.executable, "ai_training_scraper.py"], check=True)
|
|
|
| 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}")
|
|
|
|
|
| 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__":
|
|
|
| threading.Thread(target=run_scrapers_periodically, daemon=True).start()
|
|
|
|
|
|
|
| port = int(os.environ.get("PORT", 7860))
|
| app.run(host="0.0.0.0", port=port)
|
|
|