opendata

Querying the JC Decaux API using PowerShell

JC Decaux have released an official API that can be used to query the Dublin Bikes scheme as well as other schemes they manage, e.g. Dublin Bikes in Dublin (Ireland) & Cyclocity in Vilnius (Lithuania).
I threw together a quick and dirty PowerShell script for querying the API. If you'd like to play around, you'll need to register for an API key at https://developer.jcdecaux.com/


#
#	Title :: JC Decaux OpenData Client
#	Purpose :: Can be used to query city bike schemes managed by JC Decaux, e.g. Dublin Bikes
#	
#

## Variables
$api = "API KEY GOES HERE"

$contractsURL = "https://api.jcdecaux.com/vls/v1/contracts"
$stationsURL = "https://api.jcdecaux.com/vls/v1/stations"

## Functions
function invoke-jcdclient {
param (
		[string]
		$url,
		[string]
		$contract
)
	#
	
	$response = Invoke-RestMethod -Uri $url -Method Get -ContentType 'application/json'
	return $response
}

function get-jcdcontracts {
param(
		[string]
		$apikey
)
	#
	$response = invoke-jcdclient -url "$($contractsURL)?apiKey=$($apiKey)"
	return $response
}

function get-jcdstations {
param(
		[string]
		$apikey,
		[string]
		$contract
)
	#
	
	$response = invoke-jcdclient -url "$($stationsURL)?contract=$($contract)&apiKey=$($apiKey)"
	return $response
}

function get-jcdstation {
param(
		[string]
		$apikey,
		[int]
		$number,
		[string]
		$contract
)
	#
	#Write-Host "$($stationsURL)/$($number)?contract=$($contract)&apiKey=$($apiKey)"
	$response = invoke-jcdclient -url "$($stationsURL)/$($number)?contract=$($contract)&apiKey=$($apiKey)"
	return $response
}

## Runtime

# Get list of all 'contracts' aka JCD locations
$contracts = get-jcdcontracts -apikey $api

# Get all stations in 'contract' location, e.g. Dublin, Vilnius
# Output results to table
$stations = get-jcdstations -apikey $api -contract 'dublin'
$stations | ft -AutoSize

# Get specific station number in a specific 'contract' location
# e.g. Station 40 (Jervis Street) in Dublin
$station = get-jcdstation -number 40 -apikey $api -contract 'dublin'

$station
# Convert the milliseconds since epoch into DateTime
#([timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds((0 + $station.last_update)/1000))).DateTime