-
Notifications
You must be signed in to change notification settings - Fork 2
/
fordocker.py
94 lines (71 loc) · 2.22 KB
/
fordocker.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
from flask import Flask, escape, request, jsonify, send_file, render_template
from flask_cors import CORS, cross_origin
from humanfriendly import format_timespan
import worker
import threading
import asyncio
import os
import about
scrapingTaskActive = os.environ.get("SCRAPING_TASK_ACTIVE", False)
port = os.environ.get("PORT", "8080")
search = os.environ.get("SEARCH_QUERY", "Elecciones2020")
since = os.environ.get("SINCE_DATE", "2019-10-01")
interval = os.environ.get("INTERVAL_TIME", 1800)
interval = int(interval)
app = Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
stopFlag = threading.Event()
loop = asyncio.new_event_loop()
config = {
"scraping_task_active": scrapingTaskActive,
"query_search": search,
"since_date": since,
"time_interval": format_timespan(interval)
}
gw = worker.GovernmentWorker(
stopFlag, loop, workspace="/data", search=search, since=since, interval_s=interval)
@app.route('/freqs')
@cross_origin()
def freqs():
data = [{"text": freq[1], "value": freq[0]} for freq in gw.freqs]
result = {}
result["data"] = data
result["last_update"] = gw.last_update()
return jsonify(result)
@app.route('/about')
@cross_origin()
def about_us():
return render_template("pages/home.html")
@app.route('/about/json')
@cross_origin()
def about_us_json():
result = {}
result["description"] = about.description
result["about"] = about.about
result["config"] = config
result["endpoints"] = about.endpoints
result["endpoints"]["dataframes"] = [
f"/dataframe/{df}" for df in gw.available_dataframes()]
return jsonify(result)
@app.route('/dataframe/<name>')
@cross_origin()
def dataframe(name):
if not name in gw.available_dataframes():
return jsonify({"error": "invalid or not exist dataframe name"})
return send_file(gw.path_of_dataframe(name))
@app.route('/dataframes')
@cross_origin()
def dataframes():
result = {}
result["data"] = gw.available_dataframes()
return jsonify(result)
@app.route('/last-dataframe')
@cross_origin()
def lastdataframe():
result = {}
result["data"] = gw.last_dataframe
return jsonify(result)
if scrapingTaskActive:
gw.start()
app.run(host="0.0.0.0", port=port)