Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Where you want to store the pictures
- $DownloadPath = "/home/allywilson/Pictures/wallpapers"
- # The Astronomy Picture of the Day base URL
- $apod = "https://apod.nasa.gov/apod"
- # Let's get the archive page as it has all the links to the picture pages
- $ArchivePixPage = Invoke-WebRequest -Uri "$($apod)/archivepix.html"
- #Let's filter through all the links on the page and get the ones that begin with "ap" and end with ".html"
- $ArchivePixPageLinks = $ArchivePixPage.Links.href | Where-Object {$_ -like "ap*.html"}
- # While $i does not equal the number of links to ap*.html stay in this loop.
- $i = 0
- while ($i -lt $ArchivePixPageLinks.Count){
- # Let's get the JPG link within the ap*.html page
- # This line of code is also longer than 120 chars which goes against my aesthetics :-(
- # But, I don't want to sacrifice readability and invoke my code-golf past.
- $picLinkRef = ((Invoke-WebRequest -uri "$($apod)/$($ArchivePixPageLinks[$i])").Links | Where-Object {$_.href -like "image/*.jpg"}).href
- # Wait, what if there's more than 1 *.jpg link? Declaring [0] ruins single strings, so this is a bit of a hack.
- if ($picLinkRef -gt 0){
- # For every *.jpg link we find, we loop through it (these are more common than I expected).
- Foreach ($ref in $picLinkRef){
- # Let's see if we already have this file, if we do skip it (helpful if you got bored and killed script).
- if (!(Test-Path "$DownloadPath/$(($ref -split '/')[-1])")){
- # Let's download the pic and say where we are in the loop.
- "Downloading number $($i) of $(($ArchivePixPageLinks).count)...$(($ref -split '/')[-1])"
- Invoke-WebRequest -Uri "$($apod)/$($ref)" -OutFile "$($DownloadPath)/$(($ref -split '/')[-1])"
- # Please don't remove the below, I don't want to kill APOD web servers.
- Start-Sleep 2
- }
- }
- }
- # Increment $i so we're not stuck in a forever loop.
- $i++
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement