Once you have a valid Realtime feed (even one that doesn't contain any data), you can specify the method you’ll use to deliver data to Google.
You can deliver real-time transit data two ways:
- Fetch, which allows Google to retrieve your data from a web-location
- Push, which allows you to upload your data via HTTP POST
For service alert data, if you can't provide service alerts via fetch or push methods, you can manually create service alerts via the Service alerts editor.
Publish service alerts
If you can't provide service alerts via fetch or push methods, you can manually create service alerts via the Service alerts editor.
Fetch
You can host each of your realtime feeds at their own web location. Google pulls (or "fetches") the data from these locations every 30 seconds.
To securely allow data fetches from Google, you can use one of the following authentication options:
- Basic authentication (username and password)
- Specific headers to add in the request
Push
You can actively transfer, or “push,” your data to Google with push delivery. With push delivery, you set up automatic uploads of your GTFS Realtime data via HTTP POST, using OAuth2.0 authentication to ensure the security of your Realtime data feed.
To provide users with the most accurate and up-to-date transit information possible, we recommend that you push real-time data updates frequently. Ideally, you should push trip update and vehicle position data every 30 seconds and service alert data every 30 seconds to 2 minutes.
- One Google Account registered in the Transit Data Sharing Portal
- One Google Account that can open Google Cloud
- The realtime feed file you want to push in the Transit Data Sharing Portal
- On the left under the "APIs & Services" tab, click Credentials.
- At the top, click Create Credentials Service account.
- In the "Service account name" field, enter a name.
- Click Create and continue Done.
- On the Google Cloud console, click Menu IAM & Admin.
- On the left, click Service Accounts Add Key.
- Under "Key type," select JSON.
- Click Create.
- To add the service account as a user to the Transit Data Sharing Portal, click Open Invite Dialog in the top right of the page.
- In the dialog box, enter the service account email.
- To allow the push of new real-time feeds, make sure your service account has the "Manage" permission.
- Click Invite.
For more information on adding users, see Manage users for the Transit Data Sharing Portal.
https://googleapis.com/auth/partnerdash.upload
. The request contains an html form (Content-Type: multipart/form-data) with 2 fields:
- realtime_feed_id: The ID of the realtime feed being uploaded
- file: The uploaded GTFS-RT file content
#!/usr/bin/env python3
import requests
from google.oauth2 import service_account
from google.auth.transport.requests import AuthorizedSession
SCOPES = ["https://googleapis.com/auth/partnerdash.upload"]
SERVICE_ACCOUNT_FILE = 'service-account-key.json'
credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
form = {'realtime_feed_id': '[REALTIME_FEED_NAME]'}
files = {'file': open("feed_version.pb", 'rb')}
req = requests.Request( 'POST', 'https://partnerdash.google.com/push-upload',data=form, files=files).prepare()
authed_session = AuthorizedSession(credentials)
resp = authed_session.request(url="https://partnerdash.google.com/push-upload",method="POST",
data=req.body,
headers={
'Content-Length': req.headers["Content-Length"],
'Content-Type': req.headers["Content-Type"]})
print(resp.headers)
print(resp.reason)
print(resp.raise_for_status())