Skip to content

Instantly share code, notes, and snippets.

@romanpeters
Last active June 28, 2018 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romanpeters/b210f01e5641b0a7b704ea581fe2eb31 to your computer and use it in GitHub Desktop.
Save romanpeters/b210f01e5641b0a7b704ea581fe2eb31 to your computer and use it in GitHub Desktop.
Koala RSS
# -*- coding: utf-8 -*-
from app import app
from urllib.request import urlopen
import datetime
from werkzeug.contrib.atom import AtomFeed
import json
url = "wwww.your-site.com"
feed = AtomFeed("Sticky activiteiten", feed_url=f"{url}/koala.xml", url=url)
def get_koala() -> list:
req = urlopen('https://koala.svsticky.nl/api/activities/')
data = req.read().decode('utf-8')
return json.loads(data)
def event_to_feed(event: dict) -> dict:
event_id = id_from_event(event)
if not event_id:
event_id = ""
now = datetime.datetime.now()
item = dict(title=event.get("name"),
content_type='html',
author="SV Sticky",
url=f"https://koala.svsticky.nl/activities/{event_id}",
updated=now,
published=now)
return item
def id_from_event(event: dict) -> str:
try:
return "".join([c for c in event["poster"][:46] if c.isdigit()])
except:
pass
api = get_koala()
# Add old events (disabled)
#for event in api:
# feed.add(**event_to_feed(event))
@app.route('/koala-sync')
@app.route('/koala-sync/')
def sync():
"""
Syncs the Koala API and the RSS feed.
Poll this URL at an interval of your choice.
"""
global feed, api
old = api
new = get_koala()
if new != old:
# use event names to check for new additions
old_names = [event['name'] for event in old]
diff = [event for event in new if event['name'] not in old_names]
for event in diff:
feed.add(**event_to_feed(event))
api = new
return '{"new": true}'
return '{"new": false}'
@app.route('/koala.xml')
@app.route('/koala.xml/')
def koala_rss():
""" RSS feed URL """
return feed.get_response()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment