Example #1
0
        public static String ToStandardJsonStr(Dictionary <String, CourseInfo> infos)
        {
            JsonArray courseArray = new JsonArray();

            foreach (var a in infos)
            {
                CourseInfo info = a.Value;
                JsonObject obj  = new JsonObject();
                obj.Add("courseName", JsonValue.CreateStringValue(info.CourseName));

                JsonArray timeArray = new JsonArray();
                foreach (var time in info.CourseTimes)
                {
                    JsonObject t = new JsonObject();
                    t.Add("day", JsonValue.CreateStringValue(time.Day.ToString()));
                    t.Add("num", JsonValue.CreateStringValue(time.Start + "-" + time.End));
                    t.Add("week", JsonValue.CreateStringValue(time.WeekWord));
                    timeArray.Add(t);
                }
                obj.Add("times", timeArray);
                obj.Add("type", JsonValue.CreateStringValue("custom"));
                obj.Add("location", JsonValue.CreateStringValue(info.Location));
                courseArray.Add(obj);
            }
            String str = courseArray.Stringify();

            return(str);
        }
Example #2
0
        public static void SaveCourses(String storageName, Dictionary <String, CourseInfo> infos)
        {
            JsonObject jsonRoot = new JsonObject();

            jsonRoot.Add("code", JsonValue.CreateStringValue("0"));
            JsonArray courseArray = new JsonArray();

            foreach (var a in infos)
            {
                CourseInfo info = a.Value;
                JsonObject obj  = new JsonObject();
                obj.Add("courseName", JsonValue.CreateStringValue(info.CourseName));
                obj.Add("exam", JsonValue.CreateStringValue(info.Exam));
                obj.Add("location", JsonValue.CreateStringValue(info.Location));
                obj.Add("other", JsonValue.CreateStringValue(info.Others));
                obj.Add("examPlace", JsonValue.CreateStringValue(info.ExamPlace));
                JsonArray timeArray = new JsonArray();
                foreach (var time in info.CourseTimes)
                {
                    JsonObject t = new JsonObject();
                    t.Add("day", JsonValue.CreateNumberValue(time.Day));
                    t.Add("num", JsonValue.CreateStringValue(time.Start + "-" + time.End));
                    t.Add("week", JsonValue.CreateStringValue(time.WeekWord));
                    timeArray.Add(t);
                }
                obj.Add("times", timeArray);
                courseArray.Add(obj);
            }
            jsonRoot.Add("courses", courseArray);
            String str = jsonRoot.Stringify();

            Editor.putString(storageName, str);
        }
Example #3
0
        public static void DecodeDeanHtml(String html)
        {
            html = html.Trim();
            DeanCourses.Clear();
            try
            {
                HtmlDocument doc = new HtmlDocument();
                doc.LoadHtml(html);
                HtmlNode table = null;

                foreach (var node in doc.DocumentNode.ChildNodes)
                {
                    String a = node.OuterHtml;
                    if (node.OuterHtml.Contains("</table>"))
                    {
                        table = FindDeanTable(node);
                        break;
                    }
                }


                foreach (var courseNode in table.ChildNodes)
                {
                    String a = courseNode.OuterHtml;
                    try
                    {
                        if (!(courseNode.OuterHtml.EndsWith("</tr>")))
                        {
                            continue;
                        }
                        CourseInfo tempInfo = new CourseInfo("", "", "", CourseKind.Dean);
                        int        col      = 0;
                        foreach (var detail in courseNode.ChildNodes)
                        {
                            if (!detail.OuterHtml.Contains("</td>"))
                            {
                                continue;
                            }
                            String innerString = detail.InnerHtml.Replace(" ", "");
                            while (innerString.Contains("&nbsp;"))
                            {
                                innerString = innerString.Remove(innerString.IndexOf("&nbsp;"), 6);
                            }
                            col++;
                            switch (col)
                            {
                            case 2:
                            {
                                tempInfo.CourseName = innerString;
                            }
                            break;

                            case 9:
                            {
                                String[] whereAndWhen = innerString.Split(new String[] { "<br>" }, StringSplitOptions.RemoveEmptyEntries);
                                foreach (String str in whereAndWhen)
                                {
                                    if (str.StartsWith("时间"))
                                    {
                                        String   when  = str.Remove(0, 3);
                                        String[] whens = when.Split(new String[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                                        foreach (String timeStr in whens)
                                        {
                                            if (!timeStr.StartsWith("周"))
                                            {
                                                continue;
                                            }
                                            String dayOfWeek   = timeStr.Substring(0, 2);
                                            String classStr    = timeStr.Substring(2);
                                            String weekTypeStr = "";
                                            if (classStr.EndsWith(")"))
                                            {
                                                weekTypeStr = classStr.Substring(classStr.Length - 3);
                                                classStr    = classStr.Remove(classStr.Length - 3);
                                            }
                                            tempInfo.AddTime(new CourseTime(DayOfWeekConvert(dayOfWeek), classStr, WeekKindConvert(weekTypeStr)));
                                        }
                                    }
                                    else if (str.StartsWith("地点"))
                                    {
                                        tempInfo.Location = str.Substring(3);
                                    }
                                }
                            } break;

                            case 10:
                            {
                                String[] whereAndWhen = innerString.Split(new String[] { "<br>" }, StringSplitOptions.RemoveEmptyEntries);
                                foreach (String str in whereAndWhen)
                                {
                                    if (str.StartsWith("时间"))
                                    {
                                        try
                                        {
                                            String timeStr = str.Substring(3, 8);
                                            tempInfo.Exam = timeStr;
                                        }
                                        catch
                                        {
                                            tempInfo.Exam = "";
                                        }
                                    }
                                    else if (str.StartsWith("地点"))
                                    {
                                        try
                                        {
                                            String placeStr = str.Substring(3);
                                            tempInfo.ExamPlace = placeStr;
                                        }
                                        catch
                                        {
                                            tempInfo.ExamPlace = "";
                                        }
                                    }
                                }
                            }
                            break;

                            case 14:
                            {
                                tempInfo.Others = innerString;
                            } break;

                            default: break;
                            }
                        }
                        DeanCourses.Add(tempInfo.CourseName, tempInfo);
                    }
                    catch
                    {
                        Debug.WriteLine("fail to parse a course, it's HTML is\n" + courseNode.OuterHtml);
                        continue;
                    }
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine("fail to decode Dean's HTML! at" + e.StackTrace);
            }
            SaveCourses("DeanCourses", DeanCourses);
        }