Skip to content

Instantly share code, notes, and snippets.

@dotnwat
Created April 16, 2018 23:12
Show Gist options
  • Save dotnwat/62430eec025cd3f76b9c8d70e516b7d8 to your computer and use it in GitHub Desktop.
Save dotnwat/62430eec025cd3f76b9c8d70e516b7d8 to your computer and use it in GitHub Desktop.
nwatkins@location-sampler:~/ucsc-transit$ cat sample.py
import time
import datetime
import requests
from google.cloud import bigquery
bigquery_client = bigquery.Client()
dataset = bigquery_client.dataset('locations')
table = dataset.table('samples')
table.reload()
while True:
try:
r = requests.get('http://bts.ucsc.edu:8081/location/get')
timestamp = datetime.datetime.utcnow()
rows = []
for row in r.json():
rows.append((timestamp, row['id'], row['lat'], \
row['lon'], row['type']))
if len(rows) > 0:
errors = table.insert_data(rows)
if errors:
msg = "error {}".format(errors)
print msg
else:
print '{}: loaded {} rows'.format(timestamp, len(rows))
except Exception as err:
msg = "error {}".format(err)
print msg
continue
finally:
time.sleep(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment