-
Notifications
You must be signed in to change notification settings - Fork 0
/
spaceapi_app.py
executable file
·143 lines (121 loc) · 4.42 KB
/
spaceapi_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
#!/usr/bin/env python3
# -*- coding: utf8 -*-
"""
A simple forwarder to display the C4's open state on our webserver.
Send a POST request to the '/newstate' endpoint to update state information.
The 'message' field may be an empty string, in which case it will not be
included in the final json.
Request is only accepted if the correct password (shared secret) is supplied.
The password is loaded from a file called 'local_creds.py'. You need to create
this file with a contents of 'pw = "sharedsecret"'.
EXAMPLE UPDATE CODE:
>>> from urllib import request
>>> request.urlopen('http://localhost:5000/newstate', data=b'password=sharedsecret&state=open&message=Hello World!').read()
>>> request.urlopen('http://localhost:5000/newstate', data=b'password=sharedsecret&state=closed&message=').read()
DB SETUP:
>>> from spaceapi_app import db, app
>>> with app.app_context(): db.create_all()
"""
from flask import Flask, request, redirect, jsonify
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
import calendar
import time
import sys
try:
import local_creds
except:
print('ERROR: local_creds.py not found.\n')
print(__doc__)
sys.exit(1)
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.sqlite3'
app.config.from_object(__name__)
db = SQLAlchemy(app)
if not app.debug:
import logging
from logging import FileHandler
file_handler = FileHandler("production.log")
file_handler.setLevel(logging.WARNING)
app.logger.addHandler(file_handler)
class ClubState(db.Model):
id = db.Column(db.Integer, primary_key=True)
time = db.Column(db.Integer)
open = db.Column(db.Boolean)
message = db.Column(db.String)
def __init__(self, open, message=''):
self.open = open
# utc timestamp
self.time = calendar.timegm(time.gmtime()) # python2
#self.time = int(datetime.now(tz=datetime.timezone.utc).timestamp())
self.message = message
def __repr__(self):
if self.open:
s = 'open'
else:
s = 'closed'
return '<State %s / %s from %d>' % (s, repr(self.message), self.time)
def make_space_json(state):
d = {
'api_compatibility': ['13', '14'],
'api': '0.13',
'space': 'CCC Cologne',
'logo': 'https://koeln.ccc.de/images/C4-logo_transparent_black.svg',
'url': 'https://koeln.ccc.de/',
'ext_ccc': "erfa",
'location': {
'address': 'Chaos Computer Club Cologne (c4) e.V., Heliosstr. 6a, 50825 Köln, Germany',
'lat': 50.9504142,
'lon': 6.9129647,
'timezone': 'Europe/Berlin',
},
'state': {
'open': None,
'lastchange': None,
# 'icon': {'open':'url','closed':'url'},
},
'contact': {
'irc': 'ircs://irc.hackint.org/#cccc',
'email': '[email protected]',
'twitter': '@ccc_koeln',
'phone': '+49 221-49 24 120',
'mastodon': '@[email protected]',
},
'issue_report_channels': ['twitter'], #XXX
'feeds': {
'blog': { 'type': 'rss', 'url': 'https://koeln.ccc.de/backend/updates.rdf' },
# 'wiki': { 'type': '', 'url': '' },
# 'calendar': { 'type': '', 'url': '' },
},
'projects': [
'https://github.com/cccc',
],
}
if state is not None:
d['state']['open'] = state.open
d['state']['lastchange'] = state.time
if state.message:
d['state']['message'] = state.message
return d
@app.route("/")
def state():
current_state = ClubState.query.order_by(ClubState.id.desc()).first()
d = make_space_json(current_state)
return jsonify(**d)
@app.route("/newstate", methods=["POST", ])
def save():
if request.method == "GET" or request.form["password"] != local_creds.pw:
return "FAil", 500
try:
newstate = ClubState(request.form['state'] == 'open',
request.form['message'])
db.session.add(newstate)
db.session.commit()
except:
return "Faiil.", 500
return redirect("/done")
@app.route("/done")
def done():
return "Success"
if __name__ == "__main__":
app.run(debug=True)