DownloadBingFloorPlanData.ps1

<#PSScriptInfo
 
.VERSION 1.0
 
.GUID 22f075f5-3c1d-405c-a0ff-86731722696e
 
.AUTHOR rakeshma
 
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
.PRIVATEDATA
 
#>


<#
 
.DESCRIPTION
 This powershell script downloads all the floor maps and building entity information for the tenant.
  
#>
 
Param(
    [Alias("folderLocation", "path")]
    [Parameter(Mandatory=$true)]
    [string] $location
);


#region Constants
$resourceUri = "https://www.bing.com"
$getBuildingsUrl = "https://business.bing.com/api/v3/buildings"
$getFloorPlanUrl = "https://business.bing.com/api/v3/floorplan/data?buildingId={0}&floor=VENUE&format=VenueMapJson"
$folderLocationForMaps = "$location\FloorMaps"
$fileLocationForMaps = "$folderLocationForMaps\{0}.json"
$folderLocationForBuildings = "$location\Buildings"
$fileLocationForBuildings = "$folderLocationForBuildings\{0}.json"
#endregion

# Gets the access token to be used to make API calls
function Get-AccessToken {
    Connect-AzAccount
    $token = Get-AzAccessToken -ResourceUri $resourceUri
    return $token
}

# Calls the buildings API to get the list of buildings
function Get-Buildings($token) {
    $headers = @{Authorization = "Bearer $token"}
    $response = Invoke-WebRequest -Uri $getBuildingsUrl -Method POST -headers $headers
    $jsonContent = $response.Content | Out-String | ConvertFrom-Json
    return $jsonContent.results
}

# Downloads the venue maps for the given building entity. Returns $true if venuemap found, false otherwise.
function Download-VenueMaps($token, $building) {
    $headers = @{Authorization = "Bearer $token"}
    $buildingId = $building.entity.id
    $url = $getFloorPlanUrl -f $buildingId

    # Write the entity object to file for creating places directory entries
    $entityFileName = $fileLocationForBuildings -f "$($buildingId)_entity"
    $building.entity | ConvertTo-Json | Set-Content -Path $entityFileName

    Try {
        $responseContent = Invoke-WebRequest -Uri $url -Method GET -headers $headers | Select-Object -ExpandProperty Content
        $file = $fileLocationForMaps -f $buildingId
        Set-Content -Path $file -Value $responseContent
        
        return $true
    } Catch {
        # Do nothing. 404 is expected if there is no VeneueMaps for the file
        return $false
    }
}

# Execution begins here
$token = Get-AccessToken
$token = $token.Token
$buildings = Get-Buildings($token)

if ($buildings.length -gt 0) {
    # Create folder if necessary
    New-Item -Path $folderLocationForBuildings -Type Directory -Force > $null
    New-Item -Path $folderLocationForMaps -Type Directory -Force > $null
    
    Write-Host "$($buildings.length) buildings found."
} else {
    Write-Host "No buildings found"
}

$downloadedMapsCount = 0;

for ($i=0; $i -lt $buildings.length; $i++) {
    Write-Progress "Attempting to download VenueMaps for Building #$i"
    $venueMapsFound = Download-VenueMaps $token $buildings[$i]
    if ($venueMapsFound) {
        $downloadedMapsCount++
    }
}

if ($buildings.length -gt 0) {
    Write-Host "$($buildings.length) building(s) downloaded to $folderLocationForBuildings"
    Write-Host "$downloadedMapsCount Map(s) downloaded to $folderLocationForMaps"
}