-
Notifications
You must be signed in to change notification settings - Fork 14
/
Find-Factual.ps1
308 lines (212 loc) · 9.37 KB
/
Find-Factual.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
function Find-Factual {
<#
.Synopsis
Finds content on Factual
.Description
Finds content on Factual's global places API
.Example
Find-Factual Starbucks in Seattle
.Example
$l = Resolve-Location -Address 'Redmond, WA'
Find-Factual -GeoPulse -TypeOfFilter Point -Filter "$($l.longitude),$($l.Latitude)" -Verbose
.Example
Find-Factual -InTable vYrq7F -Filter 'Washington' -TypeOfFilter State -Limit 50
.Example
# Wineries
Find-Factual -InTable cQUvfi
.Link
Get-Web
#>
[OutputType([PSObject])]
param(
# The factual query
[Parameter(Position=0,ValueFromPipelineByPropertyName=$true)]
[string]
$Query,
# The type of the filter
[Parameter(Position=1)]
[ValidateSet("In","Near","Category","Country", "UPC", "EAN", "Brand", "Point", "Name", "Brewery", "Beer", "Style", "State", "PostCode")]
[string[]]
$TypeOfFilter,
# The filter
[Parameter(Position=2,ValueFromPipelineByPropertyName=$true)]
[string[]]
$Filter,
# Within. This is only used when 'near' is used
[Parameter(Position=3,ValueFromPipelineByPropertyName=$true)]
[Uint32]
$Within = 1000,
# Your Factual API Key
[Parameter(Position=4,ValueFromPipelineByPropertyName=$true)]
[string]
$FactualKey,
# A secure setting containing your factual key
[Parameter(Position=5)]
[string]
$FactualKeySetting = "FactualKey",
# If set, will only find US resturaunts
[Switch]
$Restaurants,
# If set, will only find health care providers
[Switch]
$HeathCare,
# If set, will only find products
[Switch]
$Product,
# If set, searches the places data set
[switch]
$Place,
# If set, gets the GeoPulse of an area
[Switch]
$GeoPulse,
# If set, will get data from a table
[string]
$InTable,
# If set, will limit the number of responses returned
[ValidateRange(1,50)]
[Uint32]
$Limit,
# If set, will start returning results at a point
[Uint32]
$Offset,
# If set, will query all records that match a filter. This will result in multiple queries.
[Switch]
$All
)
process {
$filters = ""
if ($TypeOfFilter.Count -ne $Filter.Count) {
throw "Must be an equal number of filters and types of filters"
}
$geoString = ""
$filterString =
for ($i = 0; $i -lt $TypeOfFilter.Count; $i++) {
if ($TypeOfFilter[$i] -eq 'Category') {
"{`"category`":{`"`$bw`":$('"' + ($Filter[$i] -join '","') + '"')}}"
} elseif ($TypeOfFilter[$i] -eq 'In') {
"{`"locality`":{`"`$in`":[$('"' + ($Filter[$i] -join '","') + '"')]}}"
} elseif ($TypeOfFilter[$i] -eq 'Upc') {
"{`"upc`":`"$($Filter[$i])`"}"
} elseif ($TypeOfFilter[$i] -eq 'Ean13') {
"{`"ean13`":`"$($Filter[$i])`"}"
} elseif ($TypeOfFilter[$i] -eq 'ProductName') {
"{`"product_name`":`"$($Filter[$i])`"}"
} elseif ($TypeOfFilter[$i] -eq 'Name') {
"{`"name`":`"$($Filter[$i])`"}"
} elseif ($TypeOfFilter[$i] -eq 'Brewery') {
"{`"brewery`":`"$($Filter[$i])`"}"
} elseif ($TypeOfFilter[$i] -eq 'Beer') {
"{`"beer`":`"$($Filter[$i])`"}"
} elseif ($TypeOfFilter[$i] -eq 'State') {
"{`"state`":`"$($Filter[$i])`"}"
} elseif ($TypeOfFilter[$i] -eq 'Country') {
"{`"country`":`"$($Filter[$i])`"}"
} elseif ($TypeOfFilter[$i] -eq 'Style') {
"{`"style`":`"$($Filter[$i])`"}"
} elseif ($TypeOfFilter[$i] -eq 'Brand') {
"{`"brand`":`"$($Filter[$i])`"}"
} elseif ($TypeOfFilter[$i] -eq 'PostCode') {
"{`"postcode`":`"$($Filter[$i])`"}"
} elseif ($TypeOfFilter[$i] -eq 'Near') {
""
$geoString = "&geo={`"`$circle`":{`"`$center`":[$(($Filter[$i] -split ",")[0]),$(($Filter[$i] -split ",")[1])],`"`$meters`":$Within }}"
} elseif ($TypeOfFilter[$i] -eq 'Point') {
""
$lat = [Math]::Round((($Filter[$i] -split ",")[0]), 5)
$long = [Math]::Round((($Filter[$i] -split ",")[1]), 5)
$geoString = "&geo={`"`$point`":[$lat,$long], `"`$meters`":$within}"
}
}
$factualUrl = "http://api.v3.factual.com/t/global?"
if ($Restaurants) {
$factualUrl = "http://api.v3.factual.com/t/restaurants-us?"
} elseif ($HeathCare) {
$factualUrl = "http://api.v3.factual.com/t/health-care-providers-us?"
} elseif ($Place) {
$factualUrl = "http://api.v3.factual.com/t/world-geographies?"
} elseif ($product) {
$factualUrl = "http://api.v3.factual.com/t/products-cpg?"
} elseif ($GeoPulse) {
$factualUrl = "http://api.v3.factual.com/places/geopulse?"
} elseif ($InTable) {
$factualUrl = "http://api.v3.factual.com/t/${InTable}?"
}
if ($Query) {
$factualUrl += "q=$Query&"
} else {
}
if ($filterString) {
$factualUrl +=
if ($filterstring -is [Array]) {
# ands
"filters={`"`$and`":[$($filterString -join ',')]}"
} else {
# simple $filter
"filters=$($filterString)"
}
} else {
$geoString= $geoString.TrimStart("&")
}
if ($geoString) {
$factualUrl += $geostring
}
if (-not $GeoPulse) {
$factualUrl +="&include_count=true"
if ($limit) {
$factualUrl +="&limit=$limit"
}
if ($Offset) {
$factualUrl +="&offset=$offset"
}
}
Write-Verbose "Querying From Factual $factualUrl&Key=******"
if (-not $FactualKey) {
$FactualKey = Get-SecureSetting -Name $FactualKeySetting -ValueOnly
}
$factualUrl +=
if($FactualKey ){
"&KEY=$FACTUALKey"
}
$factualResult = Get-Web -Url $factualUrl -AsJson -UseWebRequest
while ($factualResult) {
$rowCount = $factualResult.response.total_row_count
if ($rowCount) {
Write-Verbose "$RowCount total records to return"
}
$factualResult= $factualResult.response.data
if (-not $factualResult) { break }
$factualResult = foreach ($f in $factualResult) {
if (-not $f){ continue }
if ($geoPulse) {
$null = Update-List -InputObject $f -remove "System.Management.Automation.PSCustomObject", "System.Object" -add "Factual.GeoPulse" -Property pstypenames
} elseif ($f.Beer) {
$null = Update-List -InputObject $f -remove "System.Management.Automation.PSCustomObject", "System.Object" -add "Factual.Beer" -Property pstypenames
} elseif ($f.Operating_Name -and $f.permit_number) {
$null = Update-List -InputObject $f -remove "System.Management.Automation.PSCustomObject", "System.Object" -add "Factual.Winery" -Property pstypenames
} elseif (-not $Product) {
$f = $f |
Add-Member AliasProperty telephone tel -Force -PassThru |
Add-Member AliasProperty url website -Force -PassThru
$null = Update-List -InputObject $f -remove "System.Management.Automation.PSCustomObject", "System.Object" -add "http://schema.org/Place" -Property pstypenames
} else {
$null = Update-List -InputObject $f -remove "System.Management.Automation.PSCustomObject", "System.Object" -add "http://schema.org/Product" -Property pstypenames
}
$f
}
$factualResult
if ($all) {
if ($factualUrl -like "*offset=*") {
$factualUrl = $factualUrl -replace '\&offset=\d{1,}', ''
$Offset += 20
} else {
$Offset = 20
}
$factualUrl+="&offset=$Offset"
$factualResult = Get-Web -Url $factualUrl -AsJson -UseWebRequest
} else {
$factualResult = $null
}
}
#
}
}