mirror of
https://github.com/Ombi-app/Ombi.git
synced 2025-08-19 12:59:39 -07:00
Merge pull request #4311 from Ombi-app/feature/PlexGuids
Feature/plex guids
This commit is contained in:
commit
cb368e959a
5 changed files with 254 additions and 87 deletions
|
@ -1,3 +1,5 @@
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace Ombi.Api.Plex.Models
|
namespace Ombi.Api.Plex.Models
|
||||||
{
|
{
|
||||||
public class Metadata
|
public class Metadata
|
||||||
|
@ -44,7 +46,7 @@ namespace Ombi.Api.Plex.Models
|
||||||
public string grandparentTheme { get; set; }
|
public string grandparentTheme { get; set; }
|
||||||
public string chapterSource { get; set; }
|
public string chapterSource { get; set; }
|
||||||
public Medium[] Media { get; set; }
|
public Medium[] Media { get; set; }
|
||||||
public PlexGuids[] Guid { get; set; }
|
public List<PlexGuids> Guid { get; set; } = new List<PlexGuids>();
|
||||||
// public Director[] Director { get; set; }
|
// public Director[] Director { get; set; }
|
||||||
// public Writer[] Writer { get; set; }
|
// public Writer[] Writer { get; set; }
|
||||||
}
|
}
|
||||||
|
|
|
@ -123,6 +123,7 @@ namespace Ombi.Api.Plex
|
||||||
public async Task<PlexContainer> GetLibrary(string authToken, string plexFullHost, string libraryId)
|
public async Task<PlexContainer> GetLibrary(string authToken, string plexFullHost, string libraryId)
|
||||||
{
|
{
|
||||||
var request = new Request($"library/sections/{libraryId}/all", plexFullHost, HttpMethod.Get);
|
var request = new Request($"library/sections/{libraryId}/all", plexFullHost, HttpMethod.Get);
|
||||||
|
request.AddQueryString("includeGuids","1");
|
||||||
await AddHeaders(request, authToken);
|
await AddHeaders(request, authToken);
|
||||||
return await Api.Request<PlexContainer>(request);
|
return await Api.Request<PlexContainer>(request);
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,12 +7,13 @@
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
|
<PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
|
||||||
<PackageReference Include="MockQueryable.Moq" Version="5.0.0-preview.7" />
|
<PackageReference Include="MockQueryable.Moq" Version="5.0.0-preview.7" />
|
||||||
<PackageReference Include="Moq" Version="4.10.0" />
|
<PackageReference Include="Moq" Version="4.15.1" />
|
||||||
|
<PackageReference Include="Moq.AutoMock" Version="0.4.0" />
|
||||||
<PackageReference Include="Nunit" Version="3.11.0" />
|
<PackageReference Include="Nunit" Version="3.11.0" />
|
||||||
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="5.0.0" />
|
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="5.0.0" />
|
||||||
<PackageReference Include="NUnit.ConsoleRunner" Version="3.9.0" />
|
<PackageReference Include="NUnit.ConsoleRunner" Version="3.9.0" />
|
||||||
<PackageReference Include="NUnit3TestAdapter" Version="3.13.0" />
|
<PackageReference Include="NUnit3TestAdapter" Version="3.13.0" />
|
||||||
<packagereference Include="Microsoft.NET.Test.Sdk" Version="16.8.0"></packagereference>
|
<packagereference Include="Microsoft.NET.Test.Sdk" Version="16.11.0"></packagereference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
144
src/Ombi.Schedule.Tests/PlexContentSyncTests.cs
Normal file
144
src/Ombi.Schedule.Tests/PlexContentSyncTests.cs
Normal file
|
@ -0,0 +1,144 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Moq;
|
||||||
|
using Moq.AutoMock;
|
||||||
|
using NUnit.Framework;
|
||||||
|
using Ombi.Api.Plex;
|
||||||
|
using Ombi.Api.Plex.Models;
|
||||||
|
using Ombi.Core.Settings.Models.External;
|
||||||
|
using Ombi.Schedule.Jobs.Plex;
|
||||||
|
using Ombi.Store.Entities;
|
||||||
|
using Ombi.Store.Repository;
|
||||||
|
|
||||||
|
namespace Ombi.Schedule.Tests
|
||||||
|
{
|
||||||
|
[TestFixture]
|
||||||
|
public class PlexContentSyncTests
|
||||||
|
{
|
||||||
|
|
||||||
|
private AutoMocker _mocker;
|
||||||
|
private PlexContentSync _subject;
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void Setup()
|
||||||
|
{
|
||||||
|
_mocker = new AutoMocker();
|
||||||
|
_subject = _mocker.CreateInstance<PlexContentSync>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task DoesNotSyncExistingMovie()
|
||||||
|
{
|
||||||
|
var content = new Mediacontainer
|
||||||
|
{
|
||||||
|
Metadata = new[]
|
||||||
|
{
|
||||||
|
new Metadata
|
||||||
|
{
|
||||||
|
title = "test1",
|
||||||
|
year = 2021,
|
||||||
|
type = "movie"
|
||||||
|
},
|
||||||
|
}
|
||||||
|
};
|
||||||
|
var contentToAdd = new HashSet<PlexServerContent>();
|
||||||
|
var contentProcessed = new Dictionary<int, int>();
|
||||||
|
_mocker.Setup<IPlexContentRepository>(x =>
|
||||||
|
x.GetFirstContentByCustom(It.IsAny<Expression<Func<PlexServerContent, bool>>>()))
|
||||||
|
.Returns(Task.FromResult(new PlexServerContent()));
|
||||||
|
|
||||||
|
await _subject.MovieLoop(new PlexServers(), content, contentToAdd, contentProcessed);
|
||||||
|
|
||||||
|
Assert.That(contentToAdd, Is.Empty);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task SyncsMovieWithGuidFromInitalMetadata()
|
||||||
|
{
|
||||||
|
var content = new Mediacontainer
|
||||||
|
{
|
||||||
|
Metadata = new[]
|
||||||
|
{
|
||||||
|
new Metadata
|
||||||
|
{
|
||||||
|
title = "test1",
|
||||||
|
year = 2021,
|
||||||
|
type = "movie",
|
||||||
|
Guid = new List<PlexGuids>
|
||||||
|
{
|
||||||
|
new PlexGuids
|
||||||
|
{
|
||||||
|
Id = "imdb://tt0322259"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
ratingKey = 1
|
||||||
|
},
|
||||||
|
}
|
||||||
|
};
|
||||||
|
var contentToAdd = new HashSet<PlexServerContent>();
|
||||||
|
var contentProcessed = new Dictionary<int, int>();
|
||||||
|
|
||||||
|
await _subject.MovieLoop(new PlexServers(), content, contentToAdd, contentProcessed);
|
||||||
|
|
||||||
|
var first = contentToAdd.First();
|
||||||
|
Assert.That(first.ImdbId, Is.EqualTo("tt0322259"));
|
||||||
|
_mocker.Verify<IPlexApi>(x => x.GetMetadata(It.IsAny<string>(), It.IsAny<string>(),It.IsAny<int>()), Times.Never);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task SyncsMovieWithGuidFromCallToApi()
|
||||||
|
{
|
||||||
|
var content = new Mediacontainer
|
||||||
|
{
|
||||||
|
Metadata = new[]
|
||||||
|
{
|
||||||
|
new Metadata
|
||||||
|
{
|
||||||
|
ratingKey = 11,
|
||||||
|
title = "test1",
|
||||||
|
year = 2021,
|
||||||
|
type = "movie",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
};
|
||||||
|
var contentToAdd = new HashSet<PlexServerContent>();
|
||||||
|
var contentProcessed = new Dictionary<int, int>();
|
||||||
|
_mocker.Setup<IPlexApi>(x => x.GetMetadata(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<int>()))
|
||||||
|
.Returns(Task.FromResult(new PlexMetadata
|
||||||
|
{
|
||||||
|
MediaContainer = new Mediacontainer
|
||||||
|
{
|
||||||
|
Metadata = new[]
|
||||||
|
{
|
||||||
|
new Metadata
|
||||||
|
{
|
||||||
|
ratingKey = 11,
|
||||||
|
title = "test1",
|
||||||
|
year = 2021,
|
||||||
|
type = "movie",
|
||||||
|
Guid = new List<PlexGuids>
|
||||||
|
{
|
||||||
|
new PlexGuids
|
||||||
|
{
|
||||||
|
Id = "imdb://tt0322259"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
|
||||||
|
await _subject.MovieLoop(new PlexServers { Ip = "http://test.com/", Port = 80}, content, contentToAdd, contentProcessed);
|
||||||
|
|
||||||
|
var first = contentToAdd.First();
|
||||||
|
Assert.That(first.ImdbId, Is.EqualTo("tt0322259"));
|
||||||
|
|
||||||
|
_mocker.Verify<IPlexApi>(x => x.GetMetadata(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<int>()), Times.Once);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -261,6 +261,36 @@ namespace Ombi.Schedule.Jobs.Plex
|
||||||
await Repo.SaveChangesAsync();
|
await Repo.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
if (content.viewGroup.Equals(PlexMediaType.Movie.ToString(), StringComparison.InvariantCultureIgnoreCase))
|
if (content.viewGroup.Equals(PlexMediaType.Movie.ToString(), StringComparison.InvariantCultureIgnoreCase))
|
||||||
|
{
|
||||||
|
await MovieLoop(servers, content, contentToAdd, contentProcessed);
|
||||||
|
}
|
||||||
|
if (contentToAdd.Count > 500)
|
||||||
|
{
|
||||||
|
await Repo.AddRange(contentToAdd);
|
||||||
|
foreach (var c in contentToAdd)
|
||||||
|
{
|
||||||
|
contentProcessed.Add(c.Id, c.Key);
|
||||||
|
}
|
||||||
|
contentToAdd.Clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (contentToAdd.Any())
|
||||||
|
{
|
||||||
|
await Repo.AddRange(contentToAdd);
|
||||||
|
foreach (var c in contentToAdd)
|
||||||
|
{
|
||||||
|
contentProcessed.Add(c.Id, c.Key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
retVal.Content = contentProcessed.Values;
|
||||||
|
retVal.Episodes = episodesProcessed;
|
||||||
|
return retVal;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task MovieLoop(PlexServers servers, Mediacontainer content, HashSet<PlexServerContent> contentToAdd,
|
||||||
|
Dictionary<int, int> contentProcessed)
|
||||||
{
|
{
|
||||||
Logger.LogDebug("Processing Movies");
|
Logger.LogDebug("Processing Movies");
|
||||||
foreach (var movie in content?.Metadata ?? new Metadata[] { })
|
foreach (var movie in content?.Metadata ?? new Metadata[] { })
|
||||||
|
@ -287,14 +317,14 @@ namespace Ombi.Schedule.Jobs.Plex
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger.LogDebug("Adding movie {0}", movie.title);
|
Logger.LogDebug("Adding movie {0}", movie.title);
|
||||||
|
var guids = new List<string>();
|
||||||
|
if (!movie.Guid.Any())
|
||||||
|
{
|
||||||
var metaData = await PlexApi.GetMetadata(servers.PlexAuthToken, servers.FullUri,
|
var metaData = await PlexApi.GetMetadata(servers.PlexAuthToken, servers.FullUri,
|
||||||
movie.ratingKey);
|
movie.ratingKey);
|
||||||
|
|
||||||
var meta = metaData.MediaContainer.Metadata.FirstOrDefault();
|
var meta = metaData.MediaContainer.Metadata.FirstOrDefault();
|
||||||
var guids = new List<string>
|
guids.Add(meta.guid);
|
||||||
{
|
|
||||||
meta.guid
|
|
||||||
};
|
|
||||||
if (meta.Guid != null)
|
if (meta.Guid != null)
|
||||||
{
|
{
|
||||||
foreach (var g in meta.Guid)
|
foreach (var g in meta.Guid)
|
||||||
|
@ -302,6 +332,15 @@ namespace Ombi.Schedule.Jobs.Plex
|
||||||
guids.Add(g.Id);
|
guids.Add(g.Id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Currently a Plex Pass feature only
|
||||||
|
foreach (var g in movie.Guid)
|
||||||
|
{
|
||||||
|
guids.Add(g.Id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var providerIds = PlexHelper.GetProviderIdsFromMetadata(guids.ToArray());
|
var providerIds = PlexHelper.GetProviderIdsFromMetadata(guids.ToArray());
|
||||||
|
|
||||||
|
@ -320,14 +359,17 @@ namespace Ombi.Schedule.Jobs.Plex
|
||||||
{
|
{
|
||||||
item.ImdbId = providerIds.ImdbId;
|
item.ImdbId = providerIds.ImdbId;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (providerIds.TheMovieDb.HasValue())
|
if (providerIds.TheMovieDb.HasValue())
|
||||||
{
|
{
|
||||||
item.TheMovieDbId = providerIds.TheMovieDb;
|
item.TheMovieDbId = providerIds.TheMovieDb;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (providerIds.TheTvDb.HasValue())
|
if (providerIds.TheTvDb.HasValue())
|
||||||
{
|
{
|
||||||
item.TvDbId = providerIds.TheTvDb;
|
item.TvDbId = providerIds.TheTvDb;
|
||||||
}
|
}
|
||||||
|
|
||||||
contentToAdd.Add(item);
|
contentToAdd.Add(item);
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
|
@ -343,34 +385,11 @@ namespace Ombi.Schedule.Jobs.Plex
|
||||||
{
|
{
|
||||||
contentProcessed.Add(c.Id, c.Key);
|
contentProcessed.Add(c.Id, c.Key);
|
||||||
}
|
}
|
||||||
|
|
||||||
contentToAdd.Clear();
|
contentToAdd.Clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (contentToAdd.Count > 500)
|
|
||||||
{
|
|
||||||
await Repo.AddRange(contentToAdd);
|
|
||||||
foreach (var c in contentToAdd)
|
|
||||||
{
|
|
||||||
contentProcessed.Add(c.Id, c.Key);
|
|
||||||
}
|
|
||||||
contentToAdd.Clear();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (contentToAdd.Any())
|
|
||||||
{
|
|
||||||
await Repo.AddRange(contentToAdd);
|
|
||||||
foreach (var c in contentToAdd)
|
|
||||||
{
|
|
||||||
contentProcessed.Add(c.Id, c.Key);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
retVal.Content = contentProcessed.Values;
|
|
||||||
retVal.Episodes = episodesProcessed;
|
|
||||||
return retVal;
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task ProcessTvShow(PlexServers servers, Metadata show, HashSet<PlexServerContent> contentToAdd, Dictionary<int, int> contentProcessed)
|
private async Task ProcessTvShow(PlexServers servers, Metadata show, HashSet<PlexServerContent> contentToAdd, Dictionary<int, int> contentProcessed)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue