Skip to content

Commit

Permalink
Done the movie searching
Browse files Browse the repository at this point in the history
  • Loading branch information
tidusjar committed Apr 5, 2017
1 parent 1c301f2 commit e80f0bc
Show file tree
Hide file tree
Showing 16 changed files with 571 additions and 71 deletions.
15 changes: 15 additions & 0 deletions Ombi/Ombi.Core/IMovieEngine.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Ombi.Core.Models.Search;

namespace Ombi.Core
{
public interface IMovieEngine
{
Task<IEnumerable<SearchMovieViewModel>> NowPlayingMovies();
Task<IEnumerable<SearchMovieViewModel>> PopularMovies();
Task<IEnumerable<SearchMovieViewModel>> ProcessMovieSearch(string search);
Task<IEnumerable<SearchMovieViewModel>> TopRatedMovies();
Task<IEnumerable<SearchMovieViewModel>> UpcomingMovies();
}
}
54 changes: 54 additions & 0 deletions Ombi/Ombi.Core/Models/Search/SearchMovieViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2017 Jamie Rees
// File: SearchMovieViewModel.cs
// Created By: Jamie Rees
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion

using System;
using System.Collections.Generic;

namespace Ombi.Core.Models.Search
{
public class SearchMovieViewModel : SearchViewModel
{
public bool Adult { get; set; }
public string BackdropPath { get; set; }
public List<int> GenreIds { get; set; }
public int Id { get; set; }
public string OriginalLanguage { get; set; }
public string OriginalTitle { get; set; }
public string Overview { get; set; }
public double Popularity { get; set; }
public string PosterPath { get; set; }
public DateTime? ReleaseDate { get; set; }
public string Title { get; set; }
public bool Video { get; set; }
public double VoteAverage { get; set; }
public int VoteCount { get; set; }
public bool AlreadyInCp { get; set; }
public string Trailer { get; set; }
public string Homepage { get; set; }
public string ImdbId { get; set; }
}
}
36 changes: 36 additions & 0 deletions Ombi/Ombi.Core/Models/Search/SearchViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2017 Jamie Rees
// File: SearchViewModel.cs
// Created By: Jamie Rees
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion
namespace Ombi.Core.Models.Search
{
public class SearchViewModel
{
public bool Approved { get; set; }
public bool Requested { get; set; }
public bool Available { get; set; }
public string PlexUrl { get; set; }
}
}
163 changes: 163 additions & 0 deletions Ombi/Ombi.Core/MovieEngine.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Ombi.Core.Models.Search;
using Ombi.TheMovieDbApi.Models;

namespace Ombi.Core
{
public class MovieEngine : IMovieEngine
{

public async Task<IEnumerable<SearchMovieViewModel>> ProcessMovieSearch(string search)
{
var api = new TheMovieDbApi.TheMovieDbApi();
var result = await api.SearchMovie(search);
if (result != null)
{
return await TransformMovieResultsToResponse(result.results);
}
return null;
}
public async Task<IEnumerable<SearchMovieViewModel>> PopularMovies()
{
var api = new TheMovieDbApi.TheMovieDbApi();
var result = await api.PopularMovies();
if (result != null)
{
return await TransformMovieResultsToResponse(result.results);
}
return null;
}

public async Task<IEnumerable<SearchMovieViewModel>> TopRatedMovies()
{
var api = new TheMovieDbApi.TheMovieDbApi();
var result = await api.TopRated();
if (result != null)
{
return await TransformMovieResultsToResponse(result.results);
}
return null;
}

public async Task<IEnumerable<SearchMovieViewModel>> UpcomingMovies()
{
var api = new TheMovieDbApi.TheMovieDbApi();
var result = await api.Upcoming();
if (result != null)
{
return await TransformMovieResultsToResponse(result.results);
}
return null;
}
public async Task<IEnumerable<SearchMovieViewModel>> NowPlayingMovies()
{
var api = new TheMovieDbApi.TheMovieDbApi();
var result = await api.NowPlaying();
if (result != null)
{
return await TransformMovieResultsToResponse(result.results);
}
return null;
}


private async Task<List<SearchMovieViewModel>> TransformMovieResultsToResponse(IEnumerable<SearchResult> movies)
{
await Task.Yield();
var viewMovies = new List<SearchMovieViewModel>();
var counter = 0;
//Dictionary<int, RequestedModel> dbMovies = await RequestedMovies();
foreach (var movie in movies)
{
var viewMovie = new SearchMovieViewModel
{
Adult = movie.adult,
BackdropPath = movie.backdrop_path,
Id = movie.id,
OriginalLanguage = movie.original_language,
OriginalTitle = movie.original_title,
Overview = movie.overview,
Popularity = movie.popularity,
PosterPath = movie.poster_path,
ReleaseDate = string.IsNullOrEmpty(movie.release_date) ? DateTime.MinValue : DateTime.Parse(movie.release_date),
Title = movie.title,
Video = movie.video,
VoteAverage = movie.vote_average,
VoteCount = movie.vote_count
};
viewMovies.Add(viewMovie);

//if (counter <= 5) // Let's only do it for the first 5 items
//{
// var movieInfo = MovieApi.GetMovieInformationWithVideos(movie.Id);

// // TODO needs to be careful about this, it's adding extra time to search...
// // https://www.themoviedb.org/talk/5807f4cdc3a36812160041f2
// viewMovie.ImdbId = movieInfo?.imdb_id;
// viewMovie.Homepage = movieInfo?.homepage;
// var videoId = movieInfo?.video ?? false
// ? movieInfo?.videos?.results?.FirstOrDefault()?.key
// : string.Empty;

// viewMovie.Trailer = string.IsNullOrEmpty(videoId)
// ? string.Empty
// : $"https://www.youtube.com/watch?v={videoId}";

// counter++;
//}

// var canSee = CanUserSeeThisRequest(viewMovie.Id, Security.HasPermissions(User, Permissions.UsersCanViewOnlyOwnRequests), dbMovies);

// var plexSettings = await PlexService.GetSettingsAsync();
// var embySettings = await EmbySettings.GetSettingsAsync();
// if (plexSettings.Enable)
// {
// var content = PlexContentRepository.GetAll();
// var plexMovies = PlexChecker.GetPlexMovies(content);

// var plexMovie = PlexChecker.GetMovie(plexMovies.ToArray(), movie.Title,
// movie.ReleaseDate?.Year.ToString(),
// viewMovie.ImdbId);
// if (plexMovie != null)
// {
// viewMovie.Available = true;
// viewMovie.PlexUrl = plexMovie.Url;
// }
// }
// if (embySettings.Enable)
// {
// var embyContent = EmbyContentRepository.GetAll();
// var embyMovies = EmbyChecker.GetEmbyMovies(embyContent);

// var embyMovie = EmbyChecker.GetMovie(embyMovies.ToArray(), movie.Title,
// movie.ReleaseDate?.Year.ToString(), viewMovie.ImdbId);
// if (embyMovie != null)
// {
// viewMovie.Available = true;
// }
// }
// if (dbMovies.ContainsKey(movie.Id) && canSee) // compare to the requests db
// {
// var dbm = dbMovies[movie.Id];

// viewMovie.Requested = true;
// viewMovie.Approved = dbm.Approved;
// viewMovie.Available = dbm.Available;
// }
// else if (canSee)
// {
// bool exists = IsMovieInCache(movie, viewMovie.ImdbId);
// viewMovie.Approved = exists;
// viewMovie.Requested = exists;
// }
// viewMovies.Add(viewMovie);
//}


}
return viewMovies;
}
}
}
8 changes: 0 additions & 8 deletions Ombi/Ombi.Core/MovieProcessor.cs

This file was deleted.

6 changes: 5 additions & 1 deletion Ombi/Ombi.Core/Ombi.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard1.4</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Ombi.TheMovieDbApi\Ombi.TheMovieDbApi.csproj" />
</ItemGroup>

</Project>
30 changes: 29 additions & 1 deletion Ombi/Ombi.TheMovieDbApi/TheMovieDbApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public TheMovieDbApi()
Api = new Api.Api();
}
private const string ApiToken = "b8eabaf5608b88d0298aa189dd90bf00";
private static readonly Uri BaseUri = new Uri("https://api.themoviedb.org/3/");
private static readonly Uri BaseUri = new Uri("http://api.themoviedb.org/3/");
public Api.Api Api { get; }

public async Task<MovieResponse> GetMovieInformation(int movieId)
Expand All @@ -31,6 +31,34 @@ public async Task<TheMovieDbContainer<SearchResult>> SearchMovie(string searchTe
return await Api.Get<TheMovieDbContainer<SearchResult>>(url);
}

public async Task<TheMovieDbContainer<SearchResult>> PopularMovies()
{
var url = BaseUri.ChangePath("movie/popular");
url = AddHeaders(url);
return await Api.Get<TheMovieDbContainer<SearchResult>>(url);
}

public async Task<TheMovieDbContainer<SearchResult>> TopRated()
{
var url = BaseUri.ChangePath("movie/top_rated");
url = AddHeaders(url);
return await Api.Get<TheMovieDbContainer<SearchResult>>(url);
}

public async Task<TheMovieDbContainer<SearchResult>> Upcoming()
{
var url = BaseUri.ChangePath("movie/upcoming");
url = AddHeaders(url);
return await Api.Get<TheMovieDbContainer<SearchResult>>(url);
}

public async Task<TheMovieDbContainer<SearchResult>> NowPlaying()
{
var url = BaseUri.ChangePath("movie/now_playing");
url = AddHeaders(url);
return await Api.Get<TheMovieDbContainer<SearchResult>>(url);
}

private Uri AddHeaders(Uri url)
{
return url.AddQueryParameter("api_key", ApiToken);
Expand Down
40 changes: 34 additions & 6 deletions Ombi/Ombi/Controllers/SearchController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,48 @@
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Ombi.TheMovieDbApi.Models;
using Ombi.Core;
using Ombi.Core.Models.Search;

namespace Ombi.Controllers
{
public class SearchController : BaseApiController
{
public SearchController(IMovieEngine movie)
{
MovieEngine = movie;
}

private IMovieEngine MovieEngine { get; }

[HttpGet("movie/{searchTerm}")]
public async Task<List<SearchResult>> SearchMovie(string searchTerm)
public async Task<IEnumerable<SearchMovieViewModel>> SearchMovie(string searchTerm)
{
var api = new TheMovieDbApi.TheMovieDbApi();
var result = await api.SearchMovie(searchTerm);
return result.results;
return await MovieEngine.ProcessMovieSearch(searchTerm);
}


[HttpGet("movie/popular")]
public async Task<IEnumerable<SearchMovieViewModel>> Popular()
{
return await MovieEngine.PopularMovies();
}
[HttpGet("movie/nowplaying")]
public async Task<IEnumerable<SearchMovieViewModel>> NowPlayingMovies()
{
return await MovieEngine.NowPlayingMovies();
}
[HttpGet("movie/toprated")]
public async Task<IEnumerable<SearchMovieViewModel>> TopRatedMovies()
{
return await MovieEngine.TopRatedMovies();
}
[HttpGet("movie/upcoming")]
public async Task<IEnumerable<SearchMovieViewModel>> UpcomingMovies()
{
return await MovieEngine.UpcomingMovies();
}



}
}
Loading

0 comments on commit e80f0bc

Please sign in to comment.