Skip to content

Instantly share code, notes, and snippets.

@oxo42
Last active December 29, 2015 12:48
Show Gist options
  • Save oxo42/7672630 to your computer and use it in GitHub Desktop.
Save oxo42/7672630 to your computer and use it in GitHub Desktop.
Download multiple Digital Blasphemy Photos
$resolutions = @{"640x960" = "640x960"; "640x1136" = "640x1136"; "960x544" = "960x544"; "1024x1024" = "1024x1024"; "2048x2048" = "2048x2048"; "1080x1920" = "1080x1920"; "1920x1080" = "1080p" }
$user = $args[0]
$password = $args[1]
$url = $args[2]
$ismatch = $url -match 'i=(.+)$'
if(!$ismatch)
{
exit
}
$image = $Matches[1]
Function GetUrl([string]$image, [string]$res) {
# http://digitalblasphemy.com/content/jpgs/1440x1280/traveller2k1331440x1280.jpg
"http://digitalblasphemy.com/content/jpgs/$res/$image$res.jpg";
}
function DownloadFile($url, $targetFile)
{
"Downloading $url"
$uri = New-Object "System.Uri" "$url"
$request = [System.Net.HttpWebRequest]::Create($uri)
$request.Credentials = New-Object System.Net.NetworkCredential($user, $password)
$request.set_Timeout(15000) #15 second timeout
$response = $request.GetResponse()
$totalLength = [System.Math]::Floor($response.get_ContentLength()/1024)
$responseStream = $response.GetResponseStream()
$targetStream = New-Object -TypeName System.IO.FileStream -ArgumentList "$($(pwd).Path)/$targetFile", Create
$buffer = New-Object byte[] 10KB
$count = $responseStream.Read($buffer,0,$buffer.length)
$downloadedBytes = $count
while ($count -gt 0)
{
$downloadedKb = [System.Math]::Floor($downloadedBytes/1024)
Write-Host -NoNewline "`rDownloaded $($downloadedKb)K of $($totalLength)K"
$targetStream.Write($buffer, 0, $count)
$count = $responseStream.Read($buffer,0,$buffer.length)
$downloadedBytes = $downloadedBytes + $count
}
"`nFinished Download"
$targetStream.Flush()
$targetStream.Close()
$targetStream.Dispose()
$responseStream.Dispose()
}
foreach($dir in $($resolutions.Keys)) {
$res = $resolutions[$dir]
$downloadUrl = GetUrl $image $res
echo $downloadUrl
if(!(Test-Path -Path $dir )){
New-Item -ItemType directory -Path $dir
}
DownloadFile $downloadUrl "$dir/$image$res.jpg"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment