Skip to content

Commit

Permalink
Async skyhook, movie lookup and media cover proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
mynameisbogdan committed Oct 3, 2023
1 parent b3cacdc commit b71b8b0
Show file tree
Hide file tree
Showing 21 changed files with 139 additions and 96 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FizzWare.NBuilder;
using FluentAssertions;
using Moq;
Expand Down Expand Up @@ -39,7 +40,7 @@ public void Setup()

Mocker.GetMock<ISearchForNewMovie>()
.Setup(v => v.MapMovieToTmdbMovie(It.IsAny<MovieMetadata>()))
.Returns<MovieMetadata>(m => new MovieMetadata { TmdbId = m.TmdbId });
.Returns<MovieMetadata>(m => Task.FromResult(new MovieMetadata { TmdbId = m.TmdbId }));
}

private void GivenList(int id, bool enabled, bool enabledAuto, ImportListFetchResult fetchResult)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net;
using System.Threading.Tasks;
using FluentAssertions;
using Moq;
using NUnit.Framework;
Expand All @@ -19,7 +20,7 @@ public void Setup()
{
_httpResponse = new HttpResponse(null, new HttpHeader(), "", HttpStatusCode.OK);
Mocker.GetMock<IDiskProvider>().Setup(c => c.GetFileSize(It.IsAny<string>())).Returns(100);
Mocker.GetMock<IHttpClient>().Setup(c => c.Head(It.IsAny<HttpRequest>())).Returns(_httpResponse);
Mocker.GetMock<IHttpClient>().Setup(c => c.HeadAsync(It.IsAny<HttpRequest>())).Returns(Task.FromResult(_httpResponse));
}

private void GivenFileExistsOnDisk()
Expand All @@ -34,33 +35,37 @@ private void GivenExistingFileSize(long bytes)
}

[Test]
public void should_return_false_if_file_not_exists()
public async Task should_return_false_if_file_not_exists()
{
Subject.AlreadyExists("http://url", "c:\\file.exe").Should().BeFalse();
var result = await Subject.AlreadyExists("http://url", "c:\\file.exe");
result.Should().BeFalse();
}

[Test]
public void should_return_false_if_file_exists_but_diffrent_size()
public async Task should_return_false_if_file_exists_but_diffrent_size()
{
GivenExistingFileSize(100);
_httpResponse.Headers.ContentLength = 200;

Subject.AlreadyExists("http://url", "c:\\file.exe").Should().BeFalse();
var result = await Subject.AlreadyExists("http://url", "c:\\file.exe");
result.Should().BeFalse();
}

[Test]
public void should_return_true_if_file_exists_and_same_size_and_not_corrupt()
public async Task should_return_true_if_file_exists_and_same_size_and_not_corrupt()
{
GivenExistingFileSize(100);
_httpResponse.Headers.ContentLength = 100;
Subject.AlreadyExists("http://url", "c:\\file.exe").Should().BeTrue();
var result = await Subject.AlreadyExists("http://url", "c:\\file.exe");
result.Should().BeTrue();
}

[Test]
public void should_return_true_if_there_is_no_size_header_and_file_exist()
public async Task should_return_true_if_there_is_no_size_header_and_file_exist()
{
GivenExistingFileSize(100);
Subject.AlreadyExists("http://url", "c:\\file.exe").Should().BeFalse();
var result = await Subject.AlreadyExists("http://url", "c:\\file.exe");
result.Should().BeFalse();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using FizzWare.NBuilder;
using FluentAssertions;
using Moq;
Expand Down Expand Up @@ -78,7 +79,7 @@ public void should_resize_covers_if_main_downloaded()
{
Mocker.GetMock<ICoverExistsSpecification>()
.Setup(v => v.AlreadyExists(It.IsAny<string>(), It.IsAny<string>()))
.Returns(false);
.Returns(Task.FromResult(false));

Mocker.GetMock<IDiskProvider>()
.Setup(v => v.FileExists(It.IsAny<string>()))
Expand All @@ -95,7 +96,7 @@ public void should_resize_covers_if_missing()
{
Mocker.GetMock<ICoverExistsSpecification>()
.Setup(v => v.AlreadyExists(It.IsAny<string>(), It.IsAny<string>()))
.Returns(true);
.Returns(Task.FromResult(true));

Mocker.GetMock<IDiskProvider>()
.Setup(v => v.FileExists(It.IsAny<string>()))
Expand All @@ -112,7 +113,7 @@ public void should_not_resize_covers_if_exists()
{
Mocker.GetMock<ICoverExistsSpecification>()
.Setup(v => v.AlreadyExists(It.IsAny<string>(), It.IsAny<string>()))
.Returns(true);
.Returns(Task.FromResult(true));

Mocker.GetMock<IDiskProvider>()
.Setup(v => v.FileExists(It.IsAny<string>()))
Expand All @@ -133,7 +134,7 @@ public void should_resize_covers_if_existing_is_empty()
{
Mocker.GetMock<ICoverExistsSpecification>()
.Setup(v => v.AlreadyExists(It.IsAny<string>(), It.IsAny<string>()))
.Returns(true);
.Returns(Task.FromResult(true));

Mocker.GetMock<IDiskProvider>()
.Setup(v => v.FileExists(It.IsAny<string>()))
Expand All @@ -154,7 +155,7 @@ public void should_log_error_if_resize_failed()
{
Mocker.GetMock<ICoverExistsSpecification>()
.Setup(v => v.AlreadyExists(It.IsAny<string>(), It.IsAny<string>()))
.Returns(true);
.Returns(Task.FromResult(true));

Mocker.GetMock<IDiskProvider>()
.Setup(v => v.FileExists(It.IsAny<string>()))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Threading.Tasks;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Core.MetadataSource.SkyHook;
Expand All @@ -20,9 +21,10 @@ public void Setup()
[TestCase(11, "Star Wars")]
[TestCase(2, "Ariel")]
[TestCase(70981, "Prometheus")]
public void should_be_able_to_get_movie_detail(int tmdbId, string title)
public async Task should_be_able_to_get_movie_detail(int tmdbId, string title)
{
var details = Subject.GetMovieInfo(tmdbId).Item1;
var movieInfo = await Subject.GetMovieInfo(tmdbId);
var details = movieInfo.Item1;

ValidateMovie(details);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Threading.Tasks;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Core.MetadataSource.SkyHook;
Expand All @@ -23,9 +24,9 @@ public void Setup()
// [TestCase("The Man from U.N.C.L.E.", "The Man from U.N.C.L.E.")]
[TestCase("imdb:tt2527336", "Star Wars: The Last Jedi")]
[TestCase("imdb:tt2798920", "Annihilation")]
public void successful_search(string title, string expected)
public async Task successful_search(string title, string expected)
{
var result = Subject.SearchForNewMovie(title);
var result = await Subject.SearchForNewMovie(title);

result.Should().NotBeEmpty();

Expand All @@ -41,9 +42,9 @@ public void successful_search(string title, string expected)
[TestCase("tmdbid:1")]
[TestCase("adjalkwdjkalwdjklawjdlKAJD;EF")]
[TestCase("imdb: tt9805708")]
public void no_search_result(string term)
public async Task no_search_result(string term)
{
var result = Subject.SearchForNewMovie(term);
var result = await Subject.SearchForNewMovie(term);
result.Should().BeEmpty();

ExceptionVerification.IgnoreWarns();
Expand Down
15 changes: 8 additions & 7 deletions src/NzbDrone.Core.Test/MovieTests/AddMovieFixture.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using FizzWare.NBuilder;
using FluentAssertions;
using FluentValidation;
Expand Down Expand Up @@ -36,7 +37,7 @@ private void GivenValidMovie(int tmdbId)
{
Mocker.GetMock<IProvideMovieInfo>()
.Setup(s => s.GetMovieInfo(tmdbId))
.Returns(new Tuple<MovieMetadata, List<Credit>>(_fakeMovie, new List<Credit>()));
.Returns(Task.FromResult(new Tuple<MovieMetadata, List<Credit>>(_fakeMovie, new List<Credit>())));
}

private void GivenValidPath()
Expand All @@ -51,7 +52,7 @@ private void GivenValidPath()
}

[Test]
public void should_be_able_to_add_a_movie_without_passing_in_title()
public async Task should_be_able_to_add_a_movie_without_passing_in_title()
{
var newMovie = new Movie
{
Expand All @@ -62,13 +63,13 @@ public void should_be_able_to_add_a_movie_without_passing_in_title()
GivenValidMovie(newMovie.TmdbId);
GivenValidPath();

var series = Subject.AddMovie(newMovie);
var series = await Subject.AddMovie(newMovie);

series.Title.Should().Be(_fakeMovie.Title);
}

[Test]
public void should_have_proper_path()
public async Task should_have_proper_path()
{
var newMovie = new Movie
{
Expand All @@ -79,7 +80,7 @@ public void should_have_proper_path()
GivenValidMovie(newMovie.TmdbId);
GivenValidPath();

var series = Subject.AddMovie(newMovie);
var series = await Subject.AddMovie(newMovie);

series.Path.Should().Be(Path.Combine(newMovie.RootFolderPath, _fakeMovie.Title));
}
Expand All @@ -102,7 +103,7 @@ public void should_throw_if_movie_validation_fails()
new ValidationFailure("Path", "Test validation failure")
}));

Assert.Throws<ValidationException>(() => Subject.AddMovie(newMovie));
Assert.ThrowsAsync<ValidationException>(async () => await Subject.AddMovie(newMovie));
}

[Test]
Expand All @@ -125,7 +126,7 @@ public void should_throw_if_movie_cannot_be_found()
new ValidationFailure("Path", "Test validation failure")
}));

Assert.Throws<ValidationException>(() => Subject.AddMovie(newMovie));
Assert.ThrowsAsync<ValidationException>(async () => await Subject.AddMovie(newMovie));

ExceptionVerification.ExpectedErrors(1);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using FizzWare.NBuilder;
using Moq;
using NUnit.Framework;
Expand Down Expand Up @@ -67,7 +68,7 @@ private void GivenNewMovieInfo(MovieMetadata movie)
{
Mocker.GetMock<IProvideMovieInfo>()
.Setup(s => s.GetMovieInfo(_movie.TmdbId))
.Returns(new Tuple<MovieMetadata, List<Credit>>(movie, new List<Credit>()));
.Returns(Task.FromResult(new Tuple<MovieMetadata, List<Credit>>(movie, new List<Credit>())));
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public ImportListFetchResult FetchSingleList(ImportListDefinition definition)

private List<ImportListMovie> MapMovieReports(IEnumerable<ImportListMovie> reports)
{
var mappedMovies = reports.Select(m => _movieSearch.MapMovieToTmdbMovie(new MovieMetadata { Title = m.Title, TmdbId = m.TmdbId, ImdbId = m.ImdbId, Year = m.Year }))
var mappedMovies = reports.Select(m => _movieSearch.MapMovieToTmdbMovie(new MovieMetadata { Title = m.Title, TmdbId = m.TmdbId, ImdbId = m.ImdbId, Year = m.Year }).GetAwaiter().GetResult())
.Where(x => x != null)
.DistinctBy(x => x.TmdbId)
.ToList();
Expand Down
17 changes: 9 additions & 8 deletions src/NzbDrone.Core/ImportLists/ImportListSyncService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NLog;
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Instrumentation.Extensions;
Expand Down Expand Up @@ -41,7 +42,7 @@ public class ImportListSyncService : IExecute<ImportListSyncCommand>
_configService = configService;
}

private void SyncAll()
private async Task SyncAll()
{
if (_importListFactory.Enabled().Where(a => ((ImportListDefinition)a.Definition).EnableAuto).Empty())
{
Expand All @@ -62,16 +63,16 @@ private void SyncAll()
CleanLibrary();
}

ProcessListItems(listItemsResult);
await ProcessListItems(listItemsResult);
}

private void SyncList(ImportListDefinition definition)
private async Task SyncList(ImportListDefinition definition)
{
_logger.ProgressInfo("Starting Import List Refresh for List {0}", definition.Name);

var listItemsResult = _listFetcherAndParser.FetchSingleList(definition);

ProcessListItems(listItemsResult);
await ProcessListItems(listItemsResult);
}

private void ProcessMovieReport(ImportListDefinition importList, ImportListMovie report, List<ImportExclusion> listExclusions, List<int> dbMovies, List<Movie> moviesToAdd)
Expand Down Expand Up @@ -123,7 +124,7 @@ private void ProcessMovieReport(ImportListDefinition importList, ImportListMovie
}
}

private void ProcessListItems(ImportListFetchResult listFetchResult)
private async Task ProcessListItems(ImportListFetchResult listFetchResult)
{
listFetchResult.Movies = listFetchResult.Movies.DistinctBy(x =>
{
Expand Down Expand Up @@ -164,19 +165,19 @@ private void ProcessListItems(ImportListFetchResult listFetchResult)
if (moviesToAdd.Any())
{
_logger.ProgressInfo("Adding {0} movies from your auto enabled lists to library", moviesToAdd.Count);
_addMovieService.AddMovies(moviesToAdd, true);
await _addMovieService.AddMovies(moviesToAdd, true);
}
}

public void Execute(ImportListSyncCommand message)
{
if (message.DefinitionId.HasValue)
{
SyncList(_importListFactory.Get(message.DefinitionId.Value));
SyncList(_importListFactory.Get(message.DefinitionId.Value)).GetAwaiter().GetResult();
}
else
{
SyncAll();
SyncAll().GetAwaiter().GetResult();
}
}

Expand Down
12 changes: 7 additions & 5 deletions src/NzbDrone.Core/MediaCover/CoverAlreadyExistsSpecification.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using NLog;
using System.Threading.Tasks;
using NLog;
using NzbDrone.Common.Disk;
using NzbDrone.Common.Http;

namespace NzbDrone.Core.MediaCover
{
public interface ICoverExistsSpecification
{
bool AlreadyExists(string url, string path);
Task<bool> AlreadyExists(string url, string path);
}

public class CoverAlreadyExistsSpecification : ICoverExistsSpecification
Expand All @@ -22,16 +23,17 @@ public CoverAlreadyExistsSpecification(IDiskProvider diskProvider, IHttpClient h
_logger = logger;
}

public bool AlreadyExists(string url, string path)
public async Task<bool> AlreadyExists(string url, string path)
{
if (!_diskProvider.FileExists(path))
{
return false;
}

var headers = _httpClient.Head(new HttpRequest(url)).Headers;
var response = await _httpClient.HeadAsync(new HttpRequest(url));
var fileSize = _diskProvider.GetFileSize(path);
return fileSize == headers.ContentLength;

return fileSize == response.Headers.ContentLength;
}
}
}

0 comments on commit b71b8b0

Please sign in to comment.