public string getRemoteCourses(string id, string pw)
        {
            if (!Validation.IDValidation(id))
                return "Wrong Input";

            ProjectDBEntities PDb = new ProjectDBEntities();
            int count = PDb.StudentInfo.Where(s => s.SID == id).Count();
            if (count == 0)
                return "False";
            IQueryable<StuCourses> allCourses = from eve in PDb.StuCourses
                                            where eve.SID == id
                                            select eve;
            foreach (StuCourses item in allCourses)
            {
                PDb.StuCourses.Remove(item);
            }
            // Set up the encoding nice and neat
            Encoding GB2312Encoding = Encoding.GetEncoding("GB2312");

            // Since two continuous POST request is being sent, there has to be a cookie that remembers everything
            CookieContainer cookieContainer = new CookieContainer();

            // Login to JWC.CQU.EDU.CN
            // Note that this url changes from time to time
            string loginUrl = @"http://202.202.1.176:8080/_data/index_login.aspx";
            string loginParameters = "Sel_Type=STU&UserID=";
            loginParameters += id;
            loginParameters += "&PassWord="******"POST", GB2312Encoding, cookieContainer);

            // Retrieve the course table HTML and save in a string
            string courseUrl = @"http://202.202.1.176:8080/znpk/Pri_StuSel_rpt.aspx";
            string parametersToGetCourses = "Sel_XNXQ=20121&rad=1&px=1";
            string courseHtmlPage = RequestHandler.SendSynchronousRequest(courseUrl, parametersToGetCourses, "POST", GB2312Encoding, cookieContainer);
            try
            {
                Regex regex = new Regex(@"<td(?:\s[^>]*)?>(.*?)</td>");
                var regexMatches = regex.Matches(courseHtmlPage);

                // Stores the name and student ID of the student
                String studentName;
                String studentID;
                String studentInformation = regexMatches[0].Value;

                int subStrLimitForID = studentInformation.IndexOf("&nbsp;&nbsp;姓名:");
                studentID = studentInformation.Substring(3, subStrLimitForID - 3);
                int subStrLimitForName = studentInformation.IndexOf("&nbsp;&nbsp;讲授/上机");
                studentName = studentInformation.Substring(26, subStrLimitForName - 26);

                List<String> linearTableContent = new List<String>();

                // Remove all "<"s and ">"s
                for (int i = 0; i < regexMatches.Count; i++)
                {
                    Match match = regexMatches[i];
                    String s = match.Value.TrimStart('<');
                    int firstRightBraket = s.IndexOf('>') + 1;
                    int firstLeftBraket = s.IndexOf('<');
                    String content = s.Substring(firstRightBraket, firstLeftBraket - firstRightBraket);
                    if (i > 14) linearTableContent.Add(content);
                }

                // The number of properties of a course (usually this is the number of columns in the course table)
                int columnCount = 13;

                // fit number to the multiple of @columnCount
                int contentCount = linearTableContent.Count;
                int contentCountCopy = contentCount;
                while (contentCountCopy > columnCount)
                {
                    contentCountCopy -= columnCount;
                }
                contentCount -= contentCountCopy;

                // Save all courses in a list of Course objects
                List<Course> courses = new List<Course>();
                Course course = new Course();
                for (int i = 0; i < contentCount; i++)
                {
                    if (i % columnCount == 0)
                    {         // 序号
                        course = new Course();
                    }
                    else if (i % columnCount == 1)
                    {    // 课程
                        course.Name = linearTableContent[i];
                    }
                    else if (i % columnCount == 2)
                    {    // 学分
                        course.Credit = linearTableContent[i];
                    }
                    else if (i % columnCount == 3)
                    {    // 总学时
                        course.Duration = linearTableContent[i];
                    }
                    else if (i % columnCount == 4)
                    {    // 讲授学时
                        // Do nothing
                    }
                    else if (i % columnCount == 5)
                    {    // 上机学时
                        // Do nothing
                    }
                    else if (i % columnCount == 6)
                    {    // 类别
                        course.Category = linearTableContent[i];
                    }
                    else if (i % columnCount == 7)
                    {    // 授课方式

                    }
                    else if (i % columnCount == 8)
                    {    // 考核方式
                        course.TestType = linearTableContent[i];
                    }
                    else if (i % columnCount == 9)
                    {    // 任课教师
                        course.Teacher = linearTableContent[i];
                    }
                    else if (i % columnCount == 10)
                    {   // 周次
                        // Your processing method here
                        course.Week = OtherFunctions.FormatWeek(linearTableContent[i]);
                    }
                    else if (i % columnCount == 11)
                    {   // 节次
                        // Your processing method here
                        course.Section = OtherFunctions.FormatSection(linearTableContent[i]);
                        course.Day = OtherFunctions.FormatDay(linearTableContent[i]);
                        //course.EndTime = linearTableContent[i];
                    }
                    else if (i % columnCount == 12)
                    {   // 地点
                        course.Location = linearTableContent[i];
                        courses.Add(course);
                    }
                }
                if (courses.Count() == 0)
                    return "False";

                for (int i = 0; i < courses.Count(); i++)//each (Course item in courses)
                {
                    if ( i != 0 )
                    {
                        if(courses[i].Name == "")
                            courses[i].Name = courses[i - 1].Name;
                        if(courses[i].Teacher == "")
                            courses[i].Teacher = courses[i - 1].Teacher;
                        if (courses[i].Category == "")
                            courses[i].Category = courses[i - 1].Category;
                        if (courses[i].Credit == "")
                            courses[i].Credit = courses[i - 1].Credit;
                        if (courses[i].TestType == "")
                            courses[i].TestType = courses[i - 1].TestType;
                        if (courses[i].Duration == "")
                            courses[i].Duration = courses[i - 1].Duration;
                    }
                    StuCourses sc = new StuCourses
                    {
                        SID = id,
                        C_NAME = courses[i].Name,
                        C_WEEK = courses[i].Week,
                        C_DAY = courses[i].Day,
                        C_SECTION = courses[i].Section,
                        C_LOCATION = courses[i].Location,
                        C_TEACHER = courses[i].Teacher,
                        C_CREDIT = courses[i].Credit,
                        C_CATEGORY = courses[i].Category,
                        C_TESTTYPE = courses[i].TestType,
                        C_DURATION = courses[i].Duration
                    };
                    PDb.StuCourses.Add(sc);
                }
                PDb.SaveChanges();
            }
            catch
            {
                return "Failed";
            }
            return "True";
                //return OtherFunctions.FormatWeek(courses[k].Week) + "__" + courses[k].Week;//"True";
                //return courses[2].Day;//"True";
        }
Exemplo n.º 2
0
 public string sendStudentCourses(string id)
 {
     if (!Validation.IDValidation(id))
         return "Wrong Input";
     ProjectDBEntities PDb = new ProjectDBEntities();
     List<Course> coList = new List<Course>();
     IQueryable<StuCourses> coInfo = from co in PDb.StuCourses
                                     where co.SID == id
                                     select co;
     if (coInfo.Count() == 0)
         return "False";
     foreach (StuCourses item in coInfo)
     {
         Course co = new Course();
         co.Name = item.C_NAME;
         co.Week = item.C_WEEK;
         co.Day = item.C_DAY;
         co.Section = item.C_SECTION;
         co.Location = item.C_LOCATION;
         co.Teacher = item.C_TEACHER;
         co.Credit = item.C_CREDIT;
         co.Category = item.C_CATEGORY;
         co.TestType = item.C_TESTTYPE;
         co.Duration = item.C_DURATION;
         coList.Add(co);
     }
     string retJson = JsonConvert.SerializeObject(coList);
     return retJson;
 }