Advertisement
MarcelloGrechi

UNICAMP Crawler

Jul 27th, 2012
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.29 KB | None | 0 0
  1. public static void ParseSpecialCourses (string htmlPage, ref List<Course> courses,ref List<Subject> allSubjects, UIComponentsRef uiComps)
  2.         {
  3. Dictionary<String, int>   courseIndex;
  4.             Dictionary<String,String> htmlPages;
  5.             List<Subject>      parsedSubjects;
  6.             WebRequests        webReq           = new WebRequests();
  7.             int                semesterCounter  = 0;
  8.             string             auxHtml;
  9.             string             htmlPiece =  String.Empty;
  10.  
  11.             // Creating Html Map
  12.             courseIndex    = new Dictionary<String,int>();
  13.             htmlPages      = new Dictionary<String,String> ();
  14.             auxHtml        = htmlPage;
  15.  
  16.             // Creating Regular Expression to find name of courses
  17.             Regex regex = new Regex ("<p><br><b><font face=\"Arial,Helvetica\"><font color=\"#000099\"><font size=-1>(.+?)</font></font></font></b>", RegexOptions.Singleline);
  18.  
  19.             // Splitting HTML Page in Fragments of courses
  20.             int startIndex = 0;
  21.             while (regex.IsMatch (auxHtml, startIndex))
  22.             {
  23.                 try
  24.                 {
  25.                     // Checking name of the course and saving it's offset
  26.                     int index         = regex.Match (auxHtml, startIndex).Groups[1].Index;
  27.                     string courseName = regex.Match (auxHtml, startIndex).Groups[1].Value;
  28.  
  29.                     // Adding name of the course and offset to dictionary
  30.                     courseIndex.Add (courseName, index);
  31.                     startIndex = index;
  32.  
  33.                     // Splitting HTML Page
  34.                     if (regex.IsMatch (auxHtml, startIndex))
  35.                     {
  36.                         int endIndex = regex.Match (auxHtml, startIndex).Groups[1].Index;
  37.                         endIndex     = endIndex - startIndex;
  38.                         htmlPiece    = auxHtml.Substring (startIndex, endIndex);
  39.                     }
  40.                     else
  41.                     {
  42.                         htmlPiece = auxHtml.Substring (startIndex);
  43.                     }
  44.  
  45.                     htmlPages.Add (courseName, htmlPiece);
  46.                 }
  47.                 catch (Exception ex)
  48.                 {
  49.                     Logs.LogWriter.LogError (ex);
  50.                 }
  51.             }
  52.  
  53.             // Reading Html Fragments and Parsing Subjects
  54.             foreach (KeyValuePair<String, String> kv in htmlPages)
  55.             {
  56.                 // Loading html map based on current html fragment
  57.                 parsedSubjects       = new List<Subject> ();
  58.                 semesterCounter      = 0;
  59.                 HtmlDocument htmlMap = new HtmlDocument ();
  60.                 htmlMap.LoadHtml (kv.Value);
  61.  
  62.                 // Reaching tables with subjects
  63.                 HtmlNodeCollection semesterNodes = htmlMap.DocumentNode.SelectNodes ("//table[@bgcolor='#D7EBFF']");
  64.  
  65.                 // Iterating over tables with subjects
  66.                 foreach (HtmlNode smNode in semesterNodes)
  67.                 {
  68.                     try
  69.                     {
  70.                         // Incrementing counter of semesters
  71.                         semesterCounter++;
  72.  
  73.                         // Reaching subject nodes
  74.                         HtmlNodeCollection subjectNodes = smNode.SelectNodes (".//font[@size='-1']//a");
  75.  
  76.                         // Checking for subject with only "ELET." subject. This ignores these cases
  77.                         if (subjectNodes == null)
  78.                         {
  79.                             continue;
  80.                         }
  81.  
  82.                         // Iterating over subject nodes
  83.                         foreach (HtmlNode sbNode in subjectNodes)
  84.                         {
  85.                             // Parsing code of the subject
  86.                             string code = sbNode.InnerText;
  87.                             string name = "SEM NOME";
  88.                            
  89.                             code = String.IsNullOrEmpty(code) ? "SEMCODIGO" : code;
  90.  
  91.                             // Web Request for the page with subject names and descriptions
  92.                             string pageLink  = Consts.EMENTASSUFIX + sbNode.Attributes["href"].Value;
  93.                             pageLink         = pageLink.Replace ("..", String.Empty);
  94.                             string subjsPage = webReq.Get (pageLink);
  95.  
  96.                             // Loading HtmlMap for the page of subject names and descriptions
  97.                             HtmlDocument subjectsMap = new HtmlDocument();
  98.                             subjectsMap.LoadHtml (subjsPage);
  99.                             HtmlNodeCollection subjects = subjectsMap.DocumentNode.SelectNodes (".//font[@size='-1']//a");
  100.  
  101.                             // Iterating over subject nodes until both codes match
  102.                             // Once codes match, it parses the name of the course that matched
  103.                             foreach (HtmlNode node in subjects)
  104.                             {
  105.                                 if (node.InnerText.Contains (code))
  106.                                 {
  107.                                     name = node.InnerText.Replace (code, String.Empty).Trim();
  108.                                     break;
  109.                                 }
  110.                             }
  111.  
  112.                             // Adding Subject to list of subjects of this course
  113.                             parsedSubjects.Add (new Subject (name, code, 0, "", semesterCounter));
  114.                         }
  115.                     }
  116.                     catch (Exception ex)
  117.                     {
  118.                         Logs.LogWriter.LogError (ex);
  119.                     }
  120.                 }
  121.  
  122.                 // Creating Course Instance
  123.                 Course tmpCourse = new Course (kv.Key, semesterCounter, parsedSubjects);
  124.                 courses.Add (tmpCourse);
  125.  
  126.                 // Updating UI
  127.                 LoadCoursesUIGrid (uiComps, tmpCourse);
  128.  
  129.                 // Adding Unique subjects to list of subjects
  130.                 foreach (Subject sub in parsedSubjects)
  131.                 {
  132.                     if (!allSubjects.Exists( m => m.Name.Equals(sub.Name) && m.Code.Equals(sub.Code)))
  133.                     {
  134.                         allSubjects.Add(sub);
  135.                     }
  136.                 }
  137.             }
  138.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement