// 初始化 public static void Initializate() { if (!ApplicationStatus.isInternetConnected) { throw new WebException(); } string webInfo = WebServices.GetWebClient(ApplicationStatus.prepcalWebInfoUrl); // webInfo的示例 //string webInfo = "<prepcal><version>1.27</version><donateCount>1</donateCount><homepage>www.baidu.com</homepage> //<apiUrlJisu>http://api.jisuapi.com/huangli/date?appkey=[key]&year=[year]&month=[month]&day=[day]</apiUrlJisu> //<apiKeyJisu>9969a22083bd856e</apiKeyJisu> //<apiUrlJuhe>http://japi.juhe.cn/calendar/day?date=[year]-[month]-[day]&key=[key]</apiUrlJuhe> //<apiKeyJuhe>cea509aba88b9a19390b5dfcf9a57451</apiKeyJuhe></prepcal>"; string[] patterns = { @"\<prepcal\>.*\<version\>(?<result>.+)\</version\>.*\</prepcal\>", // versionPattern @"\<prepcal\>.*\<donateCount\>(?<result>.+)\</donateCount\>.*\</prepcal\>", // donateCountPattern @"\<prepcal\>.*\<donateInfo\>(?<result>.+)\</donateInfo\>.*\</prepcal\>", // donateInfoPattern @"\<prepcal\>.*\<homepage\>(?<result>.+)\</homepage\>.*\</prepcal\>", // homepagePattern @"\<prepcal\>.*\<notice\>(?<result>.+)\</notice\>.*\</prepcal\>" // noticePattern }; // 将patterns中的<>替换为html格式 for (int i = 0; i < patterns.Length; i++) { patterns[i] = patterns[i].Replace(@"\<", "<"); patterns[i] = patterns[i].Replace(@"\>", ">"); } // 开始匹配 Regex versionReg = new Regex(patterns[0], RegexOptions.ExplicitCapture); Regex donateCountReg = new Regex(patterns[1], RegexOptions.ExplicitCapture); Regex donateInfoReg = new Regex(patterns[2], RegexOptions.ExplicitCapture); Regex homepageReg = new Regex(patterns[3], RegexOptions.ExplicitCapture); Regex noticeReg = new Regex(patterns[4], RegexOptions.ExplicitCapture); string latestVersion = versionReg.Match(webInfo).Groups["result"].Value; string donateManTimeCount = donateCountReg.Match(webInfo).Groups["result"].Value; string donateInfo = donateInfoReg.Match(webInfo).Groups["result"].Value; string homepage = homepageReg.Match(webInfo).Groups["result"].Value; string notice = noticeReg.Match(webInfo).Groups["result"].Value; // 将结果存入ApplicationStatus类 if (latestVersion != "") { ApplicationStatus.latestVersion = latestVersion; } if (donateManTimeCount != "") { ApplicationStatus.donateManTimeCount = donateManTimeCount; } if (donateInfo != "") { donateInfo = donateInfo.Replace(" ", " "); donateInfo = donateInfo.Replace("#enter", "\n"); ApplicationStatus.donateInfo = donateInfo; } if (homepage != "") { ApplicationStatus.homepage = homepage; } if (notice != "") { notice = notice.Replace(" ", " "); notice = notice.Replace("#enter", "\n"); ApplicationStatus.notice = notice; } isInitialized = true; }
// 调用WebServices类中的ApiQuery从Webapi读取数据并保存到string[,]中 private static string[,] GetDateInfos(DateTime startDate, DateTime endDate) { TimeSpan span = endDate - startDate; int maxDaysStorage = MAXYEARSTORAGE * 12 * 31; if (span.Days > maxDaysStorage) { throw new ArgumentOutOfRangeException(); } // 黄历数据字符串,每一行的首元素为日期索引,其余为黄历数据 string[,] dateInfos = new string[span.Days, LUNARINFOLENGTH]; string tempLunarInfo = ""; DateTime currentDate = startDate; // 正则表达式,\u0022表示双引号 string[] patterns = { @"\u0022year\u0022:\u0022(?<1>\w+)\u0022", // year @"\u0022month\u0022:\u0022(?<2>\w+)\u0022", // month @"\u0022day\u0022:\u0022(?<3>\w+)\u0022", // day @"\u0022yangli\u0022:\u0022(?<4>\w+)\u0022", // yangli @"\u0022nongli\u0022:\u0022(?<5>.+?)\u0022", // nongli @"\u0022star\u0022:\u0022(?<6>\w+)\u0022", // star @"\u0022taishen\u0022:\u0022(?<7>\w+)\u0022", // taishen @"\u0022wuxing\u0022:\u0022(?<8>\w+)\u0022", // wuxing @"\u0022chong\u0022:\u0022(?<9>.+?)\u0022", // chong @"\u0022sha\u0022:\u0022(?<10>\w+)\u0022", // sha @"\u0022shengxiao\u0022:\u0022\s*(?<11>.+?)\u0022", // shengxiao @"\u0022jiri\u0022:\u0022(?<12>.+?)\u0022", // jiri @"\u0022zhiri\u0022:\u0022(?<13>.+?)\u0022", // zhiri @"\u0022xiongshen\u0022:\u0022(?<14>.+?)\u0022", // xiongshen @"\u0022jishenyiqu\u0022:\u0022(?<15>.+?)\u0022", // jishenyiqu @"\u0022caishen\u0022:\u0022(?<16>\w+)\u0022", // caishen @"\u0022xishen\u0022:\u0022(?<17>\w+)\u0022", // xishen @"\u0022fushen\u0022:\u0022(?<18>\w+)\u0022", // fushen @"\u0022suici\u0022:\[(?<19>(\u0022\w+\u0022,){0,}\u0022\w+\u0022)\]", // suici (raw) @"\u0022yi\u0022:\[(?<20>(\u0022\w+\u0022,){0,}\u0022\w+\u0022)\]", // yi (raw) @"\u0022ji\u0022:\[(?<21>(\u0022\w+\u0022,){0,}\u0022\w+\u0022)\]", // ji (raw) @"\u0022eweek\u0022:\u0022(?<22>\w+)\u0022", // eweek @"\u0022emonth\u0022:\u0022(?<23>\w+)\u0022", // emonth @"\u0022week\u0022:\u0022(?<24>\w+)\u0022", // week }; Regex[] reg = new Regex[24]; for (int i = 0; i < reg.Length; i++) { // 为提高运行效率,在需要加载离线数据库时以编译方式初始化正则表达式 reg[i] = new Regex(patterns[i], RegexOptions.ExplicitCapture /*| RegexOptions.Compiled*/); } for (int i = 0; i < span.Days; i++) { currentLoadingDayCount = (currentDate - startDate).Days + 1; // 将每一行的首元素设置为索引,即当前日期相比初始日期经过的天数 // 范围为0 ~ (MAXYEARSTORAGE * 12 * 31 - 1) dateInfos[i, 0] = (currentDate - startDate).Days.ToString(); tempLunarInfo = WebServices.ApiQueryOnJisu(currentDate); // 查询api数据并临时保存到字符串内 for (int j = 1; j < LUNARINFOLENGTH; j++) // 注意:从1开始,因为首元素是索引 { dateInfos[i, j] = reg[j - 1].Match(tempLunarInfo).Groups[j.ToString()].Value; // 进行既定规则的正则匹配 if (j == 19 || j == 20 | j == 21) // 当遍历到匹配结果的suici, yi, ji这几个多元结果时,删除其中的分隔符 { dateInfos[i, j] = dateInfos[i, j].Replace("\",", " "); // 将匹配出的raw结果中的 ", 替换为空格 dateInfos[i, j] = dateInfos[i, j].Replace("\"", ""); // 删除所有的引号 } } currentDate = currentDate.AddDays(1); // 下一天 } return(dateInfos); }