Skip to content

Instantly share code, notes, and snippets.

@mvark
Created May 14, 2014 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mvark/3de966c615f0809dcc1c to your computer and use it in GitHub Desktop.
Save mvark/3de966c615f0809dcc1c to your computer and use it in GitHub Desktop.
This PowerShell script to save min, max temperature fetched from Yahoo Weather API using YQL & store it in Windows Azure Table
Add-Type -Path "C:\Program Files\Microsoft SDKs\Windows Azure\.NET SDK\v2.1\ref\Microsoft.WindowsAzure.Storage.dll"
$refs = @("C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Data.Services.Client.dll")
$code = @"
using System;
using System.Data.Services.Common;
[DataServiceEntity]
public class WeatherEntity
{
public string PartitionKey { get; set; }
public string RowKey { get; set; }
public int high { get; set; }
public int low { get; set; }
}
"@
Add-Type -ReferencedAssemblies $refs -TypeDefinition $code
$accountName = "your_Windows_Azure_Storage_Account_Name"
$accountKey = "secret_key"
$credentials = new-object Microsoft.WindowsAzure.Storage.Auth.StorageCredentials($accountName, $accountKey);
$uri = New-Object System.Uri("http://$accountName.table.core.windows.net/");
$tableClient = New-Object Microsoft.WindowsAzure.Storage.Table.CloudTableClient($uri,$credentials);
$table = $tableClient.GetTableReference("temp"); # temp is the table name where we store min, max temperatures
$table.CreateIfNotExists();
#YQL query to get weather info for Bangalore whose woeid is 2295420
$url="https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid=2295420%20%20and%20u=%27c%27&format=json"
$result=(Invoke-WebRequest -Uri $url)
$conv=$result.Content
$convi = $conv | ConvertFrom-JSON
$high = $convi.query.results.channel.item.forecast.high[0]
$low = $convi.query.results.channel.item.forecast.low[0]
$pubDate = $convi.query.results.channel.item.pubDate
$weatherEntity = New-Object WeatherEntity
$weatherEntity.RowKey = $pubDate;
$weatherEntity.PartitionKey = "Bangalore";
$weatherEntity.high = $high;
$weatherEntity.low = $low;
$context = $tableClient.GetTableServiceContext();
$context.AddObject("temp", $weatherEntity);
$context.SaveChanges();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment