-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLambdaSpaceAPI.py
executable file
·61 lines (47 loc) · 1.62 KB
/
LambdaSpaceAPI.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
#!/usr/bin/env python2
# -*- coding: <UTF-8> -*-
"""LambdaSpace's API"""
#from sys import exit
from urllib2 import urlopen, URLError
import json
from flask_cors import CORS, cross_origin
from flask import Flask, jsonify, abort, make_response
__author__ = "Kopsacheilis Charalampos, Mpazakas Filippos"
__credits__ = ["Kopsacheilis Charalampos", "Mpazakas Filippos", "Kolokotronis Panagiotis"]
__version__ = "1.0"
LSAPI = Flask(__name__)
CORS(LSAPI)
def check_status(jsobj):
"""Changes status depending on the number of devices connected to router."""
url = 'https://www.lambdaspace.gr/hackers.txt'
try:
retv = urlopen(url)
except URLError:
raise URLError
jsobj["state"]["open"] = (int(retv.read() > 0))
return jsobj
@LSAPI.errorhandler(404)
def not_found(error):
"""Return JSONified 404"""
return make_response(jsonify({'error': 'Not found'}), 404)
@LSAPI.errorhandler(400)
def bad_request(error):
"""Return JSONified 400"""
return make_response(jsonify({'error': 'Bad request'}), 400)
@LSAPI.errorhandler(500)
def int_serv_error(error):
"""Return JSONified 500"""
return make_response(jsonify({'error': 'Internla Server Error'}), 500)
@LSAPI.route('/api/v1.0/SpaceAPI')
def change_state():
"""Load json string from 'LambdaSpaceAPI.json' and convert to dictionary"""
with open('LambdaSpaceAPI.json') as jsfile:
jstring = jsfile.read()
json_data = json.loads(jstring)
try:
json_data = check_status(json_data)
except URLError:
abort(500)
return jsonify(json_data)
if __name__ == '__main__':
LSAPI.run(debug=False, threaded=True)