-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapplication.py
More file actions
191 lines (147 loc) · 5.02 KB
/
application.py
File metadata and controls
191 lines (147 loc) · 5.02 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import os
import pugsql
from flask import Flask, request, make_response, jsonify, render_template
from flask_cors import CORS
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
from flask_jwt_extended import (
JWTManager,
jwt_required,
set_access_cookies,
unset_access_cookies,
get_jwt_identity,
jwt_optional,
)
from newsSpiders.webapi import (
sites,
articles,
publications,
stats,
monitor,
auth,
playground,
)
import dotenv
dotenv.load_dotenv()
app = Flask(__name__)
app.config["JWT_SECRET_KEY"] = os.getenv("API_SECRET_KEY")
app.config["JWT_TOKEN_LOCATION"] = ("headers", "cookies")
app.config["JWT_ACCESS_COOKIE_PATH"] = [
"/articles",
"/sites",
"/stats",
"/publications",
]
jwt = JWTManager(app)
CORS(app)
limiter = Limiter(app, key_func=get_remote_address, default_limits=["10/second"])
# scraper db
scraper_queries = pugsql.module("queries/")
scraper_queries.connect(os.getenv("DB_URL"))
# playground db
playground_queries = pugsql.module("queries/playground")
playground_queries.connect(os.getenv("PLAY_DB_URL"))
response_headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": "true",
"Access-Control-Allow-Headers": "Authorization, Content-Type",
"Access-Control-Allow-Methods": "POST, GET, OPTIONS",
"Content-Type": "application/json",
}
api_password = os.getenv("API_PASSWORD")
def create_response(body, status_code=200, headers=dict()):
if isinstance(body, list):
body = jsonify(body)
resp = make_response(body, status_code, response_headers)
resp.headers.update(headers)
return resp
@app.route("/", methods=["GET"])
@jwt_optional
def hello():
username = get_jwt_identity()
if username:
welcome_msg = f"Hello {username}, welcome to the api of g0v 0archive"
else:
welcome_msg = "Hello, welcome to the api of g0v 0archive, please login first."
return create_response(welcome_msg)
@app.route("/login", methods=["GET", "POST"])
def login():
if request.method == "GET":
headers = {"Content-Type": "text/html"}
return create_response(render_template("login_form.html"), 200, headers)
else:
result = auth.post_login(api_password)
access_token = result["body"].get("access_token")
response = create_response(**result)
if access_token:
set_access_cookies(response, access_token)
return response
@app.route("/logout", methods=["GET", "POST"])
def logout():
response = create_response("Logout successfully.")
unset_access_cookies(response)
return response
@app.route("/health", methods=["GET"])
def check_health():
result = monitor.check_health(scraper_queries)
return create_response(**result)
@app.route("/variables", methods=["GET"])
@jwt_required
def get_variable():
result = monitor.get_variables(scraper_queries, request.args)
return create_response(**result)
@app.route("/articles", methods=["GET"])
@jwt_required
def get_articles():
result = articles.get_articles(scraper_queries, request.args)
return create_response(**result)
@app.route("/articles/<int:article_id>", methods=["GET"])
@jwt_required
def get_article_by_id(article_id):
result = articles.get_article_by_id(scraper_queries, article_id)
return create_response(**result)
@app.route("/sites", methods=["GET"])
@jwt_required
def get_sites():
result = sites.get_sites(scraper_queries, request.args)
return create_response(**result)
@app.route("/sites/active", methods=["GET"])
@jwt_required
def get_active_sites():
result = sites.get_active_sites(scraper_queries)
return create_response(**result)
@app.route("/sites/<int:site_id>", methods=["GET"])
@jwt_required
def get_site_by_id(site_id):
result = sites.get_site_by_id(scraper_queries, site_id)
return create_response(**result)
@app.route("/sites/<int:site_id>/article_count", methods=["GET"])
@jwt_required
def get_site_article_count(site_id):
result = sites.get_article_count(scraper_queries, site_id)
return create_response(**result)
@app.route("/sites/<int:site_id>/latest_article", methods=["GET"])
@jwt_required
def get_site_latest_article(site_id):
result = sites.get_latest_article(scraper_queries, site_id)
return create_response(**result)
@app.route("/stats", methods=["GET"])
@jwt_required
def get_stats():
result = stats.get_stats(scraper_queries, request.args)
return create_response(**result)
@app.route("/publications", methods=["GET"])
@jwt_required
def search_publication():
result = publications.search_publication(os.getenv("PUB_SEARCH_URL"), request.args)
return create_response(**result)
@app.route("/playground/random", methods=["GET"])
def get_random_title():
result = playground.get_random_title(playground_queries)
return create_response(**result)
@app.route("/playground/add_record", methods=["POST"])
def post_token():
result = playground.add_record(playground_queries)
return create_response(**result)
if __name__ == "__main__":
app.run(debug=True)