-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelpers.ps1
314 lines (267 loc) · 8.02 KB
/
helpers.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
309
310
311
312
313
314
$PSDefaultParameterValues["Invoke-Request:UseWebRequest"] = $true
$PSDefaultParameterValues["Invoke-Request:Method"] = "GET"
function Get-WhoFollowsMe {
[CmdletBinding()]
param(
[psobject[]]$Id = $script:myid
)
$script:link = $null
Invoke-Request -Path "accounts/$Id/followers?limit=80"
while ($null -ne $script:link) {
Invoke-Request -Path $script:link
}
}
function Get-WhoAmIFollowing {
[CmdletBinding()]
param(
[psobject[]]$Id = $script:myid
)
$script:link = $null
Invoke-Request -Path "accounts/$Id/following?limit=80"
while ($null -ne $script:link) {
Invoke-Request -Path $script:link
}
}
function Invoke-Request {
param(
[string]$Method = "GET",
[string]$Server = $env:MASTODON_SERVER,
[Parameter(Mandatory)]
[Alias("Uri")]
[string]$Path,
[string]$Version = "v1",
[string]$Body,
[string]$OutFile,
[switch]$UseWebRequest,
[switch]$Raw
)
if ($Path -match "://") {
$url = $Path
} else {
$url = "https://$Server/api/$Version/$Path"
}
Write-Verbose "Going to $url"
$parms = @{
Uri = $url
ErrorAction = "Stop"
Headers = @{ Authorization = "Bearer $env:ACCESS_TOKEN" }
Method = $Method
Verbose = $false # too chatty
}
if ($Body) {
$parms.Body = $Body
$parms.ContentType = "application/json"
}
if ($OutFile) {
$parms.OutFile = $OutFile
}
if ($UseWebRequest) {
$response = Invoke-WebRequest @parms
if ($response.Headers.Link) {
$script:link = $response.Headers.Link.Split(";") | Where-Object { $PSitem -match "max_id" } | Select-Object -First 1
if ($script:link) {
foreach ($term in "<", ">") {
$script:link = $script:link.Replace($term, "")
}
}
} else {
$script:link = $null
}
if (-not $OutFile -and -not $Raw) {
$response.Content | ConvertFrom-Json -Depth 10
}
if ($Raw) {
$response.Content
}
} else {
Invoke-RestMethod @parms
}
# This keeps it from calling too many times in a 5 minute period
Start-Sleep -Seconds 1
}
function Get-Account {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string[]]$UserName
)
foreach ($user in $UserName) {
$user = $user.Replace("@$env:MASTODON_SERVER", "")
if ($user.StartsWith("@")) {
$user = $user.Substring(1)
}
$ignored = "youtube.com", "medium.com", "withkoji.com", "counter.social", "twitter.com"
foreach ($domain in $ignored) {
if ($user -match $domain) {
Write-Verbose "User ($user) matched invalid Mastodon domain ($domain). Skipping."
continue
}
}
$account = $script:accounts | Where-Object acct -eq $user
if (-not $user.StartsWith("http")) {
try {
$address = [mailaddress]$user
if ($address.Host -eq $Server) {
$account = $script:accounts | Where-Object acct -eq $address.User
if ($account) {
$account
continue
} else {
$user = "https://" + $address.Host + "/@" + $address.User
}
}
} catch {
# trying a variety of things because there is no specific
# search for username, so just ignore it if this didn't work
}
}
$user = $user.Replace("@$Server", "")
$account = $script:accounts | Where-Object acct -eq $user
if ($account.id) {
$account
continue
}
$parms = @{
Path = "search?type=accounts&q=$user&resolve=true"
Method = "GET"
Version = "v2"
}
$account = Invoke-Request @parms | Select-Object -ExpandProperty accounts
if ($account) {
# add to script variable and return
$null = $script:accounts.Add($account)
$account
} else {
throw "$user not found. The account may not exist, or it may be blocked by your account or Mastodon instance."
}
}
}
function Get-Bookmark {
[CmdletBinding()]
param(
[psobject[]]$Id = $script:myid
)
$script:link = $null
Invoke-Request -Path "bookmarks"
while ($null -ne $script:link) {
Invoke-Request -Path $script:link
}
}
function Get-AccountMute {
[CmdletBinding()]
param(
[psobject[]]$Id = $script:myid
)
$script:link = $null
Invoke-Request -Path "mutes"
while ($null -ne $script:link) {
Invoke-Request -Path $script:link
}
}
function Get-DomainBlock {
[CmdletBinding()]
param(
[psobject[]]$Id = $script:myid
)
$script:link = $null
Invoke-Request -Path "domain_blocks"
while ($null -ne $script:link) {
Invoke-Request -Path $script:link
}
}
function Get-List {
[CmdletBinding()]
param(
[psobject[]]$Id = $script:myid
)
$script:link = $null
Invoke-Request -Path "lists"
while ($null -ne $script:link) {
Invoke-Request -Path $script:link
}
}
function Get-ListMember {
[CmdletBinding()]
param(
[psobject[]]$Id = $script:myid
)
$script:link = $null
Invoke-Request -Path "lists/$Id/accounts"
while ($null -ne $script:link) {
Invoke-Request -Path $script:link
}
}
function Get-AccountBlock {
[CmdletBinding()]
param(
[psobject[]]$Id = $script:myid
)
$script:link = $null
Invoke-Request -Path "blocks"
while ($null -ne $script:link) {
Invoke-Request -Path $script:link
}
}
function Get-Post {
[CmdletBinding()]
param(
[psobject[]]$Id = $script:myid
)
$script:link = $null
Invoke-Request -Path "accounts/$id/statuses?exclude_replies=true"
while ($null -ne $script:link) {
Invoke-Request -Path $script:link
}
}
function Get-Relationship {
[CmdletBinding()]
param(
[psobject[]]$Id = $script:myid
)
$splits = Split-array -Objects $script:accounts.id -Size 100
foreach ($split in $splits) {
$idstring = $split -join "&id[]="
Invoke-Request -Path "accounts/relationships?id[]=$idstring"
}
}
# thanks https://www.powershellgallery.com/packages/PSSharedGoods/0.0.252/Content/PSSharedGoods.psm1
function Split-Array {
<#
.SYNOPSIS
Split an array into multiple arrays of a specified size or by a specified number of elements
.DESCRIPTION
Split an array into multiple arrays of a specified size or by a specified number of elements
.PARAMETER Objects
Lists of objects you would like to split into multiple arrays based on their size or number of parts to split into.
.PARAMETER Parts
Parameter description
.PARAMETER Size
Parameter description
.EXAMPLE
This splits array into multiple arrays of 3
Example below wil return 1,2,3 + 4,5,6 + 7,8,9
Split-array -Objects @(1,2,3,4,5,6,7,8,9,10) -Parts 3
.EXAMPLE
This splits array into 3 parts regardless of amount of elements
Split-array -Objects @(1,2,3,4,5,6,7,8,9,10) -Size 3
.NOTES
#>
[CmdletBinding()]
param([alias('InArray', 'List')][Array] $Objects,
[int]$Parts,
[int]$Size)
if ($Objects.Count -eq 1) { return $Objects }
if ($Parts) { $PartSize = [Math]::Ceiling($inArray.count / $Parts) }
if ($Size) {
$PartSize = $Size
$Parts = [Math]::Ceiling($Objects.count / $Size)
}
$outArray = [System.Collections.Generic.List[Object]]::new()
for ($i = 1; $i -le $Parts; $i++) {
$start = (($i - 1) * $PartSize)
$end = (($i) * $PartSize) - 1
if ($end -ge $Objects.count) { $end = $Objects.count - 1 }
$outArray.Add(@($Objects[$start..$end]))
}
, $outArray
}