Skip to content

Instantly share code, notes, and snippets.

@mavaddat
Last active December 1, 2022 22:06
Show Gist options
  • Save mavaddat/81cffe0c7c888d8a64e5a6f213e6e188 to your computer and use it in GitHub Desktop.
Save mavaddat/81cffe0c7c888d8a64e5a6f213e6e188 to your computer and use it in GitHub Desktop.
A PowerShell script to copy the e-books file formats from qBittorrent to a pre-defined directory
# Usage: "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -file "copybooks.ps1" -Src "%D"
param(
[Parameter(Position=0)]
[String]$Src="$env:USERPROFILE\Downloads\Books\"
)
$Dest="$env:USERPROFILE\Documents\Books"
if(!(Test-Path -Path $Dest -PathType Container)) {
if(Test-Path -Path $Dest -PathType Leaf) {
Get-ChildItem $Dest | Rename-Item -Path $Dest -NewName "$($_.Name)(1)"
}
New-Item -ItemType Directory -Path $Dest
}
Get-ChildItem -Path $Src -Include '*.pdf','*.epub','*.mobi','*.azw','*.azw?' -Recurse | Copy-Item -Destination $Dest -Recurse -Verbose
# Requires PSParseHTML Module for ConvertFrom-HTML
# Load ConvertTo-Slug from Gist
iex (New-Object -TypeName System.Net.WebClient).DownloadString('https://gist.githubusercontent.com/mavaddat/0bbed730a4c10a066d75894a7611d730/raw/4a37c2ec4f0d33ff2bbb1a224b0b901063dce871/slugify.ps1')
mkdir "$env:TEMP\Calibre\"
foreach($file in Get-ChildItem -Path $env:USERPROFILE\Downloads\Books\ | Where-Object -Property Length -GT 0){
$libgen = Invoke-RestMethod -Method Get -Uri "https://libgen.is/book/index.php?md5=$($file.BaseName)" | ConvertFrom-HTML -Engine AngleSharp
$title = $libgen.QuerySelector("body > table > tbody > tr > td > b > a").InnerHtml
$authors = $libgen.QuerySelector("body > table > tbody > tr > td:nth-child(2) > b").InnerHtml -replace '\([^\)]+\)', '' -replace '\s*[;,]\s*',' & '
$ext = $libgen.QuerySelector("body > table > tbody > tr:nth-child(11) > td:nth-child(4)").InnerHtml
$file | Copy-Item -Destination "$env:TEMP\Calibre\$(ConvertTo-Slug -Text $title -CapitalizeFirstLetter -Delimiter ' ') - $authors.$ext"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment