Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to keep the last episode/season for continuing shows #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion MediaCleaner/Configuration/PluginConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ public enum SeriesDeleteKind
Episode,
Season,
Series,
SeriesEnded
SeriesEnded,
EpisodeKeepLast,
SeasonKeepLast,
}

public enum LocationsListMode
Expand Down
65 changes: 53 additions & 12 deletions MediaCleaner/Filtering/SeriesFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Linq;
using MediaCleaner.Configuration;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Model.Entities;
using Microsoft.Extensions.Logging;

namespace MediaCleaner.Filtering;
Expand All @@ -26,6 +28,7 @@ public List<ExpiredItem> Apply(List<ExpiredItem> items)
switch (_kind)
{
case SeriesDeleteKind.Season:
case SeriesDeleteKind.SeasonKeepLast:
var seasons = items.GroupBy(x => ((Episode)x.Item).Season?.Id ?? ((Episode)x.Item).Series?.Id);
foreach (var season in seasons)
{
Expand All @@ -39,14 +42,30 @@ public List<ExpiredItem> Apply(List<ExpiredItem> items)

if (allWatched)
{
result.Add(new ExpiredItem
var expiredItem = new ExpiredItem
{
Item = episode.Season,
Data = first.Data,
Kind = first.Kind,
});
};

_logger.LogTrace("Season \"{SeriesName}\": \"{SeasonName}\" was added to expired items", episode.SeriesName, episode.Season.Name);
if (_kind == SeriesDeleteKind.SeasonKeepLast)
{
var seriesStatus = episode.Series.Status;
var lastSeasonId = episode.Series.GetSeasons(first.Data?.First()?.User, new DtoOptions()).LastOrDefault()?.Id;
if (seriesStatus.HasValue
&& seriesStatus.Value != SeriesStatus.Ended
&& episode.SeasonId == lastSeasonId)
{
_logger.LogTrace("Season \"{SeasonName}\" was NOT added to expired items because this is the last one and metadata indicates that show is continued",
expiredItem.FullName);
continue;
}
}

result.Add(expiredItem);

_logger.LogTrace("Season \"{SeasonName}\" was added to expired items", expiredItem.FullName);
}
}

Expand All @@ -67,30 +86,52 @@ public List<ExpiredItem> Apply(List<ExpiredItem> items)

if (allWatched)
{
var expiredItem = new ExpiredItem
{
Item = episode.Series,
Data = first.Data,
Kind = first.Kind,
};

if (_kind == SeriesDeleteKind.SeriesEnded)
{
if (episode.Series.Status.HasValue
&& episode.Series.Status.Value != MediaBrowser.Model.Entities.SeriesStatus.Ended)
var seriesStatus = episode.Series.Status;
if (seriesStatus.HasValue
&& seriesStatus.Value != SeriesStatus.Ended)
{
_logger.LogTrace("Series \"{SeriesName}\" was NOT added to expired items because metadata indicates that it's not yet finished",
episode.SeriesName);
expiredItem.FullName);
continue;
}
}

result.Add(new ExpiredItem
{
Item = episode.Series,
Data = first.Data,
Kind = first.Kind,
});
result.Add(expiredItem);

_logger.LogTrace("Series \"{SeriesName}\" was added to expired items", episode.SeriesName);
}
}

break;

case SeriesDeleteKind.EpisodeKeepLast:
foreach (var item in items)
{
var episode = (Episode)item.Item;
var seriesStatus = episode.Series.Status;
var lastEpisodeId = episode.Series.GetEpisodes(item.Data?.First()?.User, new DtoOptions()).LastOrDefault()?.Id;
if (seriesStatus.HasValue
&& seriesStatus.Value != SeriesStatus.Ended
&& episode.Id == lastEpisodeId)
{
_logger.LogTrace("Episode \"{EpisodeName}\" was NOT added to expired items because this is the last one and metadata indicates that show is not yet finished",
item.FullName);
continue;
}

result.Add(item);
}
break;

default:
result = items;
break;
Expand Down
2 changes: 2 additions & 0 deletions MediaCleaner/Web/general.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ <h2>Series</h2>
<option value="Season">Entire season</option>
<option value="Series">Entire series</option>
<option value="SeriesEnded">Entire series has ended</option>
<option value="EpisodeKeepLast">Episode (keep last until series ended)</option>
<option value="SeasonKeepLast">Entire season (keep last until series ended)</option>
</select>
<div class="fieldDescription"></div>
</div>
Expand Down
8 changes: 8 additions & 0 deletions MediaCleaner/Web/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,14 @@ function deleteEpisodesChanged(event) {
field.innerHTML = `Don't delete unless series status changes to "Ended" in metadata`
break

case 'EpisodeKeepLast':
field.innerHTML = `Keep the last episode of the show unless series status changes to "Ended" in metadata`
break

case 'SeasonKeepLast':
field.innerHTML = `Keep the last season of the show unless series status changes to "Ended" in metadata`
break

default:
field.innerHTML = ''
}
Expand Down
Loading