mirror of
https://github.com/Ombi-app/Ombi.git
synced 2025-08-14 18:47:15 -07:00
#254 Removed the cache, we are now storing the plex information into the database.
There is a big structure change around this, also increased the default check time to be in hours.
This commit is contained in:
parent
af1c93620f
commit
2608e53399
29 changed files with 479 additions and 170 deletions
|
@ -26,6 +26,9 @@
|
|||
#endregion
|
||||
using PlexRequests.Services.Models;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using PlexRequests.Store.Models;
|
||||
|
||||
namespace PlexRequests.Services.Interfaces
|
||||
{
|
||||
|
@ -43,12 +46,12 @@ namespace PlexRequests.Services.Interfaces
|
|||
/// Gets the episode's stored in the cache.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
HashSet<PlexEpisodeModel> GetEpisodeCache();
|
||||
Task<IEnumerable<PlexEpisodes>> GetEpisodes();
|
||||
/// <summary>
|
||||
/// Gets the episode's stored in the cache and then filters on the TheTvDBId.
|
||||
/// </summary>
|
||||
/// <param name="theTvDbId">The tv database identifier.</param>
|
||||
/// <returns></returns>
|
||||
IEnumerable<PlexEpisodeModel> GetEpisodeCache(int theTvDbId);
|
||||
Task<IEnumerable<PlexEpisodes>> GetEpisodes(int theTvDbId);
|
||||
}
|
||||
}
|
|
@ -29,6 +29,8 @@ using System.Collections.Generic;
|
|||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Dapper;
|
||||
|
||||
using NLog;
|
||||
|
||||
using PlexRequests.Api.Interfaces;
|
||||
|
@ -53,7 +55,7 @@ namespace PlexRequests.Services.Jobs
|
|||
public class PlexAvailabilityChecker : IJob, IAvailabilityChecker
|
||||
{
|
||||
public PlexAvailabilityChecker(ISettingsService<PlexSettings> plexSettings, IRequestService request, IPlexApi plex, ICacheProvider cache,
|
||||
INotificationService notify, IJobRecord rec, IRepository<UsersToNotify> users)
|
||||
INotificationService notify, IJobRecord rec, IRepository<UsersToNotify> users, IRepository<PlexEpisodes> repo)
|
||||
{
|
||||
Plex = plexSettings;
|
||||
RequestService = request;
|
||||
|
@ -62,9 +64,11 @@ namespace PlexRequests.Services.Jobs
|
|||
Notification = notify;
|
||||
Job = rec;
|
||||
UserNotifyRepo = users;
|
||||
EpisodeRepo = repo;
|
||||
}
|
||||
|
||||
private ISettingsService<PlexSettings> Plex { get; }
|
||||
private IRepository<PlexEpisodes> EpisodeRepo { get; }
|
||||
private IRequestService RequestService { get; }
|
||||
private static Logger Log = LogManager.GetCurrentClassLogger();
|
||||
private IPlexApi PlexApi { get; }
|
||||
|
@ -241,15 +245,23 @@ namespace PlexRequests.Services.Jobs
|
|||
|
||||
public bool IsEpisodeAvailable(string theTvDbId, int season, int episode)
|
||||
{
|
||||
var episodes = Cache.Get<HashSet<PlexEpisodeModel>>(CacheKeys.PlexEpisodes);
|
||||
if (episodes == null)
|
||||
var ep = EpisodeRepo.Custom(
|
||||
connection =>
|
||||
{
|
||||
connection.Open();
|
||||
var result = connection.Query<PlexEpisodes>("select * from PlexEpisodes where ProviderId = @ProviderId", new { ProviderId = theTvDbId});
|
||||
|
||||
return result;
|
||||
}).ToList();
|
||||
|
||||
if (!ep.Any())
|
||||
{
|
||||
Log.Info("Episode cache info is not available. tvdbid: {0}, season: {1}, episode: {2}",theTvDbId, season, episode);
|
||||
return false;
|
||||
}
|
||||
foreach (var result in episodes)
|
||||
foreach (var result in ep)
|
||||
{
|
||||
if (result.Episodes.ProviderId.Equals(theTvDbId) && result.Episodes.EpisodeNumber == episode && result.Episodes.SeasonNumber == season)
|
||||
if (result.ProviderId.Equals(theTvDbId) && result.EpisodeNumber == episode && result.SeasonNumber == season)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
@ -258,35 +270,43 @@ namespace PlexRequests.Services.Jobs
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the episode's stored in the cache.
|
||||
/// Gets the episode's db in the cache.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public HashSet<PlexEpisodeModel> GetEpisodeCache()
|
||||
public async Task<IEnumerable<PlexEpisodes>> GetEpisodes()
|
||||
{
|
||||
var episodes = Cache.Get<HashSet<PlexEpisodeModel>>(CacheKeys.PlexEpisodes);
|
||||
var episodes = await EpisodeRepo.GetAllAsync();
|
||||
if (episodes == null)
|
||||
{
|
||||
Log.Info("Episode cache info is not available.");
|
||||
return new HashSet<PlexEpisodeModel>();
|
||||
return new HashSet<PlexEpisodes>();
|
||||
}
|
||||
return episodes;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the episode's stored in the cache and then filters on the TheTvDBId.
|
||||
/// Gets the episode's stored in the db and then filters on the TheTvDBId.
|
||||
/// </summary>
|
||||
/// <param name="theTvDbId">The tv database identifier.</param>
|
||||
/// <returns></returns>
|
||||
public IEnumerable<PlexEpisodeModel> GetEpisodeCache(int theTvDbId)
|
||||
public async Task<IEnumerable<PlexEpisodes>> GetEpisodes(int theTvDbId)
|
||||
{
|
||||
var episodes = Cache.Get<List<PlexEpisodeModel>>(CacheKeys.PlexEpisodes);
|
||||
if (episodes == null)
|
||||
var ep = await EpisodeRepo.CustomAsync(async connection =>
|
||||
{
|
||||
connection.Open();
|
||||
var result = await connection.QueryAsync<PlexEpisodes>("select * from PlexEpisodes where ProviderId = @ProviderId", new { ProviderId = theTvDbId });
|
||||
|
||||
return result;
|
||||
});
|
||||
|
||||
var plexEpisodeses = ep as PlexEpisodes[] ?? ep.ToArray();
|
||||
if (!plexEpisodeses.Any())
|
||||
{
|
||||
Log.Info("Episode cache info is not available.");
|
||||
return new List<PlexEpisodeModel>();
|
||||
Log.Info("Episode db info is not available.");
|
||||
return new List<PlexEpisodes>();
|
||||
}
|
||||
|
||||
return episodes.Where(x => x.Episodes.ProviderId == theTvDbId.ToString());
|
||||
return plexEpisodeses;
|
||||
}
|
||||
|
||||
public List<PlexAlbum> GetPlexAlbums()
|
||||
|
|
|
@ -37,6 +37,8 @@ using PlexRequests.Core.SettingModels;
|
|||
using PlexRequests.Helpers;
|
||||
using PlexRequests.Services.Interfaces;
|
||||
using PlexRequests.Services.Models;
|
||||
using PlexRequests.Store.Models;
|
||||
using PlexRequests.Store.Repository;
|
||||
|
||||
using Quartz;
|
||||
|
||||
|
@ -45,12 +47,13 @@ namespace PlexRequests.Services.Jobs
|
|||
public class PlexEpisodeCacher : IJob
|
||||
{
|
||||
public PlexEpisodeCacher(ISettingsService<PlexSettings> plexSettings, IPlexApi plex, ICacheProvider cache,
|
||||
IJobRecord rec)
|
||||
IJobRecord rec, IRepository<PlexEpisodes> repo)
|
||||
{
|
||||
Plex = plexSettings;
|
||||
PlexApi = plex;
|
||||
Cache = cache;
|
||||
Job = rec;
|
||||
Repo = repo;
|
||||
}
|
||||
|
||||
private ISettingsService<PlexSettings> Plex { get; }
|
||||
|
@ -58,19 +61,23 @@ namespace PlexRequests.Services.Jobs
|
|||
private IPlexApi PlexApi { get; }
|
||||
private ICacheProvider Cache { get; }
|
||||
private IJobRecord Job { get; }
|
||||
private IRepository<PlexEpisodes> Repo { get; }
|
||||
private const int ResultCount = 25;
|
||||
private const string PlexType = "episode";
|
||||
private const string TableName = "PlexEpisodes";
|
||||
|
||||
|
||||
public void CacheEpisodes()
|
||||
{
|
||||
var videoHashset = new HashSet<Video>();
|
||||
var settings = Plex.GetSettings();
|
||||
// Ensure Plex is setup correctly
|
||||
if (string.IsNullOrEmpty(settings.PlexAuthToken))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the librarys and then get the tv section
|
||||
var sections = PlexApi.GetLibrarySections(settings.PlexAuthToken, settings.FullUri);
|
||||
var tvSection = sections.Directories.FirstOrDefault(x => x.type.Equals(PlexMediaType.Show.ToString(), StringComparison.CurrentCultureIgnoreCase));
|
||||
var tvSectionId = tvSection?.Key;
|
||||
|
@ -78,10 +85,15 @@ namespace PlexRequests.Services.Jobs
|
|||
var currentPosition = 0;
|
||||
int totalSize;
|
||||
|
||||
// Get the first 25 episodes (Paged)
|
||||
var episodes = PlexApi.GetAllEpisodes(settings.PlexAuthToken, settings.FullUri, tvSectionId, currentPosition, ResultCount);
|
||||
|
||||
// Parse the total amount of episodes
|
||||
int.TryParse(episodes.TotalSize, out totalSize);
|
||||
|
||||
currentPosition += ResultCount;
|
||||
|
||||
// Get all of the episodes in batches until we them all (Got'a catch 'em all!)
|
||||
while (currentPosition < totalSize)
|
||||
{
|
||||
videoHashset.UnionWith(PlexApi.GetAllEpisodes(settings.PlexAuthToken, settings.FullUri, tvSectionId, currentPosition, ResultCount).Video
|
||||
|
@ -89,29 +101,40 @@ namespace PlexRequests.Services.Jobs
|
|||
currentPosition += ResultCount;
|
||||
}
|
||||
|
||||
var episodesModel = new HashSet<PlexEpisodeModel>();
|
||||
var entities = new HashSet<PlexEpisodes>();
|
||||
|
||||
foreach (var video in videoHashset)
|
||||
{
|
||||
var ratingKey = video.RatingKey;
|
||||
var metadata = PlexApi.GetEpisodeMetaData(settings.PlexAuthToken, settings.FullUri, ratingKey);
|
||||
|
||||
// Get the individual episode Metadata (This is for us to get the TheTVDBId which also includes the episode number and season number)
|
||||
var metadata = PlexApi.GetEpisodeMetaData(settings.PlexAuthToken, settings.FullUri, video.RatingKey);
|
||||
|
||||
// Loop through the metadata and create the model to insert into the DB
|
||||
foreach (var metadataVideo in metadata.Video)
|
||||
{
|
||||
episodesModel.Add(new PlexEpisodeModel
|
||||
{
|
||||
RatingKey = metadataVideo.RatingKey,
|
||||
EpisodeTitle = metadataVideo.Title,
|
||||
Guid = metadataVideo.Guid,
|
||||
ShowTitle = metadataVideo.GrandparentTitle
|
||||
});
|
||||
var epInfo = PlexHelper.GetSeasonsAndEpisodesFromPlexGuid(metadataVideo.Guid);
|
||||
entities.Add(
|
||||
new PlexEpisodes
|
||||
{
|
||||
EpisodeNumber = epInfo.EpisodeNumber,
|
||||
EpisodeTitle = metadataVideo.Title,
|
||||
ProviderId = epInfo.ProviderId,
|
||||
RatingKey = metadataVideo.RatingKey,
|
||||
SeasonNumber = epInfo.SeasonNumber,
|
||||
ShowTitle = metadataVideo.Title
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Delete all of the current items
|
||||
Repo.DeleteAll(TableName);
|
||||
|
||||
if (episodesModel.Any())
|
||||
// Insert the new items
|
||||
var result = Repo.BatchInsert(entities, TableName, typeof(PlexEpisodes).GetPropertyNames());
|
||||
|
||||
if (!result)
|
||||
{
|
||||
Cache.Set(CacheKeys.PlexEpisodes, episodesModel, CacheKeys.TimeFrameMinutes.SchedulerCaching);
|
||||
Log.Error("Saving the plex episodes to the DB Failed");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -31,6 +31,10 @@
|
|||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Dapper, Version=1.50.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Dapper.1.50.0-beta8\lib\net45\Dapper.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.Build.Framework" />
|
||||
<Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\NLog.4.3.6\lib\net45\NLog.dll</HintPath>
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
<packages>
|
||||
<package id="Common.Logging" version="3.0.0" targetFramework="net45" />
|
||||
<package id="Common.Logging.Core" version="3.0.0" targetFramework="net45" />
|
||||
<package id="Dapper" version="1.50.0-beta8" targetFramework="net45" />
|
||||
<package id="MailKit" version="1.2.21" targetFramework="net45" requireReinstallation="True" />
|
||||
<package id="MimeKit" version="1.2.22" targetFramework="net45" />
|
||||
<package id="NLog" version="4.3.6" targetFramework="net45" />
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue