Created
July 22, 2020 21:55
-
-
Save marcusball/500183919ce425cd6b6e2fc66599157d to your computer and use it in GitHub Desktop.
Astro Pic of the Day
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Download the page | |
$page = Invoke-WebRequest -Uri "https://apod.nasa.gov/apod/astropix.html" | |
# Extract a list of all links to a full-size image | |
$imageLinks = $page.Links | Where-Object outerHTML -Like '*href="image/*' | |
# Extract the relative path from the HREF attribute | |
$link = $imageLinks | Select-String -Pattern '"(.*?)"' | % { $_.Matches.Groups[1].Value } | |
$link | ForEach-Object { | |
# Construct the full URL to the image | |
$fullUrl = "https://apod.nasa.gov/apod/$($_)" | |
# Create a friendly output name, changing "image/YEAR/FILE_NAME.jpg" to "YEAR_FILE_NAME.jpg" | |
$outFile = $_.Substring(6) -Replace "/", "_" | |
# Download to the current directory | |
Write-Output "Downloading file: $fullUrl to $outFile" | |
Invoke-WebRequest -Uri $fullUrl -OutFile $outFile | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment