Exemplo n.º 1
0
        public override IList <Earthquake> Collect()
        {
            try
            {
                IList <Earthquake>   earthquakes     = new List <Earthquake>();
                List <MagnitudeType> MagnitudesTypes = new List <MagnitudeType>();
                List <Place>         Places          = new List <Place>();

                var docArgentina = SourceManagerHTML.GetDocument(WebSiteUrl);


                //check if data is available
                if (string.IsNullOrEmpty(docArgentina.DocumentNode.InnerText))
                {
                    UpdateSourceStatus(false);
                    return(new List <Earthquake>());
                }
                UpdateSourceStatus(true);

                // Get all tables in the document
                HtmlNodeCollection tables = docArgentina.DocumentNode.SelectNodes("//table");

                var earthquakeTable = tables.Where(tb => tb.Id.Equals("sismos")).Single();



                // Iterate all rows in the first table
                HtmlNodeCollection rows1 = earthquakeTable.SelectNodes(".//tr");
                if (rows1.Count > 1)
                {
                    for (int i = 2; rows1.Count > i; ++i)
                    {
                        // Iterate all columns in this row
                        HtmlNodeCollection cols = rows1[i].SelectNodes(".//td");
                        var cantidadColumnas    = cols.Count;
                        if (cantidadColumnas >= 8)
                        {
                            var  sensible1  = cols[8];
                            bool isSensible = sensible1.OuterHtml.Contains("#D40000");



                            string        LocalDateTimeYear = string.Empty;
                            DateTime      UTCDateTime       = new DateTime();
                            decimal       Latitude          = 0;
                            decimal       Longitude         = 0;
                            decimal       Depth             = 0;
                            decimal       Magnitude         = 0;
                            decimal       decimalValue      = 0;
                            Place         Place             = null;
                            MagnitudeType MagnitudeType     = null;

                            for (int j = 0; j < cols.Count; ++j)
                            {
                                string value = cols[j].InnerText;
                                switch (j)
                                {
                                //Local time
                                case (1):
                                    LocalDateTimeYear = value;
                                    break;

                                case (2):
                                    StringBuilder sb          = new StringBuilder();
                                    string        stringValue = sb.Append(LocalDateTimeYear).Append(" ").Append(value).ToString();
                                    const string  format      = "dd/MM/yyyy HH:mm:ss";
                                    var           realtime    = DateTime.ParseExact(stringValue, format, CultureInfo.InvariantCulture);
                                    UTCDateTime = GlobalWebData.ToUniversalTime(realtime);
                                    break;

                                //Latitude
                                case (3):
                                    /*string[] coord1 = value.Split('�');
                                     * string[] coord2 = coord1[1].Split('\'');
                                     * var degrees = Convert.ToDecimal(coord1[0], CultureInfo.InvariantCulture);
                                     * var minutes = Convert.ToDecimal(coord2[0], CultureInfo.InvariantCulture);
                                     * var seconds = Convert.ToDecimal(coord2[1], CultureInfo.InvariantCulture);
                                     * Latitude = Earthquake.GetDecimal(degrees, minutes, seconds);*/
                                    Latitude = decimal.Parse(value, CultureInfo.InvariantCulture);
                                    break;

                                //Longitude
                                case (4):
                                    /*string[] coord3 = value.Split('�');
                                     * string[] coord4 = coord3[1].Split('\'');
                                     * var degrees1 = Convert.ToDecimal(coord3[0], CultureInfo.InvariantCulture);
                                     * var minutes1 = Convert.ToDecimal(coord4[0], CultureInfo.InvariantCulture);
                                     * var seconds1 = Convert.ToDecimal(coord4[1], CultureInfo.InvariantCulture);
                                     * Longitude = Earthquake.GetDecimal(degrees1, minutes1, seconds1);*/
                                    Longitude = decimal.Parse(value, CultureInfo.InvariantCulture);
                                    break;

                                //Depth
                                case (5):
                                    string[] depthArray = value.Split(' ');
                                    decimalValue = Convert.ToDecimal(depthArray[0], CultureInfo.InvariantCulture);
                                    Depth        = decimalValue;
                                    break;

                                //Magnitude
                                case (6):
                                    string[] magnitudArray = value.Split(' ');
                                    decimalValue = Convert.ToDecimal(magnitudArray[0], CultureInfo.InvariantCulture);
                                    Magnitude    = decimalValue;

                                    //tambien inferir que los sismos de 3.5 o mas son sensibles...

                                    double? d            = 3.5;
                                    decimal?topMagnitude = (decimal?)d.Value;
                                    // decimal topMagnitude = decimal.Parse( "3.5");
                                    if (Magnitude >= topMagnitude)
                                    {
                                        isSensible = true;
                                    }
                                    var mtypes = MagnitudesTypes.Where(mt => mt.Type.Equals(magnitudeTypeArgentina)).FirstOrDefault();

                                    if (mtypes == null)
                                    {
                                        mtypes = this.IMagnitudeTypeRepository.FindByType(magnitudeTypeArgentina);
                                        if (mtypes != null)
                                        {
                                            MagnitudesTypes.Add(mtypes);
                                        }
                                    }

                                    if (mtypes == null)
                                    {
                                        mtypes = this.IMagnitudeTypeFactory.Create(magnitudeTypeArgentina);
                                        IMagnitudeTypeRepository.DbContext.MagtitudesTypes.Add(mtypes);
                                        IMagnitudeTypeRepository.Save();
                                        MagnitudesTypes.Add(mtypes);
                                    }

                                    MagnitudeType = mtypes;
                                    break;

                                //Place
                                case (7):
                                    value = value.ToUpper();
                                    string country;

                                    if (value.Contains("CHILE"))
                                    {
                                        country = countryChile;
                                    }
                                    else if (value.Contains("BOLIVIA"))
                                    {
                                        country = countryBolivia;
                                    }
                                    else
                                    {
                                        country = countryArgentina;
                                    }

                                    var pla = Places.Where(pl => pl.PlaceName.Equals(value) && pl.Country.Equals(country)).FirstOrDefault();

                                    if (pla == null)
                                    {
                                        pla = this.IPlaceRepository.FindByPlaceCountry(value, country);
                                        if (pla != null)
                                        {
                                            Places.Add(pla);
                                        }
                                    }

                                    if (pla == null)
                                    {
                                        pla = this.IPlaceFactory.Create(value, country);
                                        IPlaceRepository.DbContext.Places.Add(pla);
                                        IPlaceRepository.Save();
                                        Places.Add(pla);
                                    }

                                    Place = pla;
                                    break;
                                }
                            }
                            Earthquake earthquake = this.IEarthqueakeFactory.Create(UTCDateTime, Latitude, Longitude, Depth,
                                                                                    Magnitude, isSensible, Place, Source, MagnitudeType);
                            earthquakes.Add(earthquake);
                        }
                        else
                        {
                            ExceptionUtility.Warn(string.Concat("cantidad de columnas", " ", cantidadColumnas, " ", this.GetType()));
                        }
                    }
                }
                return(earthquakes);
            }
            catch (Exception ex)
            {
                ExceptionUtility.Error(ex, this.GetType());
                return(new List <Earthquake>());
            }
        }
Exemplo n.º 2
0
        public override IList <Earthquake> Collect()
        {
            try
            {
                IList <Earthquake>   earthquakes     = new List <Earthquake>();
                List <MagnitudeType> MagnitudesTypes = new List <MagnitudeType>();
                List <Place>         Places          = new List <Place>();

                var docChile = SourceManagerHTML.GetDocument(WebSiteUrl);


                //check if data is available
                if (string.IsNullOrEmpty(docChile.DocumentNode.InnerText))
                {
                    UpdateSourceStatus(false);
                    return(new List <Earthquake>());
                }

                UpdateSourceStatus(true);


                // Get all tables in the document
                HtmlNodeCollection tables = docChile.DocumentNode.SelectNodes("//table");

                var earthquakeTable = tables[0];

                // Iterate all rows in the first table
                HtmlNodeCollection rows = earthquakeTable.SelectNodes(".//tr");
                if (rows.Count > 1)
                {
                    for (int i = 1; rows.Count > i; ++i)
                    {
                        bool IsSensible = false;
                        //determina si fue percibido
                        string sensible = rows[i].Attributes[0].Value;

                        if (sensible.Contains("sensible"))
                        {
                            IsSensible = true;
                        }



                        //Si es percibido entonces obtengo los datos
                        if (IsSensible)
                        {
                            // Iterate all columns in this row
                            HtmlNodeCollection cols = rows[i].SelectNodes(".//th");
                            if (cols == null)
                            {
                                cols = rows[i].SelectNodes(".//td");
                            }

                            DateTime      UTCDateTime   = new DateTime();
                            decimal       Latitude      = 0;
                            decimal       Longitude     = 0;
                            decimal       Depth         = 0;
                            decimal       Magnitude     = 0;
                            decimal       decimalValue  = 0;
                            Place         Place         = null;
                            MagnitudeType MagnitudeType = null;

                            for (int j = 1; j < cols.Count; ++j)
                            {
                                string value = cols[j].InnerText;
                                switch (j)
                                {
                                //Local time : ignore
                                case (0):
                                    break;

                                //UTC time
                                case (1):
                                    const string format = "yyyy/MM/dd HH:mm:ss";
                                    UTCDateTime = DateTime.ParseExact(value, format, CultureInfo.InvariantCulture);
                                    break;

                                //Latitude
                                case (2):
                                    Latitude = Convert.ToDecimal(value, CultureInfo.InvariantCulture);
                                    break;

                                //Longitude
                                case (3):
                                    Longitude = Convert.ToDecimal(value, CultureInfo.InvariantCulture);
                                    break;

                                //Depth
                                case (4):
                                    Depth = Convert.ToDecimal(value, CultureInfo.InvariantCulture);
                                    break;

                                //Magnitude
                                case (5):
                                    string[] magnitudArray = value.Trim().Split(' ');
                                    decimalValue = Convert.ToDecimal(magnitudArray[0], CultureInfo.InvariantCulture);
                                    Magnitude    = decimalValue;

                                    var mtypes = MagnitudesTypes.Where(mt => mt.Type.Equals(magnitudeTypeChile)).FirstOrDefault();

                                    if (mtypes == null)
                                    {
                                        mtypes = this.IMagnitudeTypeRepository.FindByType(magnitudeTypeChile);
                                        if (mtypes != null)
                                        {
                                            MagnitudesTypes.Add(mtypes);
                                        }
                                    }

                                    if (mtypes == null)
                                    {
                                        mtypes = this.IMagnitudeTypeFactory.Create(magnitudeTypeChile);
                                        IMagnitudeTypeRepository.DbContext.MagtitudesTypes.Add(mtypes);
                                        IMagnitudeTypeRepository.Save();
                                        MagnitudesTypes.Add(mtypes);
                                    }

                                    MagnitudeType = mtypes;

                                    break;

                                //Place
                                case (7):
                                    //"AR\r\n"
                                    string        realCountry /*= countryChile*/;
                                    StringBuilder countrySB = new StringBuilder();
                                    countrySB.Append("http://api.geonames.org/countryCode?");

                                    var lat1 = Latitude.ToString().Replace(",", ".");
                                    lat1 = lat1.Remove(6, 1);

                                    var long2 = Longitude.ToString().Replace(",", ".");
                                    long2 = long2.Remove(6, 1);

                                    countrySB.Append("lat=").Append(lat1);
                                    countrySB.Append("&");
                                    countrySB.Append("lng=").Append(long2);
                                    countrySB.Append("&username=argentinaearthquake");

                                    var findedCountry = SourceManagerHTML.GetDocument(countrySB.ToString());
                                    //AR\r\n
                                    if (!string.IsNullOrEmpty(findedCountry.DocumentNode.InnerText) && findedCountry.DocumentNode.InnerText.Contains("AR"))
                                    {
                                        //si informan desde chile un sismo en Argentina, da por hecho que es sensible
                                        IsSensible  = true;
                                        realCountry = countryArgentina;
                                    }
                                    //CL\r\n
                                    else if (!string.IsNullOrEmpty(findedCountry.DocumentNode.InnerText) && findedCountry.DocumentNode.InnerText.Contains("CL"))
                                    {
                                        realCountry = countryChile;
                                    }
                                    //BL\r\n   //Error: no country code found
                                    else if (!string.IsNullOrEmpty(findedCountry.DocumentNode.InnerText) && findedCountry.DocumentNode.InnerText.Contains("BL") && !findedCountry.DocumentNode.InnerText.Contains("no country code found"))
                                    {
                                        realCountry = countryBolivia;
                                    }

                                    else
                                    {
                                        realCountry = countryChile;
                                        if (!findedCountry.DocumentNode.InnerText.Contains("no country code found"))
                                        {
                                            ExceptionUtility.Warn(string.Concat("GEONAMES-API, CHILE COLLECTOR ", findedCountry.DocumentNode.InnerText));
                                        }

                                        //"no country code found" : pacific ocean geopoint
                                    }
                                    value = value.ToUpper();

                                    var pla = Places.Where(pl => pl.PlaceName.Equals(value) && pl.Country.Equals(realCountry)).FirstOrDefault();

                                    if (pla == null)
                                    {
                                        pla = this.IPlaceRepository.FindByPlaceCountry(value, realCountry);
                                        if (pla != null)
                                        {
                                            Places.Add(pla);
                                        }
                                    }

                                    if (pla == null)
                                    {
                                        pla = this.IPlaceFactory.Create(value, realCountry);
                                        IPlaceRepository.DbContext.Places.Add(pla);
                                        IPlaceRepository.Save();
                                        Places.Add(pla);
                                    }

                                    Place = pla;
                                    break;
                                }
                            }
                            Earthquake earthquake = this.IEarthqueakeFactory.Create(UTCDateTime, Latitude, Longitude, Depth,
                                                                                    Magnitude, IsSensible, Place, Source, MagnitudeType);
                            earthquakes.Add(earthquake);
                        }
                    }
                }
                return(earthquakes);
            }
            catch (Exception ex)
            {
                ExceptionUtility.Error(ex, this.GetType());
                return(new List <Earthquake>());
            }
        }