-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibraryMatchingService.cs
More file actions
84 lines (73 loc) · 3.04 KB
/
LibraryMatchingService.cs
File metadata and controls
84 lines (73 loc) · 3.04 KB
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
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Querying;
namespace StreamingCatalogs.Services
{
public class LibraryMatchingService
{
private readonly ILogger _logger;
private readonly ILibraryManager _libraryManager;
public LibraryMatchingService(ILogManager logManager, ILibraryManager libraryManager)
{
_logger = logManager.GetLogger(GetType().Name);
_libraryManager = libraryManager;
}
public Dictionary<int, bool> GetLibraryStatusForTmdbIds(IEnumerable<int> tmdbIds, CancellationToken cancellationToken)
{
var tmdbIdList = tmdbIds.ToList();
_logger.Info("Checking library status for {0} TMDB IDs.", tmdbIdList.Count);
var results = tmdbIdList.ToDictionary(id => id, id => false);
if (results.Count == 0)
{
return results;
}
// This is much more efficient than loading all items. We query only for items that have one of the desired TMDB IDs.
var query = new InternalItemsQuery
{
AnyProviderIdEquals = tmdbIdList.Select(id => new KeyValuePair<string, string>("Tmdb", id.ToString())).ToList(),
EnableTotalRecordCount = false,
IncludeItemTypes = new[] { "Movie", "Series" } // Further optimization
};
var itemsInLibrary = _libraryManager.GetItemList(query);
foreach (var item in itemsInLibrary)
{
cancellationToken.ThrowIfCancellationRequested();
var idStr = item.GetProviderId("Tmdb");
if (!string.IsNullOrEmpty(idStr) && int.TryParse(idStr, out var tmdbId) && results.ContainsKey(tmdbId))
{
results[tmdbId] = true;
}
}
_logger.Info(
"Found {0} of {1} items in the library.",
results.Count(kvp => kvp.Value),
results.Count
);
return results;
}
public IEnumerable<int> GetAllTmdbIdsInLibrary(CancellationToken cancellationToken)
{
var query = new InternalItemsQuery
{
EnableTotalRecordCount = false,
IncludeItemTypes = new[] { "Movie", "Series" } // Optimization: only scan relevant types
};
var items = _libraryManager.GetItemList(query);
_logger.Info("Scanning {0} library items for TMDB IDs.", items.Count());
foreach (var item in items)
{
cancellationToken.ThrowIfCancellationRequested();
var id = item.GetProviderId("Tmdb");
if (!string.IsNullOrEmpty(id) && int.TryParse(id, out var tmdbId))
{
yield return tmdbId;
}
}
}
}
}