PoshCode Archive  Artifact [88386c2b91]

Artifact 88386c2b9131194abbc20bc883ad7211abf1b8be68654242ddee9bab0c2d105b:

  • File Get-Weather.ps1 — part of check-in [4eb5966cd3] at 2018-06-10 14:24:23 on branch trunk — Get-Weather parses and displays the current weather and forecast from the Yahoo! RSS. Simply enter your zipcode (or IXX code from Yahoo weather) and -c(elcius) if you don’t want Fahrenheit temperatures. (user: Joel Bennett size: 6291)

# encoding: utf-8
# api: powershell
# title: Get-Weather
# description: Get-Weather parses and displays the current weather and forecast from the Yahoo! RSS.  Simply enter your zipcode (or IXX code from Yahoo weather) and -c(elcius) if you don’t want Fahrenheit temperatures.
# version: 2.1
# type: function
# author: Joel Bennett
# license: CC0
# function: get-weather
# x-poshcode-id: 821
# x-derived-from-id: 1805
# x-archived: 2017-01-05T17:10:20
# x-published: 2010-01-23T13:22:00
#
#
#require -version 2.0
#require -shellid PoshConsole
###################################################################################################
## Get-Weather 
## Parse and display the current weather and forecast from yahoo RSS
## Note that you _could_ modify this a bit to return "current condition" and "forecast" objects
##   but for now, it just prints them out in a relatively nice format
###################################################################################################
## Version History:
## 2.1 - Updated for Out-WPF 2.0, can show -Popup graphics even in PowerShell.exe (with Out-WPF)
## 2.0 - Updated for Out-WPF and PoshConsole, now shows inline graphics in PoshConsole, 
## 1.1 - Added TempColor function
## 1.0 - My initial cleanup of a script from 
##       http`://fortheloveofcode.wordpress.com/2008/04/28/powershell-webservice-client/
#########
## ToDo: Color code the "text": Showers/Rain/Snow/...
## ToDo: Pull out extreme weather *Warnings*
## ToDo: Parse wind and show the wind-chill when temp is cold
###################################################################################################
# function get-weather {
param($zip=14586,[switch]$celcius,[switch]$Popup)
$url = "http`://weather.yahooapis.com/forecastrss?p={0}{1}" -f $zip, $(if($celcius){"&u=c"})

$channel = ([xml](New-Object Net.WebClient).DownloadString($url)).rss.channel

function TempColor ($temp) {
   if($celcius) { 
      if( $temp -lt 0 ) { "blue" } elseif( $temp -le 10 ) { "cyan" } elseif( $temp -le 21 ) { "blue" } elseif( $temp -lt 27 ) { "green" } else { "red" } 
   } else { 
      if( $temp -lt 5 ) { "blue" } elseif( $temp -le 50 ) { "cyan" } elseif( $temp -le 70 ) { "blue" } elseif( $temp -lt 80 ) { "green" } else { "red" }
   }
}

if($channel) {
   if( ($Host.PrivateData.GetType().Name  -eq "PoshOptions") -OR ( $Popup -AND (Get-Command Out-WPF -Type Cmdlet -EA SilentlyContinue))) { 

      # alternate images: http`://l.yimg.com/us.yimg.com/i/us/we/52/
      $template = @"
  <StackPanel xmlns="http`://schemas.microsoft.com/winfx/2006/xaml/presentation" >
  <TextBlock FontFamily="Constantia" FontSize="12pt">{0}, {1} {2}</TextBlock>
  <StackPanel Orientation="Horizontal">
  <StackPanel VerticalAlignment="Top" Margin="6,2,6,2" ToolTip="{3}">
  <Image Source="http`://image.weather.com/web/common/wxicons/52/{4}.png" Stretch="Uniform"
         Width="{{Binding Source.PixelWidth,RelativeSource={{RelativeSource Self}}}}"
         Height="{{Binding Source.PixelHeight,RelativeSource={{RelativeSource Self}}}}" />
  <TextBlock TextAlignment="Center"><Run FontWeight="700" Text="Now: " /><Run Foreground="{5}"> {6}{7}</Run></TextBlock>
  </StackPanel>
  <StackPanel VerticalAlignment="Top" Margin="6,2,6,2" ToolTip="{8}">
  <Image Source="http`://image.weather.com/web/common/wxicons/52/{9}.png" Stretch="Uniform"
         Width="{{Binding Source.PixelWidth,RelativeSource={{RelativeSource Self}}}}"
         Height="{{Binding Source.PixelHeight,RelativeSource={{RelativeSource Self}}}}" />
  <TextBlock TextAlignment="Center">
    <Run FontWeight="700">{10}</Run><LineBreak/>
    <Run Foreground="{11}">{12}{7}</Run> - <Run Foreground="{13}">{14}{7}</Run></TextBlock>
  </StackPanel>
  <StackPanel VerticalAlignment="Top" Margin="6,2,6,2" ToolTip="{15}">
  <Image Source="http`://image.weather.com/web/common/wxicons/52/{16}.png" Stretch="Uniform"
         Width="{{Binding Source.PixelWidth,RelativeSource={{RelativeSource Self}}}}"
         Height="{{Binding Source.PixelHeight,RelativeSource={{RelativeSource Self}}}}" />
  <TextBlock TextAlignment="Center">
  <Run FontWeight="700">{17}</Run><LineBreak/>
  <Run Foreground="{18}">{19}{7}</Run> - <Run Foreground="{20}">{21}{7}</Run></TextBlock>
  </StackPanel>
  </StackPanel>
  </StackPanel>
"@ 

      $template = ($template -f $channel.location.city,  $channel.location.region, $channel.lastBuildDate, 
      $channel.item.condition.text, $channel.item.condition.code, (TempColor $channel.item.condition.temp), $channel.item.condition.temp,
      $channel.units.temperature, $channel.item.forecast[0].text, $channel.item.forecast[0].code, 
      $channel.item.forecast[0].day, (TempColor $channel.item.forecast[0].low), $channel.item.forecast[0].low, (TempColor $channel.item.forecast[0].high), $channel.item.forecast[0].high, 
      $channel.item.forecast[1].text, $channel.item.forecast[1].code, $channel.item.forecast[1].day,
      (TempColor $channel.item.forecast[1].low), $channel.item.forecast[1].low, (TempColor $channel.item.forecast[1].high), $channel.item.forecast[1].high)

      $template | out-clipboard
      ## Work around defect in Out-WPF that hides -POpup when -Popup is implied. Silly of me.
      if($Host.PrivateData.GetType().Name  -eq "PoshOptions") {
         Out-WPF -SourceTemplate $template -Popup:$Popup
      } else {
         Out-WPF -SourceTemplate $template 
      }

   } else { # "ConsoleColorProxy"
      
      $current = $channel.item.condition
      Write-Host
      Write-Host ("Location:    {0}" -f $channel.location.city)
      Write-Host ("Last Update: {0}" -f $channel.lastBuildDate)
      Write-Host ("Weather:     {0}" -f $current.text)-NoNewline
      Write-Host (" {0}°{1}" -f $current.temp, $(if($celcius){"C"}else{"F"})) -ForegroundColor $(TempColor $current.temp)
      Write-Host
      Write-Host "Forecasts"
      Write-Host "---------"
      foreach ($f in $channel.item.forecast) {
         Write-Host ("`t{0}, {1}: {2}" -f $f.day, $f.date, $f.text) -NoNewline
         Write-Host (" {0}-{1}°{2}" -f $f.low, $f.high, $(if($celcius){"C"}else{"F"})) -ForegroundColor $(TempColor $f.High)
      }
      Write-Host
   }
}
#}