Weather Updates In Console Application Using API

I am writing this article to give you a basic idea of consuming a Web API from Yahoo Weather and display it in the Console.

Let us create a project with Console App template.

Web API

Add a class and name it as WeatherReport.

Web API

Add the following properties in WeatherReport class.
  1. public class WeatherReport {  
  2.     public string date {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public long temp {  
  7.         get;  
  8.         set;  
  9.     }  
  10.     public string text {  
  11.         get;  
  12.         set;  
  13.     }  
  14. }  

Get woeid (Where on earth ID) of your city using this link (Just remove Hyderabad and keep your city name). For Hyderabad, the woeid is 2295414 which I read from the response of the below API call.

https://www.metaweather.com/api/location/search/?query=hyderabad.

The response of the above link will look like this -

  1. [{  
  2.     "title""Hyderabad",  
  3.     "location_type""City",  
  4.     "woeid": 2295414,  
  5.     "latt_long""17.508829,78.434578"  
  6. }]  

Now, let us use a wonderful Free API for weather updates and put it in our code. I used Yahoo API (since it is Free for developers) and passed the woeid 2295414 in the query.

  1. static void Main(string[] args)  
  2.         {  
  3. HttpClient client = new HttpClient();  
  4.             client.BaseAddress = new Uri("https://query.yahooapis.com/v1/public/yql?q=select%20item%20from%20weather.forecast%20where%20woeid%20in%20(2295414%20)&format=json");  
  5.             client.DefaultRequestHeaders.Accept.Clear();  
  6.             client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));  
  7.   
  8.             GetWeather(client).Wait();          
  9. }  
  10.   
  11.         static async Task GetWeather(HttpClient cons)  
  12.         {  
  13.             using (cons)  
  14.             {  
  15.                 HttpResponseMessage res = await cons.GetAsync("");  
  16.                 
  17.                 res.EnsureSuccessStatusCode();  
  18.                 if (res.IsSuccessStatusCode)  
  19.                 {  
  20.                     string weather = await res.Content.ReadAsStringAsync();                     
  21.   
  22.                    JObject jobj= JObject.Parse(weather);  
  23.                     JToken jToken = jobj.First;  
  24.                     string WeatherState = jToken.First["results"]["channel"]["item"]["condition"].ToString();  
  25.                     WeatherReport report = Newtonsoft.Json.JsonConvert.DeserializeObject<WeatherReport>(WeatherState);  
  26.                     Console.WriteLine("\n");  
  27.                  
  28.                     Console.WriteLine("Weather Station: Hyderabad" );  
  29.                     Console.WriteLine("Temperature Details");  
  30.                     Console.WriteLine("-----------------------------------------------------------");  
  31.                     Console.WriteLine("Temperature (in deg. C): "+ (report.temp-32)*0.55);// Converted from Fahrenheit to Celsius  
  32.                     Console.WriteLine("Weather State: " + report.text);  
  33.                     Console.WriteLine("Applicable Time: " + report.date);  
  34.                     Console.ReadLine();  
  35.                 }  
  36.             }  
  37.         }  

When we run the above code, the output will be something like this.

Weather Station: Hyderabad
Temperature Details
-----------------------------------------------------------
Temperature (in deg. C): 16.5
Weather State: Mostly Cloudy
Applicable Time: Tue, 19 Dec 2017 03:30 AM IST

Web API

We can get more details from JSON and display it on our Console. Here is how the JSON will look like.

  1. {  
  2.     "query": {  
  3.         "count": 1,  
  4.         "created""2017-12-18T23:34:41Z",  
  5.         "lang""en-US",  
  6.         "results": {  
  7.             "channel": {  
  8.                 "units": {  
  9.                     "distance""mi",  
  10.                     "pressure""in",  
  11.                     "speed""mph",  
  12.                     "temperature""F"  
  13.                 },  
  14.                 "title""Yahoo! Weather - Hyderabad, Telangana, IN",  
  15.                 "link""http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-2295414/",  
  16.                 "description""Yahoo! Weather for Hyderabad, Telangana, IN",  
  17.                 "language""en-us",  
  18.                 "lastBuildDate""Tue, 19 Dec 2017 05:04 AM IST",  
  19.                 "ttl""60",  
  20.                 "location": {  
  21.                     "city""Hyderabad",  
  22.                     "country""India",  
  23.                     "region"" Telangana"  
  24.                 },  
  25.                 "wind": {  
  26.                     "chill""61",  
  27.                     "direction""23",  
  28.                     "speed""7"  
  29.                 },  
  30.                 "atmosphere": {  
  31.                     "humidity""59",  
  32.                     "pressure""954.0",  
  33.                     "rising""0",  
  34.                     "visibility""16.1"  
  35.                 },  
  36.                 "astronomy": {  
  37.                     "sunrise""6:41 am",  
  38.                     "sunset""5:46 pm"  
  39.                 },  
  40.                 "image": {  
  41.                     "title""Yahoo! Weather",  
  42.                     "width""142",  
  43.                     "height""18",  
  44.                     "link""http://weather.yahoo.com",  
  45.                     "url""http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif"  
  46.                 },  
  47.                 "item": {  
  48.                     "title""Conditions for Hyderabad, Telangana, IN at 04:30 AM IST",  
  49.                     "lat""17.418409",  
  50.                     "long""78.450012",  
  51.                     "link""http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-2295414/",  
  52.                     "pubDate""Tue, 19 Dec 2017 04:30 AM IST",  
  53.                     "condition": {  
  54.                         "code""27",  
  55.                         "date""Tue, 19 Dec 2017 04:30 AM IST",  
  56.                         "temp""61",  
  57.                         "text""Mostly Cloudy"  
  58.                     },  
  59.                     "forecast": [{  
  60.                         "code""34",  
  61.                         "date""19 Dec 2017",  
  62.                         "day""Tue",  
  63.                         "high""82",  
  64.                         "low""62",  
  65.                         "text""Mostly Sunny"  
  66.                     }, {  
  67.                         "code""32",  
  68.                         "date""20 Dec 2017",  
  69.                         "day""Wed",  
  70.                         "high""81",  
  71.                         "low""60",  
  72.                         "text""Sunny"  
  73.                     }, {  
  74.                         "code""32",  
  75.                         "date""21 Dec 2017",  
  76.                         "day""Thu",  
  77.                         "high""82",  
  78.                         "low""58",  
  79.                         "text""Sunny"  
  80.                     }, {  
  81.                         "code""32",  
  82.                         "date""22 Dec 2017",  
  83.                         "day""Fri",  
  84.                         "high""82",  
  85.                         "low""59",  
  86.                         "text""Sunny"  
  87.                     }, {  
  88.                         "code""32",  
  89.                         "date""23 Dec 2017",  
  90.                         "day""Sat",  
  91.                         "high""83",  
  92.                         "low""57",  
  93.                         "text""Sunny"  
  94.                     }, {  
  95.                         "code""32",  
  96.                         "date""24 Dec 2017",  
  97.                         "day""Sun",  
  98.                         "high""80",  
  99.                         "low""63",  
  100.                         "text""Sunny"  
  101.                     }, {  
  102.                         "code""32",  
  103.                         "date""25 Dec 2017",  
  104.                         "day""Mon",  
  105.                         "high""81",  
  106.                         "low""59",  
  107.                         "text""Sunny"  
  108.                     }, {  
  109.                         "code""32",  
  110.                         "date""26 Dec 2017",  
  111.                         "day""Tue",  
  112.                         "high""81",  
  113.                         "low""61",  
  114.                         "text""Sunny"  
  115.                     }, {  
  116.                         "code""34",  
  117.                         "date""27 Dec 2017",  
  118.                         "day""Wed",  
  119.                         "high""81",  
  120.                         "low""62",  
  121.                         "text""Mostly Sunny"  
  122.                     }, {  
  123.                         "code""32",  
  124.                         "date""28 Dec 2017",  
  125.                         "day""Thu",  
  126.                         "high""79",  
  127.                         "low""60",  
  128.                         "text""Sunny"  
  129.                     }],  
  130.                     "description""<![CDATA[<img src=\"http://l.yimg.com/a/i/us/we/52/27.gif\"/>\n<BR />\n<b>Current Conditions:</b>\n<BR />Mostly Cloudy\n<BR />\n<BR />\n<b>Forecast:</b>\n<BR /> Tue - Mostly Sunny. High: 82Low: 62\n<BR /> Wed - Sunny. High: 81Low: 60\n<BR /> Thu - Sunny. High: 82Low: 58\n<BR /> Fri - Sunny. High: 82Low: 59\n<BR /> Sat - Sunny. High: 83Low: 57\n<BR />\n<BR />\n<a href=\"http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-2295414/\">Full Forecast at Yahoo! Weather</a>\n<BR />\n<BR />\n<BR />\n]]>",  
  131.                     "guid": {  
  132.                         "isPermaLink""false"  
  133.                     }  
  134.                 }  
  135.             }  
  136.         }  
  137.     }  
  138. }  

Up Next
    Ebook Download
    View all
    Learn
    View all