Exemplo n.º 1
0
        public void readJSON(Uri location)
        {
            string text = new StreamReader(Application.GetResourceStream(location).Stream).ReadToEnd();

            //Console.WriteLine(text);
            //JObject json = JObject.Parse(text);

            String[] yearTitles = { "Part II", "Part III", "Part IV" };
            JArray   courses    = JArray.Parse(text);

            for (int i = 0; i < courses.Count; i++)
            {
                CourseSet cs = new CourseSet()
                {
                    Title = yearTitles[i]
                };

                JArray yearCourses = courses[i] as JArray;

                for (int j = 0; j < yearCourses.Count; j++)
                {
                    JObject    course = yearCourses[j] as JObject;
                    CourseItem ci     = new CourseItem();
                    ci.Name   = course["name"].ToString();
                    ci.Code   = course["code"].ToString();
                    ci.Points = course["points"].ToString();
                    ci.Desc   = course["text"].ToString();

                    cs.CourseList.Add(ci.Code, ci);
                }
                CourseSets.Add(i, cs);
            }
            CurrentCourseSet = 0;
        }
Exemplo n.º 2
0
        /// <summary>
        /// 通过学期获取课表
        /// </summary>
        private CourseSet GetCourseSetNode(String term)
        {
            StreamReader reader      = new StreamReader(App.RootPath + App.CourseData, Encoding.UTF8);
            XmlDocument  xmlDocument = new XmlDocument();

            xmlDocument.Load(reader);
            reader.Close();

            XmlElement  root = xmlDocument.DocumentElement;
            XmlNodeList xnl  = root.SelectNodes("/Courses/CourseSet");
            XmlNodeList xnls = null;

            if (xnl.Count == 0)
            {
                return(null);
            }

            CourseSet courseSet = new CourseSet();

            foreach (XmlNode xn in xnl)
            {
                if (xn.Attributes["Term"].Value.Equals(term))
                {
                    xnls = xn.ChildNodes; break;
                }
            }

            if (xnls != null && xnls.Count > 0)
            {
                courseSet.Term    = term;
                courseSet.Courses = new List <Course>();
                foreach (XmlNode xn in xnls)
                {
                    Course temp = new Course
                    {
                        CourseName    = xn.Attributes["Name"].Value,
                        CourseTeacher = xn.Attributes["Teacher"].Value,
                        CourseLoc     = xn.Attributes["Loc"].Value,
                        CourseDur     = xn.Attributes["Dur"].Value,
                        CourseTime    = CourseTime.FromString(xn.Attributes["Time"].Value)
                    };

                    courseSet.Courses.Add(temp);
                }
            }

            return(courseSet);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 创建课程表
        /// </summary>
        private bool CreatCourseSetNode(CourseSet set)
        {
            StreamReader reader      = new StreamReader(App.RootPath + App.CourseData, Encoding.UTF8);
            XmlDocument  xmlDocument = new XmlDocument();

            xmlDocument.Load(reader);
            reader.Close();

            XmlElement  root = xmlDocument.DocumentElement;
            XmlNodeList xnl  = root.SelectNodes("/Courses/CourseSet");

            foreach (XmlNode xn in xnl)
            {
                if (xn.Attributes["Term"].Value.Equals(set.Term))
                {
                    root.RemoveChild(xn); break;
                }
            }

            XmlElement Courses = xmlDocument.CreateElement("CourseSet");

            Courses.SetAttribute("Term", set.Term);
            root.AppendChild(Courses);

            foreach (Course c in set.Courses)
            {
                XmlElement NewCourse = xmlDocument.CreateElement("Course");
                NewCourse.SetAttribute("Name", c.CourseName);
                NewCourse.SetAttribute("Teacher", c.CourseTeacher);
                NewCourse.SetAttribute("Loc", c.CourseLoc);
                NewCourse.SetAttribute("Dur", c.CourseDur);
                NewCourse.SetAttribute("Time", c.CourseTime.ToString());
                Courses.AppendChild(NewCourse);
            }

            xmlDocument.Save(App.RootPath + App.CourseData);
            return(true);
        }
Exemplo n.º 4
0
        // This was written from the point of view of being provided a From college and finding argreements From this college To all others
        // However this was just to make dev easier, it can be reversed to
        private async Task UpdateCollegeYearBundleRelationships(AssistDbContext db, College fromCollege, College toCollege, Year year
                                                                , List <CourseSet> dbCourseSets, List <CourseRelationship> dbCourseRelationships, List <KnownRequest> dbKnownRequests)
        {
            //Get this request from the DB
            var url          = RequestUrl(fromCollege.Shorthand, toCollege.Shorthand, year.Name);
            var requestShell = new KnownRequest()
            {
                RequestTo = RequestSource.Main, Url = url
            };
            var knownRequest = dbKnownRequests.FirstOrDefault(r => r.LooseEquals(requestShell));

            if (knownRequest == null)
            {
                db.KnownRequests.Add(requestShell);
                dbKnownRequests.Add(requestShell);
                knownRequest = requestShell;
            }
            else
            {
                // If we know this request returns nothing we can skip it
                if (!knownRequest.IsValid() && knownRequest.UpToDateAsOf > DateTimeOffset.Now.AddDays(-14))
                {
                    return;
                }
            }

            string result;

            using (var response = await client.GetAsync(url))
            {
                using (var content = response.Content)
                {
                    result = await content.ReadAsStringAsync();

                    //result = DebugManager.RequestAhcToCpp1516(); //DEBUG
                    knownRequest.Update(result.Length);
                    db.SaveChanges();
                }
            }
            // Technically this is an html doc but the bit we care about is always going to be between the only set of PRE tags
            // so we'll skip the html doc overhead and do it old school
            var dirtyList = result.Between("<PRE>", "</PRE>").Trim().Split(courseGroupSeperator);
            var validCourseRelationships = new List <string>();
            var multiCoursesRegex        = RegexManager.MultiCourseRegex();

            // Clean phase 1, remove single courses or non course data, add these to cleanList
            foreach (var potentialCourseRela in dirtyList)
            {
                // Regex match on multiple courses and exclude our known empty comparison cases
                if (multiCoursesRegex.IsMatch(potentialCourseRela) && !badRelationshipIndicators.Any(s => potentialCourseRela.ToLower().Contains(s)))
                {
                    validCourseRelationships.Add(potentialCourseRela);
                }
            }

            // Clean phase 2 + parse, all within each block of course relationships
            // parse the ones which do into to/from courses
            var courseRelaExtractedBucket = new List <CourseRelationship>();

            foreach (var courseRelaRaw in validCourseRelationships)
            //var courseRelaRaw = validCourseRelationships[0]; //DEBUG
            {
                using (StringReader reader = new StringReader(courseRelaRaw))
                {
                    var toPart   = "";
                    var fromPart = "";

                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        // if the line doesn't contain a |, skip (notes, department headers, etc. Never course data)
                        if (line.IndexOf('|') == -1)
                        {
                            continue;
                        }

                        // strip formatting
                        line = line.Replace("<B >", "").Replace("<B>", "").Replace("</B>", "");
                        line = line.Replace("<U >", "").Replace("<U>", "").Replace("</U>", "");

                        // break line into to/from parts (before/after the |  ...order matters!)
                        var lineParts = line.Split("|", 2);

                        toPart   += lineParts[0] + System.Environment.NewLine;
                        fromPart += lineParts[1] + System.Environment.NewLine;
                    }
                    toPart   = toPart.Trim();
                    fromPart = fromPart.Trim();
                    if (toPart.Length == 0 || fromPart.Length == 0)
                    {
                        continue;
                    }

                    var toCourseSet   = new CourseSet(toCollege, year, toPart);
                    var fromCourseSet = new CourseSet(fromCollege, year, fromPart);

                    // Courses stacked on one side and nothing on the other dont violate any of the rules thus far, weed them out here
                    if (toCourseSet.CommaDelimitedCourseNames.Length == 0 || fromCourseSet.CommaDelimitedCourseNames.Length == 0)
                    {
                        continue;
                    }

                    //Prevent building dupe courses
                    var toCourseSetDb   = dbCourseSets.FirstOrDefault(c => c.Equals(toCourseSet));
                    var fromCourseSetDb = dbCourseSets.FirstOrDefault(c => c.Equals(fromCourseSet));

                    var now = DateTimeOffset.Now;
                    if (toCourseSetDb != null)
                    {
                        toCourseSetDb.UpToDateAsOf = now;
                        toCourseSet = toCourseSetDb;
                    }
                    else
                    {
                        dbCourseSets.Add(toCourseSet);
                    }

                    if (fromCourseSetDb != null)
                    {
                        fromCourseSetDb.UpToDateAsOf = now;
                        fromCourseSet = fromCourseSetDb;
                    }
                    else
                    {
                        dbCourseSets.Add(fromCourseSet);
                    }


                    var courseRela = new CourseRelationship()
                    {
                        ToCourseSet = toCourseSet, FromCourseSet = fromCourseSet, UpToDateAsOf = DateTimeOffset.Now
                    };
                    courseRelaExtractedBucket.Add(courseRela);
                }
            }
            // Save after each toCollege has been processed. Ensures we walking into each save session on a clean slate.
            db.SaveChanges();


            // Whats currently in the DB for this college/college/year bucket. If no updates have happened RelaDb and RelaExtracted should be equivalent
            var courseRelaDbBucket = dbCourseRelationships.Where(
                cr => cr.FromCourseSet.College.CollegeId == fromCollege.CollegeId &&
                cr.ToCourseSet.College.CollegeId == toCollege.CollegeId &&
                cr.FromCourseSet.Year.YearId == year.YearId
                ).ToList();

            var relasToAdd    = courseRelaExtractedBucket.Except(courseRelaDbBucket).ToList();
            var relasToRemove = courseRelaDbBucket.Except(courseRelaExtractedBucket).ToList();

            foreach (var rela in relasToRemove)
            {
                dbCourseRelationships.Remove(rela);
                db.CourseRelationships.Remove(rela);
            }
            db.SaveChanges();

            foreach (var rela in relasToAdd)
            {
                dbCourseRelationships.Add(rela);
                db.CourseRelationships.Add(rela);
            }
            db.SaveChanges();
        }
Exemplo n.º 5
0
        public void readJSON(Uri location)
        {
            string text = new StreamReader(Application.GetResourceStream(location).Stream).ReadToEnd();
            //Console.WriteLine(text);
            //JObject json = JObject.Parse(text);

            String[] yearTitles = { "Part II", "Part III", "Part IV" };
            JArray courses = JArray.Parse(text);
            for (int i = 0; i < courses.Count; i++) {
                CourseSet cs = new CourseSet() {
                    Title = yearTitles[i]
                };

                JArray yearCourses = courses[i] as JArray;

                for (int j = 0; j < yearCourses.Count; j++) {
                    JObject course = yearCourses[j] as JObject;
                    CourseItem ci = new CourseItem();
                    ci.Name = course["name"].ToString();
                    ci.Code = course["code"].ToString();
                    ci.Points = course["points"].ToString();
                    ci.Desc = course["text"].ToString();

                    cs.CourseList.Add(ci.Code, ci);
                }
                CourseSets.Add(i, cs);
            }
            CurrentCourseSet = 0;
        }