Skip to content

Instantly share code, notes, and snippets.

@romanpeters
Last active June 28, 2018 13:12
Show Gist options
  • Select an option

  • Save romanpeters/b210f01e5641b0a7b704ea581fe2eb31 to your computer and use it in GitHub Desktop.

Select an option

Save romanpeters/b210f01e5641b0a7b704ea581fe2eb31 to your computer and use it in GitHub Desktop.

Revisions

  1. romanpeters revised this gist Jun 28, 2018. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions koala_rss.py
    Original file line number Diff line number Diff line change
    @@ -53,12 +53,14 @@ def sync():
    new = get_koala()

    if new != old:
    diff = [event for event in new if event not in 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 event"
    return "Synced"
    return '{"new": true}'
    return '{"new": false}'

    @app.route('/koala.xml')
    @app.route('/koala.xml/')
  2. romanpeters revised this gist Jun 12, 2018. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions koala_rss.py
    Original file line number Diff line number Diff line change
    @@ -36,8 +36,10 @@ def id_from_event(event: dict) -> str:
    pass

    api = get_koala()
    for event in api:
    feed.add(**event_to_feed(event))

    # Add old events (disabled)
    #for event in api:
    # feed.add(**event_to_feed(event))

    @app.route('/koala-sync')
    @app.route('/koala-sync/')
  3. romanpeters created this gist Jun 12, 2018.
    65 changes: 65 additions & 0 deletions koala_rss.py
    Original 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()