-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStandardSiteIndex.WebSocket.ps1
More file actions
269 lines (228 loc) · 7.52 KB
/
StandardSiteIndex.WebSocket.ps1
File metadata and controls
269 lines (228 loc) · 7.52 KB
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
#requires -Module WebSocket
<#
.SYNOPSIS
Standard Site Indexer
.DESCRIPTION
Standard Site Indexer.
Uses the [WebSocket](https://github.com/PowerShellWeb/WebSocket) module to
index new `site.standard.document` and `site.standard.publication` records.
.NOTES
This is a fairly standard script.
Feel free to copy it and reuse this convention elsewhere.
The WebSocket module is a really good simple way to build an at protocol indexer.
#>
param(
# The jetstream url
[uri]
$jetstreamUrl =
"wss://jetstream$(1,2 | Get-Random).us-west.bsky.network/subscribe",
# The collections we are interested in.
[string[]]
$Collections = @("site.standard.document","site.standard.publication"),
# Any specific dids we want to watch.
[string[]]
$Dids = @(),
# The time back we want to ask for.
# (Generally only two days are available)
[TimeSpan]
$Since = [TimeSpan]::FromHours(24),
# The timeout.
# This is how long the job should run.
[TimeSpan]
$TimeOut = [TimeSpan]::FromMinutes(7),
# The root used to store content.
[string]
$Root = $PSScriptRoot
)
#region Declare Filters
filter getAt {
$in = $_
if ($in -notmatch '^at://') { return }
$null, $did, $type, $rkey = $in -split '/{1,2}' -ne ''
if (
(-not $did) -or
(-not $type) -or
(-not $rkey)
) {return}
$xrpcUrl = "https://bsky.social/xrpc/com.atproto.repo.getRecord?repo=$(
$did
)&collection=$(
$type
)&rkey=$(
$rkey
)"
if (-not $script:atRecordCache) {
$script:atRecordCache = @{}
}
if (-not $script:atRecordCache[$xrpcUrl]) {
$script:atRecordCache[$xrpcUrl] = try {
Invoke-RestMethod -Uri $xrpcUrl -ErrorAction Ignore
} catch {
Write-Warning "$_"
$_
}
}
$script:atRecordCache[$xrpcUrl]
}
filter toAtUri {
$in = $_
$did = $in.did
$rkey = $in.commit.rkey
$recordType = $in.commit.record.'$type'
"at://$did/$recordType/$rkey"
}
filter toLocalPath {
if ($in.uri -match '^at://') {
$null, $did, $recordType, $rkey = $in.uri -split '/{1,2}'
} else {
$did = $in.did
$rkey = $in.commit.rkey
$recordType = $in.commit.record.'$type'
}
if (-not $did) { return }
if (-not $rkey) { return }
if (-not $recordType) { return }
"$(@($recordType -split '\.' -ne'')[-1])/$($did -replace ':', '_')/$rkey.json"
}
filter updateDocumentIndex {
$in = $_
$localPath = $in | toLocalPath
if (-not $localPath) { return }
$inFilePath = Join-Path $root $localPath
# We want to keep an index of the data, not the whole thing.
$index = [Ordered]@{
title = $in.commit.record.title
path = $in.commit.record.path
site = $in.commit.record.site
atUri = $in | toAtUri
publishedAt = $in.commit.record.publishedAt
}
if ($in.commit.record.tags) {
$index.tags = $in.commit.record.tags -join ';'
}
if (-not (Test-Path $inFilePath)) {
New-Item -Path $inFilePath -Force -Value (ConvertTo-Json -InputObject $index -Compress)
} else {
Get-Item -Path $inFilePath
}
}
filter updatePublicationIndex {
$in = $_
$localPath = $in | toLocalPath
if (-not $localPath) { return }
$inFilePath = Join-Path $root $localPath
# We want to keep an index of the data, not the whole thing.
if ($in.commit.record.name) {
$index = [Ordered]@{
name = $in.commit.record.name
atUri = $in | toAtUri
description = $in.commit.record.description
url = $in.commit.record.url
}
} else {
$index = [Ordered]@{
name = $in.value.name
atUri = $in.uri
description = $in.value.description
url = $in.value.url
}
}
if (
$in.commit.record.preferences.showInDiscover -eq $false -or
$in.value.preferences.showInDiscover -eq $false
) {
$index.optout = $true
}
if (-not (Test-Path $inFilePath)) {
New-Item -Path $inFilePath -Force -Value (ConvertTo-Json -InputObject $index -Compress)
} else {
Get-Item -Path $inFilePath
}
}
filter standardSiteRecord {
$message = $_
switch ($message.commit.collection) {
site.standard.document {
$message | updateDocumentIndex
}
site.standard.publication {
$message | updatePublicationIndex
}
}
}
#endregion Declare Filters
#region Ride the Jetstream
$jetstreamUrl = @(
"$jetstreamUrl"
'?'
@(
foreach ($collection in $Collections) {
"wantedCollections=$([Uri]::EscapeDataString($collection))"
}
foreach ($did in $Dids) {
"wantedDids=$([Uri]::EscapeDataString($did))"
}
"cursor=$([DateTimeOffset]::Now.Add(-$Since).ToUnixTimeMilliseconds())"
) -join '&'
) -join ''
$Jetstream = WebSocket -SocketUrl $jetstreamUrl -Query @{
wantedCollections = $collections
cursor = ([DateTimeOffset]::Now - $since).ToUnixTimeMilliseconds()
} -TimeOut $TimeOut
Write-Host "Listening To Jetstream: $jetstreamUrl" -ForegroundColor Cyan
Write-Host "Starting loop @ $([DateTime]::Now)" -ForegroundColor Cyan
$batchStart = [DateTime]::Now
$filesFound = @()
do {
$batch =$Jetstream | Receive-Job -ErrorAction Ignore
$batchStart = [DateTime]::Now
$newFiles = $batch |
standardSiteRecord |
Add-Member NoteProperty CommitMessage "Syncing from at protocol [skip ci]" -Force -PassThru
if ($batch) {
$lastPostTime = [DateTimeOffset]::FromUnixTimeMilliseconds($batch[-1].time_us / 1000).DateTime
Write-Host "Processed batch of $($batch.Length) in $([DateTime]::Now - $batchStart) - Last Post @ $($lastPostTime)" -ForegroundColor Green
if ($newFiles) {
Write-Host "Found $(@($newFiles).Length) items to index" -ForegroundColor Green
$filesFound += $newFiles
$newFiles
}
}
Start-Sleep -Milliseconds (Get-Random -Min .1kb -Max 1kb)
} while ($Jetstream.JobStateInfo.State -in 'NotStarted','Running')
$Jetstream |
Receive-Job -ErrorAction Ignore |
standardSiteRecord |
Add-Member NoteProperty CommitMessage "Syncing from at protocol [skip ci]" -Force -PassThru
#endregion Ride the Jetstream
#region Backfill Publications
# When we see new articles, we might not yet have their publications
# So find all of the site uris
$siteUris =
Get-ChildItem ./document/ -recurse -file |
Get-Content -Raw |
ConvertFrom-Json |
Select-Object -ExpandProperty site -Unique
# and see which ones we have already cached.
$pubUris =
Get-ChildItem ./publication/ -recurse -file |
Get-Content -Raw |
ConvertFrom-Json |
Select-Object -ExpandProperty atUri -Unique
# Then make a list of which are missing.
$missingPublications =
@($siteUris |
Where-Object {
$_ -match '^at://' -and
$_ -notin $pubUris
})
# If any are missing, backfill the publications
if ($missingPublications.Count) {
Write-Host "Backfilling $($missingPublications.Length) publications" -ForegroundColor Cyan
$backfilled = $missingPublications | getAt
$backfilled |
Where-Object { $_ -isnot [Management.Automation.ErrorRecord]} |
updatePublicationIndex |
Add-Member NoteProperty CommitMessage "Syncing from at protocol [skip ci]" -Force -PassThru
}
#endregion Backfill Publications