Lidarr/src/Lidarr.Api.V1/Calendar/CalendarFeedController.cs

80 lines
2.9 KiB
C#
Raw Normal View History

2017-09-04 02:20:56 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using Ical.Net;
using Ical.Net.CalendarComponents;
2017-09-04 02:20:56 +00:00
using Ical.Net.DataTypes;
using Ical.Net.Serialization;
2021-08-04 20:42:40 +00:00
using Lidarr.Http;
using Microsoft.AspNetCore.Mvc;
2017-09-04 02:20:56 +00:00
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Music;
using NzbDrone.Core.Tags;
2017-09-04 02:20:56 +00:00
2017-10-31 01:28:29 +00:00
namespace Lidarr.Api.V1.Calendar
2017-09-04 02:20:56 +00:00
{
2021-08-04 20:42:40 +00:00
[V1FeedController("calendar")]
public class CalendarFeedController : Controller
2017-09-04 02:20:56 +00:00
{
private readonly IAlbumService _albumService;
private readonly IArtistService _artistService;
2017-09-04 02:20:56 +00:00
private readonly ITagService _tagService;
2021-08-04 20:42:40 +00:00
public CalendarFeedController(IAlbumService albumService, IArtistService artistService, ITagService tagService)
2017-09-04 02:20:56 +00:00
{
_albumService = albumService;
_artistService = artistService;
2017-09-04 02:20:56 +00:00
_tagService = tagService;
}
2021-08-04 20:42:40 +00:00
[HttpGet("Lidarr.ics")]
public IActionResult GetCalendarFeed(int pastDays = 7, int futureDays = 28, string tagList = "", bool unmonitored = false)
2017-09-04 02:20:56 +00:00
{
var start = DateTime.Today.AddDays(-pastDays);
var end = DateTime.Today.AddDays(futureDays);
var tags = new List<int>();
2021-08-04 20:42:40 +00:00
if (tagList.IsNotNullOrWhiteSpace())
2017-09-04 02:20:56 +00:00
{
2021-08-04 20:42:40 +00:00
tags.AddRange(tagList.Split(',').Select(_tagService.GetTag).Select(t => t.Id));
2017-09-04 02:20:56 +00:00
}
var albums = _albumService.AlbumsBetweenDates(start, end, unmonitored);
2017-09-04 02:20:56 +00:00
var calendar = new Ical.Net.Calendar
{
ProductId = "-//lidarr.audio//Lidarr//EN"
2017-09-04 02:20:56 +00:00
};
var calendarName = "Lidarr Music Schedule";
calendar.AddProperty(new CalendarProperty("NAME", calendarName));
calendar.AddProperty(new CalendarProperty("X-WR-CALNAME", calendarName));
2017-09-04 02:20:56 +00:00
foreach (var album in albums.OrderBy(v => v.ReleaseDate.Value))
2017-09-04 02:20:56 +00:00
{
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))
2017-09-04 02:20:56 +00:00
{
continue;
}
2019-09-17 20:52:38 +00:00
var occurrence = calendar.Create<CalendarEvent>();
occurrence.Uid = "Lidarr_album_" + album.Id;
// occurrence.Status = album.HasFile ? EventStatus.Confirmed : EventStatus.Tentative;
2019-09-17 20:52:38 +00:00
occurrence.Description = album.Overview;
occurrence.Categories = album.Genres;
2017-09-04 02:20:56 +00:00
occurrence.Start = new CalDateTime(album.ReleaseDate.Value.ToLocalTime()) { HasTime = false };
2017-09-04 02:20:56 +00:00
occurrence.Summary = $"{artist.Name} - {album.Title}";
2017-09-04 02:20:56 +00:00
}
var serializer = (IStringSerializer)new SerializerFactory().Build(calendar.GetType(), new SerializationContext());
var icalendar = serializer.SerializeToString(calendar);
2021-08-04 20:42:40 +00:00
return Content(icalendar, "text/calendar");
2017-09-04 02:20:56 +00:00
}
}
}