Skip to content

Commit

Permalink
Finished #415
Browse files Browse the repository at this point in the history
  • Loading branch information
tidusjar committed Aug 25, 2016
1 parent 2aebbe0 commit 7db336e
Show file tree
Hide file tree
Showing 19 changed files with 229 additions and 89 deletions.
4 changes: 4 additions & 0 deletions PlexRequests.Core/SettingModels/PlexSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion

using Newtonsoft.Json;

namespace PlexRequests.Core.SettingModels
{
public sealed class PlexSettings : ExternalSettings
Expand All @@ -36,5 +39,6 @@ public PlexSettings()
public bool EnableTvEpisodeSearching { get; set; }

public string PlexAuthToken { get; set; }
public string MachineIdentifier { get; set; }
}
}
31 changes: 31 additions & 0 deletions PlexRequests.Core/Setup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#endregion

using System;
using System.Linq;
using System.Text.RegularExpressions;

using Mono.Data.Sqlite;
Expand Down Expand Up @@ -66,6 +67,11 @@ public string SetupDb(string urlBase)
{
MigrateToVersion1900();
}

if(version > 1899 && version <= 1910)
{
MigrateToVersion1910();
}
}

return Db.DbConnection().ConnectionString;
Expand Down Expand Up @@ -244,5 +250,30 @@ public void MigrateToVersion1900()
Log.Error(e);
}
}

/// <summary>
/// Migrates to version1910.
/// </summary>
public void MigrateToVersion1910()
{
try
{
// Get the new machine Identifier
var settings = new SettingsServiceV2<PlexSettings>(new SettingsJsonRepository(Db, new MemoryCacheProvider()));
var plex = settings.GetSettings();
if (!string.IsNullOrEmpty(plex.PlexAuthToken))
{
var api = new PlexApi(new ApiRequest());
var server = api.GetServer(plex.PlexAuthToken); // Get the server info
plex.MachineIdentifier = server.Server.FirstOrDefault(x => x.AccessToken == plex.PlexAuthToken)?.MachineIdentifier;

settings.SaveSettings(plex); // Save the new settings
}
}
catch (Exception e)
{
Log.Error(e);
}
}
}
}
15 changes: 15 additions & 0 deletions PlexRequests.Helpers.Tests/PlexHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ public int TitleToSeasonNumber(string title)
return PlexHelper.GetSeasonNumberFromTitle(title);
}

[TestCaseSource(nameof(MediaUrls))]
public string GetPlexMediaUrlTest(string machineId, string mediaId)
{
return PlexHelper.GetPlexMediaUrl(machineId, mediaId);
}

private static IEnumerable<TestCaseData> PlexGuids
{
get
Expand All @@ -75,6 +81,15 @@ private static IEnumerable<TestCaseData> PlexGuids
}
}

private static IEnumerable<TestCaseData> MediaUrls
{
get
{
yield return new TestCaseData("abcd","99").Returns("https://app.plex.tv/web/app#!/server/abcd/details/%2Flibrary%2Fmetadata%2F99").SetName("Test 1");
yield return new TestCaseData("a54d1db669799308cd704b791f331eca6648b952", "51").Returns("https://app.plex.tv/web/app#!/server/a54d1db669799308cd704b791f331eca6648b952/details/%2Flibrary%2Fmetadata%2F51").SetName("Test 2");
}
}

private static IEnumerable<TestCaseData> SeasonNumbers
{
get
Expand Down
7 changes: 7 additions & 0 deletions PlexRequests.Helpers/PlexHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ public static int GetSeasonNumberFromTitle(string title)

return 0;
}

public static string GetPlexMediaUrl(string machineId, string mediaId)
{
var url =
$"https://app.plex.tv/web/app#!/server/{machineId}/details/%2Flibrary%2Fmetadata%2F{mediaId}";
return url;
}
}

public class EpisodeModelHelper
Expand Down
2 changes: 2 additions & 0 deletions PlexRequests.Services.Tests/PlexAvailabilityCheckerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ public void GetPlexMoviesTests()
}
});
CacheMock.Setup(x => x.Get<List<PlexSearch>>(CacheKeys.PlexLibaries)).Returns(cachedMovies);
SettingsMock.Setup(x => x.GetSettings()).Returns(F.Create<PlexSettings>());
var movies = Checker.GetPlexMovies();

Assert.That(movies.Any(x => x.ProviderId == "1212"));
Expand All @@ -258,6 +259,7 @@ public void GetPlexTvShowsTests()
new Directory1 {Type = "show", Title = "title1", Year = "2016", ProviderId = "1212", Seasons = new List<Directory1>()}
}
});
SettingsMock.Setup(x => x.GetSettings()).Returns(F.Create<PlexSettings>());
CacheMock.Setup(x => x.Get<List<PlexSearch>>(CacheKeys.PlexLibaries)).Returns(cachedTv);
var movies = Checker.GetPlexTvShows();

Expand Down
3 changes: 3 additions & 0 deletions PlexRequests.Services/Interfaces/IAvailabilityChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public interface IAvailabilityChecker
List<PlexAlbum> GetPlexAlbums();
bool IsAlbumAvailable(PlexAlbum[] plexAlbums, string title, string year, string artist);
bool IsEpisodeAvailable(string theTvDbId, int season, int episode);
PlexAlbum GetAlbum(PlexAlbum[] plexAlbums, string title, string year, string artist);
PlexMovie GetMovie(PlexMovie[] plexMovies, string title, string year, string providerId = null);
PlexTvShow GetTvShow(PlexTvShow[] plexShows, string title, string year, string providerId = null, int[] seasons = null);
/// <summary>
/// Gets the episode's stored in the cache.
/// </summary>
Expand Down
49 changes: 38 additions & 11 deletions PlexRequests.Services/Jobs/PlexAvailabilityChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ public void CheckAndUpdateAll()

public List<PlexMovie> GetPlexMovies()
{
var settings = Plex.GetSettings();
var movies = new List<PlexMovie>();
var libs = Cache.Get<List<PlexSearch>>(CacheKeys.PlexLibaries);
if (libs != null)
Expand All @@ -175,13 +176,20 @@ public List<PlexMovie> GetPlexMovies()
ReleaseYear = video.Year,
Title = video.Title,
ProviderId = video.ProviderId,
Url = PlexHelper.GetPlexMediaUrl(settings.MachineIdentifier, video.RatingKey)
}));
}
}
return movies;
}

public bool IsMovieAvailable(PlexMovie[] plexMovies, string title, string year, string providerId = null)
{
var movie = GetMovie(plexMovies, title, year, providerId);
return movie != null;
}

public PlexMovie GetMovie(PlexMovie[] plexMovies, string title, string year, string providerId = null)
{
var advanced = !string.IsNullOrEmpty(providerId);
foreach (var movie in plexMovies)
Expand All @@ -191,20 +199,21 @@ public bool IsMovieAvailable(PlexMovie[] plexMovies, string title, string year,
if (!string.IsNullOrEmpty(movie.ProviderId) &&
movie.ProviderId.Equals(providerId, StringComparison.InvariantCultureIgnoreCase))
{
return true;
return movie;
}
}
if (movie.Title.Equals(title, StringComparison.CurrentCultureIgnoreCase) &&
movie.ReleaseYear.Equals(year, StringComparison.CurrentCultureIgnoreCase))
{
return true;
return movie;
}
}
return false;
return null;
}

public List<PlexTvShow> GetPlexTvShows()
{
var settings = Plex.GetSettings();
var shows = new List<PlexTvShow>();
var libs = Cache.Get<List<PlexSearch>>(CacheKeys.PlexLibaries);
if (libs != null)
Expand All @@ -224,14 +233,24 @@ public List<PlexTvShow> GetPlexTvShows()
Title = x.Title,
ReleaseYear = x.Year,
ProviderId = x.ProviderId,
Seasons = x.Seasons?.Select(d => PlexHelper.GetSeasonNumberFromTitle(d.Title)).ToArray()
Seasons = x.Seasons?.Select(d => PlexHelper.GetSeasonNumberFromTitle(d.Title)).ToArray(),
Url = PlexHelper.GetPlexMediaUrl(settings.MachineIdentifier, x.RatingKey)

}));
}
}
return shows;
}

public bool IsTvShowAvailable(PlexTvShow[] plexShows, string title, string year, string providerId = null, int[] seasons = null)
{
var show = GetTvShow(plexShows, title, year, providerId, seasons);
return show != null;
}


public PlexTvShow GetTvShow(PlexTvShow[] plexShows, string title, string year, string providerId = null,
int[] seasons = null)
{
var advanced = !string.IsNullOrEmpty(providerId);
foreach (var show in plexShows)
Expand All @@ -242,23 +261,23 @@ public bool IsTvShowAvailable(PlexTvShow[] plexShows, string title, string year,
{
if (seasons.Any(season => show.Seasons.Contains(season)))
{
return true;
return show;
}
return false;
return null;
}
if (!string.IsNullOrEmpty(show.ProviderId) &&
show.ProviderId.Equals(providerId, StringComparison.InvariantCultureIgnoreCase))
{
return true;
return show;
}
}
if (show.Title.Equals(title, StringComparison.CurrentCultureIgnoreCase) &&
show.ReleaseYear.Equals(year, StringComparison.CurrentCultureIgnoreCase))
{
return true;
return show;
}
}
return false;
return null;
}

public bool IsEpisodeAvailable(string theTvDbId, int season, int episode)
Expand Down Expand Up @@ -328,6 +347,7 @@ public async Task<IEnumerable<PlexEpisodes>> GetEpisodes(int theTvDbId)

public List<PlexAlbum> GetPlexAlbums()
{
var settings = Plex.GetSettings();
var albums = new List<PlexAlbum>();
var libs = Cache.Get<List<PlexSearch>>(CacheKeys.PlexLibaries);
if (libs != null)
Expand All @@ -344,7 +364,8 @@ public List<PlexAlbum> GetPlexAlbums()
{
Title = x.Title,
ReleaseYear = x.Year,
Artist = x.ParentTitle
Artist = x.ParentTitle,
Url = PlexHelper.GetPlexMediaUrl(settings.MachineIdentifier, x.RatingKey)
}));
}
}
Expand All @@ -355,7 +376,13 @@ public bool IsAlbumAvailable(PlexAlbum[] plexAlbums, string title, string year,
{
return plexAlbums.Any(x =>
x.Title.Contains(title) &&
//x.ReleaseYear.Equals(year, StringComparison.CurrentCultureIgnoreCase) &&
x.Artist.Equals(artist, StringComparison.CurrentCultureIgnoreCase));
}

public PlexAlbum GetAlbum(PlexAlbum[] plexAlbums, string title, string year, string artist)
{
return plexAlbums.FirstOrDefault(x =>
x.Title.Contains(title) &&
x.Artist.Equals(artist, StringComparison.CurrentCultureIgnoreCase));
}

Expand Down
19 changes: 10 additions & 9 deletions PlexRequests.Services/Models/PlexAlbum.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
namespace PlexRequests.Services.Models
{
public class PlexAlbum
{
public string Title { get; set; }
public string Artist { get; set; }
public string ReleaseYear { get; set; }
}
}
namespace PlexRequests.Services.Models
{
public class PlexAlbum
{
public string Title { get; set; }
public string Artist { get; set; }
public string ReleaseYear { get; set; }
public string Url { get; set; }
}
}
19 changes: 10 additions & 9 deletions PlexRequests.Services/Models/PlexMovie.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
namespace PlexRequests.Services.Models
{
public class PlexMovie
{
public string Title { get; set; }
public string ReleaseYear { get; set; }
public string ProviderId { get; set; }
}
}
namespace PlexRequests.Services.Models
{
public class PlexMovie
{
public string Title { get; set; }
public string ReleaseYear { get; set; }
public string ProviderId { get; set; }
public string Url { get; set; }
}
}
1 change: 1 addition & 0 deletions PlexRequests.Services/Models/PlexTvShow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ public class PlexTvShow
public string ReleaseYear { get; set; }
public string ProviderId { get; set; }
public int[] Seasons { get; set; }
public string Url { get; set; }
}
}
9 changes: 6 additions & 3 deletions PlexRequests.UI/Content/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,8 @@ $(function () {
imdb: result.imdbId,
requested: result.requested,
approved: result.approved,
available: result.available
available: result.available,
url: result.plexUrl
};

return context;
Expand All @@ -450,7 +451,8 @@ $(function () {
approved: result.approved,
available: result.available,
episodes: result.episodes,
tvFullyAvailable: result.tvFullyAvailable
tvFullyAvailable: result.tvFullyAvailable,
url: result.plexUrl
};
return context;
}
Expand All @@ -470,7 +472,8 @@ $(function () {
country: result.country,
requested: result.requested,
approved: result.approved,
available: result.available
available: result.available,
url: result.plexUrl
};

return context;
Expand Down
Loading

0 comments on commit 7db336e

Please sign in to comment.