Skip to content

Commit 4b560e1

Browse files
added deleted code to skip deleted users/creators
- added deleted column to users/creators table - added code to stop fetching if deleted - added code to skip if deleted in database
1 parent 340de87 commit 4b560e1

4 files changed

Lines changed: 351 additions & 264 deletions

File tree

CivitAI.ps1

Lines changed: 82 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,17 @@ function Download-Metadata-From-User {
160160
##########################################
161161
} else {
162162
Write-Host "`nfound user $Username in database." -ForegroundColor Green
163+
##########################################
164+
#check if deleted
165+
$temp_query = "SELECT deleted FROM Users WHERE username = '$Username'"
166+
$result = Invoke-SQLiteQuery -DataSource $DBFilePath -Query $temp_query
167+
168+
$deleted = $result[0].deleted
169+
# Check the result
170+
if ($deleted -eq 1) {
171+
Write-Host "Username $Username is deleted. Skipping..." -ForegroundColor Yellow
172+
return #go to next user
173+
}
163174
##########################################
164175
#load last_time_fetched_metadata and start search from there
165176
$temp_query = "SELECT last_time_fetched_metadata FROM Users WHERE username = '$Username'"
@@ -442,10 +453,12 @@ function Download-Metadata-From-User {
442453
Start-Sleep -Milliseconds $delay
443454
############################################
444455
} elseif ($_.Exception.Response.StatusCode -eq 500) {
445-
Write-Host "Error 500 encountered. This probably means that the user ($Username) doesn't exist." -ForegroundColor Red
456+
Write-Host "Error 500 encountered. This probably means that the user ($Username) doesn't exist. Marking user as deleted." -ForegroundColor Red
457+
$temp_query = "UPDATE Users SET deleted = 1 WHERE username = '$Username'"
458+
Invoke-SqliteQuery -DataSource $DBFilePath -Query $temp_query
446459
Start-Sleep -Milliseconds $TimeToWait # Waits for X seconds
447460
$HasMoreFiles = $false
448-
break
461+
return
449462
############################################
450463
} else {
451464
Write-Host "Failed to fetch posts for user $($Username): $($_.Exception.Message)" -ForegroundColor Red
@@ -483,7 +496,8 @@ if (-not (Test-Path $DBFilePath)) {
483496
total_files INTEGER DEFAULT 0,
484497
cur_cursor TEXT,
485498
last_time_fetched_metadata TEXT,
486-
last_time_downloaded TEXT
499+
last_time_downloaded TEXT,
500+
deleted INTEGER DEFAULT 0 CHECK (deleted IN (0,1))
487501
);
488502
"
489503

@@ -617,69 +631,72 @@ function Show-Menu {
617631
# Write-Output "Transcript stopped"
618632
}
619633
}
620-
############################################
621-
622-
623-
634+
##########################################################################
624635
if ($Function) {
625-
# Start logging
626-
$CurrentDate = Get-Date -Format "yyyyMMdd_HHmmss"
627-
Start-Transcript -Path "$PSScriptRoot/logs/CivitAI_$($CurrentDate).log" -Append
628-
switch ($Function) {
629-
'DownloadAllMetadataAndFiles' {
630-
Backup-Database
631-
$stopwatch_main = [System.Diagnostics.Stopwatch]::StartNew()
632-
Process-Users
633-
$stopwatch_main.Stop()
634-
Write-Host "`nDownloaded all metadata from users in $($stopwatch_main.Elapsed.TotalSeconds) seconds." -ForegroundColor Green
635-
$stopwatch_main = [System.Diagnostics.Stopwatch]::StartNew()
636-
Download-Files-From-Database -Type 1
637-
$stopwatch_main.Stop()
638-
Write-Host "`nDownloaded all files from database in $($stopwatch_main.Elapsed.TotalSeconds) seconds." -ForegroundColor Green
639-
}
640-
'DownloadAllMetadata' {
641-
Backup-Database
642-
$stopwatch_main = [System.Diagnostics.Stopwatch]::StartNew()
643-
Process-Users
644-
$stopwatch_main.Stop()
645-
Write-Host "`nDownloaded all metadata from users in $($stopwatch_main.Elapsed.TotalSeconds) seconds." -ForegroundColor Green
646-
}
647-
'DownloadOnlyFiles' {
648-
$stopwatch_main = [System.Diagnostics.Stopwatch]::StartNew()
649-
Download-Files-From-Database -Type 1
650-
$stopwatch_main.Stop()
651-
Write-Host "`nDownloaded all files from database in $($stopwatch_main.Elapsed.TotalSeconds) seconds." -ForegroundColor Green
652-
}
653-
'DownloadFilesFromQuery' {
654-
if ([string]::IsNullOrWhiteSpace($Query)) {
655-
Write-Host "The -Query parameter is required for the DownloadFilesFromQuery function." -ForegroundColor Red
656-
} else {
657-
$stopwatch_main = [System.Diagnostics.Stopwatch]::StartNew()
658-
Download-Files-From-Database -Type 2 -Query $Query
659-
$stopwatch_main.Stop()
660-
Write-Host "`nDownloaded all files from query in $($stopwatch_main.Elapsed.TotalSeconds) seconds." -ForegroundColor Green
661-
}
662-
}
663-
'ScanFolderForFavorites' {
664-
Backup-Database
665-
Scan-Folder-And-Add-Files-As-Favorites -Type 2
666-
}
667-
'DownloadMetadataForSingleUser' {
668-
if ([string]::IsNullOrWhiteSpace($Username)) {
669-
Write-Host "The -Username parameter is required for the DownloadMetadataForSingleUser function." -ForegroundColor Red
670-
} else {
671-
Backup-Database
672-
$stopwatch_main = [System.Diagnostics.Stopwatch]::StartNew()
673-
Download-Metadata-From-User -Username $Username
674-
$stopwatch_main.Stop()
675-
Write-Host "`nDownloaded metadata for user $Username in $($stopwatch_main.Elapsed.TotalSeconds) seconds." -ForegroundColor Green
676-
}
677-
}
678-
default { Write-Host "Invalid function name: $Function" -ForegroundColor Red }
679-
}
680-
Stop-Transcript
681-
[console]::beep()
682-
Pause
636+
try {
637+
# Start logging
638+
$CurrentDate = Get-Date -Format "yyyyMMdd_HHmmss"
639+
Start-Transcript -Path "$PSScriptRoot/logs/CivitAI_$($CurrentDate).log" -Append
640+
switch ($Function) {
641+
'DownloadAllMetadataAndFiles' {
642+
Backup-Database
643+
$stopwatch_main = [System.Diagnostics.Stopwatch]::StartNew()
644+
Process-Users
645+
$stopwatch_main.Stop()
646+
Write-Host "`nDownloaded all metadata from users in $($stopwatch_main.Elapsed.TotalSeconds) seconds." -ForegroundColor Green
647+
$stopwatch_main = [System.Diagnostics.Stopwatch]::StartNew()
648+
Download-Files-From-Database -Type 1
649+
$stopwatch_main.Stop()
650+
Write-Host "`nDownloaded all files from database in $($stopwatch_main.Elapsed.TotalSeconds) seconds." -ForegroundColor Green
651+
}
652+
'DownloadAllMetadata' {
653+
Backup-Database
654+
$stopwatch_main = [System.Diagnostics.Stopwatch]::StartNew()
655+
Process-Users
656+
$stopwatch_main.Stop()
657+
Write-Host "`nDownloaded all metadata from users in $($stopwatch_main.Elapsed.TotalSeconds) seconds." -ForegroundColor Green
658+
}
659+
'DownloadOnlyFiles' {
660+
$stopwatch_main = [System.Diagnostics.Stopwatch]::StartNew()
661+
Download-Files-From-Database -Type 1
662+
$stopwatch_main.Stop()
663+
Write-Host "`nDownloaded all files from database in $($stopwatch_main.Elapsed.TotalSeconds) seconds." -ForegroundColor Green
664+
}
665+
'DownloadFilesFromQuery' {
666+
if ([string]::IsNullOrWhiteSpace($Query)) {
667+
Write-Host "The -Query parameter is required for the DownloadFilesFromQuery function." -ForegroundColor Red
668+
} else {
669+
$stopwatch_main = [System.Diagnostics.Stopwatch]::StartNew()
670+
Download-Files-From-Database -Type 2 -Query $Query
671+
$stopwatch_main.Stop()
672+
Write-Host "`nDownloaded all files from query in $($stopwatch_main.Elapsed.TotalSeconds) seconds." -ForegroundColor Green
673+
}
674+
}
675+
'ScanFolderForFavorites' {
676+
Backup-Database
677+
Scan-Folder-And-Add-Files-As-Favorites -Type 2
678+
}
679+
'DownloadMetadataForSingleUser' {
680+
if ([string]::IsNullOrWhiteSpace($Username)) {
681+
Write-Host "The -Username parameter is required for the DownloadMetadataForSingleUser function." -ForegroundColor Red
682+
} else {
683+
Backup-Database
684+
$stopwatch_main = [System.Diagnostics.Stopwatch]::StartNew()
685+
Download-Metadata-From-User -Username $Username
686+
$stopwatch_main.Stop()
687+
Write-Host "`nDownloaded metadata for user $Username in $($stopwatch_main.Elapsed.TotalSeconds) seconds." -ForegroundColor Green
688+
}
689+
}
690+
default { Write-Host "Invalid function name: $Function" -ForegroundColor Red }
691+
}
692+
##########################################################################
693+
} catch {
694+
Write-Error "An error occurred (line $($_.InvocationInfo.ScriptLineNumber)): $($_.Exception.Message)"
695+
} finally {
696+
Stop-Transcript
697+
[console]::beep()
698+
Pause
699+
}
683700
##########################################################################
684701
} else {
685702
Show-Menu

0 commit comments

Comments
 (0)