hswaw-spaceapi/spaceapi.py

86 lines
3.0 KiB
Python

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# Warsaw Hackerspace SpaceAPI service implementation
# 2013 Piotr 'inf' Dobrowolski <informatic@hackerspace.pl>
from flask import Flask, jsonify
import json
import urllib2
import traceback
import time
app = Flask(__name__)
app.config.from_pyfile('main.cfg')
@app.route('/')
def api_index():
space_open = None
people_now_present = {'value': 0}
open_day = False
try:
at_request = urllib2.Request(app.config['AT_API_URL'])
at_request.add_header('User-Agent', 'HSWAWSpaceAPI/1.0 +https://spaceapi.hackerspace.pl')
at_response = urllib2.urlopen(at_request).read()
at_object = json.loads(at_response)
space_open = len(at_object['users']) > 0
people_now_present['value'] = len(at_object['users'])
people_now_present['names'] = list(user['login'] for user in at_object['users'])
open_day = space_open and int(time.strftime("%w")) == app.config['OPEN_DAY_WEEKDAY'] \
and int(time.strftime("%H")) >= app.config['OPEN_DAY_BEGIN_HOUR']
except:
traceback.print_exc()
result = {
"api": "0.13",
"space": "Warsaw Hackerspace",
"logo": "https://static.hackerspace.pl/img/syrenka-black.png",
"url": "https://hackerspace.pl",
"location": {
"lat": 52.2462275,
"lon": 21.003067,
"address": "ul. DÅ‚uga 44/50, 00-241 Warszawa, Poland",
},
"state": {
"open": space_open,
"message": "open for public" if open_day else "members only",
# @TODO: customized *space* logo
"icon": {
"open": "https://static.hackerspace.pl/img/status-open-small.png",
"closed": "https://static.hackerspace.pl/img/status-closed-small.png",
}
},
"contact": {
"irc": "irc://chat.freenode.net/#hackerspace-pl",
"twitter": "@hackerspacepl",
"facebook": "hackerspacepl",
"ml": "waw@lists.hackerspace.pl",
},
"issue_report_channels": [
"ml"
],
"projects": [
"https://wiki.hackerspace.pl/projects",
],
"feeds": {
"blog": {"type": "rss", "url": "https://blog.hackerspace.pl/feed/"},
"calendar": {"type": "ical", "url": "https://www.google.com/calendar/ical/hackerspacewaw%40gmail.com/public/basic.ics"},
"wiki": {"type": "rss", "url": "https://wiki.hackerspace.pl/feed.php"},
},
"sensors": {
"people_now_present": [people_now_present]
},
}
# SpaceAPI version <0.13 compliance
result['open'] = result['state']['open']
result['icon'] = result['state']['icon']
return jsonify(result)
if __name__ == '__main__':
app.run(debug=True)