PoshCode Archive  Artifact [5a0de1dc7b]

Artifact 5a0de1dc7b28ad35df25a1e270e93fe31b1d8f33b6c982bbf49136ccff271c28:

  • File Get-FactualData.ps1 — part of check-in [08bad3c516] at 2018-06-10 13:00:36 on branch trunk — Retrieve data from factual.com. Note: Requires you to establish an API key before you can use it. (user: unknown size: 1346)

# encoding: ascii
# api: powershell
# title: Get-FactualData
# description: Retrieve data from factual.com. Note: Requires you to establish an API key before you can use it.
# version: 0.1
# type: function
# license: CC0
# function: Get-FactualData
# x-poshcode-id: 1732
# x-archived: 2010-04-09T14:52:23
#
#
# Get data from factual.com. Currently you need an API key to even query data so you'll
# have to set one up before you can use this. Set your API key to the global variable
# APIKEY. Hopefully one day factual will allow reads without requiring API keys, which
# is why I chose not to make it an argument to the function.
#
# Note that table name must be the internal table name (e.g. Dbopve) and not any sort
# of name meaningful to humans.
Function Get-FactualData {
	param($tableName)

	$wc = New-Object Net.Webclient
	$url = "http://api.factual.com/v1/" + $APIKEY + "/tables/" + $tableName + "/read.xml"
	$xml = [xml]$wc.DownloadString($url)

	# Get the column titles.
	$nFields = $xml.result.response.fields.field.length - 1
	$fields = $xml.result.response.fields.field[1 .. ($nFields + 1)]

	# Create objects for each entry.
	$xml.result.response.data.row | Foreach {
		$t = "" | Select $fields
		foreach ($i in 1 .. $nFields) {
			$t.($fields[$i-1]) = $_.cell[$i]
		}
		Write-Output $t
	}
}