Beispiel #1
0
        public static RatingList ParseRfg(ref string text)
        {
            var result = new RatingList();

            DateTime actualDate = CoreConvert.EmptyDate;

            if (!string.IsNullOrEmpty(text))
            {
                string datePattern = "<h2 id=\"rating-name\">Текущий рейтинг лист.<br /> Рейтинг–лист по состоянию на ";
                int    d           = text.IndexOf(datePattern);
                if (d < 0)
                {
                    datePattern = "Рейтинг-лист по состоянию на ";
                    d           = text.IndexOf(datePattern);
                }
                if (d < 0)
                {
                    datePattern = "Рейтинг–лист по состоянию на ";
                    d           = text.IndexOf(datePattern);
                }

                if (d < 0)
                {
                    datePattern = @"<div id=""leftcol"">";
                    d           = text.IndexOf(datePattern);
                }

                /*
                 * if (d > 0)
                 * {
                 *  int dEnd = text.IndexOf(" года</h2>", d);
                 *  if (dEnd > d + datePattern.Length)
                 *  {
                 *      string date = text.Substring(d + datePattern.Length, dEnd - (d + datePattern.Length));
                 *      if (!string.IsNullOrEmpty(date))
                 *      {
                 *          string[] arr = date.Split();
                 *          if (arr.Length > 1)
                 *          {
                 *              string m = null;
                 *              switch (arr[1])
                 *              {
                 *                  case "января": m = "01"; break;
                 *                  case "февраля": m = "02"; break;
                 *                  case "марта": m = "03"; break;
                 *                  case "апреля": m = "04"; break;
                 *                  case "мая": m = "05"; break;
                 *                  case "июня": m = "06"; break;
                 *                  case "июля": m = "07"; break;
                 *                  case "августа": m = "08"; break;
                 *                  case "сентября": m = "09"; break;
                 *                  case "октября": m = "10"; break;
                 *                  case "ноября": m = "11"; break;
                 *                  case "декабря": m = "12"; break;
                 *              }
                 *
                 *              if (!string.IsNullOrEmpty(m))
                 *                  result.Date = arr[0] + "." + m + "." + arr[2];
                 *          }
                 *      }
                 *  }
                 * }
                 */
                int p = text.IndexOf("<table", d);
                if (p >= 0)
                {
                    int place = 1;
                    int p2    = text.IndexOf("<td>1</td>", p);
                    int pEnd  = text.IndexOf("</tbody>", p2);

                    do
                    {
                        if (p2 > p)
                        {
                            int p2End = text.IndexOf("</tr>", p2);

                            if (p2End > p2)
                            {
                                RtItem rec = new RtItem();
                                for (int i = 0; i < 6; i++)
                                {
                                    string s;
                                    int    val;
                                    int    p3 = text.IndexOf("<td", p2);
                                    if (p3 > -1)
                                    {
                                        int p3End = text.IndexOf(">", p3);
                                        int p4    = text.IndexOf(@"</td>", p3End);
                                        if (p4 > p3End)
                                        {
                                            switch (i)
                                            {
                                            case 0:     //Place
                                                rec.Place = place++;
                                                break;

                                            case 1:     //Name
                                                int p5    = text.IndexOf("<a", p3End);
                                                int p5a   = text.IndexOf(">", p5);
                                                int p5End = text.IndexOf("</a>", p5);
                                                s = text.Substring(p5a + 1, p5End - p5a - 1);
                                                int j = s.IndexOf(' ');
                                                if (j > -1)
                                                {
                                                    rec.LastName  = s.Substring(0, j);
                                                    rec.FirstName = s.Substring(j + 1);
                                                }
                                                else
                                                {
                                                    rec.LastName = s;
                                                }
                                                break;

                                            case 2:     //City
                                                s   = text.Substring(p3End + 1, p4 - p3End - 1);
                                                val = 0;
                                                //Avoid mistakes in the table
                                                if (int.TryParse(s, out val))
                                                {
                                                    rec.Rating = val;
                                                }
                                                else
                                                {
                                                    rec.City = s;
                                                }
                                                break;

                                            case 3:     //Rating
                                                if (rec.Rating == 0)
                                                {
                                                    s   = text.Substring(p3End + 1, p4 - p3End - 1);
                                                    val = 0;
                                                    if (int.TryParse(s, out val))
                                                    {
                                                        rec.Rating = val;
                                                    }
                                                }
                                                break;

                                            case 4:     //Diff
                                                break;

                                            case 5:     //Date
                                                s = text.Substring(p3End + 1, p4 - p3End - 1);
                                                var date = CoreConvert.ToDate(s);
                                                rec.Date = CoreConvert.ToDateString(date);

                                                if (DateTime.Compare(date, actualDate) > 0)
                                                {
                                                    actualDate = date;
                                                }
                                                break;
                                            }
                                            p2 = p4 + 1;
                                        }
                                        else
                                        {
                                            break;
                                        }
                                    }
                                }
                                result.Items.Add(rec);
                            }
                            else
                            {
                                break;
                            }

                            p2 = text.IndexOf("<tr>", p2);
                        }
                        else
                        {
                            break;
                        }
                    } while (p2 < pEnd || p2 == -1);
                }
            }

            result.Date = CoreConvert.ToDateString(actualDate);

            return(result);
        }
Beispiel #2
0
        public static RatingList Parse(ref string text)
        {
            var result = new RatingList();


            if (!string.IsNullOrEmpty(text))
            {
                //int p = text.IndexOf("<tbody>");
                int p = text.IndexOf("<table");
                if (p >= 0)
                {
                    int place = 1;
                    //int pEnd = text.IndexOf("</tbody>", p);
                    int pEnd = text.IndexOf("</table>", p);
                    int p2   = text.IndexOf("<tr>", p);

                    do
                    {
                        if (p2 > p)
                        {
                            int p2End = text.IndexOf("</tr>", p2);

                            bool isRankFirst = false;
                            if (p2End > p2)
                            {
                                RtItem rec = new RtItem();
                                for (int i = 0; i < 7; i++)
                                {
                                    string s;
                                    int    p3 = text.IndexOf("<td", p2);
                                    if ((p3 > -1) && (p3 < p2End))
                                    {
                                        int p3End = text.IndexOf(">", p3);
                                        int p4    = text.IndexOf(@"</td>", p3End);
                                        if (p4 > p3End)
                                        {
                                            switch (i)
                                            {
                                            case 0:     //Place
                                                rec.Place = place++;
                                                break;

                                            case 1:     //Name
                                                s = StripHTML(text.Substring(p3End + 1, p4 - p3End - 1));
                                                int j = s.IndexOf(' ');
                                                if (j > -1)
                                                {
                                                    rec.LastName  = s.Substring(0, j).Trim();
                                                    rec.FirstName = s.Substring(j + 1).Trim();
                                                    if (rec.LastName.LastIndexOf(',') == rec.LastName.Length - 1)
                                                    {
                                                        rec.LastName = rec.LastName.Remove(rec.LastName.Length - 1);
                                                    }
                                                }
                                                else
                                                {
                                                    rec.LastName = s;
                                                }
                                                break;

                                            case 2:     //City
                                                rec.City = StripHTML(text.Substring(p3End + 1, p4 - p3End - 1));
                                                if (string.IsNullOrWhiteSpace(rec.City))
                                                {
                                                    rec.City = string.Empty;
                                                }
                                                break;

                                            case 3:     //Rating
                                                s = text.Substring(p3End + 1, p4 - p3End - 1);
                                                int val = 0;
                                                if (int.TryParse(s, out val))
                                                {
                                                    rec.Rating = val;
                                                }
                                                else
                                                {
                                                    rec.Rank    = StripHTML(s);
                                                    isRankFirst = true;
                                                }
                                                break;

                                            case 4:     //Rank
                                                s = text.Substring(p3End + 1, p4 - p3End - 1);
                                                if (isRankFirst)
                                                {
                                                    if (int.TryParse(s, out val))
                                                    {
                                                        rec.Rating = val;
                                                    }
                                                }
                                                else
                                                {
                                                    rec.Rank = StripHTML(s);
                                                }

                                                if (string.IsNullOrWhiteSpace(rec.Rank))
                                                {
                                                    rec.Rank = string.Empty;
                                                }
                                                break;

                                            case 5:     //Grade
                                                rec.Grade = StripHTML(text.Substring(p3End + 1, p4 - p3End - 1));
                                                if (string.IsNullOrWhiteSpace(rec.Grade))
                                                {
                                                    rec.Grade = string.Empty;
                                                }
                                                break;

                                            case 6:     //Comment
                                                rec.Comment = StripHTML(text.Substring(p3End + 1, p4 - p3End - 1));
                                                if (string.IsNullOrWhiteSpace(rec.Comment))
                                                {
                                                    rec.Comment = string.Empty;
                                                }
                                                break;
                                            }
                                            p2 = p4 + 1;
                                        }
                                        else
                                        {
                                            break;
                                        }
                                    }
                                }

                                string corrected;

                                if (RepairApostrof(rec.City, out corrected))
                                {
                                    rec.City = corrected;
                                }

                                if (RepairApostrof(rec.FirstName, out corrected))
                                {
                                    rec.FirstName = corrected;
                                }

                                if (RepairApostrof(rec.LastName, out corrected))
                                {
                                    rec.LastName = corrected;
                                }

                                if (RepairApostrof(rec.Rank, out corrected))
                                {
                                    rec.Rank = corrected;
                                }

                                if (RepairApostrof(rec.Grade, out corrected))
                                {
                                    rec.Grade = corrected;
                                }

                                if (RepairApostrof(rec.Comment, out corrected))
                                {
                                    rec.Comment = corrected;
                                }

                                result.Items.Add(rec);
                            }
                            else
                            {
                                break;
                            }

                            p2 = text.IndexOf("<tr>", p2);
                        }
                        else
                        {
                            break;
                        }
                    } while (p2 < pEnd || p2 == -1);
                }
            }

            return(result);
        }