-
Notifications
You must be signed in to change notification settings - Fork 1
/
NetworkLib.ps1
157 lines (153 loc) · 4.94 KB
/
NetworkLib.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
# NetworkLib.ps1 - Network Library
# Version 0.3
# Copyright © 2019 Nonki Takahashi. The MIT License.
function Convert-Text ($in) {
# Convert &*; to unicode character
# param $in - text to convert
# return - text converted
if ($in) {
$out = ''
while ($in -and (0 -lt ($c = $in.IndexOf('&'))) `
-and (0 -lt ($l = ($in.Substring($c)).IndexOf(';')))) {
$kw = $in.Substring($c + 1, $l - 1)
if ($kw.StartsWith('#') -and ($kw.Length -le 4)) {
$to = [char][byte]($kw.Substring(1, $kw.Length - 1))
} elseif ($kw.StartsWith('#') -and ($kw.Length -le 6)) {
$to = [char][int32]($kw.Substring(1, $kw.Length - 1))
} elseif ($kw -eq 'quot') {
$to = '"'
} elseif ($kw -eq 'amp') {
$to = '&'
} else {
$to = '&'
$l = 0
}
$out += $in.Substring(0, $c) + $to
$in = $in.Substring($c + $l + 1)
}
$out + $in
}
}
function Find-Tag ($tagName, $class, $rel, $id){
# Find tag from html buffer
# param $tagName - tag name
# param $class - class name
# param $rel - rel name
# param $id - id
# param $script:p - pointer for buffer
# param $buf - html buffer
# return $script:p - pointer for buffer
# return - found tag
$p = $script:p
$pSave = $p
$tag = ''
$findNext = $true
while ($findNext) {
# tag may be not found
$findNext = $false
$pTag = ($buf.Substring($p)).IndexOf('<' + $tagName)
if (0 -le $pTag) {
$pTag += $p
$len = ($buf.Substring($pTag)).IndexOf('/' + $tagName + '>')
if (0 -le $len) {
# tag may be different
$findNext = $true
$lTag = $tag.Length + ('/' + $tagName + '>').Length
$len += $lTag
$tag = $buf.Substring($pTag, $len)
Get-AttrAndText $tag
if ($id) {
$value = $id
$target = 'id'
} elseif ($class) {
$value = $class
$target = 'class'
} else {
$value = $rel
$target = 'rel'
}
if ($attr[$target] -eq $value) {
# found the tag
$findNext = $false
} else {
$tag = ''
}
$p = $pTag + $len
}
}
}
if ($tag -eq '') {
$p = $pSave
}
$script:p = $p
$tag
}
function Get-AttrAndText ($tag) {
# Get attributes and text from given tag
# param $tag - given tag
# return $script:attr{} - hash of attributes in the tag
# return $script:txt - text in the tag
$pTag = $tag.IndexOf(' ') + 1
$pEnd = $tag.IndexOf('>')
$script:attr = @{}
while ($pTag -lt $pEnd) {
$pEq = ($tag.Substring($pTag)).IndexOf('=')
if (0 -le $pEq) {
$pEq += $pTag
$pQ = '''"'.IndexOf($tag.Substring($pEq + 1, 1))
if (0 -le $pQ) {
$Q = '''"'.Substring($pQ, 1)
$pQ = ($tag.Substring($pEq + 2)).IndexOf($Q)
if (0 -le $pQ) {
$pQ += ($pEq + 2)
$txt = $tag.Substring($pEq + 2, $pQ - $pEq - 2)
$script:attr[$tag.Substring($pTag, $pEq - $pTag)] = $txt
$pTag = $pQ + 2
}
} else {
# to avoid hang with no quotes after equal
$txt = $tag.Substring($pEq + 2, $pEnd - $pEq - 2)
$script:attr[$tag.Substring($pTag, $pEq - $pTag)] = $txt
$pTag = $pEnd + 1
}
} else {
$pTag = $pEnd + 1
}
}
$len = $tag.Length
$script:txt = ''
while ($pTag -lt $len) {
$pL = ($tag.Substring($pTag)).IndexOf('</')
if ($pL -lt 0) {
# '</' not found
$script:txt += $tag.Substring($pTag)
$pTag = $len
} else {
# '</' found
$pL += $pTag
$script:txt += $tag.Substring($pTag, $pL - $pTag)
$pR = ($tag.Substring($pL)).IndexOf('>')
if (0 -le $pR) {
# '>' found
$pTag += $pL
} else {
# '>' not found
$pTag = $len
}
}
}
if ($script:txt) {
$script:txt = Convert-Text $script:txt
}
}
function Get-WebPageContents($url) {
# Simulates Small Basic NetWork.GetWebPageContents()
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
$wc = New-Object System.Net.WebClient
$st = $wc.OpenRead($url)
$enc = [System.Text.Encoding]::GetEncoding('UTF-8')
$sr = New-Object System.IO.StreamReader($st, $enc)
$html = $sr.ReadToEnd()
$sr.Close()
$html
}