Advertisement
MarcelloGrechi

UNICAMP Crawler

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