-
Notifications
You must be signed in to change notification settings - Fork 4
/
api.py
55 lines (46 loc) · 1.73 KB
/
api.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
import os
import logging
import configparser
import uptime
from datetime import datetime
from flask import Flask, jsonify
config = configparser.ConfigParser()
config.read('config.ini')
API_PATH = config.get('API', 'API_FILE_PATH', fallback='~')
app = Flask(__name__)
# Set log level to Warning
log = logging.getLogger('werkzeug')
log.setLevel(logging.WARNING)
@app.route('/check_view')
def api_check_view():
# Construct the path to the file in the user's home directory
view_status_file = os.path.join(os.path.expanduser(API_PATH), 'view_status.txt')
with open(view_status_file, 'r') as f:
result = f.read()
return jsonify(view_status=result)
@app.route('/get_system_uptime')
def get_system_uptime():
# Get the uptime in seconds
uptime_seconds = uptime.uptime()
return jsonify(system_uptime=uptime_seconds)
@app.route('/get_script_uptime')
def api_get_script_uptime():
# Construct the path to the file in the user's home directory
script_start_time_file = os.path.join(os.path.expanduser(API_PATH), 'script_start_time.txt')
with open(script_start_time_file, 'r') as f:
script_start_time = datetime.strptime(f.read(), '%Y-%m-%d %H:%M:%S.%f')
script_uptime = datetime.now() - script_start_time
uptime_seconds = script_uptime.total_seconds()
return jsonify(script_uptime=uptime_seconds)
@app.route('/admin')
def admin():
view_status = api_check_view().json['view_status']
script_uptime = api_get_script_uptime().json['script_uptime']
system_uptime = get_system_uptime().json['system_uptime']
return jsonify(
view_status=view_status,
script_uptime=script_uptime,
system_uptime=system_uptime
)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)