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

Commit deb34e0

Browse files
authored
Merge 3.0.0-beta6 changes into main branch (#4)
* Export: Use List<T> instead of ArrayList * Tests: Improved module loading... Remove any previously-loaded copy of the module. * Clear: Breaking change in syntax and behavior... * Remove usage of ShouldProcess because it is overkill for a script like this. * Remove -Force because ditto. * Fix typo. * Update documentation. * Update syntax in tests. * Find: Suppress excess API calls when username isn't found... This is for when the function is given a list or array of lookups. * Export: Add -ExcludeApiKey * Import: Major rework... * Change ConfirmImpact to "Low". * Remove ShouldContinue. * Remove writing errors on missing data sections. * Change handling of conflicting data. * Add Clip to Username mapping and improve video url testing * Export: Change ConfirmImpact to Low * Find: Remove some excess data checking * Add data persistence functions * Docs: Update documentation * Update version number to 3.0.0-beta6 * Import: Update warning message * Tests: Update data files and remove obsolete tests * Update .gitignore * Tests: Update for Clip to User mapping behavior * Tests: Minor code tidying * Remove C# source code fallback * Import: Add alternative basic fallback method * Update .gitignore * Update azure-pipelines.yml * Changed all instances of 'XRefLookupData' to 'XRefData'
1 parent 0514ed9 commit deb34e0

39 files changed

+1773
-1241
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# Debugging/testing output
1+
Module/typedata/
22
debug/
3+
build/
34
src/dotnet/obj/
45
src/dotnet/bin/

Module/PSCurrent/Find-TwitchXRef.ps1

Lines changed: 72 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ function Find-TwitchXRef {
7272

7373
$NewDataAdded = $false
7474

75+
# Temporary list for suppressing additional API calls when the username isn't found while processing a list/array of inputs
76+
$NotFoundList = [System.Collections.Generic.List[string]]::new()
77+
7578
}
7679

7780
Process {
@@ -107,13 +110,15 @@ function Find-TwitchXRef {
107110
$Source = $Source.ToLowerInvariant()
108111
$XRef = $XRef.ToLowerInvariant()
109112

113+
# Initial basic sorting
114+
$SourceIsVideo = $Source -imatch ".*twitch\.tv/videos/.+" ? $true : $false
115+
$XRefIsVideo = $XRef -imatch ".*twitch\.tv/videos/.+" ? $true : $false
116+
110117
#region Source Lookup ##########################
111118

112-
if ($Source -imatch ".*twitch\.tv/videos/.+") {
119+
if ($SourceIsVideo) {
113120
# Video URL provided
114121

115-
$SourceParsedAsClip = $false
116-
117122
# Check if missing timestamp
118123
if ($Source -inotmatch ".*twitch\.tv/videos/.+[?&]t=.+") {
119124

@@ -146,8 +151,6 @@ function Find-TwitchXRef {
146151
else {
147152
# Clip provided
148153

149-
$SourceParsedAsClip = $true
150-
151154
# Strip potential URL formatting
152155
$Slug = $Source | Get-LastUrlSegment
153156

@@ -156,6 +159,13 @@ function Find-TwitchXRef {
156159
if (-not $Force -and $script:TwitchData.ClipInfoCache.ContainsKey($Slug)) {
157160
# Found cached values to use
158161

162+
# Quick return path using cached data
163+
if (-not $XRefIsVideo -and $script:TwitchData.ClipInfoCache[$Slug].Mapping.ContainsKey($XRef)) {
164+
165+
return $script:TwitchData.ClipInfoCache[$Slug].Mapping[$XRef]
166+
167+
}
168+
159169
try {
160170

161171
[timespan]$TimeOffset = New-TimeSpan -Seconds $script:TwitchData.ClipInfoCache[$Slug].Offset
@@ -198,18 +208,36 @@ function Find-TwitchXRef {
198208
# Get Video ID from API response
199209
[int]$VideoID = $ClipResponse.vod.id
200210

211+
# Add username to cache
212+
if (-not $script:TwitchData.UserInfoCache.ContainsKey($ClipResponse.broadcaster.name)) {
213+
214+
$script:TwitchData.UserInfoCache[$ClipResponse.broadcaster.name] = $ClipResponse.broadcaster.id
215+
216+
}
217+
218+
# Populate the Clip to Username hashtable with the originating video
219+
$ClipMapping = @{}
220+
$ClipMapping[$ClipResponse.broadcaster.name] = $ClipResponse.vod.url
221+
201222
# Ensure timestamp was converted correctly
202223
$ClipResponse.created_at = $ClipResponse.created_at | ConvertTo-UtcDateTime
203224

204225
# Add data to clip cache
205-
$obj = [PSCustomObject]@{
226+
$script:TwitchData.ClipInfoCache[$Slug] = [PSCustomObject]@{
206227
Offset = $ClipResponse.vod.offset
207228
VideoID = $VideoID
208229
Created = $ClipResponse.created_at
230+
Mapping = $ClipMapping
209231
}
210-
$script:TwitchData.ClipInfoCache[$Slug] = $obj
211232
$NewDataAdded = $true
212233

234+
# Quick return path for when XRef is original broadcaster
235+
if ($XRef -ieq $ClipResponse.broadcaster.name) {
236+
237+
return $ClipResponse.vod.url
238+
239+
}
240+
213241
}
214242
catch [Microsoft.PowerShell.Commands.WriteErrorException] {
215243

@@ -245,7 +273,7 @@ function Find-TwitchXRef {
245273
# Set absolute timestamp of event
246274

247275
# Check cache to see if this video is already known
248-
if (-not $Force -and $script:TwitchData.VideoInfoCache.ContainsKey($VideoID) -and $script:TwitchData.VideoInfoCache[$VideoID] -is [datetime]) {
276+
if (-not $Force -and $script:TwitchData.VideoInfoCache.ContainsKey($VideoID)) {
249277

250278
# Use start time from cache
251279
[datetime]$EventTimestamp = $script:TwitchData.VideoInfoCache[$VideoID] + $TimeOffset
@@ -262,7 +290,7 @@ function Find-TwitchXRef {
262290
if ($VodResponse.broadcast_type -ine "archive") {
263291

264292
# Set error message based on Source type
265-
$ErrSrc = $SourceParsedAsClip ? "(Clip) Referenced" : "(Video) Source"
293+
$ErrSrc = $SourceIsVideo ? "(Video) Source" : "(Clip) Referenced"
266294

267295
# Use "ErrorAction Stop" with specific catch block for forwarding
268296
Write-Error "$ErrSrc video is not an archived broadcast" -ErrorId InvalidVideoType -Category InvalidOperation -ErrorAction Stop
@@ -310,7 +338,7 @@ function Find-TwitchXRef {
310338

311339
#region XRef Lookup ############################
312340

313-
if ($XRef -imatch ".*twitch\.tv/videos/.+") {
341+
if ($XRefIsVideo) {
314342
# Using VOD link
315343

316344
[int]$XRefID = $XRef | Get-LastUrlSegment
@@ -325,8 +353,21 @@ function Find-TwitchXRef {
325353
# Strip potential URL formatting
326354
$XRef = $XRef | Get-LastUrlSegment
327355

356+
# Check if repeated search using a name that wasn't found during this instance
357+
if ($NotFoundList -icontains $XRef) {
358+
359+
Write-Error "(XRef Username) `"$XRef`" not found" -ErrorId UserNotFound -Category ObjectNotFound -CategoryTargetName XRef -TargetObject $XRef
360+
if ($ExplicitNull) {
361+
return $null
362+
}
363+
else {
364+
return
365+
}
366+
367+
}
368+
328369
# Check ID cache for user
329-
if (-not $Force -and $script:TwitchData.UserInfoCache.ContainsKey($XRef) -and $script:TwitchData.UserInfoCache[$XRef] -is [int]) {
370+
if (-not $Force -and $script:TwitchData.UserInfoCache.ContainsKey($XRef)) {
330371

331372
# Get cached ID number
332373
[int]$UserIdNum = $script:TwitchData.UserInfoCache[$XRef]
@@ -347,6 +388,7 @@ function Find-TwitchXRef {
347388
# Unlike other API requests, this doesn't return a 404 error if not found
348389
if ($UserLookup._total -eq 0) {
349390

391+
$NotFoundList.Add($XRef)
350392
Write-Error "(XRef Username) `"$XRef`" not found" -ErrorId UserNotFound -Category ObjectNotFound -CategoryTargetName XRef -TargetObject $XRef -ErrorAction Stop
351393

352394
}
@@ -471,7 +513,25 @@ function Find-TwitchXRef {
471513
else {
472514

473515
$NewOffset = $EventTimestamp - $VideoToCompare.recorded_at
474-
return "$($VideoToCompare.url)?t=$($NewOffset.Hours)h$($NewOffset.Minutes)m$($NewOffset.Seconds)s"
516+
$NewUrl = "$($VideoToCompare.url)?t=$($NewOffset.Hours)h$($NewOffset.Minutes)m$($NewOffset.Seconds)s"
517+
518+
if (-not ($SourceIsVideo -and $XRefIsVideo)) {
519+
520+
try {
521+
522+
$script:TwitchData.ClipInfoCache[$Slug].Mapping[$XRef] = $NewUrl
523+
$NewDataAdded = $true
524+
525+
}
526+
catch {
527+
528+
Write-Verbose "Unable to add result to clip mapping hashtable"
529+
530+
}
531+
532+
}
533+
534+
return $NewUrl
475535

476536
}
477537

Module/PSLegacy/Find-TwitchXRef.Legacy.ps1

Lines changed: 85 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ function Find-TwitchXRef {
7272

7373
$NewDataAdded = $false
7474

75+
# Temporary list for suppressing additional API calls when the username isn't found while processing a list/array of inputs
76+
$NotFoundList = [System.Collections.Generic.List[string]]::new()
77+
7578
}
7679

7780
Process {
@@ -107,13 +110,26 @@ function Find-TwitchXRef {
107110
$Source = $Source.ToLowerInvariant()
108111
$XRef = $XRef.ToLowerInvariant()
109112

113+
# Initial basic sorting
114+
if ($Source -imatch ".*twitch\.tv/videos/.+") {
115+
$SourceIsVideo = $true
116+
}
117+
else {
118+
$SourceIsVideo = $false
119+
}
120+
121+
if ($XRef -imatch ".*twitch\.tv/videos/.+") {
122+
$XRefIsVideo = $true
123+
}
124+
else {
125+
$XRefIsVideo = $false
126+
}
127+
110128
#region Source Lookup ##########################
111129

112-
if ($Source -imatch ".*twitch\.tv/videos/.+") {
130+
if ($SourceIsVideo) {
113131
# Video URL provided
114132

115-
$SourceParsedAsClip = $false
116-
117133
# Check if missing timestamp
118134
if ($Source -inotmatch ".*twitch\.tv/videos/.+[?&]t=.+") {
119135

@@ -156,8 +172,6 @@ function Find-TwitchXRef {
156172
else {
157173
# Clip provided
158174

159-
$SourceParsedAsClip = $true
160-
161175
# Strip potential URL formatting
162176
$Slug = $Source | Get-LastUrlSegment
163177

@@ -166,6 +180,13 @@ function Find-TwitchXRef {
166180
if (-not $Force -and $script:TwitchData.ClipInfoCache.ContainsKey($Slug)) {
167181
# Found cached values to use
168182

183+
# Quick return path using cached data
184+
if (-not $XRefIsVideo -and $script:TwitchData.ClipInfoCache[$Slug].Mapping.ContainsKey($XRef)) {
185+
186+
return $script:TwitchData.ClipInfoCache[$Slug].Mapping[$XRef]
187+
188+
}
189+
169190
try {
170191

171192
[timespan]$TimeOffset = New-TimeSpan -Seconds $script:TwitchData.ClipInfoCache[$Slug].Offset
@@ -208,18 +229,36 @@ function Find-TwitchXRef {
208229
# Get Video ID from API response
209230
[int]$VideoID = $ClipResponse.vod.id
210231

232+
# Add username to cache
233+
if (-not $script:TwitchData.UserInfoCache.ContainsKey($ClipResponse.broadcaster.name)) {
234+
235+
$script:TwitchData.UserInfoCache[$ClipResponse.broadcaster.name] = $ClipResponse.broadcaster.id
236+
237+
}
238+
239+
# Populate the Clip to Username hashtable with the originating video
240+
$ClipMapping = @{}
241+
$ClipMapping[$ClipResponse.broadcaster.name] = $ClipResponse.vod.url
242+
211243
# Ensure timestamp was converted correctly
212244
$ClipResponse.created_at = $ClipResponse.created_at | ConvertTo-UtcDateTime
213245

214246
# Add data to clip cache
215-
$obj = [PSCustomObject]@{
247+
$script:TwitchData.ClipInfoCache[$Slug] = [PSCustomObject]@{
216248
Offset = $ClipResponse.vod.offset
217249
VideoID = $VideoID
218250
Created = $ClipResponse.created_at
251+
Mapping = $ClipMapping
219252
}
220-
$script:TwitchData.ClipInfoCache[$Slug] = $obj
221253
$NewDataAdded = $true
222254

255+
# Quick return path for when XRef is original broadcaster
256+
if ($XRef -ieq $ClipResponse.broadcaster.name) {
257+
258+
return $ClipResponse.vod.url
259+
260+
}
261+
223262
}
224263
catch [Microsoft.PowerShell.Commands.WriteErrorException] {
225264

@@ -255,7 +294,7 @@ function Find-TwitchXRef {
255294
# Set absolute timestamp of event
256295

257296
# Check cache to see if this video is already known
258-
if (-not $Force -and $script:TwitchData.VideoInfoCache.ContainsKey($VideoID) -and $script:TwitchData.VideoInfoCache[$VideoID] -is [datetime]) {
297+
if (-not $Force -and $script:TwitchData.VideoInfoCache.ContainsKey($VideoID)) {
259298

260299
# Use start time from cache
261300
[datetime]$EventTimestamp = $script:TwitchData.VideoInfoCache[$VideoID] + $TimeOffset
@@ -272,14 +311,14 @@ function Find-TwitchXRef {
272311
if ($VodResponse.broadcast_type -ine "archive") {
273312

274313
# Set error message based on Source type
275-
if ($SourceParsedAsClip) {
314+
if ($SourceIsVideo) {
276315

277-
$ErrSrc = "(Clip) Referenced"
316+
$ErrSrc = "(Video) Source"
278317

279318
}
280319
else {
281320

282-
$ErrSrc = "(Video) Source"
321+
$ErrSrc = "(Clip) Referenced"
283322

284323
}
285324

@@ -329,7 +368,7 @@ function Find-TwitchXRef {
329368

330369
#region XRef Lookup ############################
331370

332-
if ($XRef -imatch ".*twitch\.tv/videos/.+") {
371+
if ($XRefIsVideo) {
333372
# Using VOD link
334373

335374
[int]$XRefID = $XRef | Get-LastUrlSegment
@@ -344,8 +383,21 @@ function Find-TwitchXRef {
344383
# Strip potential URL formatting
345384
$XRef = $XRef | Get-LastUrlSegment
346385

386+
# Check if repeated search using a name that wasn't found during this instance
387+
if ($NotFoundList -icontains $XRef) {
388+
389+
Write-Error "(XRef Username) `"$XRef`" not found" -ErrorId UserNotFound -Category ObjectNotFound -CategoryTargetName XRef -TargetObject $XRef
390+
if ($ExplicitNull) {
391+
return $null
392+
}
393+
else {
394+
return
395+
}
396+
397+
}
398+
347399
# Check ID cache for user
348-
if (-not $Force -and $script:TwitchData.UserInfoCache.ContainsKey($XRef) -and $script:TwitchData.UserInfoCache[$XRef] -is [int]) {
400+
if (-not $Force -and $script:TwitchData.UserInfoCache.ContainsKey($XRef)) {
349401

350402
# Get cached ID number
351403
[int]$UserIdNum = $script:TwitchData.UserInfoCache[$XRef]
@@ -366,6 +418,7 @@ function Find-TwitchXRef {
366418
# Unlike other API requests, this doesn't return a 404 error if not found
367419
if ($UserLookup._total -eq 0) {
368420

421+
$NotFoundList.Add($XRef)
369422
Write-Error "(XRef Username) `"$XRef`" not found" -ErrorId UserNotFound -Category ObjectNotFound -CategoryTargetName XRef -TargetObject $XRef -ErrorAction Stop
370423

371424
}
@@ -495,7 +548,25 @@ function Find-TwitchXRef {
495548
else {
496549

497550
$NewOffset = $EventTimestamp - $VideoToCompare.recorded_at
498-
return "$($VideoToCompare.url)?t=$($NewOffset.Hours)h$($NewOffset.Minutes)m$($NewOffset.Seconds)s"
551+
$NewUrl = "$($VideoToCompare.url)?t=$($NewOffset.Hours)h$($NewOffset.Minutes)m$($NewOffset.Seconds)s"
552+
553+
if (-not ($SourceIsVideo -and $XRefIsVideo)) {
554+
555+
try {
556+
557+
$script:TwitchData.ClipInfoCache[$Slug].Mapping[$XRef] = $NewUrl
558+
$NewDataAdded = $true
559+
560+
}
561+
catch {
562+
563+
Write-Verbose "Unable to add result to clip mapping hashtable"
564+
565+
}
566+
567+
}
568+
569+
return $NewUrl
499570

500571
}
501572

0 commit comments

Comments
 (0)