Skip to content

Instantly share code, notes, and snippets.

@RobbWatershed
Last active October 24, 2020 17:31
Show Gist options
  • Save RobbWatershed/855b82dbff5f0ba8f5ad759b7455b811 to your computer and use it in GitHub Desktop.
Save RobbWatershed/855b82dbff5f0ba8f5ad759b7455b811 to your computer and use it in GitHub Desktop.
Hentoid books organizer script (by logicdefyer)
#Enter The Master Porn Folder Here , Must Have Proper Json Information , the whole shit is about having the right jsons
$SortMasterFolder = $PSScriptRoot + "\Dataset"
$OutputMasterFolder = $PSScriptRoot + "\OutDataset"
$PSDefaultParameterValues['*:Encoding'] = 'utf8'
function CopyFilesToFolder ($fromFolder, $toFolder) {
$childItems = Get-ChildItem -LiteralPath $fromFolder
$childItems | ForEach-Object {
Copy-Item -LiteralPath $_.FullName -Destination $toFolder -Recurse -Force
}
}
<#ModuleCounter#>
#Counter For Folders and Files
$FolderCount = 0
$FileCount = 0
foreach ($subfolder in Get-ChildItem -LiteralPath $SortMasterFolder) {
$FolderCount++
$DirectoryPath = Join-Path -Path $SortMasterFolder -ChildPath $subfolder
foreach ($jsonfile in Get-ChildItem -LiteralPath $DirectoryPath) {
$FileCount++
}
}
Write-Host "Dealing with $FileCount Files in $FolderCount Folders"
$jsoncount = 0
#Counter For Jsons
foreach ($subfolder in Get-ChildItem -LiteralPath $SortMasterFolder) {
$DirectoryPath = Join-Path -Path $SortMasterFolder -ChildPath $subfolder
foreach ($jsonfile in Get-ChildItem -LiteralPath $DirectoryPath) {
$Filepath = Join-Path -Path $DirectoryPath -ChildPath $jsonfile
if ($Filepath -like "*contentV2.json") {
#Write-Host $Filepath
$jsoncount++
}
}
}
#shitty code to log the no Json
$dirs = Get-ChildItem -Path $SortMasterFolder -Recurse -Directory
foreach ($dir In $dirs.Fullname) {
if(-not(Test-Path -LiteralPath "$dir\contentV2.json")) {
Write-Output $dir | Out-File $PSScriptRoot\nojson.txt -Append
}
}
if ($FolderCount -eq $jsoncount) {
write-host "Okay To Proceed as FolderCount is Equals JsonCount"
}
else {
write-host "You sure You Want to Proceed FolderCount = $FolderCount and JsonCount = $JsonCount"
}
write-host -nonewline "Continue? Point Of No Return (Y/N) "
$response = read-host
if ( $response -ne "Y" ) { exit }
#Precount Categorized and Uncategorized
$UncategorizedCount = 0
$Categorizedcount = 0
foreach ($subfolder in Get-ChildItem -LiteralPath $SortMasterFolder) {
Try {
$DirectoryPath = Join-Path -Path $SortMasterFolder -ChildPath $subfolder
foreach ($jsonfile in Get-ChildItem -LiteralPath $DirectoryPath) {
$Filepath = Join-Path -Path $DirectoryPath -ChildPath $jsonfile
if ($Filepath -like "*contentV2.json") {
$ParseFile = (Get-Content -LiteralPath $Filepath) -join '`n' | ConvertFrom-Json
try {
if ($ParseFile.Title -eq $null) {
write-host "Continue? Json Looks Invalid at $Filepath (Y/N) "
$response = read-host
if ( $response -ne "Y" ) { exit }
}
else {
$newitemfoldername = $ParseFile.Title + "_" + $ParseFile.author
#Replace This Line With What You Want To Sort By
$newcategoryfoldername = $ParseFile.attributes.SERIE.name
$newcategoryfoldername = ( Get-Culture ).TextInfo.ToTitleCase( $newcategoryfoldername.ToLower() )
if ($newcategoryfoldername -eq $null) {
$newcategoryfoldername = "uncategorized"
$UncategorizedCount++
}
else {
$Categorizedcount++
}
}
}
catch {
$newitemfoldername = $subfolder
$newcategoryfoldername = "error"
}
}
}
#make String Okay for FileSystem
#Transform Category Folder Name To Remove Bullshit Characters
$newcategoryfoldername = $newcategoryfoldername.ToString().Trim()
$newcategoryfoldername = $newcategoryfoldername -replace '[^a-zA-Z0-9/ ]', ''
[IO.Path]::GetinvalidFileNameChars() | ForEach-Object { $newcategoryfoldername = $newcategoryfoldername.Replace($_, " ") }
$newcategoryfoldername = $newcategoryfoldername.trim() -replace '\s+', ' '
#Transform Item Folder Name To Remove Bullshit Characters
$newitemfoldername = $newitemfoldername.ToString().Trim()
$newitemfoldername = $newitemfoldername.Trim(" ", "_")
#$newitemfoldername = $newitemfoldername -replace '[^a-zA-Z0-9/ ]', ''
[IO.Path]::GetinvalidFileNameChars() | ForEach-Object { $newitemfoldername = $newitemfoldername.Replace($_, " ") }
$newitemfoldername = $newitemfoldername.trim() -replace '\s+', ' '
#Make OutPut Path
$CategoryOutPutPath = Join-Path -Path $OutputMasterFolder -ChildPath $newcategoryfoldername
$OutPutPath = Join-Path -Path $CategoryOutPutPath -ChildPath $newitemfoldername
#Copy the Shitz
if (!(Test-Path -path $OutPutPath)) {
New-Item $OutPutPath -Type Directory
CopyFilesToFolder $DirectoryPath $OutPutPath
$DirectoryPath | Out-File $PSScriptRoot\log.txt -Append
}
}
Catch {
$DirectoryPath | Out-File $PSScriptRoot\errors.txt -Append
}
}
Write-Host "Dealing with $Categorizedcount Categorized Folders and $UncategorizedCount UnCategorized Folders"
#CBR PART OF THE SCRIPT
$WinRAR = "C:\Program Files\WinRAR\rar"
Function CreateRAR($Folder, $RAR, $Name) {
$Path = $RAR + "\" + $Name + ".cbr"
& $WinRAR a -ep1 -m5 -DF $Path $Folder
}
$childItems = Get-ChildItem -LiteralPath $OutputMasterFolder
$childItems | ForEach-Object {
$archivetoFolder = $_.FullName;
$tocbritems = Get-ChildItem -Directory -LiteralPath ($_.FullName)
$tocbritems | ForEach-Object {
$archiveFolder = $_.FullName;
$archiveName = ($_.FullName).split("\")[-1];
#Write-Host($archivetoFolder);
CreateRAR $archiveFolder $archivetoFolder $archiveName
}
}
write-host -nonewline "Tally Your Log Files Now"
write-host "Delete Logged Items"
$response = read-host
if ( $response -ne "Y" ) { exit }
foreach($line in Get-Content $PSScriptRoot\log.txt) {
Write-Host($line)
Remove-Item -LiteralPath $line -Recurse –force
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment