public ActionResult ICalendar(string shortcut, Guid hallID, string accessToken) { var iCal = new iCalendar(); var studio = _context.Photostudios.Single(x => x.Shortcut == shortcut); var hall = studio.Halls.Single(x => x.ID == hallID); foreach (var @event in hall.Calendar.Events) { var evt = iCal.Create <DDay.iCal.Event>(); // Set information about the event evt.Start = new iCalDateTime(@event.Start); evt.End = new iCalDateTime(@event.End); // This also sets the duration evt.Description = @event.Description; evt.Location = hall.Photostudio.Adress; evt.Summary = "18 hour event summary"; evt.UID = @event.ID.ToString(); } // Create a serialization context and serializer factory. // These will be used to build the serializer for our object. ISerializationContext ctx = new SerializationContext(); ISerializerFactory factory = new DDay.iCal.Serialization.iCalendar.SerializerFactory(); // Get a serializer for our object IStringSerializer serializer = factory.Build(iCal.GetType(), ctx) as IStringSerializer; string output = serializer.SerializeToString(iCal); var contentType = "text/calendar"; var bytes = Encoding.UTF8.GetBytes(output); return(File(bytes, contentType, hallID + ".ics")); }
public byte[] ICalSerializeToBytes(iCalendar iCal, string filename) { ISerializationContext ctx = new SerializationContext(); ISerializerFactory factory = new SerializerFactory(); IStringSerializer serializer = factory.Build(iCal.GetType(), ctx) as IStringSerializer; string output = serializer.SerializeToString(iCal); return(Encoding.UTF8.GetBytes(output)); }
private Response GetCalendarFeed() { var start = DateTime.Today.AddDays(-7); var end = DateTime.Today.AddDays(28); var queryStart = Request.Query.Start; var queryEnd = Request.Query.End; if (queryStart.HasValue) { start = DateTime.Parse(queryStart.Value); } if (queryEnd.HasValue) { end = DateTime.Parse(queryEnd.Value); } var episodes = _episodeService.EpisodesBetweenDates(start, end); var icalCalendar = new iCalendar(); foreach (var episode in episodes.OrderBy(v => v.AirDateUtc.Value)) { var occurrence = icalCalendar.Create <Event>(); occurrence.UID = "NzbDrone_episode_" + episode.Id.ToString(); occurrence.Status = episode.HasFile ? EventStatus.Confirmed : EventStatus.Tentative; occurrence.Start = new iCalDateTime(episode.AirDateUtc.Value) { HasTime = true }; occurrence.End = new iCalDateTime(episode.AirDateUtc.Value.AddMinutes(episode.Series.Runtime)) { HasTime = true }; occurrence.Description = episode.Overview; occurrence.Categories = new List <string>() { episode.Series.Network }; switch (episode.Series.SeriesType) { case SeriesTypes.Daily: occurrence.Summary = string.Format("{0} - {1}", episode.Series.Title, episode.Title); break; default: occurrence.Summary = string.Format("{0} - {1}x{2:00} - {3}", episode.Series.Title, episode.SeasonNumber, episode.EpisodeNumber, episode.Title); break; } } var serializer = new DDay.iCal.Serialization.iCalendar.SerializerFactory().Build(icalCalendar.GetType(), new DDay.iCal.Serialization.SerializationContext()) as DDay.iCal.Serialization.IStringSerializer; var icalendar = serializer.SerializeToString(icalCalendar); return(new TextResponse(icalendar, "text/calendar")); }
/// <summary> /// Function is called to create the course ics file /// Function is called everytime the course calendar needs to be updated /// </summary> /// <param name="id"></param> public void CreateCourseCalendar(int id) { //Get the Course var course = (from d in db.Courses where d.ID == id select new { d.StartDate, d.EndDate, d.Prefix, d.Number, d.Semester, d.Year, d.CourseMeetings, d.CourseBreaks, d.ID }).FirstOrDefault(); //get course events var events = (from e in db.Events where e.Poster.AbstractCourseID == ActiveCourseUser.AbstractCourseID && e.StartDate >= course.StartDate && e.StartDate <= course.EndDate && e.Approved select new { e.StartDate, e.EndDate, e.Title, e.Description }).ToList(); //Create the calendar object iCalendar courseCalendar = new iCalendar(); //initalize the Calendar object courseCalendar.AddTimeZone(DateTimeExtensions.GetTimeZone(((Course)ActiveCourseUser.AbstractCourse).TimeZoneOffset)); courseCalendar.Method = "PUBLISH"; courseCalendar.Name = "VCALENDAR"; courseCalendar.Version = "2.0"; courseCalendar.ProductID = "-//Washington State University//PLUS.OSBLE.org//EN"; courseCalendar.Scale = "GREGORIAN"; courseCalendar.AddProperty("X-WR-CALNAME", course.Prefix + "-" + course.Number + "-" + course.Semester + "-" + course.Year); //get course breaks if (ActiveCourseUser.AbstractCourse is Course && ((ActiveCourseUser.AbstractCourse as Course).ShowMeetings == true)) { foreach (CourseMeeting cm in course.CourseMeetings) { StringBuilder rpPattern = new StringBuilder("FREQ=WEEKLY;UNTIL="); rpPattern.Append(new iCalDateTime(course.EndDate.UTCToCourse(ActiveCourseUser.AbstractCourseID)).ToString(@"yyyyMMdd\THHmmss\Z")); rpPattern.Append(";WKST=SU;BYDAY="); if (cm.Sunday) { rpPattern.Append("SU,"); } if (cm.Monday) { rpPattern.Append("MO,"); } if (cm.Tuesday) { rpPattern.Append("TU,"); } if (cm.Wednesday) { rpPattern.Append("WE,"); } if (cm.Thursday) { rpPattern.Append("TH,"); } if (cm.Friday) { rpPattern.Append("FR,"); } if (cm.Saturday) { rpPattern.Append("SA"); } //trim trailing comma if it is there if (rpPattern[rpPattern.Length - 1] == ',') { rpPattern.Remove(rpPattern.Length - 1, 1); } RecurringComponent recurringComponent = new RecurringComponent(); RecurrencePattern pattern = new RecurrencePattern(rpPattern.ToString()); DDay.iCal.Event evt = courseCalendar.Create <DDay.iCal.Event>(); //may cause issues DateTime evtStart = DateTime.SpecifyKind(course.StartDate, DateTimeKind.Utc); evtStart = evtStart.Add(cm.StartTime.TimeOfDay); DateTime evtEnd = DateTime.SpecifyKind(course.StartDate, DateTimeKind.Utc); evtEnd = evtEnd.Add(cm.EndTime.TimeOfDay); //we need to test if right NOW is in DST. //case: course starts during DST, and it's now after DST, result: after DST event is now wrong. DateTime currentTime = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, DateTime.UtcNow.Day, evtStart.Hour, evtStart.Minute, evtStart.Second, DateTimeKind.Utc); bool isDaylight = TimeZoneInfo.Local.IsDaylightSavingTime(currentTime.UTCToCourse(ActiveCourseUser.AbstractCourseID)); if (isDaylight) { evt.Start = new iCalDateTime(evtStart.UTCToCourse(ActiveCourseUser.AbstractCourseID)); evt.End = new iCalDateTime(evtEnd.UTCToCourse(ActiveCourseUser.AbstractCourseID)); evt.LastModified = new iCalDateTime(DateTime.Now); evt.Summary = cm.Name; evt.Location = cm.Location; evt.RecurrenceRules.Add(pattern); } else //no longer DST { bool matchingStartDay = false; /* * if there happens to be a "meeting times" in the database without a day of the week associated with * the meeting, we need to skip the while and switch case, otherwise it's an infinite loop! */ bool meetingTimeWithoutDaysOfWeek = cm.Sunday || cm.Monday || cm.Tuesday || cm.Wednesday || cm.Thursday || cm.Friday || cm.Saturday ? false : true; int count = 0; //infinite loop protection x2! //WHILE matchingStartDay is false (default false) AND the meeting time has at least one day of the week //AND if we've hit a count of 7, that means we've already checked each day of the week and will never find one! //exit while if we fail either of these conditions! while ((!matchingStartDay && !meetingTimeWithoutDaysOfWeek) && count < 7) { switch (currentTime.DayOfWeek) //find which day of the week for this meeting { case DayOfWeek.Monday: if (cm.Monday) { matchingStartDay = true; } break; case DayOfWeek.Tuesday: if (cm.Tuesday) { matchingStartDay = true; } break; case DayOfWeek.Wednesday: if (cm.Wednesday) { matchingStartDay = true; } break; case DayOfWeek.Thursday: if (cm.Thursday) { matchingStartDay = true; } break; case DayOfWeek.Friday: if (cm.Friday) { matchingStartDay = true; } break; case DayOfWeek.Saturday: if (cm.Saturday) { matchingStartDay = true; } break; case DayOfWeek.Sunday: if (cm.Sunday) { matchingStartDay = true; } break; default: matchingStartDay = false; break; } if (!matchingStartDay) { currentTime = new DateTime(currentTime.Year, currentTime.Month, currentTime.AddDays(1).Day, evtStart.Hour, evtStart.Minute, evtStart.Second, DateTimeKind.Utc); } count++; //infinite loop protection x2! if we've reached 7 we've cycled all weekdays, so get us out of here! } //proceed only if there was a day of the week found for this course meeting time if (!meetingTimeWithoutDaysOfWeek && matchingStartDay) { evt.Start = new iCalDateTime(currentTime.UTCToCourse(ActiveCourseUser.AbstractCourseID)); evt.End = new iCalDateTime(evtEnd.UTCToCourse(ActiveCourseUser.AbstractCourseID)); evt.LastModified = new iCalDateTime(DateTime.Now); evt.Summary = cm.Name; evt.Location = cm.Location; evt.RecurrenceRules.Add(pattern); } } } //create the course breaks foreach (CourseBreak cb in course.CourseBreaks) { DDay.iCal.Event evt = courseCalendar.Create <DDay.iCal.Event>(); DateTime evtStart = cb.StartDate.Date; DateTime evtEnd = cb.EndDate.Date.AddDays(1); evt.Summary = cb.Name; evt.Start = new iCalDateTime(evtStart); evt.End = new iCalDateTime(evtEnd); evt.LastModified = new iCalDateTime(DateTime.Now); } }//end if //add all the events to the calendar foreach (var e in events) { DDay.iCal.Event evt = courseCalendar.Create <DDay.iCal.Event>(); evt.Start = new iCalDateTime(e.StartDate.UTCToCourse(ActiveCourseUser.AbstractCourseID)); if (e.EndDate == null) { evt.End = new iCalDateTime(e.StartDate.UTCToCourse(ActiveCourseUser.AbstractCourseID)); evt.End.AddMinutes(1); } else { evt.End = new iCalDateTime(((DateTime)e.EndDate).UTCToCourse(ActiveCourseUser.AbstractCourseID)); } evt.Summary = e.Title; if (e.Description != null) { evt.Description = e.Description; } } // Create a serialization context and serializer factory. // These will be used to build the serializer for our object. ISerializationContext ctx = new SerializationContext(); ISerializerFactory factory = new DDay.iCal.Serialization.iCalendar.SerializerFactory(); // Get a serializer for our object IStringSerializer serializer = factory.Build(courseCalendar.GetType(), ctx) as IStringSerializer; string output = serializer.SerializeToString(courseCalendar); var bytes = Encoding.UTF8.GetBytes(output); SaveCourseCalendar(bytes, course.ID); }
private Response GetCalendarFeed() { var pastDays = 7; var futureDays = 28; var start = DateTime.Today.AddDays(-pastDays); var end = DateTime.Today.AddDays(futureDays); var unmonitored = false; var premiersOnly = false; var tags = new List <int>(); // TODO: Remove start/end parameters in v3, they don't work well for iCal var queryStart = Request.Query.Start; var queryEnd = Request.Query.End; var queryPastDays = Request.Query.PastDays; var queryFutureDays = Request.Query.FutureDays; var queryUnmonitored = Request.Query.Unmonitored; var queryPremiersOnly = Request.Query.PremiersOnly; var queryTags = Request.Query.Tags; if (queryStart.HasValue) { start = DateTime.Parse(queryStart.Value); } if (queryEnd.HasValue) { end = DateTime.Parse(queryEnd.Value); } if (queryPastDays.HasValue) { pastDays = int.Parse(queryPastDays.Value); start = DateTime.Today.AddDays(-pastDays); } if (queryFutureDays.HasValue) { futureDays = int.Parse(queryFutureDays.Value); end = DateTime.Today.AddDays(futureDays); } if (queryUnmonitored.HasValue) { unmonitored = bool.Parse(queryUnmonitored.Value); } if (queryPremiersOnly.HasValue) { premiersOnly = bool.Parse(queryPremiersOnly.Value); } if (queryTags.HasValue) { var tagInput = (string)queryTags.Value.ToString(); tags.AddRange(tagInput.Split(',').Select(_tagService.GetTag).Select(t => t.Id)); } var episodes = _episodeService.EpisodesBetweenDates(start, end, unmonitored); var icalCalendar = new iCalendar(); foreach (var episode in episodes.OrderBy(v => v.AirDateUtc.Value)) { if (premiersOnly && (episode.SeasonNumber == 0 || episode.EpisodeNumber != 1)) { continue; } if (tags.Any() && tags.None(episode.Series.Tags.Contains)) { continue; } var occurrence = icalCalendar.Create <Event>(); occurrence.UID = "NzbDrone_episode_" + episode.Id.ToString(); occurrence.Status = episode.HasFile ? EventStatus.Confirmed : EventStatus.Tentative; occurrence.Start = new iCalDateTime(episode.AirDateUtc.Value) { HasTime = true }; occurrence.End = new iCalDateTime(episode.AirDateUtc.Value.AddMinutes(episode.Series.Runtime)) { HasTime = true }; occurrence.Description = episode.Overview; occurrence.Categories = new List <string>() { episode.Series.Network }; switch (episode.Series.SeriesType) { case SeriesTypes.Daily: occurrence.Summary = string.Format("{0} - {1}", episode.Series.Title, episode.Title); break; default: occurrence.Summary = string.Format("{0} - {1}x{2:00} - {3}", episode.Series.Title, episode.SeasonNumber, episode.EpisodeNumber, episode.Title); break; } } var serializer = new DDay.iCal.Serialization.iCalendar.SerializerFactory().Build(icalCalendar.GetType(), new DDay.iCal.Serialization.SerializationContext()) as DDay.iCal.Serialization.IStringSerializer; var icalendar = serializer.SerializeToString(icalCalendar); return(new TextResponse(icalendar, "text/calendar")); }