Sunday, September 4, 2016

Download Earthporn wallpapers

This little PowerShell function will go out to Reddit and download all jpeg images and save them into the folder of your choosing. I recommend running it for about 5 minutes to get about 100 HD wallpapers. I say this because if you do not stop the script by hitting '[Ctrl] + c' or close the PowerShell window this script will slowly go through every page of this subreddit and could possibly fill up your hard drive.


Enjoy!

Function Download-EarthPorn {
<#
.Synopsis
   Downloads EarthPorn images from Reddit
.DESCRIPTION
   Goes out to reddit's EarthPorn subreddit and downloads all jpg images, `
   then it removes any smaller images that might not look so well as a wallpaper.
.EXAMPLE
   PS:\> Download-EarthPorn
#>
    [CmdletBinding()]
        Param(
            # URL to download images from.
            [Parameter(Mandatory=$False, ValueFromPipelineByPropertyName=$true, Position=0)]
            [string]$URL = "www.reddit.com/r/earthporn",
            # File location that you would like to save the downloaded images to.
            [Parameter(Mandatory=$False, ValueFromPipelineByPropertyName=$true, Position=)]
            [String]$Destination = "C:\Users\$env:USERNAME\Desktop\Earthporn_Wallpapers"
        )
    Begin {
            # Imports bittransfer cmdlets
            Import-Module BitsTransfer -verbose
          }
    Process{
             # checks if the file exists, else creates it.
             IF (!(Test-Path $Destination)) {New-Item -Path $Destination -ItemType Directory -Force | Out-Null}
                 Do {
                      $Links = (Invoke-WebRequest -Uri $URL).links
                      ($links | Where { $_.href -match ".jpg" } | Where { $_.class -match "title" }).href | Foreach {Start-BitsTransfer -Source $_ -Destination $Destination }
                      $URL = ($Links | Where { $_.innerHTML  -eq "next ›" }).href
                      $URL
                    } While (1 -eq 1)
           }
    End {
          # Removes smaller images.
          (Get-ChildItem -Filter *.jpg).FullName | % { $img = [Drawing.Image]::FromFile($_); if ($img.Width -lt 1680 -OR $img.Height -lt 1050) { Remove-Item $_ }}
        }
} # End of Download-EarthPorn function
Download-EarthPorn

No comments:

Post a Comment