Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ///////////////////////////////////////////
- # Author: DarthFarious
- # Dated: 20 April 2017
- # -------------------------------------------
- # This script will download manga from MangaEden.Com,
- # in their respective chapter-wise folders,
- # and finally make a archive from the chapter folder
- # to read in a CBR/CBZ reader
- # -------------------------------------------
- # Contact me on Reddit:
- # https://www.reddit.com/user/DarthFarious/
- # ///////////////////////////////////////////
- Add-Type -As System.IO.Compression.FileSystem
- function New-ZipFile {
- #.Synopsis
- # Create a new zip file, optionally appending to an existing zip...
- [CmdletBinding()]
- param(
- # The path of the zip to create
- [Parameter(Position=0, Mandatory=$true)]
- $ZipFilePath,
- # Items that we want to add to the ZipFile
- [Parameter(Position=1, Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
- [Alias("PSPath","Item")]
- [string[]]$InputObject = $Pwd,
- # Append to an existing zip file, instead of overwriting it
- [Switch]$Append,
- # The compression level (defaults to Optimal):
- # Optimal - The compression operation should be optimally compressed, even if the operation takes a longer time to complete.
- # Fastest - The compression operation should complete as quickly as possible, even if the resulting file is not optimally compressed.
- # NoCompression - No compression should be performed on the file.
- [System.IO.Compression.CompressionLevel]$Compression = "Optimal"
- )
- begin {
- # Make sure the folder already exists
- [string]$File = Split-Path $ZipFilePath -Leaf
- [string]$Folder = $(if($Folder = Split-Path $ZipFilePath) { Resolve-Path $Folder } else { $Pwd })
- $ZipFilePath = Join-Path $Folder $File
- # If they don't want to append, make sure the zip file doesn't already exist.
- if(!$Append) {
- if(Test-Path $ZipFilePath) { Remove-Item $ZipFilePath }
- }
- $Archive = [System.IO.Compression.ZipFile]::Open( $ZipFilePath, "Update" )
- }
- process {
- foreach($path in $InputObject) {
- foreach($item in Resolve-Path $path) {
- # Push-Location so we can use Resolve-Path -Relative
- Push-Location (Split-Path $item)
- # This will get the file, or all the files in the folder (recursively)
- foreach($file in Get-ChildItem $item -Recurse -File -Force | % FullName) {
- # Calculate the relative file path
- $relative = (Resolve-Path $file -Relative).TrimStart(".\")
- # Add the file to the zip
- $null = [System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($Archive, $file, $relative, $Compression)
- }
- Pop-Location
- }
- }
- }
- end {
- $Archive.Dispose()
- Get-Item $ZipFilePath
- }
- }
- function checkUrlExists {
- <#
- .SYNOPSIS
- Return 1 or 0 depending if the input URL exists
- .PARAMETER $URL
- This will be a url,
- for eg "http://www.mangaeden.com/en/en-manga/tegami-bachi/"
- .EXAMPLE
- checkUrlExists "http://www.mangaeden.com/en/en-manga/tegami-bachi/"
- Output:
- 1
- #>
- param([string]$URL)
- $HTTP_Request = [System.Net.WebRequest]::Create($URL)
- try
- {
- $HTTP_Response = $HTTP_Request.GetResponse()
- $HTTP_Status = [int]$HTTP_Response.StatusCode
- }
- catch{}
- finally{}
- if($HTTP_Status -eq 200)
- {
- return 1
- }
- else
- {
- return 0
- }
- if($HTTP_Response)
- {
- $HTTP_Response.Close()
- }
- }
- $URL_SERIES = Read-Host -Prompt "Enter Mangaeden URL"
- $SERIES_NAME = $URL_SERIES.Replace("http://www.mangaeden.com/en/en-manga/","")
- $SERIES_NAME = $SERIES_NAME.Replace("http://tmp.mangaeden.com/en/en-manga/","")
- $SERIES_NAME = $SERIES_NAME.Replace("/","")
- #This variable will be used later to search for the chapter URLs in getChapterNos function
- #Since the URLs will contain a hyphenated name like
- #"tegami-bachi"
- #and not
- #"Tegami Bachi"
- #This variable is stored before the name is correctly capitalised.
- $url_title = $SERIES_NAME
- $SERIES_NAME = $SERIES_NAME.Replace("-"," ")
- "Checking url, please wait..."
- if(-Not(checkUrlExists -URL $URL_SERIES) )
- {
- "Invalid URL. Try again."
- "Exiting..."
- Start-Sleep 2
- exit
- }
- function caseCorrecter {
- <#
- .SYNOPSIS
- This function will return a string with the first letter of each word capitalized.
- .PARAMETER $Name
- This represents the argument string
- .EXAMPLE
- $str = "abc def ghi"
- $correct = caseCorrecter -Name $str
- $correct
- Output of this will be:
- Abc Def Ghi
- #>
- param([string]$Name)
- $ogString = $Name
- [string]$nullCheck = "a" #arbitrary non null value
- $tempHold = $ogString[0]
- #Capitalize the first letter of the string
- $tempHold = $tempHold.ToString().ToUpper()
- #$ogStringCased will store the correctly capitalised string
- $ogStringCased = $tempHold
- [int]$arrayPlace = 1
- while($nullCheck)
- {
- if($ogString[$arrayPlace] -like " ")
- {
- #If the current letter is a space, capitalize the next letter
- $ogStringCased = $ogStringCased + $ogString[$arrayPlace]
- $tempHold = $ogString[$arrayPlace + 1]
- $tempHold = $tempHold.ToString().ToUpper()
- $ogStringCased = $ogStringCased + $tempHold
- $arrayPlace = $arrayPlace + 1
- }
- else
- {
- # If the current letter is not a space, just add it to the $ogStringCased
- # string without any modification
- $ogStringCased = $ogStringCased + $ogString[$arrayPlace]
- }
- $arrayPlace = $arrayPlace + 1
- $nullCheck = $ogString[$arrayPlace]
- }
- return $ogStringCased
- }
- $SERIES_NAME = caseCorrecter -Name $SERIES_NAME
- #SET CURRENT FOLDER AS MAIN MANGA FOLDER
- $COMICS_PARENT = $pwd
- "`nEnter range:"
- $requested_issue_lower = Read-Host -Prompt "Lower Limit: "
- $requested_issue_higher = Read-Host -Prompt "Higher Limit: "
- function Is-Numeric($value)
- {
- #Will return true if the input is numeric
- return $value -match "^[\d\.]+$"
- }
- if(-Not(Is-Numeric $requested_issue_lower) -or -Not(Is-Numeric $requested_issue_higher))
- {
- "Chapters are not numbers!"
- "Exiting..."
- Start-Sleep 2
- exit
- }
- if($requested_issue_lower -gt $requested_issue_higher)
- {
- "Input order is wrong!"
- "Swapping..."
- $t = $requested_issue_lower
- $requested_issue_lower = $requested_issue_higher
- $requested_issue_higher = $t
- }
- function getChapterNos {
- <#
- .SYNOPSIS
- Will return a string array of the chapter numbers.
- .PARAMETER $URL
- The url to the main page of the manga
- "http://www.mangaeden.com/en/en-manga/super-secret/"
- .PARAMETER $url_title
- The name of the series as it appears in the url
- "super-secret"
- and
- NOT "Super Secret"
- .EXAMPLE
- getChapterNos -URL "http://www.mangaeden.com/en/en-manga/super-secret/" -url-title "super-secret'
- #>
- param([string]$URL,[string]$url_title)
- $eden =Invoke-WebRequest -Uri "$URL"
- $links = ($eden.Content).Split("`n")
- $nos =""
- foreach($line in $links)
- {
- if($line.Contains("en-manga/$url_title/") -and $line.Contains("<a") -and (-Not($line.Contains("eden"))))
- {
- $number =$line.Substring(0,$line.LastIndexOf("/"))
- $number = $number.Replace("<a href=`"/en/en-manga/$url_title/","").Replace("/1","")
- $nos = $nos + $number+"`n"
- }
- }
- $nos = $nos -split "`n"
- [array]::Reverse($nos)
- #Removing the first line
- $nos = $nos[1..($nos.Length-1)]
- return $nos
- }
- $chapterNos = getChapterNos -URL $URL_SERIES -url_title $url_title
- $i = 0
- #$chapterNos contains all the existing chapter numbers that the website has.
- #The while loop is used to find the index of the requested chapter numbers
- while($chapterNos[$i])
- {
- if($chapterNos[$i] -eq $requested_issue_lower)
- {
- $lowIndex = $i
- "Lower limit $requested_issue_lower exists!"
- }
- if($chapterNos[$i] -eq $requested_issue_higher)
- {
- $highIndex = $i
- "Higher limit $requested_issue_higher exists!"
- }
- $i++
- }
- if(($lowIndex -eq $null) -or ($highIndex -eq $null))
- {
- "Requested chapters do not exist!"
- "Exiting..."
- Start-Sleep 2
- exit
- }
- function getImageLinks {
- <#
- .SYNOPSIS
- This function will return a string array containing all the urls of the individual pages in the chapter
- .DESCRIPTION
- The first page of the manga contains the direct imagelinks to the rest of the pages.
- The function will first obtain that line, and then after performing string operations,
- it will return a array of strings which are the direct URLs to the pages.
- All these are stored on one line in the source code, ie the line which starts with
- "var pages"
- .PARAMETER $ChapterURL
- The url of the first page of a chapter
- "http://www.mangaeden.com/en/en-manga/super-secret/1/1/"
- .EXAMPLE
- getImageLinks -ChapterURL "http://www.mangaeden.com/en/en-manga/super-secret/1/1/"
- #>
- param([string]$ChapterURL)
- $Chapter = Invoke-WebRequest -Uri $ChapterURL
- $links = ($Chapter.Content).Split("`n")
- $imageLinks = ""
- foreach($line in $links)
- {
- if($line.Contains("var pages"))
- #We need only the line which contains the URLs to the images,
- #hence break is used, and the line is stored in $PARA
- {
- $PARA = $line
- break
- }
- }
- #This line trims $PARA so that the content before '{"fs"' which is unneeded gets removed.
- $PARA = $PARA.Substring($PARA.IndexOf('{"fs"'))
- #Now, all the lines of $PARA start will the direct URL
- # ie "//cdn.mangaeden.com/mangasimg/a4/a401ad23155480090504fc6a226a0acc05ff945a4cede2e1918b8d82.png ..."
- $PARA = $PARA.Replace('{"fs": "',"`n")
- #$PARA is now a string array, so now we can use a foreach loop to obtain the url from each line
- $PARA = $PARA.Split("`n")
- foreach($line in $PARA)
- {
- if($line.Contains('"'))
- {
- #The required URL is only till before the occurence of '"'
- $singlelink = $line.Substring(0,$line.IndexOf('"'))
- $imageLinks = $imageLinks + "http:" + $singlelink + "`n"
- }
- }
- $imageLinks = $imageLinks -split "`n"
- return $imageLinks
- }
- $SERIES_FOLDER_PATH = "$COMICS_PARENT" + "\" + "$SERIES_NAME"
- if( -Not(Test-Path $SERIES_FOLDER_PATH))
- {
- New-Item -Path "$SERIES_FOLDER_PATH" -ItemType Directory | Out-Null
- "`n#################################"
- "`n$SERIES_NAME folder didnt exist, it has been created"
- }
- $chapterID = $lowIndex
- while($chapterID -le $highIndex)
- {
- Set-Location $SERIES_FOLDER_PATH
- $chapterNumber = $chapterNos[$chapterID]
- $URL_CHAPTER = $URL_SERIES + $chapterNumber + "/1/"
- $imageURLs = getImageLinks -ChapterURL $URL_CHAPTER
- #The $chapterNumberCorrect variable exists since Windows puts 56.4 before 56
- #So to preserve the correct reading order
- #I have put a ".0" after the integer chapters
- # and stored that value to $chapterNumberCorrect
- if($chapterNumber.Contains("."))
- {
- $chapterNumberCorrect = "$chapterNumber"
- }
- else
- {
- $chapterNumberCorrect = "$chapterNumber.0"
- }
- $CHAPTER_FOLDER_PATH = "$SERIES_FOLDER_PATH" + "\" + "Chapter " + "$chapterNumberCorrect"
- if(-Not(Test-Path $CHAPTER_FOLDER_PATH))
- {
- New-Item -Path "$CHAPTER_FOLDER_PATH" -ItemType Directory | Out-Null
- "`n#################################"
- # "`nChapter $chapterNumber folder didnt exist, it has been created"
- "`nChapter $chapterNumberCorrect folder didnt exist, it has been created"
- }
- Set-Location $CHAPTER_FOLDER_PATH
- "`n#################################"
- "`nDownloading Chapter $chapterNumberCorrect `n"
- #DOWNLOAD LOOP FOR SINGLE CHAPTER
- $pageID =0
- while($imageURLs[$pageID])
- {
- #[int] is used since, these variabls are used as strings in the following lines
- #without [int] there might be problems where
- #insead of 1+1=2, we might get 1+1=11
- [int]$pageNo = [int]$pageID + 1
- #These if-else make sure that the reader correctly arranges the pages
- #Since some readers put the order as
- # 1 10 11 12 ... 19 2 20 21 22
- if($pageNo -ge 1 -and $pageNo -lt 10)
- {
- $pageNoCorrect = "00" + $pageNo
- }
- if($pageNo -ge 10 -and $pageNo -lt 100)
- {
- $pageNoCorrect = "0" + $pageNo
- }
- if($pageNo -ge 100 -and $pageNo -lt 1000)
- {
- $pageNoCorrect = $pageNo
- }
- $PAGE_URL = $imageURLs[$pageID]
- $PAGE_PATH = $CHAPTER_FOLDER_PATH + "\" + $pageNoCorrect + ".jpg"
- if( Test-Path $PAGE_PATH)
- {
- "File " + $pageNoCorrect + " exists! Skipping file..."
- }
- else
- {
- $web = New-Object System.Net.WebClient
- "Downloading Page $pageNo"
- $web.DownloadFile($PAGE_URL,$PAGE_PATH)
- }
- #$pageID doesn't need a increment since $pageNO is already one more than $pageID
- $pageID =$pageNo
- }
- "`nChapter $chapterNumberCorrect is downloaded."
- #$zipName is the filename of the zip file that will be created for each folder
- #This variable can be customised to suit the user's needs
- #Currently it is set as
- #Chapter Number - Series Name - Chapter Number
- $zipName = "$chapterNumberCorrect - $SERIES_NAME - Chapter $chapterNumberCorrect"
- $chapterZipPath = $SERIES_FOLDER_PATH + "\" + $zipName + ".zip"
- if( -Not(Test-Path $chapterZipPath))
- {
- "`nCreating $zipName.zip"
- New-ZipFile -ZipFilePath $chapterZipPath -InputObject $CHAPTER_FOLDER_PATH | Out-Null
- }
- else
- {
- "$zipName.zip exists!"
- }
- $chapterID++
- }
- Read-Host -Prompt "Press Enter to exit."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement