commit a013922f4a9806587714f94cd41a0dfba4b1baf0 Author: Piotr Dobrowolski Date: Mon Sep 2 23:02:56 2013 +0200 Initial commit diff --git a/spaceapi.py b/spaceapi.py new file mode 100644 index 0000000..46042a8 --- /dev/null +++ b/spaceapi.py @@ -0,0 +1,88 @@ +#!/usr/bin/env python2 +# -*- coding: utf-8 -*- + +# Warsaw Hackerspace SpaceAPI service implementation +# 2013 Piotr 'inf' Dobrowolski + +from flask import Flask +import json +import urllib2 +import traceback +import time + +AT_API_URL = "https://at.hackerspace.pl/api" +OPEN_DAY_WEEKDAY = 4 # Thursday (according to %w of http://docs.python.org/2/library/time.html#time.strftime) +OPEN_DAY_BEGIN_HOUR = 18 + +app = Flask(__name__) + +@app.route('/') +def api_index(): + space_open = None + people_now_present = {'value': 0} + open_day = False + + try: + at_request = urllib2.Request(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")) == OPEN_DAY_WEEKDAY \ + and int(time.strftime("%H")) >= 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 json.dumps(result) + +if __name__ == '__main__': + app.run(debug=True)