using System.Collections.Generic;
using System.Threading.Tasks;
public class Program : Weather
public static void Main()
public static List<City> GetCityList()
List<City> Cities = new List<City>()
new City{name="Marlboro", State="MA",ZipCode="01752"},
new City{name="San Diego", State="CA",ZipCode="92112"},
new City{name="Cheyenne",State="WY",ZipCode="82009"},
new City{name="Anchorage", State="AK",ZipCode="99507"},
new City{name="Austin", State="TX",ZipCode="78731"},
new City{name="Orlando", State="FL",ZipCode="32806"},
new City{name="Seattle", State="WA",ZipCode="98109"},
new City{name="Cleveland", State="OH",ZipCode="44109"},
new City{name="Portland", State="ME",ZipCode="04101"},
new City{name="Honolulu", State="HI",ZipCode="96813"}
public static void GetForecast()
const string apiURL = "http://api.openweathermap.org/data/2.5/forecast";
const string siteKey = "3268b129d6a9c36a4ea3bfb126f0f4a0";
const string units = "imperial";
List<City> Cities = GetCityList();
if (apiURL != string.Empty && siteKey != string.Empty && Cities.Count>0)
foreach (City items in Cities)
await Readforecast(items.name,items.State,items.ZipCode, apiURL, siteKey, units);
throw new ArgumentNullException("Please check your variables!");
Console.WriteLine(DateTime.Now.ToString("yyyyMMdd HH:mm:ss.fff ") + e.ToString() + " " + e.Message);
public DateTime Day { get; set; }
public decimal Temperature { get; set; }
public string Unit { get; set; }
public string Precipitation { get; set; }
public string Snow { get; set; }
public string Rain { get; set; }
public const string units = "imperial";
public static async Task Readforecast(string CityName, string State, string ZipCode, string URL, string siteKey,string units)
string uriString = "{0}?zip={1},{2}&units={3}&appid={4}";
string URI = string.Format(uriString, URL,ZipCode,country,units,siteKey);
HttpClient httpClient = new HttpClient();
using (HttpClient vhttpClient = httpClient)
using (var vhttpResponse = vhttpClient.GetAsync(URI, HttpCompletionOption.ResponseHeadersRead).Result)
if (vhttpResponse.IsSuccessStatusCode)
using (HttpContent content = vhttpResponse.Content)
var resultContent = await vhttpResponse.Content.ReadAsStreamAsync().ConfigureAwait(false);
ProcessJson(resultContent,State, ZipCode, units);
throw new System.ArgumentException("There was an error retrieving data for {0} {1}", CityName);
Console.WriteLine(DateTime.Now.ToString("yyyyMMdd HH:mm:ss.fff ") + e.ToString() + " " + e.Message);
public static void ProcessJson(Stream jsonResponse,string State, string ZipCode, string units)
const string precipIndicator = "*";
Forecast jResult = new Forecast();
using (var sr = new StreamReader(jsonResponse))
using (var jtr = new JsonTextReader(sr))
var js = new JsonSerializer();
jResult = js.Deserialize<Forecast>(jtr);
string cName = jResult.city.name;
List<TemperatureInfo> dailyForecast = jResult.list.ToList();
var qry = from o in dailyForecast
Day = DateTime.Parse(o.dt_txt),
Snow = o.snow == null || string.IsNullOrEmpty(o.snow.snowVolume.ToString()) ? null : precipIndicator,
Rain = o.rain == null || string.IsNullOrEmpty(o.rain.rainVolume.ToString()) ? null : precipIndicator,
Temperature = o.main.temp
List<string> DaysPrecipitation = qry
.Where(x => !string.IsNullOrEmpty(x.Snow) || !string.IsNullOrEmpty(x.Rain))
.GroupBy(s => s.Day.ToShortDateString())
.Select(grp => grp.Key).ToList();
List<Weather> FinalList = (qry.GroupBy(d => d.Day.Date)
Temperature = Math.Round(g.Average(s => s.Temperature), 2)
foreach (var bItem in from string aItem in DaysPrecipitation
from Weather bItem in FinalList
let date = Convert.ToDateTime(aItem)
where date.Date == bItem.Day.Date
bItem.Precipitation = precipIndicator;
PrintResult(units, FinalList, cName, ZipCode, State);
Console.WriteLine(DateTime.Now.ToString("yyyyMMdd HH:mm:ss.fff ") + e.ToString() + " " + e.Message);
public static void PrintResult(string units, List<Weather> FinalList,string CityName, string ZipCode, string State)
string selectedUnid = string.Empty;
Console.WriteLine("____________________________");
Console.WriteLine("{0},{1}\t({2})", CityName, State, ZipCode);
Console.WriteLine("Date\t\tAvg\tTemp ({0})", selectedUnid);
Console.WriteLine("_ _ _ _ _ _ _ _ _ _ _ _ _ _ ");
foreach (Weather op in FinalList)
var date = op.Day.ToShortDateString();
if ((date != DateTime.Today.ToShortDateString()) && (qty<=5))
string vdate = string.Format("{0}{1}", date, op.Precipitation);
Console.WriteLine("{0}\t{1}\t{2}", vdate, op.Temperature.ToString(), selectedUnid);
Console.WriteLine(DateTime.Now.ToString("yyyyMMdd HH:mm:ss.fff ") + e.ToString() + " " + e.Message);
public string cod { get; set; }
public int message { get; set; }
public int cnt { get; set; }
public List<TemperatureInfo> list { get; set; }
public GeoInfo city { get; set; }
public class TemperatureInfo
public int dt { get; set; }
public Main main { get; set; }
public string dt_txt { get; set; }
public Rain rain { get; set; }
public Snow snow { get; set; }
public decimal temp { get; set; }
public decimal feels_like { get; set; }
public decimal temp_min { get; set; }
public decimal temp_max { get; set; }
public int pressure { get; set; }
public int sea_level { get; set; }
public int grnd_level { get; set; }
public int humidity { get; set; }
public decimal temp_kf { get; set; }
public decimal rainVolume { get; set; }
public decimal snowVolume { get; set; }
public int id { get; set; }
public string name { get; set; }
public string country { get; set; }
public int population { get; set; }
public int timezone { get; set; }
public int sunrise { get; set; }
public int sunset { get; set; }
public string name { get; set; }
public string ZipCode { get; set; }
public string State { get; set; }