summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPiotr Dobrowolski <admin@tastycode.pl>2013-09-03 15:51:30 +0200
committerPiotr Dobrowolski <admin@tastycode.pl>2013-09-03 15:51:30 +0200
commit3a8c5412faf92b6654e69cf77215e62ee64f1d3e (patch)
tree1cecd76946c9e5d81e2bde535f80008a39a8ea99
parent745889e920367d2074578602a4bad46e5e673b29 (diff)
downloadwww-main-3a8c5412faf92b6654e69cf77215e62ee64f1d3e.tar.gz
www-main-3a8c5412faf92b6654e69cf77215e62ee64f1d3e.tar.bz2
www-main-3a8c5412faf92b6654e69cf77215e62ee64f1d3e.zip
spaceapi support
-rw-r--r--main.cfg.dist5
-rw-r--r--main.py75
2 files changed, 78 insertions, 2 deletions
diff --git a/main.cfg.dist b/main.cfg.dist
index cfe04f4..8137962 100644
--- a/main.cfg.dist
+++ b/main.cfg.dist
@@ -6,3 +6,8 @@ FEEDS = [
]
MAX_ENTRIES = 10
+
+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
diff --git a/main.py b/main.py
index f9b6d8c..72ee6b2 100644
--- a/main.py
+++ b/main.py
@@ -4,8 +4,9 @@ import feedparser
import requests
import string
import random
-from flask import Flask, render_template, request, flash, session, abort
-from time import mktime
+import json
+from flask import Flask, render_template, request, flash, session, abort, jsonify
+from time import mktime, strftime
from datetime import datetime
app = Flask('main')
@@ -81,6 +82,76 @@ def about_en():
if request.method == "POST":
parse_subscribe_requests()
return render_template('about_en.html', entries=pull_feed_entries())
+
+@app.route('/spaceapi')
+def spaceapi():
+ space_open = None
+ people_now_present = {'value': 0}
+ open_day = False
+
+ try:
+ at_response = requests.get(app.config['AT_API_URL'], headers={
+ 'User-Agent': 'HSWAWSpaceAPI/1.0 +https://hackerspace.pl/spaceapi'
+ })
+ at_object = json.loads(at_response.content)
+
+ 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(strftime("%w")) == app.config['OPEN_DAY_WEEKDAY'] \
+ and int(strftime("%H")) >= app.config['OPEN_DAY_BEGIN_HOUR']
+ except:
+ import traceback
+ 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": "atom", "url": "https://blog.hackerspace.pl/feed/atom/"},
+ "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)
+
@app.before_request
def csrf_protect():
if request.method == "POST":