Skip to content

Instantly share code, notes, and snippets.

@marcusball
Created July 22, 2020 21:55
Show Gist options
  • Save marcusball/500183919ce425cd6b6e2fc66599157d to your computer and use it in GitHub Desktop.
Save marcusball/500183919ce425cd6b6e2fc66599157d to your computer and use it in GitHub Desktop.
Astro Pic of the Day
# 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