forked from gangstanthony/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Search-BoardGameGeek.ps1
74 lines (59 loc) · 2.32 KB
/
Search-BoardGameGeek.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
# https://boardgamegeek.com/wiki/page/BGG_XML_API&redirectedfrom=XML_API#
# https://www.boardgamegeek.com/xmlapi2/thing?id=13&stats=1
# https://boardgamegeek.com/xmlapi/search?search=catan
function Search-BoardGameGeek {
param (
$name
)
$search = iwr https://boardgamegeek.com/xmlapi/search?search=$name
$objs = ([xml]$search.content).boardgames.boardgame
$all = $objs | % {$_.name} | % {if ($_.'#text') {$_.'#text'} else {$_}}
if ($all.count -eq 0) {
[pscustomobject]@{
Game = $name
ID = ''
Average = ''
BayesAverage = ''
}
return
} elseif ($all.count -eq 1) {
$selection = $all
} else {
$msd = try{
Get-Command Measure-StringDistance -ea Stop
} catch {
# uncomment to load function from github
iex (iwr https://raw.githubusercontent.com/michaellwest/PowerShell-Modules/master/CorpApps/Measure-StringDistance.ps1).content
$?
}
if ($msd) {
$guess = @($all | select @{n='result';e={$_}}, @{n='diff';e={ 1- (Measure-StringDistance $name $_) / $name.length }} | sort diff | select -l 1 | % result)
} else {
$guess = $null
}
$selection = ($guess + $all) | Out-GridView -PassThru
#$selection = ($guess + $all) | Out-Menu
#$selection = Get-Choice -Choices ($guess + $all)
#$selection = Read-Choice -Choices ($guess + $all)
#$selection = Show-ConsoleMenu -Choices ($guess + $all)
}
$hash = @{}
for ($i = 0; $i -lt $objs.count; $i++) {
try {
$hash.Add($all[$i], $objs[$i])
} catch {}
}
$obj = $hash[$selection]
#$n = $obj.InnerText
$id = $obj.objectid
#$game = ([xml](iwr https://boardgamegeek.com/xmlapi/boardgame/$id).content).boardgames.boardgame
$average = ([xml](iwr https://www.boardgamegeek.com/xmlapi2/thing?id=$id`&stats=1).content).items.item.statistics.ratings.average.value
$bayesaverage = ([xml](iwr https://www.boardgamegeek.com/xmlapi2/thing?id=$id`&stats=1).content).items.item.statistics.ratings.bayesaverage.value
[pscustomobject]@{
Game = $selection
ID = $id
Average = $average
BayesAverage = $bayesaverage
}
}
Set-Alias search-bgg Search-BoardGameGeek