Exemplo n.º 1
0
        public ActionResult Index()
        {
            string              airportCode = Request.QueryString["airportCode"];
            DateTime            date        = DateTime.Parse(Request.QueryString["date"]);
            WeatherIncidentType?filterType  = GetWeatherIncidentTypeFromRequest(Request);

            double radius;

            if (!double.TryParse(Request.QueryString["radius"], out radius))
            {
                radius = 15;
            }

            var mainAirport = AirportList.GetAirport(airportCode);
            var airports    = new List <Airport> {
                mainAirport
            };

            airports.AddRange(AirportList.FindNearbyAirports(mainAirport.Geocode, radius));
            airports = airports.Distinct().ToList();

            var weatherUnderground = new WeatherUnderground();
            List <WeatherIncident> weatherUndergroundIncidents = new List <WeatherIncident>();

            foreach (var airport in airports)
            {
                weatherUndergroundIncidents.AddRange(weatherUnderground.GetEvents(airport.AirportCode, date, filterType));
            }
            return(Json(weatherUndergroundIncidents.Distinct(), JsonRequestBehavior.AllowGet));
        }
        private static IEnumerable <WeatherIncident> ParseWundergroundResponse(string airportCode, string responseString, DateTime date, WeatherIncidentType?typeToFilter)
        {
            var     moreInfoUrl       = new Uri(string.Format(WeatherHistoryUrl, airportCode, date.Year, date.Month, date.Day));
            Airport currentAirpot     = AirportList.GetAirport(airportCode);
            var     locationOfAirport = Address.Search(string.Empty, currentAirpot.City, currentAirpot.State.Abbreviation, string.Empty, false);

            // Remove HTML comments and line breaks
            responseString =
                Regex.Replace(responseString, "<!--.*?-->", string.Empty, RegexOptions.Singleline).Replace("<br />", "");

            IEnumerable <string> rows = responseString.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);

            rows = rows.Skip(1); // Skip header row


            var incidents = new List <WeatherIncident>();

            foreach (string row in rows)
            {
                var entry = new WeatherUndergroundEntry(date, row);
                WeatherIncidentType incidentType = WeatherIncidentClassifier.Classify(entry);
                if (typeToFilter != null)
                {
                    if (incidentType == typeToFilter)
                    {
                        incidents.Add(new WeatherIncident(locationOfAirport, incidentType,
                                                          date, moreInfoUrl));
                    }
                }
                else
                {
                    if (incidentType != WeatherIncidentType.Unclassified)
                    {
                        incidents.Add(new WeatherIncident(locationOfAirport, incidentType,
                                                          date, moreInfoUrl));
                    }
                }
            }
            return(incidents);
        }