Update app.py
Browse files
app.py
CHANGED
|
@@ -1,56 +1,66 @@
|
|
| 1 |
-
from flask import Flask, jsonify
|
| 2 |
-
from flask_cors import CORS
|
| 3 |
-
import threading
|
| 4 |
-
import time
|
| 5 |
-
import subprocess
|
| 6 |
-
import sys
|
| 7 |
-
import os
|
| 8 |
-
import json
|
| 9 |
-
|
| 10 |
-
app = Flask(__name__)
|
| 11 |
-
CORS(app) # Enable CORS so the React frontend can fetch data
|
| 12 |
-
|
| 13 |
-
def run_scrapers_periodically():
|
| 14 |
-
"""Background thread to run scrapers every 60 minutes."""
|
| 15 |
-
while True:
|
| 16 |
-
print(f"\n[{time.strftime('%Y-%m-%d %H:%M:%S')}] Starting scheduled background aggregation...")
|
| 17 |
-
try:
|
| 18 |
-
# Run aggregator.py
|
| 19 |
-
subprocess.run([sys.executable, "aggregator.py"], check=True)
|
| 20 |
-
# Run ai_training_scraper.py
|
| 21 |
-
subprocess.run([sys.executable, "ai_training_scraper.py"], check=True)
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
print(f"[{time.strftime('%Y-%m-%d %H:%M:%S')}]
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, jsonify
|
| 2 |
+
from flask_cors import CORS
|
| 3 |
+
import threading
|
| 4 |
+
import time
|
| 5 |
+
import subprocess
|
| 6 |
+
import sys
|
| 7 |
+
import os
|
| 8 |
+
import json
|
| 9 |
+
|
| 10 |
+
app = Flask(__name__)
|
| 11 |
+
CORS(app) # Enable CORS so the React frontend can fetch data
|
| 12 |
+
|
| 13 |
+
def run_scrapers_periodically():
|
| 14 |
+
"""Background thread to run scrapers every 60 minutes."""
|
| 15 |
+
while True:
|
| 16 |
+
print(f"\n[{time.strftime('%Y-%m-%d %H:%M:%S')}] Starting scheduled background aggregation...")
|
| 17 |
+
try:
|
| 18 |
+
# Run aggregator.py
|
| 19 |
+
subprocess.run([sys.executable, "aggregator.py"], check=True)
|
| 20 |
+
# Run ai_training_scraper.py
|
| 21 |
+
subprocess.run([sys.executable, "ai_training_scraper.py"], check=True)
|
| 22 |
+
# Run upwork_scraper.py
|
| 23 |
+
subprocess.run([sys.executable, "upwork_scraper.py"], check=True)
|
| 24 |
+
print(f"[{time.strftime('%Y-%m-%d %H:%M:%S')}] All scrapers synced successfully.")
|
| 25 |
+
except Exception as e:
|
| 26 |
+
print(f"[{time.strftime('%Y-%m-%d %H:%M:%S')}] Aggregation error: {e}")
|
| 27 |
+
|
| 28 |
+
# Wait for 60 minutes
|
| 29 |
+
time.sleep(3600)
|
| 30 |
+
|
| 31 |
+
@app.route("/")
|
| 32 |
+
def health_check():
|
| 33 |
+
return jsonify({"status": "running", "platform": "Firstify Engine"})
|
| 34 |
+
|
| 35 |
+
@app.route("/api/startups")
|
| 36 |
+
def get_startups():
|
| 37 |
+
try:
|
| 38 |
+
with open("data.json", "r") as f:
|
| 39 |
+
return jsonify(json.load(f))
|
| 40 |
+
except:
|
| 41 |
+
return jsonify([])
|
| 42 |
+
|
| 43 |
+
@app.route("/api/training")
|
| 44 |
+
def get_training():
|
| 45 |
+
try:
|
| 46 |
+
with open("training_data.json", "r") as f:
|
| 47 |
+
return jsonify(json.load(f))
|
| 48 |
+
except:
|
| 49 |
+
return jsonify([])
|
| 50 |
+
|
| 51 |
+
@app.route("/api/upwork")
|
| 52 |
+
def get_upwork():
|
| 53 |
+
try:
|
| 54 |
+
with open("upwork_data.json", "r") as f:
|
| 55 |
+
return jsonify(json.load(f))
|
| 56 |
+
except:
|
| 57 |
+
return jsonify([])
|
| 58 |
+
|
| 59 |
+
if __name__ == "__main__":
|
| 60 |
+
# Start the scraping thread
|
| 61 |
+
threading.Thread(target=run_scrapers_periodically, daemon=True).start()
|
| 62 |
+
|
| 63 |
+
# Start the Flask server
|
| 64 |
+
# Hugging Face Spaces use port 7860 by default
|
| 65 |
+
port = int(os.environ.get("PORT", 7860))
|
| 66 |
+
app.run(host="0.0.0.0", port=port)
|