# edit of Pastebin downloader 0.1 # - Open a PowerShell window (press the Windows key, write powershell, press enter) # - Navigate to the folder you want to download the pastes to # - Copy all the contents from this file and paste them to the window (right click) # - Use any of the functions here: # + DownloadPaste expects a valid paste url, it will download a single paste: # > DownloadPaste https://pastebin.com/xxx # + DownloadUser expects a valid user url, it will download all the pastes: # > DownloadUser https://pastebin.com/u/xxx # + Pastebin can be used as a replacement for both the previous functions, # it also allows downloading all the links (pastes and users) from a file: # > Pastebin https://pastebin.com/xxx # > Pastebin https://pastebin.com/u/xxx # > Pastebin input.txt function DownloadPaste { param ( $URLPaste, [switch]$GetUser ) Write-Host "Downloading paste $URLPaste..." $session = New-Object Microsoft.PowerShell.Commands.WebRequestSession $request = Invoke-WebRequest -Uri $URLPaste -WebSession $session if (!$request) { Write-Host "Error downloading paste" return } if (!$request.ParsedHtml) { Write-Host "Error parsing paste" return } if ( $request.ParsedHtml.title -eq "Pastebin.com - Potentially offensive content ahead!" ) { $csrf = $request.ParsedHtml.querySelector("input[name=_csrf-frontend]").value $postParams = @{'_csrf-frontend'=$csrf;'is_spam'='0'} $request = Invoke-WebRequest $URLPaste -WebSession $session -Method POST -Body $postParams } $title = $request.ParsedHtml.querySelector(".info-top h1").innerText.trim() $author = "Anonymous" $authorURL = $request.ParsedHtml.querySelector(".username a").href $authorExtra = "" if ( $authorURL ) { $author = $request.ParsedHtml.querySelector(".username").innerText.trim() $authorURL = $authorURL.replace("about:", "https://pastebin.com") $authorExtra = " ($authorURL)" if ( $GetUser ) { DownloadUser $authorURL } } $created = $request.ParsedHtml.querySelector(".info-bottom .date span").title $modified = $request.ParsedHtml.querySelector(".info-bottom .date span+span").title $lastEdit = "" if ( $modified ) { $lastEdit = "`r`n$modified" } $retrieved = Get-Date $retrieved = $retrieved.ToUniversalTime().ToString('dddd d "of" MMMM yyyy hh:mm:ss tt UTC', [CultureInfo]'en-us') $header = "`"$title`"`r`nBy $author$authorExtra`r`n`r`nURL: $URLPaste`r`nCreated on: $created$lastEdit`r`nRetrieved on: $retrieved`r`n`r`n`r`n" $idPaste = $URLPaste.split("/")[3] $paste = Invoke-WebRequest "https://pastebin.com/raw/$idPaste" -WebSession $session $paste = $paste.Content if ( $paste ) { $text = "$header$paste" $folder = $author.Split([IO.Path]::GetInvalidFileNameChars()) -join '_' if ( -not (Test-Path $folder) ) { mkdir $folder | Out-Null } $filename = $title.Split([IO.Path]::GetInvalidFileNameChars()) -join '_' $filename = "$filename-$idPaste.txt" Set-Content -Encoding utf8 -LiteralPath "$folder/$filename" -Value "$text" Write-Host "Saved to $folder/$filename" } else { Write-Host "Empty paste" } } function DownloadUser { param ( $URLUser ) Write-Host $URLUser $user = $URLUser.split("/")[4] Write-Host "Downloading pastes from user $user..." $last = 1 $page = 1 Do { Write-Host "Downloading page $page..." $request = Invoke-WebRequest "https://pastebin.com/u/$user/$page" $last = $request.ParsedHtml.querySelector(".pagination div").lastChild.href if ( $last ) { $last = $last.split("/")[3] } $links = $request.ParsedHtml.querySelectorAll("tr td span+a") for ($i=0; $i -le $links.length-1; $i++) { $paste = $links[$i].href.split("/")[1] DownloadPaste "https://pastebin.com/$paste" } $page += 1 } while ( $page -le $last ) } function Pastebin { param ( $UserInput ) if ( Test-Path $UserInput ) { Get-Content -Path $UserInput | ForEach-Object { Pastebin $_ } } elseif ( -not ($UserInput -like "*pastebin.com/*") ) { Write-Host "Invalid URL $UserInput!" } elseif ( $UserInput -like "*/u/*" ) { DownloadUser $UserInput } else { DownloadPaste $UserInput -GetUser } }