Skip to content

Instantly share code, notes, and snippets.

@mamachanko
Last active January 10, 2017 08:31
Show Gist options
  • Save mamachanko/ba51ec4285171b6c393e5dba22ba0ac7 to your computer and use it in GitHub Desktop.
Save mamachanko/ba51ec4285171b6c393e5dba22ba0ac7 to your computer and use it in GitHub Desktop.
List DriveNow car coordinates for Berlin

DriveNow car coordinates

This is a script to return all DriveNow car locations for Berlin, Germany. It is easily changed to work with any other city DriveNow provides service in.

You need a DriveNow API key and auth token. It's easiest to get your's by logging into the DriveNow website and using your browser's dev tools.

Usage

$ git clone git@gist.github.com:ba51ec4285171b6c393e5dba22ba0ac7.git drivenow
$ cd drivenow
$ chmod +x ./drivenow_car_coordinates.sh
$ DRIVENOW_API_KEY=abc DRIVENOW_AUTH_TOKEN=123 ./drivenow_car_coordinates.sh | jq
[
  {
    "latitude": 52.519503,
    "longitude": 13.405613,
    "address": [
      "Spandauer Str. 9",
      "10178 Berlin"
    ]
  },
  ...
  {
    "latitude": 52.500897,
    "longitude": 13.420673,
    "address": [
      "Oranienstraße 183",
      "10999 Berlin"
    ]
  }
]
#!/usr/bin/env bash -e
displayUsage() {
echo "This script depends on both DRIVENOW_API_KEY and DRIVENOW_AUTH_TOKEN being set."
}
if [[ -z ${DRIVENOW_API_KEY+x} || -z ${DRIVENOW_AUTH_TOKEN+x} ]]; then
displayUsage
exit 1
fi
request() {
local driveNowApiUrl="https://api2.drive-now.com"
local language="en"
local userAgent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36"
local endpoint=$1
echo $(
curl -s --compressed \
"${driveNowApiUrl}/${endpoint}" \
-H "X-Api-Key:${DRIVENOW_API_KEY}" \
-H "X-Auth-Token: ${DRIVENOW_AUTH_TOKEN}" \
-H "X-Language: ${language}" \
-H "User-Agent: ${userAgent}" \
)
}
getCities() {
echo `request "cities?expand=cities"`
}
getCityId() {
local cityName=$1
echo $(
getCities \
| jq --raw-output \
".items|map(select(.name == \"${cityName}\"))[0].id"
)
}
getCarsForCity() {
local cityName=$1
local cityId=`getCityId "${cityName}"`
echo `request "cities/${cityId}?expand=full"`
}
getCarCoordinatesForCity() {
local cityName=$1
echo $(
getCarsForCity "${cityName}" \
| jq \
'.cars.items|map({latitude, longitude, address})'
)
}
getCarCoordinatesForCity "Berlin"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment