Skip to content

Instantly share code, notes, and snippets.

@Pusungwi
Forked from MichaelPote/himawari.ps1
Last active February 4, 2016 03:05
Show Gist options
  • Save Pusungwi/c69bf1fe041dbbe8cc3c to your computer and use it in GitHub Desktop.
Save Pusungwi/c69bf1fe041dbbe8cc3c to your computer and use it in GitHub Desktop.

Revisions

  1. Pusungwi revised this gist Feb 4, 2016. 1 changed file with 9 additions and 6 deletions.
    15 changes: 9 additions & 6 deletions himawari.ps1
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@
    #
    #

    function CreateWallpaperByDateTime($targetTime)
    function CreateWallpaperByDateTime($targetTime, $saveOnlyRecentFile = $true)
    {
    $width = 550
    $level = "4d" #Level can be 4d, 8d, 16d, 20d
    @@ -71,11 +71,14 @@ function CreateWallpaperByDateTime($targetTime)
    [void](New-Item -ItemType directory -Path $outpath)
    }

    #The filename that will be saved:
    #Uncomment this to have the files accumulate in the directory:
    #$outfile = "$year$month$day"+"_" + $time + ".jpg"
    #Use this to have the script just store the latest file only:
    $outfile = "latest.jpg"
    If ($saveOnlyRecentFile -eq $true)
    {
    $outfile = "latest.jpg"
    }
    else
    {
    $outfile = "$year$month$day"+"_" + $time + ".jpg"
    }


    $url = "http://himawari8-dl.nict.go.jp/himawari8/img/D531106/$level/$width/$year/$month/$day/$time"
  2. Pusungwi revised this gist Feb 4, 2016. 1 changed file with 5 additions and 4 deletions.
    9 changes: 5 additions & 4 deletions himawari.ps1
    Original file line number Diff line number Diff line change
    @@ -54,7 +54,7 @@ function CreateWallpaperByDateTime($targetTime)
    break
    }
    else {
    Write-Output "Failed to download image file. Back 1 Hour and retrying..."
    Write-Output "Failed to download image file. Back 1 hour and retrying..."
    $targetTime = $targetTime.AddHours(-1)
    }
    } While (1)
    @@ -183,9 +183,10 @@ function CreateWallpaperByDateTime($targetTime)
    Write-Output "Done"
    }

    $ts = New-TimeSpan
    $ts = New-TimeSpan -Minutes -30 #Number of minutes to add/subtract to the date.
    $now = ((Get-Date -Second 00) + $ts)
    #$now = $now.AddMinutes(-($now.minute % 10)) # download recent file
    $now = $now.AddMinutes(-($now.minute % 10)).AddDays(-1) # if recent file code is not work or viewing 'No Image', use this.

    $now = $now.ToUniversalTime().AddMinutes(-($now.minute % 10)) # download recent file
    #$now = $now.AddMinutes(-($now.minute % 10)).AddDays(-1) # if recent file code is not work or viewing 'No Image', use this.

    CreateWallpaperByDateTime $now
  3. Pusungwi revised this gist Feb 4, 2016. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion himawari.ps1
    Original file line number Diff line number Diff line change
    @@ -185,6 +185,7 @@ function CreateWallpaperByDateTime($targetTime)

    $ts = New-TimeSpan
    $now = ((Get-Date -Second 00) + $ts)
    $now = $now.AddMinutes(-($now.minute % 10)).AddDays(-1)
    #$now = $now.AddMinutes(-($now.minute % 10)) # download recent file
    $now = $now.AddMinutes(-($now.minute % 10)).AddDays(-1) # if recent file code is not work or viewing 'No Image', use this.

    CreateWallpaperByDateTime $now
  4. Pusungwi revised this gist Feb 4, 2016. 1 changed file with 0 additions and 27 deletions.
    27 changes: 0 additions & 27 deletions himawari.ps1
    Original file line number Diff line number Diff line change
    @@ -10,33 +10,6 @@
    #
    #

    function TestDownloadFileByURL($url)
    {
    Write-Output "[TEST] Downloading: $url"

    try
    {
    $request = [System.Net.WebRequest]::create($url)
    $response = $request.getResponse()
    $HTTP_Status = [int]$response.StatusCode

    if ($HTTP_Status -eq 200)
    {
    return $true
    }
    }
    Catch
    {
    $ErrorMessage = $_.Exception.Message
    $FailedItem = $_.Exception.ItemName
    Write-Output "Failed! $ErrorMessage with $FailedItem"

    return $false
    }

    return $true
    }

    function CreateWallpaperByDateTime($targetTime)
    {
    $width = 550
  5. Pusungwi revised this gist Feb 4, 2016. 1 changed file with 154 additions and 81 deletions.
    235 changes: 154 additions & 81 deletions himawari.ps1
    Original file line number Diff line number Diff line change
    @@ -10,83 +10,150 @@
    #
    #


    $ts = New-TimeSpan -Hours -2 -Minutes -30 #Number of hours and minutes to add/subtract to the date. Adjust this to offset your system date to around about GMT (I think?)
    $now = ((Get-Date -Second 00) + $ts)
    $now = $now.AddMinutes(-($now.minute % 10)).AddDays(-1)
    function TestDownloadFileByURL($url)
    {
    Write-Output "[TEST] Downloading: $url"

    try
    {
    $request = [System.Net.WebRequest]::create($url)
    $response = $request.getResponse()
    $HTTP_Status = [int]$response.StatusCode

    if ($HTTP_Status -eq 200)
    {
    return $true
    }
    }
    Catch
    {
    $ErrorMessage = $_.Exception.Message
    $FailedItem = $_.Exception.ItemName
    Write-Output "Failed! $ErrorMessage with $FailedItem"

    return $false
    }

    $width = 550
    $level = "4d" #Level can be 4d, 8d, 16d, 20d
    $numblocks = 4 #this apparently corresponds directly with the level, keep this exactly the same as level without the 'd'
    $time = $now.ToString("HHmmss")
    $year = $now.ToString("yyyy")
    $month = $now.ToString("MM")
    $day = $now.ToString("dd")
    return $true
    }

    #Create the folder My Pictures\Himawari\ if it doesnt exist
    $outpath = [Environment]::GetFolderPath("MyPictures") + "\Himawari\"
    if(!(Test-Path -Path $outpath ))
    function CreateWallpaperByDateTime($targetTime)
    {
    [void](New-Item -ItemType directory -Path $outpath)
    }
    $width = 550
    $level = "4d" #Level can be 4d, 8d, 16d, 20d
    $numblocks = 4 #this apparently corresponds directly with the level, keep this exactly the same as level without the 'd'

    #The filename that will be saved:
    #Uncomment this to have the files accumulate in the directory:
    #$outfile = "$year$month$day"+"_" + $time + ".jpg"
    #Use this to have the script just store the latest file only:
    $outfile = "latest.jpg"
    #if image file doesn't exist, find this recent image (-1 hour every try)
    Do
    {
    $tmpTime = $targetTime.ToString("HHmmss")
    $tmpYear = $targetTime.ToString("yyyy")
    $tmpMonth = $targetTime.ToString("MM")
    $tmpDay = $targetTime.ToString("dd")

    $testurl = "http://himawari8-dl.nict.go.jp/himawari8/img/D531106/$level/$width/$tmpYear/$tmpMonth/$tmpDay/$tmpTime" + "_0_0.png"
    Write-Output "[TEST] Downloading: $testurl"

    $testResult = $false
    try
    {
    $request = [System.Net.WebRequest]::create($testurl)
    $response = $request.getResponse()
    $HTTP_Status = [int]$response.StatusCode

    if ($HTTP_Status -eq 200)
    {
    $testResult = $true
    }
    }
    Catch
    {
    #$ErrorMessage = $_.Exception.Message
    #$FailedItem = $_.Exception.ItemName
    #Write-Output "Failed! $ErrorMessage with $FailedItem"

    $url = "http://himawari8-dl.nict.go.jp/himawari8/img/D531106/$level/$width/$year/$month/$day/$time"
    $testResult = $false
    }

    [void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
    If ($testResult -eq $true)
    {
    Write-Output "SUCCESS! : " $targetTime.ToString("yyyy-mm-dd HH:mm:ss")
    break
    }
    else {
    Write-Output "Failed to download image file. Back 1 Hour and retrying..."
    $targetTime = $targetTime.AddHours(-1)
    }
    } While (1)

    $image = New-Object System.Drawing.Bitmap(($width * $numblocks), ($width * $numblocks))
    $graphics = [System.Drawing.Graphics]::FromImage($image)
    $graphics.Clear([System.Drawing.Color]::Black)
    $time = $targetTime.ToString("HHmmss")
    $year = $targetTime.ToString("yyyy")
    $month = $targetTime.ToString("MM")
    $day = $targetTime.ToString("dd")

    for ($y = 0; $y -lt $numblocks; $y++)
    {
    for ($x = 0; $x -lt $numblocks; $x++)
    {
    $thisurl = $url + "_" + [String]$x + "_" + [String]$y + ".png"
    Write-Output "Downloading: $thisurl"

    try
    #Create the folder My Pictures\Himawari\ if it doesnt exist
    $outpath = [Environment]::GetFolderPath("MyPictures") + "\Himawari\"
    if(!(Test-Path -Path $outpath ))
    {

    $request = [System.Net.WebRequest]::create($thisurl)
    $response = $request.getResponse()
    $HTTP_Status = [int]$response.StatusCode
    If ($HTTP_Status -eq 200)
    {
    $imgblock = [System.Drawing.Image]::fromStream($response.getResponseStream())
    $graphics.DrawImage($imgblock,($x*$width),($y*$width) , $width, $width)
    $imgblock.dispose()
    $response.Close()
    }
    [void](New-Item -ItemType directory -Path $outpath)
    }
    Catch

    #The filename that will be saved:
    #Uncomment this to have the files accumulate in the directory:
    #$outfile = "$year$month$day"+"_" + $time + ".jpg"
    #Use this to have the script just store the latest file only:
    $outfile = "latest.jpg"


    $url = "http://himawari8-dl.nict.go.jp/himawari8/img/D531106/$level/$width/$year/$month/$day/$time"

    [void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")

    $image = New-Object System.Drawing.Bitmap(($width * $numblocks), ($width * $numblocks))
    $graphics = [System.Drawing.Graphics]::FromImage($image)
    $graphics.Clear([System.Drawing.Color]::Black)


    for ($y = 0; $y -lt $numblocks; $y++)
    {
    $ErrorMessage = $_.Exception.Message
    $FailedItem = $_.Exception.ItemName
    Write-Output "Failed! $ErrorMessage with $FailedItem"
    for ($x = 0; $x -lt $numblocks; $x++)
    {
    $thisurl = $url + "_" + [String]$x + "_" + [String]$y + ".png"
    Write-Output "Downloading: $thisurl"

    try
    {

    $request = [System.Net.WebRequest]::create($thisurl)
    $response = $request.getResponse()
    $HTTP_Status = [int]$response.StatusCode
    If ($HTTP_Status -eq 200)
    {
    $imgblock = [System.Drawing.Image]::fromStream($response.getResponseStream())
    $graphics.DrawImage($imgblock,($x*$width),($y*$width) , $width, $width)
    $imgblock.dispose()
    $response.Close()
    }
    }
    Catch
    {
    $ErrorMessage = $_.Exception.Message
    $FailedItem = $_.Exception.ItemName
    Write-Output "Failed! $ErrorMessage with $FailedItem"
    }
    }
    }
    }
    }


    $qualityEncoder = [System.Drawing.Imaging.Encoder]::Quality
    $encoderParams = New-Object System.Drawing.Imaging.EncoderParameters(1)
    $qualityEncoder = [System.Drawing.Imaging.Encoder]::Quality
    $encoderParams = New-Object System.Drawing.Imaging.EncoderParameters(1)

    # Set JPEG quality level here: 0 - 100 (inclusive bounds)
    $encoderParams.Param[0] = New-Object System.Drawing.Imaging.EncoderParameter($qualityEncoder, 90)
    $jpegCodecInfo = [System.Drawing.Imaging.ImageCodecInfo]::GetImageEncoders() | where {$_.MimeType -eq 'image/jpeg'}
    # Set JPEG quality level here: 0 - 100 (inclusive bounds)
    $encoderParams.Param[0] = New-Object System.Drawing.Imaging.EncoderParameter($qualityEncoder, 90)
    $jpegCodecInfo = [System.Drawing.Imaging.ImageCodecInfo]::GetImageEncoders() | where {$_.MimeType -eq 'image/jpeg'}

    $image.save(($outpath + $outfile), $jpegCodecInfo, $encoderParams)
    $image.Dispose()
    $image.save(($outpath + $outfile), $jpegCodecInfo, $encoderParams)
    $image.Dispose()

    <#
    Different settings for the wallpaper:
    @@ -113,32 +180,38 @@ $image.Dispose()
    break;
    #>

    Write-Output "Setting Wallpaper..."
    Set-ItemProperty -path "HKCU:Control Panel\Desktop" -name Wallpaper -value ($outpath + $outfile)
    Set-ItemProperty -path "HKCU:Control Panel\Desktop" -name WallpaperStyle -value 6
    Set-ItemProperty -path "HKCU:Control Panel\Desktop" -name TileWallpaper -value 0
    Set-ItemProperty 'HKCU:\Control Panel\Colors' -name Background -Value "0 0 0"
    #rundll32.exe user32.dll, UpdatePerUserSystemParameters


    Write-Output "Setting Wallpaper..."
    Set-ItemProperty -path "HKCU:Control Panel\Desktop" -name Wallpaper -value ($outpath + $outfile)
    Set-ItemProperty -path "HKCU:Control Panel\Desktop" -name WallpaperStyle -value 6
    Set-ItemProperty -path "HKCU:Control Panel\Desktop" -name TileWallpaper -value 0
    Set-ItemProperty 'HKCU:\Control Panel\Colors' -name Background -Value "0 0 0"
    #rundll32.exe user32.dll, UpdatePerUserSystemParameters
    $setwallpapersource = @"
    using System.Runtime.InteropServices;
    public class wallpaper
    {
    public const int SetDesktopWallpaper = 20;
    public const int UpdateIniFile = 0x01;
    public const int SendWinIniChange = 0x02;
    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    private static extern int SystemParametersInfo (int uAction, int uParam, string lpvParam, int fuWinIni);
    public static void SetWallpaper ( string path )
    {
    SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange );
    }
    }
    "@
    Add-Type -TypeDefinition $setwallpapersource
    [wallpaper]::SetWallpaper(($outpath + $outfile))


    $setwallpapersource = @"
    using System.Runtime.InteropServices;
    public class wallpaper
    {
    public const int SetDesktopWallpaper = 20;
    public const int UpdateIniFile = 0x01;
    public const int SendWinIniChange = 0x02;
    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    private static extern int SystemParametersInfo (int uAction, int uParam, string lpvParam, int fuWinIni);
    public static void SetWallpaper ( string path )
    {
    SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange );
    }
    Write-Output "Done"
    }
    "@
    Add-Type -TypeDefinition $setwallpapersource
    [wallpaper]::SetWallpaper(($outpath + $outfile))

    $ts = New-TimeSpan
    $now = ((Get-Date -Second 00) + $ts)
    $now = $now.AddMinutes(-($now.minute % 10)).AddDays(-1)

    Write-Output "Done"
    CreateWallpaperByDateTime $now
  6. Pusungwi revised this gist Feb 4, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion himawari.ps1
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@

    $ts = New-TimeSpan -Hours -2 -Minutes -30 #Number of hours and minutes to add/subtract to the date. Adjust this to offset your system date to around about GMT (I think?)
    $now = ((Get-Date -Second 00) + $ts)
    $now = $now.AddMinutes(-($now.minute % 10))
    $now = $now.AddMinutes(-($now.minute % 10)).AddDays(-1)



  7. @MichaelPote MichaelPote created this gist Feb 3, 2016.
    144 changes: 144 additions & 0 deletions himawari.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,144 @@
    #
    # Himawari-8 Downloader
    #
    #
    #
    # This script will scrape the latest image from the Himawari-8 satellite, recombining the tiled image,
    # converting it to a JPG which is saved in My Pictures\Himawari\ and then set as the desktop background.
    #
    # http://himawari8.nict.go.jp/himawari8-image.htm
    #
    #


    $ts = New-TimeSpan -Hours -2 -Minutes -30 #Number of hours and minutes to add/subtract to the date. Adjust this to offset your system date to around about GMT (I think?)
    $now = ((Get-Date -Second 00) + $ts)
    $now = $now.AddMinutes(-($now.minute % 10))



    $width = 550
    $level = "4d" #Level can be 4d, 8d, 16d, 20d
    $numblocks = 4 #this apparently corresponds directly with the level, keep this exactly the same as level without the 'd'
    $time = $now.ToString("HHmmss")
    $year = $now.ToString("yyyy")
    $month = $now.ToString("MM")
    $day = $now.ToString("dd")

    #Create the folder My Pictures\Himawari\ if it doesnt exist
    $outpath = [Environment]::GetFolderPath("MyPictures") + "\Himawari\"
    if(!(Test-Path -Path $outpath ))
    {
    [void](New-Item -ItemType directory -Path $outpath)
    }

    #The filename that will be saved:
    #Uncomment this to have the files accumulate in the directory:
    #$outfile = "$year$month$day"+"_" + $time + ".jpg"
    #Use this to have the script just store the latest file only:
    $outfile = "latest.jpg"


    $url = "http://himawari8-dl.nict.go.jp/himawari8/img/D531106/$level/$width/$year/$month/$day/$time"

    [void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")

    $image = New-Object System.Drawing.Bitmap(($width * $numblocks), ($width * $numblocks))
    $graphics = [System.Drawing.Graphics]::FromImage($image)
    $graphics.Clear([System.Drawing.Color]::Black)

    for ($y = 0; $y -lt $numblocks; $y++)
    {
    for ($x = 0; $x -lt $numblocks; $x++)
    {
    $thisurl = $url + "_" + [String]$x + "_" + [String]$y + ".png"
    Write-Output "Downloading: $thisurl"

    try
    {

    $request = [System.Net.WebRequest]::create($thisurl)
    $response = $request.getResponse()
    $HTTP_Status = [int]$response.StatusCode
    If ($HTTP_Status -eq 200)
    {
    $imgblock = [System.Drawing.Image]::fromStream($response.getResponseStream())
    $graphics.DrawImage($imgblock,($x*$width),($y*$width) , $width, $width)
    $imgblock.dispose()
    $response.Close()
    }
    }
    Catch
    {
    $ErrorMessage = $_.Exception.Message
    $FailedItem = $_.Exception.ItemName
    Write-Output "Failed! $ErrorMessage with $FailedItem"
    }
    }
    }


    $qualityEncoder = [System.Drawing.Imaging.Encoder]::Quality
    $encoderParams = New-Object System.Drawing.Imaging.EncoderParameters(1)

    # Set JPEG quality level here: 0 - 100 (inclusive bounds)
    $encoderParams.Param[0] = New-Object System.Drawing.Imaging.EncoderParameter($qualityEncoder, 90)
    $jpegCodecInfo = [System.Drawing.Imaging.ImageCodecInfo]::GetImageEncoders() | where {$_.MimeType -eq 'image/jpeg'}

    $image.save(($outpath + $outfile), $jpegCodecInfo, $encoderParams)
    $image.Dispose()

    <#
    Different settings for the wallpaper:
    Tile :
    key.SetValue(@"WallpaperStyle", "0") ;
    key.SetValue(@"TileWallpaper", "1") ;
    break;
    Center :
    key.SetValue(@"WallpaperStyle", "0") ;
    key.SetValue(@"TileWallpaper", "0") ;
    break;
    Stretch :
    key.SetValue(@"WallpaperStyle", "2") ;
    key.SetValue(@"TileWallpaper", "0") ;
    break;
    Fill :
    key.SetValue(@"WallpaperStyle", "10") ;
    key.SetValue(@"TileWallpaper", "0") ;
    break;
    Fit :
    key.SetValue(@"WallpaperStyle", "6") ;
    key.SetValue(@"TileWallpaper", "0") ;
    break;
    #>


    Write-Output "Setting Wallpaper..."
    Set-ItemProperty -path "HKCU:Control Panel\Desktop" -name Wallpaper -value ($outpath + $outfile)
    Set-ItemProperty -path "HKCU:Control Panel\Desktop" -name WallpaperStyle -value 6
    Set-ItemProperty -path "HKCU:Control Panel\Desktop" -name TileWallpaper -value 0
    Set-ItemProperty 'HKCU:\Control Panel\Colors' -name Background -Value "0 0 0"
    #rundll32.exe user32.dll, UpdatePerUserSystemParameters


    $setwallpapersource = @"
    using System.Runtime.InteropServices;
    public class wallpaper
    {
    public const int SetDesktopWallpaper = 20;
    public const int UpdateIniFile = 0x01;
    public const int SendWinIniChange = 0x02;
    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    private static extern int SystemParametersInfo (int uAction, int uParam, string lpvParam, int fuWinIni);
    public static void SetWallpaper ( string path )
    {
    SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange );
    }
    }
    "@
    Add-Type -TypeDefinition $setwallpapersource
    [wallpaper]::SetWallpaper(($outpath + $outfile))


    Write-Output "Done"