Skip to content
This repository was archived by the owner on Jul 23, 2022. It is now read-only.

Commit 634a6cf

Browse files
committed
Find: Use Target for name of 2nd parameter...
and when referring to it.
1 parent 57243d5 commit 634a6cf

File tree

10 files changed

+119
-108
lines changed

10 files changed

+119
-108
lines changed

Module/PSCurrent/Find-TwitchXRef.ps1

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ function Find-TwitchXRef {
88
[string]$Source,
99

1010
[Parameter(Mandatory = $true, Position = 1, ValueFromPipelineByPropertyName = $true)]
11+
[Alias("XRef")]
1112
[ValidateNotNullOrEmpty()]
12-
[string]$XRef,
13+
[string]$Target,
1314

1415
[Parameter()]
1516
[ValidateRange(1, 100)]
@@ -105,7 +106,7 @@ function Find-TwitchXRef {
105106

106107
# Initial basic sorting
107108
$SourceIsVideo = $Source -imatch $VideoPattern ? $true : $false
108-
$XRefIsVideo = $XRef -imatch $VideoPattern ? $true : $false
109+
$TargetIsVideo = $Target -imatch $VideoPattern ? $true : $false
109110

110111
#region Source Lookup ##########################
111112

@@ -144,9 +145,9 @@ function Find-TwitchXRef {
144145
if (-not $Force -and $script:TwitchData.ClipInfoCache.ContainsKey($Slug)) {
145146
# Found cached values to use
146147

147-
if (-not $XRefIsVideo -and $script:TwitchData.ClipInfoCache[$Slug].Mapping.ContainsKey($XRef)) {
148+
if (-not $TargetIsVideo -and $script:TwitchData.ClipInfoCache[$Slug].Mapping.ContainsKey($Target)) {
148149
# Quick return path using cached data
149-
return $script:TwitchData.ClipInfoCache[$Slug].Mapping[$XRef]
150+
return $script:TwitchData.ClipInfoCache[$Slug].Mapping[$Target]
150151
}
151152
else {
152153
$TimeOffset = New-TimeSpan -Seconds $script:TwitchData.ClipInfoCache[$Slug].Offset
@@ -194,8 +195,8 @@ function Find-TwitchXRef {
194195

195196
$NewDataAdded = $true
196197

197-
# Quick return path for when XRef is original broadcaster
198-
if ($XRef -ieq $ClipResponse.broadcaster.name) {
198+
# Quick return path for when Target is original broadcaster
199+
if ($Target -ieq $ClipResponse.broadcaster.name) {
199200
return $ClipResponse.vod.url
200201
}
201202
}
@@ -265,26 +266,26 @@ function Find-TwitchXRef {
265266

266267
#endregion Source Lookup =======================
267268

268-
#region XRef Lookup ############################
269+
#region Target Lookup ############################
269270

270-
if ($XRefIsVideo) {
271+
if ($TargetIsVideo) {
271272
# Using VOD link
272273

273274
# 32-bit integer to match Twitch backend
274-
[Int32]$XRefID = $XRef | Get-LastUrlSegment
275-
$RestArgs["Uri"] = "$API/videos/$XRefID"
275+
[Int32]$TargetID = $Target | Get-LastUrlSegment
276+
$RestArgs["Uri"] = "$API/videos/$TargetID"
276277

277278
$Multi = $false
278279
}
279280
else {
280281
# Using username/channel
281282

282283
# Strip potential URL formatting
283-
$XRef = $XRef | Get-LastUrlSegment
284+
$Target = $Target | Get-LastUrlSegment
284285

285286
# Check if repeated search using a name that wasn't found during this instance
286-
if ($NotFoundList -icontains $XRef) {
287-
Write-Error "(XRef Username) `"$XRef`" not found." -ErrorId UserNotFound -Category ObjectNotFound -CategoryTargetName XRef -TargetObject $XRef
287+
if ($NotFoundList -icontains $Target) {
288+
Write-Error "(Target Username) `"$Target`" not found." -ErrorId UserNotFound -Category ObjectNotFound -CategoryTargetName Target -TargetObject $Target
288289
if ($ExplicitNull) {
289290
return $null
290291
}
@@ -294,29 +295,29 @@ function Find-TwitchXRef {
294295
}
295296

296297
# Get cached user ID number if available or call API if not
297-
if (-not $Force -and $script:TwitchData.UserInfoCache.ContainsKey($XRef)) {
298-
$UserIdNum = $script:TwitchData.UserInfoCache[$XRef]
298+
if (-not $Force -and $script:TwitchData.UserInfoCache.ContainsKey($Target)) {
299+
$UserIdNum = $script:TwitchData.UserInfoCache[$Target]
299300
}
300301
else {
301302
# Get ID number for username using API
302303
$RestArgs["Uri"] = "$API/users"
303304
$RestArgs["Body"] = @{
304-
"login" = $XRef
305+
"login" = $Target
305306
}
306307

307308
$UserLookup = Invoke-RestMethod @RestArgs
308309

309310
try {
310311
# Unlike other API requests, this doesn't return a 404 error if not found
311312
if ($UserLookup._total -eq 0) {
312-
$NotFoundList.Add($XRef)
313-
Write-Error "(XRef Username) `"$XRef`" not found." -ErrorId UserNotFound -Category ObjectNotFound -CategoryTargetName XRef -TargetObject $XRef -ErrorAction Stop
313+
$NotFoundList.Add($Target)
314+
Write-Error "(Target Username) `"$Target`" not found." -ErrorId UserNotFound -Category ObjectNotFound -CategoryTargetName Target -TargetObject $Target -ErrorAction Stop
314315
}
315316

316317
[int]$UserIdNum = $UserLookup.users[0]._id
317318

318319
# Save ID number in user cache
319-
$script:TwitchData.UserInfoCache[$XRef] = $UserIdNum
320+
$script:TwitchData.UserInfoCache[$Target] = $UserIdNum
320321
$NewDataAdded = $true
321322
}
322323
catch [Microsoft.PowerShell.Commands.WriteErrorException] {
@@ -349,9 +350,9 @@ function Find-TwitchXRef {
349350
$XRefResponse = Invoke-RestMethod @RestArgs
350351

351352
try {
352-
# Check for incorrect video type if XRef is a video URL ($Multi will be $false)
353+
# Check for incorrect video type if Target is a video URL ($Multi will be $false)
353354
if (-not $Multi -and $XRefResponse.broadcast_type -ine "archive") {
354-
Write-Error "(XRef Video) Video is not an archived broadcast." -ErrorId InvalidVideoType -Category InvalidOperation -CategoryTargetName XRef -TargetObject $XRef -ErrorAction Stop
355+
Write-Error "(Target Video) Video is not an archived broadcast." -ErrorId InvalidVideoType -Category InvalidOperation -CategoryTargetName Target -TargetObject $Target -ErrorAction Stop
355356
}
356357

357358
$XRefSet = $Multi ? $XRefResponse.videos : $XRefResponse
@@ -379,7 +380,7 @@ function Find-TwitchXRef {
379380
$PSCmdlet.ThrowTerminatingError($_)
380381
}
381382

382-
#endregion XRef Lookup =========================
383+
#endregion Target Lookup =========================
383384

384385
# Look for first video that starts before the timestamp
385386

@@ -398,10 +399,10 @@ function Find-TwitchXRef {
398399
$NewOffset = $EventTimestamp - $VideoToCompare.recorded_at
399400
$NewUrl = "$($VideoToCompare.url)?t=$($NewOffset.Hours)h$($NewOffset.Minutes)m$($NewOffset.Seconds)s"
400401

401-
if (-not $SourceIsVideo -and -not $XRefIsVideo) {
402+
if (-not $SourceIsVideo -and -not $TargetIsVideo) {
402403
try {
403404
# Add to clip result mapping
404-
$script:TwitchData.ClipInfoCache[$Slug].Mapping[$XRef] = $NewUrl
405+
$script:TwitchData.ClipInfoCache[$Slug].Mapping[$Target] = $NewUrl
405406
$NewDataAdded = $true
406407
}
407408
catch {

Module/PSLegacy/Find-TwitchXRef.Legacy.ps1

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ function Find-TwitchXRef {
88
[string]$Source,
99

1010
[Parameter(Mandatory = $true, Position = 1, ValueFromPipelineByPropertyName = $true)]
11+
[Alias("XRef")]
1112
[ValidateNotNullOrEmpty()]
12-
[string]$XRef,
13+
[string]$Target,
1314

1415
[Parameter()]
1516
[ValidateRange(1, 100)]
@@ -111,11 +112,11 @@ function Find-TwitchXRef {
111112
$SourceIsVideo = $false
112113
}
113114

114-
if ($XRef -imatch $VideoPattern) {
115-
$XRefIsVideo = $true
115+
if ($Target -imatch $VideoPattern) {
116+
$TargetIsVideo = $true
116117
}
117118
else {
118-
$XRefIsVideo = $false
119+
$TargetIsVideo = $false
119120
}
120121

121122
#region Source Lookup ##########################
@@ -165,9 +166,9 @@ function Find-TwitchXRef {
165166
if (-not $Force -and $script:TwitchData.ClipInfoCache.ContainsKey($Slug)) {
166167
# Found cached values to use
167168

168-
if (-not $XRefIsVideo -and $script:TwitchData.ClipInfoCache[$Slug].Mapping.ContainsKey($XRef)) {
169+
if (-not $TargetIsVideo -and $script:TwitchData.ClipInfoCache[$Slug].Mapping.ContainsKey($Target)) {
169170
# Quick return path using cached data
170-
return $script:TwitchData.ClipInfoCache[$Slug].Mapping[$XRef]
171+
return $script:TwitchData.ClipInfoCache[$Slug].Mapping[$Target]
171172
}
172173
else {
173174
$TimeOffset = New-TimeSpan -Seconds $script:TwitchData.ClipInfoCache[$Slug].Offset
@@ -215,8 +216,8 @@ function Find-TwitchXRef {
215216

216217
$NewDataAdded = $true
217218

218-
# Quick return path for when XRef is original broadcaster
219-
if ($XRef -ieq $ClipResponse.broadcaster.name) {
219+
# Quick return path for when Target is original broadcaster
220+
if ($Target -ieq $ClipResponse.broadcaster.name) {
220221
return $ClipResponse.vod.url
221222
}
222223
}
@@ -291,26 +292,26 @@ function Find-TwitchXRef {
291292

292293
#endregion Source Lookup =======================
293294

294-
#region XRef Lookup ############################
295+
#region Target Lookup ############################
295296

296-
if ($XRefIsVideo) {
297+
if ($TargetIsVideo) {
297298
# Using VOD link
298299

299300
# 32-bit integer to match Twitch backend
300-
[Int32]$XRefID = $XRef | Get-LastUrlSegment
301-
$RestArgs["Uri"] = "$API/videos/$XRefID"
301+
[Int32]$TargetID = $Target | Get-LastUrlSegment
302+
$RestArgs["Uri"] = "$API/videos/$TargetID"
302303

303304
$Multi = $false
304305
}
305306
else {
306307
# Using username/channel
307308

308309
# Strip potential URL formatting
309-
$XRef = $XRef | Get-LastUrlSegment
310+
$Target = $Target | Get-LastUrlSegment
310311

311312
# Check if repeated search using a name that wasn't found during this instance
312-
if ($NotFoundList -icontains $XRef) {
313-
Write-Error "(XRef Username) `"$XRef`" not found." -ErrorId UserNotFound -Category ObjectNotFound -CategoryTargetName XRef -TargetObject $XRef
313+
if ($NotFoundList -icontains $Target) {
314+
Write-Error "(Target Username) `"$Target`" not found." -ErrorId UserNotFound -Category ObjectNotFound -CategoryTargetName Target -TargetObject $Target
314315
if ($ExplicitNull) {
315316
return $null
316317
}
@@ -320,29 +321,29 @@ function Find-TwitchXRef {
320321
}
321322

322323
# Get cached user ID number if available or call API if not
323-
if (-not $Force -and $script:TwitchData.UserInfoCache.ContainsKey($XRef)) {
324-
$UserIdNum = $script:TwitchData.UserInfoCache[$XRef]
324+
if (-not $Force -and $script:TwitchData.UserInfoCache.ContainsKey($Target)) {
325+
$UserIdNum = $script:TwitchData.UserInfoCache[$Target]
325326
}
326327
else {
327328
# Get ID number for username using API
328329
$RestArgs["Uri"] = "$API/users"
329330
$RestArgs["Body"] = @{
330-
"login" = $XRef
331+
"login" = $Target
331332
}
332333

333334
$UserLookup = Invoke-RestMethod @RestArgs
334335

335336
try {
336337
# Unlike other API requests, this doesn't return a 404 error if not found
337338
if ($UserLookup._total -eq 0) {
338-
$NotFoundList.Add($XRef)
339-
Write-Error "(XRef Username) `"$XRef`" not found." -ErrorId UserNotFound -Category ObjectNotFound -CategoryTargetName XRef -TargetObject $XRef -ErrorAction Stop
339+
$NotFoundList.Add($Target)
340+
Write-Error "(Target Username) `"$Target`" not found." -ErrorId UserNotFound -Category ObjectNotFound -CategoryTargetName Target -TargetObject $Target -ErrorAction Stop
340341
}
341342

342343
[int]$UserIdNum = $UserLookup.users[0]._id
343344

344345
# Save ID number in user cache
345-
$script:TwitchData.UserInfoCache[$XRef] = $UserIdNum
346+
$script:TwitchData.UserInfoCache[$Target] = $UserIdNum
346347
$NewDataAdded = $true
347348
}
348349
catch [Microsoft.PowerShell.Commands.WriteErrorException] {
@@ -375,9 +376,9 @@ function Find-TwitchXRef {
375376
$XRefResponse = Invoke-RestMethod @RestArgs
376377

377378
try {
378-
# Check for incorrect video type if XRef is a video URL ($Multi will be $false)
379+
# Check for incorrect video type if Target is a video URL ($Multi will be $false)
379380
if (-not $Multi -and $XRefResponse.broadcast_type -ine "archive") {
380-
Write-Error "(XRef Video) Video is not an archived broadcast." -ErrorId InvalidVideoType -Category InvalidOperation -CategoryTargetName XRef -TargetObject $XRef -ErrorAction Stop
381+
Write-Error "(Target Video) Video is not an archived broadcast." -ErrorId InvalidVideoType -Category InvalidOperation -CategoryTargetName Target -TargetObject $Target -ErrorAction Stop
381382
}
382383

383384
if ($Multi) {
@@ -410,7 +411,7 @@ function Find-TwitchXRef {
410411
$PSCmdlet.ThrowTerminatingError($_)
411412
}
412413

413-
#endregion XRef Lookup =========================
414+
#endregion Target Lookup =========================
414415

415416
# Look for first video that starts before the timestamp
416417

@@ -429,10 +430,10 @@ function Find-TwitchXRef {
429430
$NewOffset = $EventTimestamp - $VideoToCompare.recorded_at
430431
$NewUrl = "$($VideoToCompare.url)?t=$($NewOffset.Hours)h$($NewOffset.Minutes)m$($NewOffset.Seconds)s"
431432

432-
if (-not $SourceIsVideo -and -not $XRefIsVideo) {
433+
if (-not $SourceIsVideo -and -not $TargetIsVideo) {
433434
try {
434435
# Add to clip result mapping
435-
$script:TwitchData.ClipInfoCache[$Slug].Mapping[$XRef] = $NewUrl
436+
$script:TwitchData.ClipInfoCache[$Slug].Mapping[$Target] = $NewUrl
436437
$NewDataAdded = $true
437438
}
438439
catch {

0 commit comments

Comments
 (0)