Exemple #1
0
        private void CreateEvent(Ical.Net.Calendar calendar, Movie movie, bool cinemasRelease)
        {
            var date = cinemasRelease ? movie.InCinemas : movie.PhysicalRelease;

            if (!date.HasValue)
            {
                return;
            }

            var occurrence = calendar.Create <CalendarEvent>();

            occurrence.Uid    = "NzbDrone_movie_" + movie.Id + (cinemasRelease ? "_cinemas" : "_physical");
            occurrence.Status = movie.Status == MovieStatusType.Announced ? EventStatus.Tentative : EventStatus.Confirmed;

            occurrence.Start    = new CalDateTime(date.Value);
            occurrence.End      = occurrence.Start;
            occurrence.IsAllDay = true;

            occurrence.Description = movie.Overview;
            occurrence.Categories  = new List <string>()
            {
                movie.Studio
            };

            var physicalText = "(Physical Release)";

            occurrence.Summary = $"{movie.Title} " + (cinemasRelease ? "(Theatrical Release)" : physicalText);
        }
Exemple #2
0
        public IActionResult GetCalendarFeed(int pastDays = 7, int futureDays = 28, string tagList = "", bool unmonitored = false)
        {
            var start = DateTime.Today.AddDays(-pastDays);
            var end   = DateTime.Today.AddDays(futureDays);
            var tags  = new List <int>();

            if (tagList.IsNotNullOrWhiteSpace())
            {
                tags.AddRange(tagList.Split(',').Select(_tagService.GetTag).Select(t => t.Id));
            }

            var books    = _bookService.BooksBetweenDates(start, end, unmonitored);
            var calendar = new Ical.Net.Calendar
            {
                ProductId = "-//readarr.com//Readarr//EN"
            };

            var calendarName = "Readarr Book Schedule";

            calendar.AddProperty(new CalendarProperty("NAME", calendarName));
            calendar.AddProperty(new CalendarProperty("X-WR-CALNAME", calendarName));

            foreach (var book in books.OrderBy(v => v.ReleaseDate.Value))
            {
                var author = _authorService.GetAuthor(book.AuthorId); // Temp fix TODO: Figure out why Book.Author is not populated during BooksBetweenDates Query

                if (tags.Any() && tags.None(author.Tags.Contains))
                {
                    continue;
                }

                var occurrence = calendar.Create <CalendarEvent>();
                occurrence.Uid = "Readarr_book_" + book.Id;

                //occurrence.Status = book.HasFile ? EventStatus.Confirmed : EventStatus.Tentative;
                occurrence.Description = book.Editions.Value.Single(x => x.Monitored).Overview;
                occurrence.Categories  = book.Genres;

                occurrence.Start = new CalDateTime(book.ReleaseDate.Value.ToLocalTime())
                {
                    HasTime = false
                };

                occurrence.Summary = $"{author.Name} - {book.Title}";
            }

            var serializer = (IStringSerializer) new SerializerFactory().Build(calendar.GetType(), new SerializationContext());
            var icalendar  = serializer.SerializeToString(calendar);

            return(Content(icalendar, "text/calendar"));
        }
Exemple #3
0
        private void CreateEvent(Ical.Net.Calendar calendar, Movie movie, string releaseType)
        {
            var    date        = movie.InCinemas;
            string eventType   = "_cinemas";
            string summaryText = "(Theatrical Release)";

            if (releaseType == "digital")
            {
                date        = movie.DigitalRelease;
                eventType   = "_digital";
                summaryText = "(Digital Release)";
            }
            else if (releaseType == "physical")
            {
                date        = movie.PhysicalRelease;
                eventType   = "_physical";
                summaryText = "(Physical Release)";
            }

            if (!date.HasValue)
            {
                return;
            }

            var occurrence = calendar.Create <CalendarEvent>();

            occurrence.Uid    = "Radarr_movie_" + movie.Id + eventType;
            occurrence.Status = movie.Status == MovieStatusType.Announced ? EventStatus.Tentative : EventStatus.Confirmed;

            occurrence.Start    = new CalDateTime(date.Value);
            occurrence.End      = occurrence.Start;
            occurrence.IsAllDay = true;

            occurrence.Description = movie.Overview;
            occurrence.Categories  = new List <string>()
            {
                movie.Studio
            };

            occurrence.Summary = $"{movie.Title} " + summaryText;
        }
        private object GetCalendarFeed()
        {
            var pastDays    = 7;
            var futureDays  = 28;
            var start       = DateTime.Today.AddDays(-pastDays);
            var end         = DateTime.Today.AddDays(futureDays);
            var unmonitored = Request.GetBooleanQueryParameter("unmonitored");
            var tags        = new List <int>();

            var queryPastDays   = Request.Query.PastDays;
            var queryFutureDays = Request.Query.FutureDays;
            var queryTags       = Request.Query.Tags;

            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 (queryTags.HasValue)
            {
                var tagInput = (string)queryTags.Value.ToString();
                tags.AddRange(tagInput.Split(',').Select(_tagService.GetTag).Select(t => t.Id));
            }

            var books    = _bookService.BooksBetweenDates(start, end, unmonitored);
            var calendar = new Ical.Net.Calendar
            {
                ProductId = "-//readarr.com//Readarr//EN"
            };

            var calendarName = "Readarr Book Schedule";

            calendar.AddProperty(new CalendarProperty("NAME", calendarName));
            calendar.AddProperty(new CalendarProperty("X-WR-CALNAME", calendarName));

            foreach (var book in books.OrderBy(v => v.ReleaseDate.Value))
            {
                var author = _authorService.GetAuthor(book.AuthorId); // Temp fix TODO: Figure out why Album.Artist is not populated during AlbumsBetweenDates Query

                if (tags.Any() && tags.None(author.Tags.Contains))
                {
                    continue;
                }

                var occurrence = calendar.Create <CalendarEvent>();
                occurrence.Uid = "Readarr_book_" + book.Id;

                //occurrence.Status = album.HasFile ? EventStatus.Confirmed : EventStatus.Tentative;
                occurrence.Description = book.Editions.Value.Single(x => x.Monitored).Overview;
                occurrence.Categories  = book.Genres;

                occurrence.Start = new CalDateTime(book.ReleaseDate.Value.ToLocalTime())
                {
                    HasTime = false
                };

                occurrence.Summary = $"{author.Name} - {book.Title}";
            }

            var serializer = (IStringSerializer) new SerializerFactory().Build(calendar.GetType(), new SerializationContext());
            var icalendar  = serializer.SerializeToString(calendar);

            return(new TextResponse(icalendar, "text/calendar"));
        }
        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 movies   = _movieService.GetMoviesBetweenDates(start, end, unmonitored);
            var calendar = new Ical.Net.Calendar
            {
                ProductId = "-//radarr.video//Bonarr//EN"
            };

            foreach (var movie in movies.OrderBy(v => v.Added))
            {
                if (tags.Any() && tags.None(movie.Tags.Contains))
                {
                    continue;
                }

                var occurrence = calendar.Create <Event>();
                occurrence.Uid    = "NzbDrone_movie_" + movie.Id;
                occurrence.Status = movie.HasFile ? EventStatus.Confirmed : EventStatus.Tentative;

                switch (movie.Status)
                {
                case MovieStatusType.PreDB:
                    if (movie.PhysicalRelease != null)
                    {
                        occurrence.Start = new CalDateTime(movie.PhysicalRelease.Value)
                        {
                            HasTime = true
                        };
                        occurrence.End = new CalDateTime(movie.PhysicalRelease.Value.AddMinutes(movie.Runtime))
                        {
                            HasTime = true
                        };
                    }
                    break;

                case MovieStatusType.InCinemas:
                    if (movie.InCinemas != null)
                    {
                        occurrence.Start = new CalDateTime(movie.InCinemas.Value)
                        {
                            HasTime = true
                        };
                        occurrence.End = new CalDateTime(movie.InCinemas.Value.AddMinutes(movie.Runtime))
                        {
                            HasTime = true
                        };
                    }
                    break;

                case MovieStatusType.Announced:
                    continue;     // no date

                default:
                    if (movie.PhysicalRelease != null)
                    {
                        occurrence.Start = new CalDateTime(movie.PhysicalRelease.Value)
                        {
                            HasTime = true
                        };
                        occurrence.End = new CalDateTime(movie.PhysicalRelease.Value.AddMinutes(movie.Runtime))
                        {
                            HasTime = true
                        };
                    }
                    break;
                }

                occurrence.Description = movie.Overview;
                occurrence.Categories  = new List <string>()
                {
                    movie.Studio
                };

                occurrence.Summary = $"{movie.Title}";
            }

            var serializer = (IStringSerializer) new SerializerFactory().Build(calendar.GetType(), new SerializationContext());
            var icalendar  = serializer.SerializeToString(calendar);

            return(new TextResponse(icalendar, "text/calendar"));
        }
Exemple #6
0
        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 asAllDay     = 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 queryAsAllDay     = Request.Query.AsAllDay;
            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 (queryAsAllDay.HasValue)
            {
                asAllDay = bool.Parse(queryAsAllDay.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 calendar = new Ical.Net.Calendar
            {
                ProductId = "-//sonarr.tv//Sonarr//EN"
            };



            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 = calendar.Create <Event>();
                occurrence.Uid         = "NzbDrone_episode_" + episode.Id;
                occurrence.Status      = episode.HasFile ? EventStatus.Confirmed : EventStatus.Tentative;
                occurrence.Description = episode.Overview;
                occurrence.Categories  = new List <string>()
                {
                    episode.Series.Network
                };

                if (asAllDay)
                {
                    occurrence.Start = new CalDateTime(episode.AirDateUtc.Value)
                    {
                        HasTime = false
                    };
                }
                else
                {
                    occurrence.Start = new CalDateTime(episode.AirDateUtc.Value)
                    {
                        HasTime = true
                    };
                    occurrence.End = new CalDateTime(episode.AirDateUtc.Value.AddMinutes(episode.Series.Runtime))
                    {
                        HasTime = true
                    };
                }

                switch (episode.Series.SeriesType)
                {
                case SeriesTypes.Daily:
                    occurrence.Summary = $"{episode.Series.Title} - {episode.Title}";
                    break;

                default:
                    occurrence.Summary = $"{episode.Series.Title} - {episode.SeasonNumber}x{episode.EpisodeNumber:00} - {episode.Title}";
                    break;
                }
            }

            var serializer = (IStringSerializer) new SerializerFactory().Build(calendar.GetType(), new SerializationContext());
            var icalendar  = serializer.SerializeToString(calendar);

            return(new TextResponse(icalendar, "text/calendar"));
        }
Exemple #7
0
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        try
        {
            if (!FileUpload1.HasFile)
            {
                throw new MyFlightbookException("Please provide a file.");
            }

            using (Calendar ic = new Calendar())
            {
                using (CSVReader cr = new CSVReader(FileUpload1.FileContent))
                {
                    string[] rgCols = cr.GetCSVLine(true);

                    int iColSubject   = -1;
                    int iColStartDate = -1;
                    int iColStartTime = -1;
                    int iColEndDate   = -1;
                    int iColEndTime   = -1;
                    int iColLocation  = -1;

                    for (int i = 0; i < rgCols.Length; i++)
                    {
                        switch (rgCols[i])
                        {
                        case "Subject":
                            iColSubject = i;
                            break;

                        case "Start Date":
                            iColStartDate = i;
                            break;

                        case "Start Time":
                            iColStartTime = i;
                            break;

                        case "End Date":
                            iColEndDate = i;
                            break;

                        case "End Time":
                            iColEndTime = i;
                            break;

                        case "Location":
                            iColLocation = i;
                            break;
                        }
                    }

                    if (iColSubject < 0)
                    {
                        throw new MyFlightbookException("No subject column found");
                    }
                    if (iColStartDate < 0)
                    {
                        throw new MyFlightbookException("No start date column found");
                    }
                    if (iColStartTime < 0)
                    {
                        throw new MyFlightbookException("No start time column found");
                    }
                    if (iColEndDate < 0)
                    {
                        throw new MyFlightbookException("No end date column found");
                    }
                    if (iColEndTime < 0)
                    {
                        throw new MyFlightbookException("No end time column found");
                    }
                    if (iColLocation < 0)
                    {
                        throw new MyFlightbookException("No location column found");
                    }

                    int id = 0;
                    while ((rgCols = cr.GetCSVLine()) != null)
                    {
                        if (String.IsNullOrEmpty(rgCols[iColSubject]))
                        {
                            continue;
                        }

                        Event ev = ic.Create <Event>();
                        ev.Uid         = id++.ToString(System.Globalization.CultureInfo.InvariantCulture);
                        ev.IsAllDay    = false;
                        ev.Description = ev.Summary = rgCols[iColSubject];
                        ev.Location    = rgCols[iColLocation];

                        DateTime dtStart = Convert.ToDateTime(String.Format(System.Globalization.CultureInfo.CurrentCulture, "{0} {1}", rgCols[iColStartDate], rgCols[iColStartTime]), System.Globalization.CultureInfo.CurrentCulture);
                        DateTime dtEnd   = Convert.ToDateTime(String.Format(System.Globalization.CultureInfo.CurrentCulture, "{0} {1}", rgCols[iColEndDate], rgCols[iColEndTime]), System.Globalization.CultureInfo.CurrentCulture);
                        ev.Start = new CalDateTime(dtStart, "Pacific Standard Time");
                        ev.End   = new CalDateTime(dtEnd, "Pacific Standard Time");

                        Alarm a = new Alarm();
                        a.Action           = AlarmAction.Display;
                        a.Description      = ev.Summary;
                        a.Trigger          = new Trigger();
                        a.Trigger.DateTime = ev.Start.AddMinutes(-30);
                        ev.Alarms.Add(a);

                        ic.Method = "PUBLISH";
                    }

                    CalendarSerializer s = new CalendarSerializer();

                    string output = s.SerializeToString(ic);
                    Page.Response.Clear();
                    Page.Response.ContentType = "text/calendar";
                    Response.AddHeader("Content-Disposition", String.Format(System.Globalization.CultureInfo.InvariantCulture, "inline;filename={0}.ics", txtTitle.Text));
                    Response.Write(output);
                    Response.Flush();
                    Response.End();
                }
            }
        }
        catch (CSVReaderInvalidCSVException ex)
        {
            lblErr.Text = ex.Message;
        }
        catch (MyFlightbookException ex)
        {
            lblErr.Text = ex.Message;
        }
    }
Exemple #8
0
        private Response GetCalendarFeed()
        {
            var pastDays    = 7;
            var futureDays  = 28;
            var start       = DateTime.Today.AddDays(-pastDays);
            var end         = DateTime.Today.AddDays(futureDays);
            var unmonitored = Request.GetBooleanQueryParameter("unmonitored");
            var tags        = new List <int>();

            var queryPastDays   = Request.Query.PastDays;
            var queryFutureDays = Request.Query.FutureDays;
            var queryTags       = Request.Query.Tags;

            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 (queryTags.HasValue)
            {
                var tagInput = (string)queryTags.Value.ToString();
                tags.AddRange(tagInput.Split(',').Select(_tagService.GetTag).Select(t => t.Id));
            }

            var albums   = _albumService.AlbumsBetweenDates(start, end, unmonitored);
            var calendar = new Ical.Net.Calendar
            {
                ProductId = "-//lidarr.audio//Lidarr//EN"
            };

            var calendarName = "Lidarr Music Schedule";

            calendar.AddProperty(new CalendarProperty("NAME", calendarName));
            calendar.AddProperty(new CalendarProperty("X-WR-CALNAME", calendarName));

            foreach (var album in albums.OrderBy(v => v.ReleaseDate.Value))
            {
                var artist = _artistService.GetArtist(album.ArtistId); // Temp fix TODO: Figure out why Album.Artist is not populated during AlbumsBetweenDates Query

                if (tags.Any() && tags.None(artist.Tags.Contains))
                {
                    continue;
                }

                var occurrence = calendar.Create <Event>();
                occurrence.Uid = "NzbDrone_album_" + album.Id;
                //occurrence.Status = album.HasFile ? EventStatus.Confirmed : EventStatus.Tentative;
                //occurrence.Description = album.Overview;
                //occurrence.Categories = new List<string>() { album.Series.Network };

                occurrence.Start = new CalDateTime(album.ReleaseDate.Value.ToLocalTime())
                {
                    HasTime = false
                };

                occurrence.Summary = $"{artist.Name} - {album.Title}";
            }

            var serializer = (IStringSerializer) new SerializerFactory().Build(calendar.GetType(), new SerializationContext());
            var icalendar  = serializer.SerializeToString(calendar);

            return(new TextResponse(icalendar, "text/calendar"));
        }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!Page.User.Identity.IsAuthenticated)
            {
                throw new MyFlightbookException("Unauthorized!");
            }

            int idClub = util.GetIntParam(Request, "c", 0);
            if (idClub == 0)
            {
                throw new MyFlightbookException("Invalid club");
            }

            Club c = Club.ClubWithID(idClub);

            if (c == null)
            {
                throw new MyFlightbookException(String.Format(System.Globalization.CultureInfo.InvariantCulture, "Invalid club: {0}", idClub));
            }

            string   szIDs  = util.GetStringParam(Request, "sid");
            string[] rgSIDs = szIDs.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            if (rgSIDs.Length == 0)
            {
                throw new MyFlightbookException("No scheduled events to download specified");
            }

            bool fIsAdmin = c.HasAdmin(Page.User.Identity.Name);

            using (Ical.Net.Calendar ic = new Ical.Net.Calendar())
            {
                ic.AddTimeZone(new VTimeZone(c.TimeZone.Id));

                string szTitle = string.Empty;

                foreach (string sid in rgSIDs)
                {
                    ScheduledEvent se = ScheduledEvent.AppointmentByID(sid, c.TimeZone);

                    if (se == null)
                    {
                        throw new MyFlightbookException(String.Format(System.Globalization.CultureInfo.InvariantCulture, "Invalid scheduled event ID: {0}", sid));
                    }

                    if (!fIsAdmin && Page.User.Identity.Name.CompareOrdinal(se.OwningUser) != 0)
                    {
                        throw new MyFlightbookException("Attempt to download appointment that you don't own!");
                    }

                    ClubAircraft ca = c.MemberAircraft.FirstOrDefault(ca2 => ca2.AircraftID.ToString(System.Globalization.CultureInfo.InvariantCulture).CompareOrdinal(se.ResourceID) == 0);

                    Event ev = ic.Create <Event>();
                    ev.Uid           = se.ID;
                    ev.IsAllDay      = false;
                    ev.Start         = new CalDateTime(se.StartUtc, TimeZoneInfo.Utc.Id);
                    ev.End           = new CalDateTime(se.EndUtc, TimeZoneInfo.Utc.Id);
                    ev.Start.HasTime = ev.End.HasTime = true;   // has time is false if the ultimate time is midnight.
                    szTitle          = ev.Description = ev.Summary = String.Format(System.Globalization.CultureInfo.CurrentCulture, "{0}{1}", ca == null ? string.Empty : String.Format(System.Globalization.CultureInfo.CurrentCulture, "{0} - ", ca.DisplayTailnumber), se.Body);
                    ev.Location      = c.HomeAirport == null ? c.HomeAirportCode : String.Format(System.Globalization.CultureInfo.CurrentCulture, "{0} - {1}", c.HomeAirportCode, c.HomeAirport.Name);

                    Alarm a = new Alarm();
                    a.Action           = AlarmAction.Display;
                    a.Description      = ev.Summary;
                    a.Trigger          = new Trigger();
                    a.Trigger.DateTime = ev.Start.AddMinutes(-30);
                    ev.Alarms.Add(a);

                    ic.Method = "PUBLISH";
                }

                CalendarSerializer s = new CalendarSerializer();

                string output = s.SerializeToString(ic);
                Page.Response.Clear();
                Page.Response.ContentType = "text/calendar";
                Response.AddHeader("Content-Disposition", String.Format(System.Globalization.CultureInfo.InvariantCulture, "inline;filename={0}", Branding.ReBrand(String.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}appt.ics", szTitle)).Replace(" ", "-")));
                Response.Write(output);
                Response.Flush();
                Response.End();
            }
        }
    }
Exemple #10
0
        private object GetCalendarFeed()
        {
            var pastDays    = 7;
            var futureDays  = 28;
            var start       = DateTime.Today.AddDays(-pastDays);
            var end         = DateTime.Today.AddDays(futureDays);
            var unmonitored = Request.GetBooleanQueryParameter("unmonitored");
            // There was a typo, recognize both the correct 'premieresOnly' and mistyped 'premiersOnly' boolean for background compat.
            var premieresOnly = Request.GetBooleanQueryParameter("premieresOnly") || Request.GetBooleanQueryParameter("premiersOnly");
            var asAllDay      = Request.GetBooleanQueryParameter("asAllDay");
            var tags          = new List <int>();

            var queryPastDays   = Request.Query.PastDays;
            var queryFutureDays = Request.Query.FutureDays;
            var queryTags       = Request.Query.Tags;

            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 (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 calendar = new Ical.Net.Calendar
            {
                ProductId = "-//sonarr.tv//Sonarr//EN"
            };

            var calendarName = "Sonarr TV Schedule";

            calendar.AddProperty(new CalendarProperty("NAME", calendarName));
            calendar.AddProperty(new CalendarProperty("X-WR-CALNAME", calendarName));

            foreach (var episode in episodes.OrderBy(v => v.AirDateUtc.Value))
            {
                if (premieresOnly && (episode.SeasonNumber == 0 || episode.EpisodeNumber != 1))
                {
                    continue;
                }

                if (tags.Any() && tags.None(episode.Series.Tags.Contains))
                {
                    continue;
                }

                var occurrence = calendar.Create <Event>();
                occurrence.Uid         = "NzbDrone_episode_" + episode.Id;
                occurrence.Status      = episode.HasFile ? EventStatus.Confirmed : EventStatus.Tentative;
                occurrence.Description = episode.Overview;
                occurrence.Categories  = new List <string>()
                {
                    episode.Series.Network
                };

                if (asAllDay)
                {
                    occurrence.Start = new CalDateTime(episode.AirDateUtc.Value.ToLocalTime())
                    {
                        HasTime = false
                    };
                }
                else
                {
                    occurrence.Start = new CalDateTime(episode.AirDateUtc.Value)
                    {
                        HasTime = true
                    };
                    occurrence.End = new CalDateTime(episode.AirDateUtc.Value.AddMinutes(episode.Series.Runtime))
                    {
                        HasTime = true
                    };
                }

                switch (episode.Series.SeriesType)
                {
                case SeriesTypes.Daily:
                    occurrence.Summary = $"{episode.Series.Title} - {episode.Title}";
                    break;

                default:
                    occurrence.Summary = $"{episode.Series.Title} - {episode.SeasonNumber}x{episode.EpisodeNumber:00} - {episode.Title}";
                    break;
                }
            }

            var serializer = (IStringSerializer) new SerializerFactory().Build(calendar.GetType(), new SerializationContext());
            var icalendar  = serializer.SerializeToString(calendar);

            return(new TextResponse(icalendar, "text/calendar"));
        }