Exemple #1
0
        /// <summary>
        /// 获取中国节日列表
        /// </summary>
        /// <param name="calenderYear">公历年</param>
        /// <returns></returns>
        public List <HolidayEntity> GetChinaFestivalList(string calenderYear)
        {
            string appkey = Settings.Instance.GetCalendarAppKey;

            List <HolidayEntity> result = new List <HolidayEntity>();

            for (int i = 1; i < 13; i++)
            {
                string url2        = "http://japi.juhe.cn/calendar/month";
                var    parameters2 = new Dictionary <string, string>();
                parameters2.Add("key", appkey);                                   //你申请的key
                parameters2.Add("year-month", calenderYear + "-" + i.ToString()); //指定月份,格式为YYYY-MM,如月份和日期小于10,则取个位,如:2012-1
                string     result2    = sendPost(url2, parameters2, "get");
                JsonObject newObj2    = new JsonObject(result2);
                String     errorCode2 = newObj2["error_code"].Value;
                if (errorCode2 == "0")
                {
                    var holidayObject = new JsonObject("{holiday:" + newObj2["result"].Object["holiday"].Value + "}");
                    var itemsList     = holidayObject["holiday"].Items;
                    foreach (var jsonProperty in itemsList)
                    {
                        var name = jsonProperty.Object["name"].Value;
                        if (result.Where(q => q.Name == name).ToList().Count > 0)
                        {
                            continue;
                        }
                        var desc     = jsonProperty.Object["desc"].Value;
                        var year     = newObj2["result"].Object["year"].Value;
                        var daysList = jsonProperty.Object["list"].Items;

                        string refDateFrom = string.Empty;
                        string refDateTo   = string.Empty;
                        string refStatus   = string.Empty;

                        for (int j = 0; j < daysList.Count; j++)
                        {
                            var    day        = daysList[j];
                            string tempDate   = day.Object["date"].Value;
                            string tempStatus = day.Object["status"].Value;
                            if (!string.IsNullOrWhiteSpace(refDateFrom))
                            {
                                if (refStatus != tempStatus)
                                {
                                    result.Add(new HolidayEntity {
                                        Description = desc, From = Convert.ToDateTime(refDateFrom), To = Convert.ToDateTime(refDateTo), Name = name, Status = Convert.ToInt32(refStatus)
                                    });
                                    refDateFrom = tempDate;
                                    refStatus   = tempStatus;
                                }
                            }
                            else if (string.IsNullOrWhiteSpace(refDateFrom))
                            {
                                refDateTo = refDateFrom = tempDate;
                                refStatus = tempStatus;
                            }
                            if (j == daysList.Count - 1)
                            {
                                if (tempDate == refDateTo && tempStatus == refStatus && !string.IsNullOrWhiteSpace(refStatus))
                                {
                                    refDateTo = tempDate;
                                    result.Add(new HolidayEntity {
                                        Description = desc, From = Convert.ToDateTime(refDateFrom), To = Convert.ToDateTime(refDateTo), Name = name, Status = Convert.ToInt32(refStatus)
                                    });
                                }
                                else if (tempDate != refDateTo && !string.IsNullOrWhiteSpace(tempDate) && !string.IsNullOrWhiteSpace(refDateTo) && refStatus != tempStatus)
                                {
                                    result.Add(new HolidayEntity {
                                        Description = desc, From = Convert.ToDateTime(refDateFrom), To = Convert.ToDateTime(refDateTo), Name = name, Status = Convert.ToInt32(refStatus)
                                    });
                                    result.Add(new HolidayEntity {
                                        Description = desc, From = Convert.ToDateTime(tempDate), To = Convert.ToDateTime(tempDate), Name = name, Status = Convert.ToInt32(refStatus)
                                    });
                                }
                                else if (tempDate != refDateTo && !string.IsNullOrWhiteSpace(tempDate) && !string.IsNullOrWhiteSpace(refDateTo) && refStatus == tempStatus)
                                {
                                    refDateTo = tempDate;
                                    result.Add(new HolidayEntity {
                                        Description = desc, From = Convert.ToDateTime(refDateFrom), To = Convert.ToDateTime(refDateTo), Name = name, Status = Convert.ToInt32(refStatus)
                                    });
                                }
                            }
                        }
                    }
                }
                else
                {
                    //Debug.WriteLine("失败");
                    LoggerHelper.Error("ErrorCode:" + newObj2["error_code"].Value + " ErrorReason:" + newObj2["reason"].Value + " URL:http://japi.juhe.cn/calendar/month" + " year-month:" + calenderYear + " - " + i.ToString());
                    //Debug.WriteLine(newObj2["error_code"].Value + ":" + newObj2["reason"].Value);
                }
            }
            return(result);
        }