Last active
October 11, 2023 17:43
-
-
Save mrreband/e97889ba5933afe0904229cf4a20eedc to your computer and use it in GitHub Desktop.
Scripts to get the NASA Image Of the Day (iotd) and NASA Astronomy Picture of the Day (apod)
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 and open the NASA Astronomy picture of the day (apod) | |
# Download to target folder $TargetFolder (relative to this script's location) | |
# https://apod.nasa.gov/apod/archivepix.html | |
param | |
( | |
[string]$TargetFolder = "images" | |
) | |
$ErrorActionPreference = "Stop" | |
. "$PSScriptRoot/util.ps1" | |
$api_key = Get-Content("$PSScriptRoot/ApiKey.txt") | |
$date = Get-Date -format "yyyy-MM-dd" | |
$RootUrl = "https://api.nasa.gov/planetary/apod" | |
$PageUrl = "${RootUrl}?api_key=${api_key}&date=${date}" | |
Write-Output "PageUrl = $PageUrl" | |
$hdurl = GetHdUrl($PageUrl) | |
Write-Output "ImageUrl = $hdurl" | |
DownloadImage $hdurl "images/apod" | |
exit |
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 and open the NASA Image of the day (iotd) | |
# Download to a path $TargetFolder (relative to this script's location) | |
# https://www.nasa.gov/multimedia/imagegallery/iotd.html | |
param | |
( | |
[string]$TargetFolder = "images/iotd" | |
) | |
$ErrorActionPreference = "Stop" | |
# Get the most recent item from NASA's RSS feed | |
$rssFeed = [xml](Invoke-WebRequest "https://www.nasa.gov/rss/dyn/lg_image_of_the_day.rss") | |
$PageUrl = $rssFeed.rss.channel.item[0].link | |
echo "PageUrl = $PageUrl" | |
$mostRecentItem = (Invoke-WebRequest $PageUrl) | |
# Find the image in the page | |
$ImageOfTheDay = $mostRecentItem.AllElements | Where-Object {$_.tagName -eq "meta" -and $_.property -eq "og:image"} | |
$ImageUrl = $ImageOfTheDay.content | |
echo "ImageUrl = $ImageUrl" | |
# Set the absolute target file path | |
$FileName = $ImageUrl.SubString($ImageUrl.LastIndexOf('/') + 1) | |
$AbsolutePath = Join-Path $PSScriptRoot ($TargetFolder) | |
$TargetFilePath = Join-Path $AbsolutePath ($FileName) | |
if (!(test-path($TargetFilePath))) | |
{ | |
# Download | |
echo "Downloading $TargetFilePath" | |
mkdir $AbsolutePath -Force | Out-Null | |
Invoke-WebRequest $ImageUrl -OutFile $TargetFilePath | |
# Open locally with the default app | |
start $TargetFilePath | |
} else { | |
echo "Target file already exists" | |
} | |
exit |
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
function GetHDUrl ($PageUrl) | |
{ | |
$response = Invoke-WebRequest -URI $PageUrl | |
if ($response.StatusCode -eq 200) | |
{ | |
$responseContent = $response.Content | |
$hdurlMatch = ($responseContent.split(",") | select-string("hdurl")) | |
if ($hdurlMatch) | |
{ | |
$hdurl = $hdurlMatch.Line -replace '"hdurl":"', '' -replace '"', '' | |
return $hdurl | |
} else { | |
# sometimes the picture of the day is a video | |
Throw "hdurl not found sry" | |
} | |
} | |
else | |
{ | |
Throw "invalid response code" | |
} | |
} | |
function DownloadImage ($ImageUrl, $TargetFolder) | |
{ | |
# Set the absolute target file path | |
$AbsolutePath = Join-Path $PSScriptRoot $TargetFolder | |
$startPosition = $ImageUrl.LastIndexOf('/') | |
$FileName = $ImageUrl.SubString($ImageUrl.LastIndexOf('/') + 1) | |
$TargetFilePath = Join-Path $AbsolutePath $FileName | |
if (!(test-path($TargetFilePath))) | |
{ | |
# Download | |
mkdir $AbsolutePath -Force | Out-Null | |
Invoke-WebRequest -Uri $ImageUrl -OutFile $TargetFilePath | |
# Open locally with the default app | |
Start-Process $TargetFilePath | |
} else { | |
Write-Output "Target file already exists" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
moved to a normal repo: https://github.com/mrreband/nasa-images