Skip to content

Instantly share code, notes, and snippets.

@andreburto
Last active January 17, 2018 18:12
Show Gist options
  • Save andreburto/db42e10daf596ad0291a7fffb0957cce to your computer and use it in GitHub Desktop.
Save andreburto/db42e10daf596ad0291a7fffb0957cce to your computer and use it in GitHub Desktop.
# Query Yahoo!Weather to get the results. More can be gleaned than just the forecast.
function getWeather([string]$city, [string]$state){
$query = "select * from weather.forecast where woeid in (select woeid from geo.places(1) where text='{0}, {1}')"
$url = "https://query.yahooapis.com/v1/public/yql?q={0}&format=xml&env=store://datatables.org/alltableswithkeys"
$newQuery = $query -f $city, $state
$newUrl = $url -f $newQuery
[xml]$data = Invoke-RestMethod -Uri $newUrl
return $data.query.results.channel.item.forecast
}
# To be clear this is the worst way possible to do this. I need to dig deeper into data binding.
function makeStacks($reportedWeather){
$whole = ""
$c = 0
$stack = [String]@"
<StackPanel Orientation="Horizontal" Margin="5,0,0,0">
<TextBlock Height="20" Width="100" HorizontalAlignment="Center">{0}</TextBlock>
<TextBlock Height="20" Width="60" HorizontalAlignment="Center">{1}</TextBlock>
<TextBlock Height="20" Width="60" HorizontalAlignment="Center">{2}</TextBlock>
<TextBlock Height="20" Width="60" HorizontalAlignment="Center">{3}</TextBlock>
<TextBlock Height="20" Width="200" HorizontalAlignment="Center">{4}</TextBlock>
</StackPanel>
"@
ForEach($row in $reportedWeather)
{
$whole += $stack -f $row.date, $row.day, $row.high, $row.low, $row.text
$c += 1
if ($c -eq 5) {break}
}
return $whole
}
# Main widget window code. Our XAML, event listeners, and procedural code all live here.
function weatherWidget([string]$city, [string]$state){
Add-Type -AssemblyName presentationframework
$forecast = getWeather -city $city -state $state
$weatherStacks = makeStacks -reportedWeather $forecast
$xaml = [xml]@"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="Window"
Title="Weather Widget"
Width="480" Height="200" WindowStyle="ToolWindow">
<StackPanel HorizontalAlignment="Left">
<TextBlock Margin="5,5,5,5">$city, $state</TextBlock>
<StackPanel Orientation="Horizontal" Margin="5,0,0,0">
<TextBlock Height="20" Width="100" HorizontalAlignment="Center">Date</TextBlock>
<TextBlock Height="20" Width="60" HorizontalAlignment="Center">Day</TextBlock>
<TextBlock Height="20" Width="60" HorizontalAlignment="Center">High</TextBlock>
<TextBlock Height="20" Width="60" HorizontalAlignment="Center">Low</TextBlock>
<TextBlock Height="20" Width="200" HorizontalAlignment="Center">Forecast</TextBlock>
</StackPanel>
$weatherStacks
</StackPanel>
</Window>
"@
$reader = New-Object System.Xml.XmlNodeReader $xaml
$form = [Windows.Markup.XamlReader]::Load($reader)
# No mouse needed to close window. Press the ESC key.
$form.add_KeyDown{
param([Parameter(Mandatory)][Object]$sender, [Parameter(Mandatory)][Windows.Input.KeyEventArgs]$e)
if ($e.Key -eq 'Escape')
{
$form.Close()
}
}
$form.ShowDialog() | Out-Null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment