コード例 #1
0
        public static ICalCalendar ParseICalendarFile(string filePath, string countryCode, string countryName)
        {
            ICalCalendar result = new ICalCalendar(countryCode, countryName);

            if (!File.Exists(filePath))
            {
                throw new FileNotFoundException(string.Format("Could not find {0}.", filePath));
            }
            if (Path.GetExtension(filePath).Trim().ToLower() != ICALENDAR_FILE_EXTENSION)
            {
                throw new ArgumentException(string.Format(
                                                "Invalid file extension on iCalendar file. Expected file extension of '{0}'.",
                                                ICALENDAR_FILE_EXTENSION));
            }
            string   calendarText = File.ReadAllText(filePath);
            Calendar calendar     = Calendar.Load(calendarText);

            foreach (CalendarEvent e in calendar.Events)
            {
                if (e.Class.ToLower() == PUBLIC_HOLIDAY_EVENT_CLASS_NAME) //This is a public holiday event.
                {
                    string      name        = e.Name;
                    string      eventName   = e.Summary;
                    string      description = e.Description;
                    CalDateTime startDate   = e.Start as CalDateTime;
                    CalDateTime endDate     = e.End as CalDateTime;
                    if (!startDate.HasDate || !endDate.HasDate)
                    {
                        continue;
                    }
                    result.PublicHolidays.Add(new ICalPublicHoliday(eventName, startDate.Year, startDate.Month, startDate.Day));
                }
            }
            return(result);
        }
コード例 #2
0
        /// <summary>
        /// The country code, start date and end date will be applied to the download URL of this ICalCalendarDownloader.
        /// The Country Name is only used for display purposes when constructing the resulting the ICalCalendar.
        /// If the output file name is not specified, a temp file path will be generated in the current user's temp folder.
        /// Country codes should be supplied as ISO 3166-1 alpha-3 Standard: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3
        /// </summary>
        /// <returns></returns>
        public ICalCalendar DownloadICalCalendar(
            string countryCode,
            string countryName,
            string startDate,
            string endDate,
            string outputFilePath,
            bool deleteOutputFileAfterParsing)
        {
            if (string.IsNullOrEmpty(countryCode))
            {
                throw new NullReferenceException(string.Format("Country Code may not be null or empty when downloading an {0}", typeof(ICalCalendar).Name));
            }
            if (string.IsNullOrEmpty(countryName))
            {
                throw new NullReferenceException(string.Format("Country Name may not be null or empty when downloading an {0}", typeof(ICalCalendar).Name));
            }
            if (string.IsNullOrEmpty(startDate))
            {
                throw new NullReferenceException(string.Format("Start Date may not be null or empty when downloading an {0}", typeof(ICalCalendar).Name));
            }
            if (string.IsNullOrEmpty(endDate))
            {
                throw new NullReferenceException(string.Format("End Date may not be null or empty when downloading an {0}", typeof(ICalCalendar).Name));
            }
            if (string.IsNullOrEmpty(outputFilePath))
            {
                string outputFileName = string.Format("{0}-{1}-{2}{3}", countryCode, startDate, endDate, ICalPublicHolidayParser.ICALENDAR_FILE_EXTENSION);
                outputFilePath = Path.Combine(Path.GetTempPath(), outputFileName);
            }
            using (WebClient webClient = new WebClient())
            {
                string formattedDownloadUrl = string.Format(_downloadUrl, countryCode, startDate, endDate);
                if (File.Exists(outputFilePath))
                {
                    File.Delete(outputFilePath);
                }
                webClient.DownloadFile(formattedDownloadUrl, outputFilePath);
            }
            ICalCalendar result = ICalPublicHolidayParser.ParseICalendarFile(outputFilePath, countryCode, countryName);

            if (deleteOutputFileAfterParsing)
            {
                File.Delete(outputFilePath);
            }
            return(result);
        }