-
Notifications
You must be signed in to change notification settings - Fork 0
/
fund-holdings.ps1
85 lines (71 loc) · 2.78 KB
/
fund-holdings.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
$baseURL = "https://www.hl.co.uk/funds/fund-discounts,-prices--and--factsheets/search-results"
$funds = @()
foreach ($l in [char[]](97..122) + '0') {
$html = $(Invoke-WebRequest -uri "$baseURL/$l").RawContent
if ($html.IndexOf("no funds starting with") -lt 0) {
$x1 = $html.IndexOf('<ul class="list-unstyled list-indent"')
$x1 = $html.IndexOf('>', $x1) + 1
$x2 = $html.IndexOf('</ul>', $x1)
$tbl = $html.substring($x1, $x2 - $x1).trim()
for ($x1 = $tbl.IndexOf("href="); $x1 -ge 0; $x1 = $tbl.IndexOf("href=", $x2)) {
$x1 = $tbl.IndexOf('"', $x1) + 1
$x2 = $tbl.IndexOf('"', $x1)
$funds += $tbl.Substring($x1, $x2 - $x1)
}
}
}
$funds | Export-Csv -Append funds.csv
$holdings = @()
for ($f = 1388; $f -lt $funds.count; $f++) {
$html = $(Invoke-WebRequest -uri $funds[$f]).RawContent
if ($html.IndexOf("Factsheet unavailable") -ge 0 -or
$html.IndexOf("Market data not available") -ge 0 -or
$html.IndexOf("holdings currently unavailable") -ge 0) {
Write-Host -ForegroundColor Red $f $funds[$f].substring($baseURL.length) "- unavailable"
continue
}
$x1 = $html.IndexOf('Fund size')
$x1 = $html.IndexOf('<td', $x1)
$x1 = $html.IndexOf(">", $x1) + 1
$x2 = $html.IndexOf('</td', $x1)
$fundSize = $html.Substring($x1, $x2 - $x1).trim()
$fundSize = $fundSize -replace "£", "GBP "
$fundSize = $fundSize -replace "€", "EUR "
$fundSize = $fundSize -replace "¥", "YEN "
$fundSize = $fundSize -replace "\$", "USD "
$x1 = $html.IndexOf('<table class="factsheet-table" summary="Top 10 holdings"')
$x1 = $html.IndexOf('>', $x1) + 1
$x2 = $html.IndexOf('</table>', $x1)
$tbl = $html.substring($x1, $x2 - $x1).trim()
$headings = @()
for ($x1 = $tbl.IndexOf('<th', 1); $x1 -gt 0; $x1 = $tbl.IndexOf('<th', $x2)) {
$x1 = $tbl.IndexOf(">", $x1) + 1
$x2 = $tbl.IndexOf("</th>", $x1)
$headings += $tbl.Substring($x1, $x2 - $x1)
}
if ($headings.count -eq 0) {
Write-Host -ForegroundColor Red $f $funds[$f].substring($baseURL.length) "- no table"
continue
}
$i = 0
for ($x1 = $tbl.IndexOf('<td', 0); $x1 -gt 0; $x1 = $tbl.IndexOf('<td', $x2)) {
if ($i % $headings.count -eq 0) {
$h = New-Object -TypeName PSObject -Property @{Fund=$funds[$f].substring($baseURL.length);Size=$fundSize}
}
$x1 = $tbl.IndexOf(">", $x1) + 1
$x2 = $tbl.IndexOf("</td", $x1)
$cell = $tbl.Substring($x1, $x2 - $x1).trim()
if ($cell.Substring(0, 1) -eq '<') {
$x1 = $tbl.IndexOf(">", $x1) + 1
$x2 = $tbl.IndexOf("</a", $x1)
$cell = $tbl.Substring($x1, $x2 - $x1).trim()
}
Add-Member -InputObject $h -MemberType NoteProperty -Name $headings[$i % $headings.count] -Value $cell
$i++
if ($i % $headings.count -eq 0) {
$holdings += $h
}
}
Write-Host $f $funds[$f].substring($baseURL.length) $fundSize ($i / 2) "holdings"
}
$holdings | Export-Csv holdings.csv