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 Sep 17, 2023
1 parent 7fc46a6 commit 8006b2c
Show file tree
Hide file tree
Showing 23 changed files with 145 additions and 98 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 Down Expand Up @@ -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
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;
}
}
}
8 changes: 5 additions & 3 deletions src/NzbDrone.Core/MediaCover/MediaCoverProxy.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 NzbDrone.Common.Cache;
using NzbDrone.Common.Http;
using NzbDrone.Core.Configuration;
Expand All @@ -12,7 +13,7 @@ public interface IMediaCoverProxy
string RegisterUrl(string url);

string GetUrl(string hash);
byte[] GetImage(string hash);
Task<byte[]> GetImage(string hash);
}

public class MediaCoverProxy : IMediaCoverProxy
Expand Down Expand Up @@ -52,13 +53,14 @@ public string GetUrl(string hash)
return result;
}

public byte[] GetImage(string hash)
public async Task<byte[]> GetImage(string hash)
{
var url = GetUrl(hash);

var request = new HttpRequest(url);
var response = await _httpClient.GetAsync(request);

return _httpClient.Get(request).ResponseData;
return response.ResponseData;
}
}
}
15 changes: 8 additions & 7 deletions src/NzbDrone.Core/MediaCover/MediaCoverService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using NLog;
using NzbDrone.Common;
using NzbDrone.Common.Disk;
Expand Down Expand Up @@ -143,7 +144,7 @@ private string GetMovieCoverPath(int movieId)
return Path.Combine(_coverRootFolder, movieId.ToString());
}

private bool EnsureCovers(Movie movie)
private async Task<bool> EnsureCovers(Movie movie)
{
var updated = false;
var toResize = new List<Tuple<MediaCover, bool>>();
Expand All @@ -160,11 +161,11 @@ private bool EnsureCovers(Movie movie)

try
{
alreadyExists = _coverExistsSpecification.AlreadyExists(cover.RemoteUrl, fileName);
alreadyExists = await _coverExistsSpecification.AlreadyExists(cover.RemoteUrl, fileName);

if (!alreadyExists)
{
DownloadCover(movie, cover);
await DownloadCover(movie, cover);
updated = true;
}
}
Expand All @@ -186,7 +187,7 @@ private bool EnsureCovers(Movie movie)

try
{
_semaphore.Wait();
await _semaphore.WaitAsync();

foreach (var tuple in toResize)
{
Expand All @@ -201,12 +202,12 @@ private bool EnsureCovers(Movie movie)
return updated;
}

private void DownloadCover(Movie movie, MediaCover cover)
private async Task DownloadCover(Movie movie, MediaCover cover)
{
var fileName = GetCoverPath(movie.Id, cover.CoverType);

_logger.Info("Downloading {0} for {1} {2}", cover.CoverType, movie, cover.RemoteUrl);
_httpClient.DownloadFile(cover.RemoteUrl, fileName);
await _httpClient.DownloadFileAsync(cover.RemoteUrl, fileName);
}

private void EnsureResizedCovers(Movie movie, MediaCover cover, bool forceResize)
Expand Down Expand Up @@ -265,7 +266,7 @@ private string GetExtension(MediaCoverTypes coverType)

public void HandleAsync(MovieUpdatedEvent message)
{
var updated = EnsureCovers(message.Movie);
var updated = EnsureCovers(message.Movie).GetAwaiter().GetResult();

_eventAggregator.PublishEvent(new MediaCoversUpdatedEvent(message.Movie, updated));
}
Expand Down

0 comments on commit 8006b2c

Please sign in to comment.