Last active
June 28, 2018 13:12
-
-
Save romanpeters/b210f01e5641b0a7b704ea581fe2eb31 to your computer and use it in GitHub Desktop.
Revisions
-
romanpeters revised this gist
Jun 28, 2018 . 1 changed file with 5 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -53,12 +53,14 @@ def sync(): 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/') -
romanpeters revised this gist
Jun 12, 2018 . 1 changed file with 4 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -36,8 +36,10 @@ def id_from_event(event: dict) -> str: 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/') -
romanpeters created this gist
Jun 12, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,65 @@ # -*- 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() 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: diff = [event for event in new if event not in old] for event in diff: feed.add(**event_to_feed(event)) api = new return "New event" return "Synced" @app.route('/koala.xml') @app.route('/koala.xml/') def koala_rss(): """ RSS feed URL """ return feed.get_response()