initial commit, completed

This commit is contained in:
fanyx 2021-03-14 15:21:35 +01:00
commit e53e58f1f4
1 changed files with 102 additions and 0 deletions

102
dl.ps1 Normal file
View File

@ -0,0 +1,102 @@
param(
[parameter(mandatory=$true)]
[string]$seriesId,
[switch]$completeSeries,
[string]$seriesName,
[string]$outputFolder=(Resolve-Path .),
[int]$startChapter=1,
[int]$endChapter=$startChapter
)
### Init ###
$baseUrl = "https://www.mangareader.net"
$usedVarsDownloadChapter = @("chapterNr",
"outputPath",
"chapterUrl",
"chapterBaseHtml",
"chapterPattern",
"chapterPages",
"chapterPage")
$usedVarsForeachChapterPage = @("pageUrl",
"pageFormat",
"pageHtml")
$usedVarsCurrentChapter = @("chapterFormat",
"chapterPath",
"ebookName")
### Functions ###
Function DownloadChapter($chapterNr,$outputPath){
$chapterUrl = "${seriesUrl}/${chapterNr}"
$chapterBaseHtml = Invoke-WebRequest -UseBasicParsing -Uri $chapterUrl
# Download first image, continue afterwards
Invoke-WebRequest -UseBasicParsing -Uri $chapterBaseHtml.Images.src -OutFile (Join-Path -Path $outputPath -ChildPath "01.jpg") #"${outputPath}\01.jpg"
# evaluate regex
$chapterPattern = '(?<=<option value=\"\/' + [Regex]::Escape("${seriesId}") + '\/' + [Regex]::Escape("${chapterNr}") + '\/)\d+(?=\">\d+<\/option>)'
$chapterPages = $chapterBaseHtml.Content | Select-String -Pattern $chapterPattern -AllMatches | ForEach-Object {$_.Matches} | ForEach-Object {$_.Value}
foreach($chapterPage in $chapterPages) {
$pageUrl = "${chapterUrl}/${chapterPage}"
# Download every page and save in target file
$pageFormat = ('{0:d2}' -f [int]$chapterPage)
$pageHtml = Invoke-WebRequest -UseBasicParsing -Uri $pageUrl
Invoke-WebRequest -UseBasicParsing -Uri $pageHtml.Images.src -OutFile (Join-Path -Path $outputPath -ChildPath "${pageFormat}.jpg") #"${outputPath}\${pageFormat}.jpg"
Remove-Variable $usedVarsForeachChapterPage
}
Remove-Variable $usedVarsDownloadChapter
}
### Main ###
$currentChapter = $startChapter
$seriesUrl = "${baseUrl}/${seriesId}"
$basePath = (Resolve-Path $outputFolder)
$seriesPath = (Join-Path -Path $basePath -ChildPath $seriesId)
If (!(Test-Path $seriesPath)) {
New-Item -Path $seriesPath -ItemType Directory
}
if ($completeSeries) {
$seriesHtml = Invoke-WebRequest -UseBasicParsing -Uri $seriesUrl
$seriesHtmlCut1 = $seriesHtml.Content.Substring($seriesHtml.Content.IndexOf("<table id=`"listing`">"))
$seriesHtmlCut2 = $seriesHtmlCut1.Substring(0, ($seriesHtmlCut1.IndexOf("</table>")-1))
$seriesPattern = '(?<=<a href=\"\/' + [Regex]::Escape("${seriesId}") +'\/)\d+(?=\">)'
$seriesListing = $seriesHtmlCut2 | Select-String -Pattern $seriesPattern -AllMatches | % {$_.Matches} | % {$_.Value}
$endChapter = $seriesListing.Count
}
While ($currentChapter -le $endChapter) {
[string]$chapterFormat = ('{0:d3}' -f [int]$currentChapter)
[string]$chapterPath = (Join-Path -Path $seriesPath -ChildPath $chapterFormat)
If (!(Test-Path $chapterPath)) {
New-Item -ItemType Directory -Path $chapterPath
}
DownloadChapter -chapterNr $currentChapter -outputPath $chapterPath
if ($seriesName) {
$ebookName = "${seriesName} - Chapter ${chapterFormat}"
}
else {
$ebookName = "${chapterFormat}"
}
# Dependency on ImageMagick 'convert.exe' being in $PATH
convert "${chapterPath}\*.jpg" "${seriesPath}\${ebookName}.pdf"
Remove-Item -Path "${chapterPath}" -Recurse
Remove-Variable $usedVarsCurrentChapter
$currentChapter++
}