forked from fixme-lausanne/Twitter-Hackerspace-Status
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spaceapi.py
170 lines (158 loc) · 5.04 KB
/
spaceapi.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
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
#!/usr/bin/env python
# -*- coding: utf8 -*-
import cgitb, json, MySQLdb, sys, time
from datetime import datetime
import vobject
from pprint import pprint
cgitb.enable()
print 'Content-Type: application/json'
#print 'Access-Control-Allow-Origin: *' #Provide this server side or in this script
#print 'Cache-Control: no-cache'
print
#
# Default description of the Space API
#
api = {
'api': '0.13',
'space': 'FIXME',
'logo': 'https://fixme.ch/sites/default/files/Logo5_v3-mini.png',
'url': 'https://fixme.ch',
'contact': {
'phone': '+41216220734',
'keymaster': ['+41797440880'],
'irc': 'irc://freenode/#fixme',
'twitter': '@_fixme',
'email': '[email protected]',
'ml': '[email protected]',
'facebook': 'https://www.facebook.com/fixmehackerspace',
'wiki': 'https://wiki.fixme.ch',
'chat': 'https://chat.fixme.ch',
},
'location': {
'lon': 6.591292,
'lat': 46.532372,
'address': 'Chemin du Closel 5, 1020 Renens, Switzerland',
},
'state': {
'icon': {
'open': 'https://fixme.ch/sites/default/files/logo-open.png',
'closed': 'https://fixme.ch/sites/default/files/logo-closed.png',
},
'ext_duration': 0, # Custom field for the open duration
},
#'events': [],
'feeds': {
'blog': {'type': 'rss', 'url': 'https://fixme.ch/rss.xml'},
'wiki': {'type': 'rss', 'url': 'https://fixme.ch/w/index.php?title=Special:RecentChanges&feed=atom'},
'calendar': {'type': 'ical','url': 'https://www.google.com/calendar/ical/sruulkb8vh28dim9bcth8emdm4%40group.calendar.google.com/public/basic.ics'},
},
#'stream': {
# 'html': 'http://webcam.fixme.ch',
# },
'issue_report_channels': ['email', 'twitter'],
'sensors': {
#'temperature': [
# {
# 'value': 0,
# 'unit': '°C',
# 'location': 'Bitcoin farm',
# },
# {
# 'value': 0,
# 'unit': '°C',
# 'location': 'Room',
# }
#],
'people_now_present': [{
'value': 0,
'unit': 'device(s)',
'description': 'Number of devices in the DHCP range',
}],
'total_member_count': [ #2016-10-21
{
'value': 46,
'unit': 'premium members',
},
{
'value': 65,
'unit': 'standard members',
},
{
'value': 111,
'unit': 'total members',
},
],
}
}
#
# Get sensors data
#
# People
user_online = 0
try:
user_online = int(open('/var/log/user_online.log', 'r').read())
except:
pass
if user_online > 0:
api['sensors']['people_now_present'][0]['value'] = user_online
# Temp
nb_temp = 2
temp = 0.0
for i in xrange(nb_temp):
try:
temp = open('/var/log/temp_%d.log' % (i+1), 'r').read()
except:
pass
if temp != '':
api['sensors']['temperature'][i]['value'] = float(temp)
#
# Get last 10 events
#
#try:
# ical = vobject.readOne(open('./civicrm_ical.ics', 'r').read())
# for e in ical.vevent_list[:10]:
# ts = int(time.mktime(e.dtstart.value.timetuple()))
# api['events'].append({
# 'name': e.summary.value,
# 'type': e.categories.value[0],
# 'timestamp': ts,
# 't': ts,
# })
#except Exception,e:
# pass
#
# Get Open/Close status
#
try:
db = MySQLdb.connect(host="localhost", user="hs_status", passwd="r3fG!yNx", db="hackerspace_status")
c = db.cursor()
c.execute('select pub_date, duration, open from hackerspace_status order by id desc limit 1;')
result = c.fetchone()
db.close()
except Exception,e:
result = None
if result != None and len(result) == 3:
res_date = result[0]
res_duration = result[1]
res_open = result[2]
else:
res_date = datetime.now()
res_duration = 0
res_open = False
#
# Update API
#
api['state']['open'] = bool(res_open)
api['state']['lastchange'] = time.mktime(res_date.timetuple())
api['state']['ext_duration'] = int(res_duration)
diff = datetime.now() - res_date
if res_open == True and diff.seconds / 3600 >= res_duration:
api['state']['message'] = 'The space may be closed, the initial duration of %i hour(s) is exceeded.' % res_duration
elif res_open == True:
api['state']['message'] = 'The space is open.'
else:
api['state']['message'] = 'The space is closed.'
#
# Pretty print JSON
#
print json.dumps(api)