Skip to content

Instantly share code, notes, and snippets.

@mrreband
Last active October 11, 2023 17:43
Show Gist options
  • Save mrreband/e97889ba5933afe0904229cf4a20eedc to your computer and use it in GitHub Desktop.
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)
# 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
# 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
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"
}
}
@mrreband
Copy link
Author

moved to a normal repo: https://github.com/mrreband/nasa-images

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment