private WeatherFeed ToWeatherFeed(XElement channelNode)
        {
            System.Globalization.CultureInfo convCulture = new System.Globalization.CultureInfo("en-US");

            if (channelNode != null)
            {
                RSS.Feed defaultFeed = RSS.ImportExport.XML.ToFeed(channelNode);
                if (defaultFeed != null)
                {
                    defaultFeed.Items.Clear();
                    WeatherFeed feed = new WeatherFeed();
                    feed.CopyValues(defaultFeed);

                    foreach (XElement prpNode in channelNode.Elements())
                    {
                        if (prpNode.Name.NamespaceName.ToLower() == "yweather")
                        {
                            switch (prpNode.Name.LocalName.ToLower())
                            {
                            case "location":
                                feed.Location = new LocationInfo();
                                foreach (XAttribute att in prpNode.Attributes())
                                {
                                    switch (att.Name.LocalName)
                                    {
                                    case "city":
                                        feed.Location.City = att.Value;
                                        break;

                                    case "region":
                                        feed.Location.Region = att.Value;
                                        break;

                                    case "country":
                                        feed.Location.Country = att.Value;
                                        break;
                                    }
                                }

                                break;

                            case "units":
                                foreach (XAttribute att in prpNode.Attributes())
                                {
                                    switch (att.Name.LocalName)
                                    {
                                    case "temperature":
                                        if (att.Value.ToLower() == "f")
                                        {
                                            feed.TemperatureUnit = DegreesUnit.Fahrenheit;
                                        }
                                        else
                                        {
                                            feed.TemperatureUnit = DegreesUnit.Celsius;
                                        }
                                        break;

                                    case "distance":
                                        if (att.Value.ToLower() == "mi")
                                        {
                                            feed.DistanceUnit = DistanceUnit.Miles;
                                        }
                                        else
                                        {
                                            feed.DistanceUnit = DistanceUnit.Kilometer;
                                        }
                                        break;

                                    case "pressure":
                                        if (att.Value.ToLower() == "in")
                                        {
                                            feed.PressureUnit = PressureUnit.PoundsPerSquareInch;
                                        }
                                        else
                                        {
                                            feed.PressureUnit = PressureUnit.Milibars;
                                        }
                                        break;

                                    case "speed":
                                        if (att.Value.ToLower() == "mph")
                                        {
                                            feed.SpeedUnit = DistanceUnit.Miles;
                                        }
                                        else
                                        {
                                            feed.SpeedUnit = DistanceUnit.Kilometer;
                                        }
                                        break;
                                    }
                                }

                                break;

                            case "wind":
                                feed.Wind = new WindInfo();
                                foreach (XAttribute att in prpNode.Attributes())
                                {
                                    switch (att.Name.LocalName)
                                    {
                                    case "chill":
                                        int n;
                                        if (int.TryParse(att.Value, out n))
                                        {
                                            feed.Wind.Chill = n;
                                        }
                                        break;

                                    case "direction":
                                        if (int.TryParse(att.Value, out n))
                                        {
                                            feed.Wind.Direction = n;
                                        }
                                        break;

                                    case "speed":
                                        double t;
                                        if (double.TryParse(att.Value, System.Globalization.NumberStyles.Any, convCulture, out t))
                                        {
                                            feed.Wind.Speed = t;
                                        }
                                        break;
                                    }
                                }

                                break;

                            case "atmosphere":
                                feed.Atmosphere = new AtmosphereInfo();
                                foreach (XAttribute att in prpNode.Attributes())
                                {
                                    switch (att.Name.LocalName)
                                    {
                                    case "humidity":
                                        int n;
                                        if (int.TryParse(att.Value, out n))
                                        {
                                            feed.Atmosphere.HumidityInPercent = n;
                                        }
                                        break;

                                    case "visibility":
                                        double t;
                                        if (double.TryParse(att.Value, System.Globalization.NumberStyles.Any, convCulture, out t))
                                        {
                                            feed.Atmosphere.VisibilityDistance = t;
                                        }
                                        break;

                                    case "pressure":
                                        if (double.TryParse(att.Value, System.Globalization.NumberStyles.Any, convCulture, out t))
                                        {
                                            feed.Atmosphere.Pressure = t;
                                        }
                                        break;

                                    case "rising":
                                        if (att.Value == "0")
                                        {
                                            feed.Atmosphere.StateOfBarometricPressure = PressureState.Steady;
                                        }
                                        else if (att.Value == "1")
                                        {
                                            feed.Atmosphere.StateOfBarometricPressure = PressureState.Rising;
                                        }
                                        else if (att.Value == "2")
                                        {
                                            feed.Atmosphere.StateOfBarometricPressure = PressureState.Falling;
                                        }
                                        break;
                                    }
                                }

                                break;

                            case "astronomy":
                                feed.Astronomy = new AstronomyInfo();
                                foreach (XAttribute att in prpNode.Attributes())
                                {
                                    switch (att.Name.LocalName)
                                    {
                                    case "sunrise":
                                        DateTime d;
                                        if (System.DateTime.TryParse(att.Value, convCulture, System.Globalization.DateTimeStyles.AssumeUniversal, out d))
                                        {
                                            feed.Astronomy.Sunrise = d;
                                        }
                                        break;

                                    case "sunset":
                                        if (System.DateTime.TryParse(att.Value, convCulture, System.Globalization.DateTimeStyles.AssumeUniversal, out d))
                                        {
                                            feed.Astronomy.Sunset = d;
                                        }
                                        break;
                                    }
                                }

                                break;
                            }
                        }
                        else if (prpNode.Name.LocalName.ToLower() == "item")
                        {
                            RSS.FeedItem defaultItem = RSS.ImportExport.XML.ToFeedItem(prpNode);
                            if (defaultItem != null)
                            {
                                WeatherFeedItem newItem = new WeatherFeedItem();
                                newItem.CopyValues(defaultItem);

                                foreach (XElement itemNode in prpNode.Elements())
                                {
                                    if (itemNode.Name.NamespaceName == "yweather")
                                    {
                                        switch (itemNode.Name.LocalName.ToLower())
                                        {
                                        case "condition":
                                            newItem.ActualCondition = new ActualCondition();
                                            foreach (XAttribute att in itemNode.Attributes())
                                            {
                                                switch (att.Name.LocalName.ToLower())
                                                {
                                                case "text":
                                                    newItem.ActualCondition.Description = att.Value;
                                                    break;

                                                case "code":
                                                    int i = 0;
                                                    if (int.TryParse(att.Value, out i) && (i <= Convert.ToInt32(ConditionType.Isolated_Thundershowers) | i == Convert.ToInt32(ConditionType.Not_Available)))
                                                    {
                                                        newItem.ActualCondition.Type = (ConditionType)i;
                                                    }
                                                    break;

                                                case "temp":
                                                    int n;
                                                    if (int.TryParse(att.Value, out n))
                                                    {
                                                        newItem.ActualCondition.Temperature = n;
                                                    }
                                                    break;

                                                case "date":
                                                    //Wed, 17 Aug 2011 10:18 pm CEST

                                                    string dateStr = att.Value;
                                                    int    index   = Math.Max(dateStr.LastIndexOf("am"), dateStr.LastIndexOf("pm"));
                                                    if (index > 0)
                                                    {
                                                        System.DateTime d = default(System.DateTime);
                                                        if (System.DateTime.TryParseExact(att.Value.Substring(0, index + 2), "ddd, dd MMM yyyy hh:mm tt", convCulture, System.Globalization.DateTimeStyles.None, out d))
                                                        {
                                                            newItem.ActualCondition.ForecastDate = d;
                                                        }
                                                    }
                                                    break;
                                                }
                                            }

                                            break;

                                        case "forecast":
                                            Forecast cond = new Forecast();
                                            foreach (XAttribute att in itemNode.Attributes())
                                            {
                                                switch (att.Name.LocalName.ToLower())
                                                {
                                                case "text":
                                                    cond.Description = att.Value;
                                                    break;

                                                case "code":
                                                    int i = 0;
                                                    if (int.TryParse(att.Value, out i) && (i <= Convert.ToInt32(ConditionType.Isolated_Thundershowers) | i == Convert.ToInt32(ConditionType.Not_Available)))
                                                    {
                                                        cond.Type = (ConditionType)i;
                                                    }
                                                    break;

                                                case "day":
                                                    switch (att.Value.ToLower())
                                                    {
                                                    case "mon":
                                                        cond.Day = DayOfWeek.Monday;
                                                        break;

                                                    case "tue":
                                                        cond.Day = DayOfWeek.Tuesday;
                                                        break;

                                                    case "wed":
                                                        cond.Day = DayOfWeek.Wednesday;
                                                        break;

                                                    case "thu":
                                                        cond.Day = DayOfWeek.Thursday;
                                                        break;

                                                    case "fri":
                                                        cond.Day = DayOfWeek.Friday;
                                                        break;

                                                    case "sat":
                                                        cond.Day = DayOfWeek.Saturday;
                                                        break;

                                                    case "sun":
                                                        cond.Day = DayOfWeek.Sunday;
                                                        break;
                                                    }
                                                    break;

                                                case "date":
                                                    DateTime d;
                                                    if (System.DateTime.TryParse(att.Value, convCulture, System.Globalization.DateTimeStyles.AssumeUniversal, out d))
                                                    {
                                                        cond.ForecastDate = d;
                                                    }
                                                    break;

                                                case "low":
                                                    int n;
                                                    if (int.TryParse(att.Value, out n))
                                                    {
                                                        cond.LowTemperature = n;
                                                    }
                                                    break;

                                                case "high":
                                                    if (int.TryParse(att.Value, out n))
                                                    {
                                                        cond.HighTemperature = n;
                                                    }
                                                    break;
                                                }
                                            }

                                            newItem.ForecastConditions.Add(cond);
                                            break;
                                        }
                                    }
                                    else if (itemNode.Name.NamespaceName == "geo")
                                    {
                                        switch (itemNode.Name.LocalName.ToLower())
                                        {
                                        case "lat":
                                            double d = 0;
                                            if (double.TryParse(itemNode.Value, System.Globalization.NumberStyles.Any, convCulture, out d))
                                            {
                                                newItem.Coordinates = new YahooManaged.Geo.Coordinates(newItem.Coordinates.Longitude, d);
                                            }
                                            break;

                                        case ("long"):
                                            if (double.TryParse(itemNode.Value, System.Globalization.NumberStyles.Any, convCulture, out d))
                                            {
                                                newItem.Coordinates = new YahooManaged.Geo.Coordinates(d, newItem.Coordinates.Latitude);
                                            }
                                            break;
                                        }
                                    }
                                }
                                feed.Items.Add(newItem);
                            }
                        }
                    }
                    return(feed);
                }
            }
            return(null);
        }
Esempio n. 2
0
        public static void GetRSSDeals(int PercentageLimitation)
        {
            DateTime starttime = DateTime.Now;
                DataSet ds = GetRtDealsDataSet();
                Similarity s=new Similarity();
                string strHtml="";
                int TotalInsert = 0;
                int TotalFailed = 0;
                int TotalDuplicate = 0;
                int TotalSimilar = 0;
                int TotalExsitDuplicate = 0;
                int TotalExsitSameSource = 0;
                int k = 0;  //For SubCount
                string[] SubSourceName=new string[ds.Tables["sourcerssseed"].Rows.Count];
                int[] SubInsert = new int[ds.Tables["sourcerssseed"].Rows.Count];
                int[] SubDuplicate = new int[ds.Tables["sourcerssseed"].Rows.Count];
                int[] SubFailed = new int[ds.Tables["sourcerssseed"].Rows.Count];
                string SimilarDuplicateResult="";
                string SourceFailedResult = "";
                if (ds != null)
                {
                     foreach (DataRow dr in ds.Tables["sourcerssseed"].Rows)
                     {

                             int id = Convert.ToInt16(dr["SourceID"]);

                             string Additional = dr["Additional"].ToString();
                             string[] AddiURL = Additional.Split(',');
                             SubSourceName[k]=id.ToString();

                             for (int i = 0; i < AddiURL.Length; i++)
                             {
                                 try
                                 {
                                     if (AddiURL[i] == "")
                                         continue;

                                     string[] temp = AddiURL[i].Split('*');
                                     string RssType = temp[0];
                                     string RssUrl = temp[1];

                                     List<RssDealsModel> lrd = new List<RssDealsModel>();
                                     RSS.Feed feed = new RSS.Feed(RssUrl, DateTime.Parse(System.DateTime.Now.AddDays(-3).ToShortDateString()));
                                     feed.Read();
                                     for (int j = 0; j < feed.Channel.Items.Count; j++)
                                     {

                                         strHtml += "  <a href=" + feed.Channel.Items[j].link + " target=_blank><B>" + feed.Channel.Items[j].title + "</B></a><br>";
                                         strHtml += "  <font color=red>" + feed.Channel.Items[j].pubDate + "</font><br>";
                                         strHtml += "  " + feed.Channel.Items[j].description + "<br>";
                                         // if (strHtml.Length >= 8000)
                                         // strHtml = strHtml.Substring(0, 7999);
                                         strHtml = "";

                                         int isAppliances=0;
                                         int isFinance = 0;
                                         int isTravel = 0;
                                         int isDrug = 0;
                                         int isElectronic = 0;
                                         int isBeauty = 0;
                                         int isOfficeSupplies = 0;
                                         int isRestaurant = 0;
                                         int isJewelry = 0;
                                         int isOthers = 0;
                                         int isAppeal = 0;
                                         int isHot = 0;

                                         if (RssType.ToLower().Contains("hot"))
                                             isHot = 1;
                                         //else if (RssType.ToLower().Contains("finance"))
                                         //    isFinance = 1;
                                         //else if (RssType.ToLower().Contains("travel"))
                                         //    isTravel = 1;
                                         //else if (RssType.ToLower().Contains("drug"))
                                         //    isDrug = 1;
                                         //else if (RssType.ToLower().Contains("electronic"))
                                         //    isElectronic = 1;

                                         switch (GetDealsCatetogry(feed.Channel.Items[j].title,ds))
                                         {
                                             case "Appeal":
                                                 isAppeal = 1;
                                                 break;
                                             case "Appliances":
                                                 isAppliances = 1;
                                                 break;
                                             case "Beauty":
                                                 isBeauty = 1;
                                                 break;
                                             case "Drug":
                                                 isBeauty = 1;
                                                 break;
                                             case "Electronics":
                                                 isElectronic = 1;
                                                 break;
                                             case "Finance":
                                                 isFinance = 1;
                                                 break;
                                             case "Jewelry & Watches":
                                                 isJewelry = 1;
                                                 break;
                                             case "Office Supplies":
                                                 isOfficeSupplies = 1;
                                                 break;
                                             case "Restaurant & Food":
                                                 isRestaurant = 1;
                                                 break;
                                             case "Travels":
                                                 isTravel = 1;
                                                 break;

                                             default:
                                                 isOthers = 1;
                                                 break;

                                         }

                                         string OriginalLink = feed.Channel.Items[j].link; // Keep as Unique Parameter

                                         if (ds.Tables["rssdeals"].Rows.Count == 0)
                                         {
                                             string ss = InsertRssDeals(id,feed.Channel.Items[j].title, feed.Channel.Items[j].link, 0, OriginalLink, strHtml, feed.Channel.Items[j].pubDate,isAppliances,isFinance,isTravel,isDrug,isElectronic,isBeauty,isOfficeSupplies,isRestaurant,isJewelry,isOthers,isAppeal,isHot);

                                         }

                                         int count = 0;
                                         bool isSimilar = false;
                                         bool isExist = false;
                                         bool isDuplicateExsit = false;
                                         bool isExsitSameSource = false;
                                         string Title = "";
                                         string SourceID="";
                                         string DealsID="";
                                         string tempDuplicate = "";
                                         //string UniqueParameter="";
                                         int Percentage = 0;

                                        // if (feed.Channel.Items[j].title.Contains("Direct"))
                                           //  Percentage = 0;
                                          isExsitSameSource = isRssTitleExsit(feed.Channel.Items[j].title);
                                          if (!isExsitSameSource)
                                              isExist = isRssDealExist(feed.Channel.Items[j].link);
                                         if(!isExist)
                                             isDuplicateExsit = isRssDealDuplicateExist(feed.Channel.Items[j].link); ;

                                          if (!isExist && !isDuplicateExsit && !isExsitSameSource) //Skip Compare if already exsit
                                          {
                                              ds = GetRtDealsDataSet();
                                              foreach (DataRow drr in ds.Tables["rssdeals"].Rows)
                                              {

                                                  Title = drr["Title"].ToString();
                                                  SourceID = drr["SourceID"].ToString();
                                                  DealsID = drr["dealsID"].ToString();
                                                  count++;
                                                  if (SubSourceName[k] == SourceID)
                                                      continue; //Skipped Compare with its' Source

                                                  Percentage = Convert.ToInt16(s.sim(feed.Channel.Items[j].title, Title) * 100);    //get_semblance_By_2words(feed.Channel.Items[j].title, Title);
                                                  if (Percentage >= PercentageLimitation)
                                                  {
                                                      isSimilar = true;
                                                      break;  //Stop Comparation
                                                  }
                                                  else
                                                  {
                                                      isSimilar = false;
                                                  }
                                                  //  }
                                              }
                                          }

                                          if (isSimilar || isExist || isDuplicateExsit || isExsitSameSource)
                                         {
                                             if (isExist)
                                             {
                                                 Console.ForegroundColor = ConsoleColor.DarkCyan;
                                                 SimilarDuplicateResult = string.Format(feed.Channel.Items[j].title + " from {1} Found Duplicate Record At {0}", DateTime.Now.ToString(),SubSourceName[k]);
                                             }
                                             else if(isSimilar)
                                             {
                                                 TotalSimilar++;
                                                 Console.ForegroundColor = ConsoleColor.Cyan;
                                                 SimilarDuplicateResult = string.Format(feed.Channel.Items[j].title + " from {2} Found Duplicate Record {3}% At {0}-{1}: {4}", SourceID, DealsID, SubSourceName[k], Percentage, DateTime.Now.ToString());
                                                 tempDuplicate = InsertRssDealsDuplicate(id,feed.Channel.Items[j].title,feed.Channel.Items[j].link,0,OriginalLink, strHtml, feed.Channel.Items[j].pubDate,isAppliances,isFinance,isTravel,isDrug,isElectronic,isBeauty,isOfficeSupplies,isRestaurant,isJewelry,isOthers,isAppeal,isHot);
                                             }
                                             else if (isDuplicateExsit)
                                             {
                                                 TotalExsitDuplicate++;
                                                 Console.ForegroundColor = ConsoleColor.Magenta;
                                                 SimilarDuplicateResult = string.Format(feed.Channel.Items[j].title + " from {1} Found Duplicate Record  At {0}", DateTime.Now.ToString(),SubSourceName[k]);

                                             }
                                             else if (isExsitSameSource)
                                             {
                                                 TotalExsitSameSource++;
                                                 Console.ForegroundColor = ConsoleColor.Yellow;
                                                 SimilarDuplicateResult = string.Format(feed.Channel.Items[j].title + " From {0} found Duplicate Record at {1}", DateTime.Now.ToString(), SubSourceName[k]);

                                             }
                                             TotalDuplicate++;
                                             SubDuplicate[k]++;

                                             Console.WriteLine(SimilarDuplicateResult);
                                             continue;  //Fetch next Node

                                         }
                                         // if (id == 1)
                                           //   id = 1;
                                          string tempURLReasult = AlterDealURL(feed.Channel.Items[j].link, id, ds); // Check the Alter URL
                                          if (tempURLReasult != "")
                                              feed.Channel.Items[j].link = tempURLReasult + feed.Channel.Items[j].link;

                                          if (feed.Channel.Items[j].title.ToLower().Contains("’s"))
                                              feed.Channel.Items[j].title = feed.Channel.Items[j].title.Replace("’s", "");

                                          string result = InsertRssDeals(id,feed.Channel.Items[j].title,feed.Channel.Items[j].link,0,OriginalLink,strHtml,feed.Channel.Items[j].pubDate,isAppliances,isFinance,isTravel,isDrug,isElectronic,isBeauty,isOfficeSupplies,isRestaurant,isJewelry,isOthers,isAppeal,isHot);
                                          string resultHistory = InsertRssDealsHistory(id,feed.Channel.Items[j].title,feed.Channel.Items[j].link,0,OriginalLink, strHtml,feed.Channel.Items[j].pubDate,isAppliances,isFinance,isTravel,isDrug,isElectronic,isBeauty,isOfficeSupplies,isRestaurant,isJewelry,isOthers,isAppeal,isHot);

                                                 if (result == "")
                                                 {
                                                     TotalInsert++;
                                                     SubInsert[k]++;
                                                     Console.ForegroundColor = ConsoleColor.White;
                                                     Console.WriteLine(feed.Channel.Items[j].title + " Inserted Successfully! from "+ SubSourceName[k] +" At " + DateTime.Now.ToShortTimeString());
                                                 }
                                                 else
                                                 {
                                                     TotalFailed++;
                                                     SubFailed[k]++;
                                                     SendEmail.SendDealsEmail("*****@*****.**", "*****@*****.**", "Scaner Error @ " + DateTime.Now.ToString(), result);
                                                     Console.ForegroundColor = ConsoleColor.Red;
                                                     Console.WriteLine(result + " At " + DateTime.Now.ToShortTimeString());

                                                 }

                                                 //Thread.Sleep(100);

                                     }
                                 }
                                 catch (Exception ex)
                                 {

                                     string ss = ex.Message;
                                     SourceFailedResult += string.Format("{0} {1} At {2}*", SubSourceName[k], ss, DateTime.Now.ToString());
                                     SendEmail.SendDealsEmail("*****@*****.**", "*****@*****.**", "Scaner Error @ " + DateTime.Now.ToString(), ex.Message);
                                     //continue;
                                 }

                             }

                             k++;
                     }

                }
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine(string.Format("Total Inserted: {0}, Total Duplicate: {1}, Total Failed: {2}", TotalInsert, TotalDuplicate, TotalFailed));
                for (int m = 0; m < ds.Tables["sourcerssseed"].Rows.Count; m++)
                {
                    Console.WriteLine(string.Format("{0} Inserted: {1}, Duplicate: {2}, Failed: {3}", SubSourceName[m], SubInsert[m], SubDuplicate[m], SubFailed[m]));
                }
               // string[] SourceErrors=Regex.Split(SourceFailedResult,"***");
             //   for (int p = 0; p < SourceErrors.Length; p++)
              //  {
              //      Console.ForegroundColor = ConsoleColor.White;
              //      Console.WriteLine(SourceErrors[p]);
             //   }
            Console.WriteLine("Total similar deals found : " + TotalSimilar.ToString());
            Console.WriteLine("Total Exsit Duplicate deals found : " + TotalExsitDuplicate.ToString());
            Console.WriteLine("Total Exsit Duplicate in same source found : " + TotalExsitSameSource.ToString());
            Console.WriteLine("Scan Time is : " + (DateTime.Now - starttime).Minutes + " Minutes " + (DateTime.Now - starttime).Seconds + " Seconds ");
        }