babaTEEpe commited on
Commit
6fd696a
·
verified ·
1 Parent(s): 655b3af

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -56
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
- print(f"[{time.strftime('%Y-%m-%d %H:%M:%S')}] Aggregation complete.")
23
- except Exception as e:
24
- print(f"[{time.strftime('%Y-%m-%d %H:%M:%S')}] Aggregation error: {e}")
25
-
26
- # Wait for 60 minutes
27
- time.sleep(3600)
28
-
29
- @app.route("/")
30
- def health_check():
31
- return jsonify({"status": "running", "platform": "Firstify Engine"})
32
-
33
- @app.route("/api/startups")
34
- def get_startups():
35
- try:
36
- with open("data.json", "r") as f:
37
- return jsonify(json.load(f))
38
- except:
39
- return jsonify([])
40
-
41
- @app.route("/api/training")
42
- def get_training():
43
- try:
44
- with open("training_data.json", "r") as f:
45
- return jsonify(json.load(f))
46
- except:
47
- return jsonify([])
48
-
49
- if __name__ == "__main__":
50
- # Start the scraping thread
51
- threading.Thread(target=run_scrapers_periodically, daemon=True).start()
52
-
53
- # Start the Flask server
54
- # Hugging Face Spaces use port 7860 by default
55
- port = int(os.environ.get("PORT", 7860))
56
- app.run(host="0.0.0.0", port=port)
 
 
 
 
 
 
 
 
 
 
 
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)