示例#1
0
        public static void WriteWeatherObject(WeatherClass weatherObject, int ErrorCode = 0)
        {
            //The following block of code is used to make directories and/or the actual file as required. The paths are, of course, complicated.
            const string BasePath         = "D:\\Runka's Files\\WeatherData\\";
            DateTime     CurrentTimestamp = DateTime.Now;
            string       DataToWrite      = null;

            if (!Directory.Exists(String.Format($"{BasePath}{CurrentTimestamp.Year}")))
            {
                Directory.CreateDirectory(String.Format($"{BasePath}{CurrentTimestamp.Year}"));
            }

            if (!Directory.Exists(String.Format($"{BasePath}{CurrentTimestamp.Year}\\{CurrentTimestamp.ToString("MMMM")}")))
            {
                Directory.CreateDirectory(String.Format($"{BasePath}{CurrentTimestamp.Year}\\{CurrentTimestamp.ToString("MMMM")}"));
            }

            string CurrentFilePath = String.Format($"{BasePath}{CurrentTimestamp.Year}\\{CurrentTimestamp.ToString("MMMM")}\\{CurrentTimestamp.Day}.csv");


            if (!File.Exists(CurrentFilePath))
            {
                //If the file does not exist, create it and write "headers".
                using (StreamWriter streamWriter = new StreamWriter(CurrentFilePath))
                {
                    streamWriter.WriteLine("Day,Time,Temperature,Relative Humidity,Feels Like,Weather,Observation Day,Observation Time");
                }
            }

            if (ErrorCode == 0)
            {
                DataToWrite = weatherObject.Serialize();
            }
            else if (ErrorCode == 1)
            {
                DataToWrite = "Error code 1: Possible API problem";
            }
            else if (ErrorCode == 2)
            {
                DataToWrite = "Error code 2: Possible network problem";
            }

            //Finally, we can write the data to the file.

            DataToWrite = String.Format($"{CurrentTimestamp.Day},{CurrentTimestamp.TimeOfDay}, {DataToWrite}");
            using (StreamWriter streamWriter = new StreamWriter(CurrentFilePath, true))
            {
                streamWriter.WriteLine(DataToWrite);
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            int          ErrorCode = 0;
            WeatherClass weatherObject;

            // The following block of code is used to request the .json file and subsequently parse and store it.
            try
            {
                WebRequest request = WebRequest.Create(@"http://api.wunderground.com/api/XXXXXXXXX/conditions/q/IN/Kolkata.json"); // An API key has been censored here.
                request.ContentType = "application/json; charset=utf-8";
                string responseString;

                using (WebResponse response = request.GetResponse())
                {
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        using (StreamReader streamReader = new StreamReader(responseStream))
                        {
                            responseString = streamReader.ReadToEnd();
                        }
                    }
                }
                JObject jObject = JObject.Parse(responseString);
                weatherObject = new WeatherClass(jObject);
            }

            //A WebException occurs if there is a problem with the API; for example, a corrupt json file is returned.
            catch (WebException webException)
            {
                weatherObject = null;
                ErrorCode     = 1;
            }
            //An Exception implies a more general problem; for example, there is no internet access.
            catch (Exception exception)
            {
                weatherObject = null;
                ErrorCode     = 2;
            }

            IOHelpers.WriteWeatherObject(weatherObject, ErrorCode);
        }