-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
147 lines (115 loc) · 3.68 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import json
from flask import Flask, jsonify, request
from flask.helpers import send_from_directory
from flask_cors import CORS, cross_origin
from pymongo_db import PyMongo_DB
from os.path import join, dirname
from dotenv import load_dotenv
from bson import json_util
from bson.objectid import ObjectId
from flask_apscheduler import APScheduler
import requests
class Config:
SCHEDULER_API_ENABLED = True
dotenv_path = join(dirname(__file__), '.env')
load_dotenv(dotenv_path)
app = Flask(__name__, static_folder='frontend/build', static_url_path='')
app.config.from_object(Config())
mongo = PyMongo_DB()
db = mongo.get_database()
collection = db["internships-2025"]
scheduler = APScheduler()
# Schedule updates to mongodb
@scheduler.task('interval', id='update-listings', seconds=300,
misfire_grace_time=900)
def update_db():
url = 'https://application-scraper-4f768c7eaca5.herokuapp.com/internships'
internships = requests.get(url).content
mongo.insert_docs(json.loads(internships))
scheduler.start()
CORS(app)
@app.route('/')
@cross_origin()
def serve():
return send_from_directory(app.static_folder, 'index.html')
# Serve data to frontend
@app.route('/api/internships', methods=['GET'])
@cross_origin()
def get_data():
data = collection.find()
return json.loads(json_util.dumps(data))
# Update internship data points
@app.route('/api/internships/update/<_id>', methods=['PUT'])
@cross_origin()
def update_applied(_id):
try:
data = request.get_json()
collection.update_one(
{'_id': ObjectId(_id)},
{'$set': data}
)
return jsonify({'message': 'Item updated successfully'}), 200
except Exception as e:
return jsonify({'error': str(e)}), 404
@app.route('/api/internships/update/date_applied/<_id>', methods=['PUT'])
@cross_origin()
def update_date_applied(_id):
try:
data = request.get_json()
collection.update_one(
{'_id': ObjectId(_id)},
{'$set': data}
)
return jsonify({'message': 'Item updated successfully'}), 200
except Exception as e:
return jsonify({'error': str(e)}), 404
@app.route('/api/internships/update/referral/<_id>', methods=['PUT'])
@cross_origin()
def update_referral(_id):
try:
data = request.get_json()
collection.update_one(
{'_id': ObjectId(_id)},
{'$set': data}
)
return jsonify({'message': 'Item updated successfully'}), 200
except Exception as e:
return jsonify({'error': str(e)}), 404
# Link to Sirus' microservice
auth_url = 'https://glacial-plains-67311-bdf01ddd306c.herokuapp.com/'
@app.route('/api/new_user', methods=['POST'])
@cross_origin()
def new_user():
url = auth_url + 'auth/register'
try:
data = request.get_json()
print(data)
response = requests.post(url, json=data)
print(response.json())
return response.json()
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/login', methods=['POST'])
@cross_origin()
def login():
url = auth_url + 'auth/login'
try:
data = request.get_json()
print(data)
response = requests.post(url, json=data)
print(response.json())
return response.json()
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/logout', methods=['POST'])
@cross_origin()
def logout():
url = auth_url + 'auth/logout'
try:
response = requests.post(url)
print(response.json())
return response.json()
except Exception as e:
return jsonify({'error': str(e)}), 500
if __name__ == '__main__':
app.run()