Skip to content

Instantly share code, notes, and snippets.

@scascketta
Created February 18, 2016 06:26
Show Gist options
  • Save scascketta/27804594c3a8bac55201 to your computer and use it in GitHub Desktop.
Save scascketta/27804594c3a8bac55201 to your computer and use it in GitHub Desktop.
Monitor GTFS-realtime vehicle positions using Cronitor.io.
from __future__ import print_function
import os
import json
import urllib2
from datetime import datetime
LAMBDA_API = 'https://vni1fkrx5b.execute-api.us-east-1.amazonaws.com/prod/gtfsrt-debug'
GTFSRT_FEED = 'https://data.texas.gov/download/eiei-9rpf/application/octet-stream'
CRONITOR = '<YOUR-CRONITOR-URL>'
def lambda_handler(event, context):
url = '{}?url={}'.format(LAMBDA_API, GTFSRT_FEED)
res = urllib2.urlopen(url)
data = json.load(res)
now = datetime.now().isoformat()
print('*********************************')
if len(data['vehicle_positions']) > 0:
print('{} - SUCCESS - vehicle positions is NOT empty, url: {}'.format(now, url))
urllib2.urlopen(CRONITOR)
else:
print('{} - FAILURE - vehicle positions is empty, url: {}'.format(now, url))
print('*********************************')
@luqmaan
Copy link

luqmaan commented Feb 19, 2016

header {
  gtfs_realtime_version: "1"
  timestamp: 1455894359
}

Current time: 1455894457

@luqmaan
Copy link

luqmaan commented Feb 19, 2016

I think we need to check that the time isn't between 12am and 5:30am.

The 801 and 1 are inactive between those times open-austin/project-ideas#3 (comment)

Some days there are active vehicles in that time range. But sending false alarms in the middle of the night means real alarms will go ignored. Even if there is a real alarm in the middle of the night, nobody will fix it.

@luqmaan
Copy link

luqmaan commented Mar 9, 2016

from __future__ import print_function

import json
import urllib2
from datetime import datetime

LAMBDA_API = 'https://lnykjry6ze.execute-api.us-west-2.amazonaws.com/prod/gtfsrt-debug'
GTFSRT_FEED = 'https://data.texas.gov/download/eiei-9rpf/application/octet-stream'
CRONITOR = '...'


def lambda_handler(event, context):
    url = '{}?url={}'.format(LAMBDA_API, GTFSRT_FEED)
    res = urllib2.urlopen(url)
    data = json.load(res)

    date = datetime.now()
    now = date.isoformat()

    print('*********************************')
    if len(data['vehicle_positions']) > 0:
        print('{} - SUCCESS - vehicle positions is NOT empty, url: {}'.format(now, url))
        urllib2.urlopen(CRONITOR)
    elif date.hour > 6 and date.hour <= 11:
        print('{} - FAILURE - vehicle positions is empty, but IGNORED because hour is {}, url: {}'.format(now, date.hour, url))
        urllib2.urlopen(CRONITOR)
    else:
        print('{} - FAILURE - vehicle positions is empty, url: {}'.format(now, url))
    print('*********************************')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment