-
-
Notifications
You must be signed in to change notification settings - Fork 403
/
Copy pathEmbyAvaliabilityChecker.cs
181 lines (153 loc) · 6.18 KB
/
EmbyAvaliabilityChecker.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Ombi.Core;
using Ombi.Core.Services;
using Ombi.Helpers;
using Ombi.Hubs;
using Ombi.Notifications.Models;
using Ombi.Settings.Settings.Models;
using Ombi.Store.Entities;
using Ombi.Store.Repository;
using Ombi.Store.Repository.Requests;
using Quartz;
namespace Ombi.Schedule.Jobs.Emby
{
public class EmbyAvaliabilityChecker : AvailabilityChecker, IEmbyAvaliabilityChecker
{
public EmbyAvaliabilityChecker(IEmbyContentRepository repo, ITvRequestRepository t, IMovieRequestRepository m,
INotificationHelper n, ILogger<EmbyAvaliabilityChecker> log, INotificationHubService notification, IFeatureService featureService)
: base(t, n, log, notification)
{
_repo = repo;
_movieRepo = m;
_featureService = featureService;
}
private readonly IMovieRequestRepository _movieRepo;
private readonly IEmbyContentRepository _repo;
private readonly IFeatureService _featureService;
public async Task Execute(IJobExecutionContext job)
{
_log.LogInformation("Starting Emby Availability Check");
await NotificationHubService.SendNotificationToAdmins("Emby Availability Checker Started");
await ProcessMovies();
await ProcessTv();
_log.LogInformation("Finished Emby Availability Check");
await NotificationHubService.SendNotificationToAdmins("Emby Availability Checker Finished");
}
private async Task ProcessMovies()
{
var feature4kEnabled = await _featureService.FeatureEnabled(FeatureNames.Movie4KRequests);
var movies = _movieRepo.GetAll().Include(x => x.RequestedUser).Where(x => !x.Available || (!x.Available4K && x.Has4KRequest));
foreach (var movie in movies)
{
var has4kRequest = movie.Has4KRequest;
EmbyContent embyContent = null;
if (movie.TheMovieDbId > 0)
{
embyContent = await _repo.GetByTheMovieDbId(movie.TheMovieDbId.ToString());
}
else if(movie.ImdbId.HasValue())
{
embyContent = await _repo.GetByImdbId(movie.ImdbId);
}
if (embyContent == null)
{
// We don't have this yet
continue;
}
_log.LogInformation("We have found the request {0} on Emby, sending the notification", movie?.Title ?? string.Empty);
var notify = false;
if (has4kRequest && embyContent.Has4K && !movie.Available4K)
{
movie.Available4K = true;
movie.MarkedAsAvailable4K = DateTime.Now;
notify = true;
}
// If we have a non-4k version or we don't care about versions, then mark as available
if (!movie.Available && ( !feature4kEnabled || embyContent.Quality != null ))
{
movie.Available = true;
movie.MarkedAsAvailable = DateTime.Now;
notify = true;
}
if (notify)
{
var recipient = movie.RequestedUser.Email.HasValue() ? movie.RequestedUser.Email : string.Empty;
_log.LogDebug("MovieId: {0}, RequestUser: {1}", movie.Id, recipient);
await _notificationService.Notify(new NotificationOptions
{
DateTime = DateTime.Now,
NotificationType = NotificationType.RequestAvailable,
RequestId = movie.Id,
RequestType = RequestType.Movie,
Recipient = recipient,
});
}
}
await _movieRepo.Save();
}
/// <summary>
/// TODO This is EXCATLY the same as the PlexAvailabilityChecker. Refactor Please future Jamie
/// </summary>
/// <returns></returns>
private async Task ProcessTv()
{
var tv = _tvRepo.GetChild().Where(x => !x.Available);
var embyEpisodes = _repo.GetAllEpisodes().Include(x => x.Series);
foreach (var child in tv)
{
var useImdb = false;
var useTvDb = false;
if (child.ParentRequest.ImdbId.HasValue())
{
useImdb = true;
}
if (child.ParentRequest.TvDbId.ToString().HasValue())
{
useTvDb = true;
}
var tvDbId = child.ParentRequest.TvDbId;
var imdbId = child.ParentRequest.ImdbId;
IQueryable<IMediaServerEpisode> seriesEpisodes = null;
if (useImdb)
{
seriesEpisodes = embyEpisodes.Where(x => x.Series.ImdbId == imdbId.ToString());
}
if (useTvDb && (seriesEpisodes == null || !seriesEpisodes.Any()))
{
seriesEpisodes = embyEpisodes.Where(x => x.Series.TvDbId == tvDbId.ToString());
}
if (seriesEpisodes == null)
{
continue;
}
if (!seriesEpisodes.Any())
{
// Let's try and match the series by name
seriesEpisodes = embyEpisodes.Where(x =>
x.Series.Title == child.Title);
}
await ProcessTvShow(seriesEpisodes, child);
}
await _tvRepo.Save();
}
private bool _disposed;
protected virtual void Dispose(bool disposing)
{
if (_disposed)
return;
if (disposing)
{
}
_disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
}