/// <summary>
 ///
 /// </summary>
 /// <param name="c"></param>
 /// <returns>False means any of these have any error. Returns true means successful</returns>
 public bool EntryWholeCountryFromCounrtyTemp(CountryTemp c)
 {
     if (IsCountryExist(c.CountryName))
     {
         var country = GetCountry(c.CountryName);
         if (country != null)
         {
             if (!IsPlaceExist(c, country))
             {
                 var district  = AddDistrict(c, country);
                 var placeType = AddPlaceType(c);
                 var place     = AddPlace(c, country, placeType);
                 if (c.IsState)
                 {
                     var state         = AddState(c);
                     var stateCountry  = AddRelStateCountry(c, state, country);
                     var stateDistrict = AddRelStateDistrict(c, state, district);
                 }
                 if (c.IsErrorExist)
                 {
                     Console.WriteLine("\nError Exist on:");
                     DisplayCountrytemp.Print(c);
                 }
                 return(!c.IsErrorExist);
             }
         }
         //country exist
         return(true);
     }
     else
     {
         AddError(c, "Country not found.");
     }
     return(false);
 }
        public CountryStateDistrictRelation AddRelStateDistrict(CountryTemp c, CountrySate p1, CountryDistrict p2)
        {
            bool isFailed;

            if (p1 != null)
            {
                if (!IsStateDistrictExist(p1, p2))
                {
                    var o = new CountryStateDistrictRelation();

                    o.CountrySateID     = p1.CountrySateID;
                    o.CountryDistrictID = p2.CountryDistrictID;


                    db.CountryStateDistrictRelations.Add(o);
                    var failed = db.SaveChanges(o, "AddRelStateDistrict");
                    //var failed = db.SaveChanges();
                    isFailed = (failed == -1) ? true : false;
                    if (!isFailed)
                    {
                        return(o);
                    }
                    else
                    {
                        AddError(c, "StateDistrictRel");
                        return(null);
                    }
                }
            }
            return(null);
        }
Пример #3
0
        public CountryTemp ProcessDistrictState(CountryTemp c, HtmlNode tdForCountry)
        {
            string district = null, innerHTML = tdForCountry.InnerHtml;


            if (!string.IsNullOrEmpty(innerHTML) && IsCommaExist(innerHTML))
            {
                district            = innerHTML.Split(',').ToList()[1];
                c.IsStateOrDistrict = true;



                if (!string.IsNullOrEmpty(district))
                {
                    district = district.ToLower();
                    var indexOfBr = district.IndexOf("<br>");
                    if (indexOfBr > -1)
                    {
                        district = district.Substring(0, indexOfBr);
                    }
                    district = district.Trim();
                    if (district.IndexOf("district") > -1)
                    {
                        c.IsConfirmDistrict = true;
                    }
                    district   = EachWordUpperCase(district);
                    c.District = district;
                }

                //state process
                var smallNode = tdForCountry.Descendants("small").FirstOrDefault();
                if (smallNode != null)
                {
                    var smallNodeText = smallNode.InnerText;
                    if (!string.IsNullOrEmpty(smallNodeText))
                    {
                        c.State    = district;
                        c.IsState  = true;
                        c.District = EachWordUpperCase(smallNodeText.Trim());
                        var districtLowercase = c.District.ToLower();

                        if (districtLowercase.IndexOf("county") > -1)
                        {
                            c.IsCounty = true;
                        }

                        if (c.State.IndexOf("Division") > -1)
                        {
                            c.IsDivision = true;
                        }
                    }
                }
            }

            return(c);
        }
        //public void SaveErrorList() {
        //    try {
        //        readerWriter.WriteToJsonFile<List<CountryTemp>>(ErrorFileLocation, ErrorItemsList, true);
        //    } catch (Exception ex) {
        //        DevMVCComponent.Starter.HanldeError.HandleBy(ex, "SaveErrorList");
        //    }
        //}

        #endregion

        #region Error Maintain in Country temp
        public void AddError(CountryTemp c, string error)
        {
            if (c.ErrorsList == null)
            {
                c.ErrorsList = new List <string>(10);
            }
            c.ErrorsList.Add(error);
            c.IsErrorExist = true;
            AddErrorToErrorList(c);
        }
Пример #5
0
        public static void Print(CountryTemp c, int CountingIndex = -1)
        {
            if (CountingIndex > -1)
            {
                Console.WriteLine("[ " + CountingIndex + " ]Page No:" + c.PageNo + ", Item No:" + c.ItemNumber);
            }
            else
            {
                Console.WriteLine("Page No:" + c.PageNo + ", Item No:" + c.ItemNumber);
            }
            Console.WriteLine(" \t\r " + "Country : " + c.CountryName);
            if (c.IsState)
            {
                Console.WriteLine(" \t\r " + "State : " + c.State);
                Console.Write(" \t\r " + "District : " + c.District);

                if (c.IsCounty)
                {
                    Console.Write(" \t" + " : It's a county");
                }
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine(" \t\r " + "District : " + c.District);
            }
            Console.WriteLine(" \t\r " + "area : " + c.Area);
            Console.WriteLine(" \t\r " + "place(in blue) : " + c.Place);
            if (c.AlterNativePlaceNames != null)
            {
                Console.WriteLine(" \t\r " + "Alternative Names :");
                Console.Write(" \t\r\t  ");
                foreach (var alt in c.AlterNativePlaceNames)
                {
                    Console.Write(alt + " ");
                }
                Console.WriteLine("");
            }
            Console.WriteLine(" \t\r " + "type of place : " + c.PlaceType);
            Console.WriteLine(" \t\r " + "WikiLink : " + c.WikiLink);
            Console.WriteLine(" \t\r " + "X Lating : " + c.XLating + ", YLating: " + c.YLating + "\n");
            if (c.ErrorsList != null)
            {
                Console.WriteLine(" \t\r " + "Errors : ");
                foreach (var err in c.ErrorsList)
                {
                    Console.Write(" \t\r\t  " + err + " ; ");
                }
                Console.WriteLine("");
            }
        }
        /// <summary>
        /// Add items to the error list of CountryTemp
        /// </summary>
        /// <param name="c"></param>
        public void AddErrorToErrorList(CountryTemp c)
        {
            if (ErrorItemsList == null)
            {
                ErrorItemsList = new List <CountryTemp>(2000);
            }
            if (LastErrorCached != null && LastErrorCached.PageNo == c.PageNo && LastErrorCached.ItemNumber == c.ItemNumber)
            {
                return; //already added
            }

            if (!ErrorItemsList.Any(n => n.PageNo == c.PageNo && n.ItemNumber == c.ItemNumber))
            {
                //not exist in the error list.
                LastErrorCached = c;
                ErrorItemsList.Add(c);
            }
        }
        public CountryDistrict AddDistrict(CountryTemp c, Country country)
        {
            bool isFailed;

            if (IsStringNullOrWhite(c.District))
            {
                return(null);
            }
            if (c.IsStateOrDistrict)
            {
                if (!IsDistrictExist(c.District))
                {
                    var district = new CountryDistrict();

                    district.DistrictName      = c.District;
                    district.CountryID         = country.CountryID;
                    district.IsCounty          = c.IsCounty;
                    district.IsConfirmDistrict = c.IsConfirmDistrict;
                    db.CountryDistricts.Add(district);
                    var failed = db.SaveChanges(district, "AddDistrict");
                    //var failed = db.SaveChanges();
                    isFailed = (failed == -1) ? true : false;
                    if (isFailed)
                    {
                        AddError(c, "District");
                    }
                    return(district);
                }
                else
                {
                    var x = db.CountryDistricts.FirstOrDefault(n => n.DistrictName == c.District);
                    if (x == null)
                    {
                        isFailed = true;
                        AddError(c, "District");
                    }
                    return(x);
                }
            }

            return(null);
        }
        public CountryPlace AddPlace(CountryTemp c, Country country, CountryPlaceType cp)
        {
            bool isFailed;

            if (IsStringNullOrWhite(c.Place))
            {
                return(null);
            }
            if (!IsPlaceExist(c))
            {
                var o = new CountryPlace();
                o.Name      = c.Place;
                o.XLating   = c.XLating;
                o.YLating   = c.YLating;
                o.WikiLink  = c.WikiLink;
                o.CountryID = country.CountryID;
                o.Area      = c.Area;
                if (cp != null)
                {
                    o.CountryPlaceTypeID = cp.CountryPlaceTypeID;
                }
                db.CountryPlaces.Add(o);
                var failed = db.SaveChanges(o, "AddPlace");
                //var failed = db.SaveChanges();
                isFailed = (failed == -1) ? true : false;
                if (isFailed)
                {
                    AddError(c, "AddPlace");
                }
                return(o);
            }
            else
            {
                var x = GetPlace(c, country);
                if (x == null)
                {
                    isFailed = true;
                    AddError(c, "Place");
                }
                return(x);
            }
        }
 public void AddRelPlaceAlternatives(CountryTemp p1, CountryPlace p2, ref bool isFailed)
 {
     isFailed = false;
     if (p1.AlterNativePlaceNames != null)
     {
         foreach (var item in p1.AlterNativePlaceNames)
         {
             var o = new CountryPlaceAlternative();
             o.AlternativeName = item;
             o.CountryPlaceID  = p2.CountryPlaceID;
             var failed = db.SaveChanges(o, "AddRelPlaceAlternatives");
             //var failed = db.SaveChanges();
             isFailed = (failed == -1) ? true : false;
             if (isFailed)
             {
                 AddError(p1, "Add Alter Place name :" + item);
             }
         }
     }
 }
        public CountryPlaceType AddPlaceType(CountryTemp c)
        {
            bool isFailed;

            if (IsStringNullOrWhite(c.PlaceType))
            {
                return(null);
            }
            if (!IsPlaceTypeExist(c.PlaceType))
            {
                if (!string.IsNullOrWhiteSpace(c.PlaceType.Trim()))
                {
                    var o = new CountryPlaceType();

                    o.PlaceType = c.PlaceType;

                    db.CountryPlaceTypes.Add(o);
                    var failed = db.SaveChanges(o, "AddPlaceType");
                    //var failed = db.SaveChanges();
                    isFailed = (failed == -1) ? true : false;
                    if (isFailed)
                    {
                        AddError(c, "AddPlaceType");
                    }
                    return(o);
                }
                return(null);
            }
            else
            {
                var x = db.CountryPlaceTypes.FirstOrDefault(n => n.PlaceType == c.PlaceType);
                if (x == null)
                {
                    isFailed = true;
                    AddError(c, "PlaceType");
                }
                return(x);
            }
        }
        public CountrySate AddState(CountryTemp c)
        {
            bool isFailed;

            if (c.IsState)
            {
                if (IsStringNullOrWhite(c.State))
                {
                    return(null);
                }
                if (!IsStateExist(c.State))
                {
                    var state = new CountrySate();

                    state.StateName  = c.State;
                    state.IsDivision = c.IsDivision;
                    db.CountrySates.Add(state);
                    var failed = db.SaveChanges(state, "AddState");
                    //var failed = db.SaveChanges();
                    isFailed = (failed == -1) ? true : false;
                    if (isFailed)
                    {
                        AddError(c, "State");
                    }
                    return(state);
                }
            }
            var x = db.CountrySates.FirstOrDefault(n => n.StateName == c.State);

            if (x == null)
            {
                isFailed = true;
                AddError(c, "State");
            }
            return(x);
        }
Пример #12
0
        /// <summary>
        /// Parse HTML only from GEONames org not from any other location.
        /// </summary>
        /// <param name="url">Give url like this : http://www.geonames.org/search.html?q=YourQuery</param>
        /// <param name="skipPage">Number of pages should skip, skipPage= skipPage * 50, so if 0 no page skip.</param>
        /// <param name="skipItems">Number of places skips from that page, highest 50. number 0 means nothing to skip. 1 means skip 1</param>
        /// <param name="EndingItemLength">-1 means end after how many exist, or stop after getting only the given number</param>

        public List <CountryTemp> ParseHTML(string url, int skipPage = 0, int skipItems = 0, int EndingItemLength = -1, bool featureClassA = false, Country countryEntity = null)
        {
            HtmlWeb htmlWeb = new HtmlWeb()
            {
                AutoDetectEncoding = false, OverrideEncoding = Encoding.GetEncoding("iso-8859-2")
            };
            //HtmlDocument htmlDocument = htmlWeb.Load("http://www.geonames.org/search.html?q=&startRow=0");
            //HtmlDocument htmlDocument = htmlWeb.Load("http://www.geonames.org/search.html?q=bangladesh+dhaka+division&country=");
            int pageNo = skipPage;

            skipPage *= 50;
            url      += "&startRow=" + skipPage;
            HtmlDocument htmlDocument = htmlWeb.Load(url);
            HtmlNode     links        = null;
            int          length       = 0;
            var          tables       = htmlDocument.DocumentNode.Descendants("table").ToList();

            if (tables.Count >= 2)
            {
                if (featureClassA)
                {
                    links = tables[2];
                }
                else
                {
                    links = tables[1];
                }
            }
            if (links == null)
            {
                DevMVCComponent.Starter.Mailer.QuickSend("*****@*****.**", "table not found in table", "table not found in table");
                return(null);
            }
            var tableRows = links.ChildNodes.Where(x => x.Name == "tr").ToList();



            int startingNumber = 2 + skipItems;

            if (EndingItemLength > 0)
            {
                length = startingNumber + EndingItemLength;
            }
            else
            {
                length = tableRows.Count;
            }
            int itemsListRequired   = length - startingNumber - 1;
            List <CountryTemp> list = new List <CountryTemp>(itemsListRequired + 1);

            for (int i = startingNumber; i < length; i++)
            //for (int i =51; i < length; i++)
            {
                var tableData = tableRows[i].ChildNodes.Where(x => x.Name == "td").ToList();
                //Console.WriteLine((i - 1) + " : ");
                if (tableData.Count >= 4)
                {
                    string   country = "", place = "", typeOfPlace = "", area = "", geoMapLink = "";
                    float    startingLating = -1, endingLating = -1;
                    string   lastPageUrl = "";
                    string   alternativePlaceNames;
                    string[] alternativePlaceNamesList = new string[1];
                    string   wikiLink = "";

                    var aCountry = tableData[2].Descendants("a").FirstOrDefault();
                    if (aCountry != null)
                    {
                        country = aCountry.InnerText;
                    }
                    else
                    {
                        if (countryEntity != null)
                        {
                            country = countryEntity.CountryName;
                        }
                    }


                    area        = tableData[0].Descendants("a").FirstOrDefault().Attributes["href"].Value;
                    lastPageUrl = LastSlashFileOrPageName(area);
                    if (!string.IsNullOrEmpty(lastPageUrl))
                    {
                        lastPageUrl = lastPageUrl.Replace("google_", "").Replace("_", " ").Replace("-", " ").Replace(".html", "");
                    }
                    area        = lastPageUrl;
                    geoMapLink  = tableData[1].Descendants("a").FirstOrDefault().Attributes["href"].Value;
                    lastPageUrl = LastSlashFileOrPageName(geoMapLink);
                    if (!string.IsNullOrEmpty(lastPageUrl))
                    {
                        lastPageUrl = lastPageUrl.Replace("google_", "").Replace("_", " ").Replace(".html", "");
                        var lating = lastPageUrl.Split(' ');
                        if (lating.Length >= 2)
                        {
                            startingLating = float.Parse(lating[0]);
                            endingLating   = float.Parse(lating[1]);
                        }
                    }
                    typeOfPlace = tableData[3].InnerHtml;
                    if (!string.IsNullOrEmpty(typeOfPlace))
                    {
                        typeOfPlace = typeOfPlace.Replace("<br>", " ").Replace("<small>", "").Replace("</small>", "").Trim();
                    }
                    var placeColumnData = tableData[1];
                    var aLinksInPlace   = tableData[1].Descendants("a").ToList();
                    if (aLinksInPlace != null)
                    {
                        place = aLinksInPlace.FirstOrDefault().InnerText;
                        if (aLinksInPlace.Count > 1)
                        {
                            wikiLink = aLinksInPlace[1].GetAttributeValue("href", "");
                            wikiLink = LastSlashFileOrPageName(wikiLink);
                        }
                    }

                    var tempList = placeColumnData.Descendants("small").FirstOrDefault();
                    if (tempList != null)
                    {
                        alternativePlaceNames     = tempList.InnerText;
                        alternativePlaceNamesList = GetAlternativeNamesOfPlace(alternativePlaceNames);
                    }
                    else
                    {
                        alternativePlaceNamesList = null;
                    }

                    var c = new CountryTemp();
                    c.PageNo      = pageNo;
                    c.ItemNumber  = i - 1;
                    c.CountryName = country;

                    c.Area  = EachWordUpperCase(area);
                    c.Place = place;
                    c.AlterNativePlaceNames = alternativePlaceNamesList;
                    c.PlaceType             = typeOfPlace;
                    c.XLating  = startingLating;
                    c.YLating  = endingLating;
                    c.WikiLink = wikiLink;


                    ProcessDistrictState(c, tableData[2]);

                    list.Add(c);

                    //Console.WriteLine(" \t\r " + "Country : " + country);
                    //Console.WriteLine(" \t\r " + "District : " + district);
                    //Console.WriteLine(" \t\r " + "area : " + area);
                    //Console.WriteLine(" \t\r " + "place(in blue) : " + place);
                    //if (alternativePlaceNamesList != null)
                    //{
                    //    Console.WriteLine(" \t\r " + "Alternative Names :");
                    //    Console.Write(" \t\r\t  ");
                    //    foreach (var alt in alternativePlaceNamesList)
                    //    {
                    //        Console.Write(alt + " ");
                    //    }
                    //    Console.WriteLine("");

                    //}
                    //Console.WriteLine(" \t\r " + "type of place : " + typeOfPlace);
                    //Console.WriteLine(" \t\r " + "geomap starting : " + startingLating);
                    //Console.WriteLine(" \t\r " + "geomap ending : " + endingLating + "\n");
                    //Console.WriteLine(" \t\r " + "WikiLink : " + wikiLink + "\n");
                }
            }
            return(list);
        }
 public CountryPlace GetPlace(CountryTemp c, Country country)
 {
     return(db.CountryPlaces.FirstOrDefault(n => n.Name == c.Place && n.CountryID == country.CountryID && (c.XLating >= (n.XLating - 1) && c.XLating <= (n.XLating + 1)) && (c.YLating >= (n.YLating - 1) && c.YLating <= (n.YLating + 1))));
 }
 public bool IsPlaceExist(CountryTemp c, Country country)
 {
     return(db.CountryPlaces.Any(n => n.Name == c.Place && n.CountryID == country.CountryID && (c.XLating >= (n.XLating - 1) && c.XLating <= (n.XLating + 1)) && (c.YLating >= (n.YLating - 1) && c.YLating <= (n.YLating + 1))));
 }
 public bool IsPlaceExist(CountryTemp c)
 {
     return(db.CountryPlaces.Any(n => n.Name == c.Place && n.XLating == c.XLating && n.YLating == c.YLating));
 }