Last active
August 29, 2015 14:12
-
-
Save mvark/48e9e675ae6e0ed68c87 to your computer and use it in GitHub Desktop.
Generic Handler (.ASHX) which draws info from Yahoo Weather API & stores in Windows Azure Table. More info - http://mvark.blogspot.in/2014/01/how-to-make-automated-call-to-web-page.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This Generic Handler (.ASHX) code sample shows how to draw info from Yahoo Weather API & store it in Windows Azure Table. | |
The code can be configured to be triggered by an automated call to the Generic Handler (.ASHX) through Windows Azure Scheduler | |
*/ | |
<%@ WebHandler Language="C#" Class="temp" %> | |
using System; | |
using System.Web; | |
using Microsoft.WindowsAzure.Storage; | |
using Microsoft.WindowsAzure.Storage.Auth; | |
using Microsoft.WindowsAzure.Storage.Table; | |
using Newtonsoft.Json.Linq; | |
public class temp : IHttpHandler { | |
public void ProcessRequest (HttpContext context) { | |
//This YQL Query gets the high and low temperatures in Celsius for Hyderabad, India (whose WOE Id is 2295414) | |
var jsonString = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid=2295414%20%20and%20u=%27c%27&format=json"; | |
string results = ""; | |
using (System.Net.WebClient wc = new System.Net.WebClient()) | |
{ | |
results = wc.DownloadString(jsonString); | |
} | |
dynamic dataObject = Newtonsoft.Json.JsonConvert.DeserializeObject(results); | |
string pub = dataObject["query"]["results"]["channel"]["item"]["pubDate"]; | |
int high = dataObject["query"]["results"]["channel"]["item"]["forecast"].First["high"]; | |
int low = dataObject["query"]["results"]["channel"]["item"]["forecast"].First["low"]; | |
CloudStorageAccount account = new CloudStorageAccount(new StorageCredentials("your_Windows_Azure_Storage_Table_Name", "secret_key"), true); | |
CloudTableClient tableClient = account.CreateCloudTableClient(); | |
//Azure Storage Table "temp" has been created before-hand. | |
CloudTable table = tableClient.GetTableReference("temp"); | |
//Hyderabad is hard-coded here. A better way would be to get this dynamically so that this code can work for any city that has a WOE Id | |
WeatherEntity weatherEntity = new WeatherEntity(pub, "Hyderabad"); | |
weatherEntity.high = high; | |
weatherEntity.low = low; | |
// Create the TableOperation that inserts the customer entity. | |
TableOperation insertOperation = TableOperation.Insert(weatherEntity); | |
// Execute the insert operation. | |
table.Execute(insertOperation); | |
//context.Response.Write("Details saved to Azure Table"); | |
} | |
public class WeatherEntity : TableEntity | |
{ | |
public WeatherEntity(string pubdate, string city) | |
{ | |
this.PartitionKey = city; | |
this.RowKey = pubdate; | |
} | |
public WeatherEntity() { } | |
public int high { get; set; } | |
public int low { get; set; } | |
} | |
public bool IsReusable | |
{ | |
get { | |
return false; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment