Example #1
0
def send_to_printer(subscription_id, delays, user_settings):
    content = render_template('edition.html', results=delays,
                              from_station=user_settings['from_station'],
                              to_station=user_settings['to_station'])

    # check if we have new content
    tag = '"%s"' % (
        hashlib.md5(
            content + dt.datetime.utcnow().strftime('%d%m%Y')
        ).hexdigest())


    stored_tag = db().hget('train_delays:content_tag', subscription_id)
    #print "stored: " + stored_content
    #print "content: " + content
    print "new content %s" % (stored_tag != tag)
    if not stored_tag or stored_tag != tag:
        db().hset('train_delays:content_tag', subscription_id, tag)
    elif stored_tag == tag:
        return

    print 'DO REQUEST'
    # Post this content to BERG Cloud using OAuth.
    response, data = client().request(
        user_settings['endpoint'],
        method='POST',
        body=content,
        headers={'Content-Type': 'text/html; charset=utf-8'})

    print 'RESPONSE STATUS: %d' % response.status
    if response.status == '410':
        # By sending a 410 status code, BERG Cloud has informed us this
        # user has unsubscribed. So delete their subscription from our
        # database.
        db().hdel('train_delays:subscriptions', subscription_id)
    else:
        pass
Example #2
0
def push_post():
    subscribed_count = 0
    unsubscribed_count = 0

    for subscription_id, config in db().hgetall('train_delays:subscriptions').iteritems():
        # config contains the subscriber's language, name and endpoint.
        config = json.loads(config)

        # Get a random greeting in this subscriber's chosen language.
        #greeting = choice(app.config['GREETINGS'][ config['lang'] ])

        # Make the HTML content to push to the printer.
        content = render_template(
                                'edition.html')
                                #greeting="%s, %s" % (greeting, config['name']))

        # Post this content to BERG Cloud using OAuth.
        response, data = client().request(
                        config['endpoint'],
                        method='POST',
                        body=content,
                        headers={'Content-Type': 'text/html; charset=utf-8'})

        if response.status == '410':
            # By sending a 410 status code, BERG Cloud has informed us this
            # user has unsubscribed. So delete their subscription from our
            # database.
            db().hdel('push_example:subscriptions', subscription_id)
            unsubscribed_count += 1
        else:
            subscribed_count += 1

    # Show the same form again, with a message to confirm this worked.
    return render_template('push.html',
                            pushed=True,
                            subscribed_count=subscribed_count,
                            unsubscribed_count=unsubscribed_count)