Skip to content

Commit

Permalink
More rework to use the Plex DB
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie.Rees authored and Jamie.Rees committed Oct 19, 2016
1 parent dc92285 commit ec49ab7
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 86 deletions.
6 changes: 6 additions & 0 deletions PlexRequests.Helpers/PlexHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ public static string GetPlexMediaUrl(string machineId, string mediaId)
$"https://app.plex.tv/web/app#!/server/{machineId}/details/%2Flibrary%2Fmetadata%2F{mediaId}";
return url;
}

public static string FormatGenres(string tags)
{
var split = tags.Split(new[] {'|'}, StringSplitOptions.RemoveEmptyEntries);
return string.Join(", ", split);
}
}

public class EpisodeModelHelper
Expand Down
61 changes: 37 additions & 24 deletions PlexRequests.Services/Jobs/RecentlyAdded.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public RecentlyAdded(IPlexApi api, ISettingsService<PlexSettings> plexSettings,
private IPlexApi Api { get; }
private TvMazeApi TvApi = new TvMazeApi();
private readonly TheMovieDbApi _movieApi = new TheMovieDbApi();
private const int MetadataTypeTv = 4;
private const int MetadataTypeMovie = 1;
private ISettingsService<PlexSettings> PlexSettings { get; }
private ISettingsService<EmailNotificationSettings> EmailSettings { get; }
private ISettingsService<PlexRequestSettings> PlexRequestSettings { get; }
Expand Down Expand Up @@ -144,12 +146,12 @@ private void StartDb(bool testEmail = false)
var sb = new StringBuilder();
var plexSettings = PlexSettings.GetSettings();

var recentlyAdded = PlexDb.GetItemsAddedAfterDate(DateTime.Now.AddDays(-7));
var recentlyAdded = PlexDb.GetItemsAddedAfterDate(DateTime.Now.AddDays(-12)).ToList();

var movies = recentlyAdded.Where(x => x.MetadataType == 1);
var tv = recentlyAdded.Where(x => x.MetadataType == 4);
GenerateMovieHtmlDb(movies, ref sb);
var movies = recentlyAdded.Where(x => x.metadata_type == MetadataTypeMovie);
var tv = recentlyAdded.Where(x => x.metadata_type == MetadataTypeTv);

GenerateMovieHtml(movies, ref sb);
GenerateTvHtml(tv, ref sb);

var template = new RecentlyAddedTemplate();
Expand Down Expand Up @@ -217,21 +219,26 @@ private void GenerateMovieHtml(IEnumerable<RecentlyAddedChild> movies, PlexSetti
sb.Append("</table><br/><br/>");
}

private void GenerateMovieHtmlDb(IEnumerable<MetadataItems> movies, ref StringBuilder sb)
private void GenerateMovieHtml(IEnumerable<MetadataItems> movies, ref StringBuilder sb)
{
var items = movies as MetadataItems[] ?? movies.ToArray();
if (!items.Any())
{
return;
}
sb.Append("<h1>New Movies:</h1><br/><br/>");
sb.Append(
"<table border=\"0\" cellpadding=\"0\" align=\"center\" cellspacing=\"0\" style=\"border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%;\" width=\"100%\">");
foreach (var movie in movies)
foreach (var movie in items)
{
var plexGUID = string.Empty;
try
{
plexGUID = movie.Guid;
plexGUID = movie.guid;

var imdbId = PlexHelper.GetProviderIdFromPlexGuid(plexGUID);

var info = _movieApi.GetMovieInformation(imdbId).Result;
var info = _movieApi.GetMovieInformation(imdbId).Result; // TODO remove this and get the image info from Plex https://github.com/jakewaldron/PlexEmail/blob/master/scripts/plexEmail.py#L391

sb.Append("<tr>");
sb.Append("<td align=\"center\">");
Expand All @@ -245,20 +252,20 @@ private void GenerateMovieHtmlDb(IEnumerable<MetadataItems> movies, ref StringBu
"<td align=\"center\" style=\"font-family: sans-serif; font-size: 14px; vertical-align: top;\" valign=\"top\">");

sb.AppendFormat(
"<a href=\"https://www.imdb.com/title/{0}/\"><h3 style=\"font-family: sans-serif; font-weight: normal; margin: 0; Margin-bottom: 15px;\">{1} {2}</p></a>",
info.ImdbId, info.Title, info.ReleaseDate?.ToString("yyyy") ?? string.Empty);
"<a href=\"https://www.imdb.com/title/{0}/\"><h3 style=\"font-family: sans-serif; font-weight: normal; margin: 0; Margin-bottom: 15px;\">{1} {2:yyyy}</p></a>",
imdbId, string.IsNullOrEmpty(movie.original_title) ? movie.title : movie.original_title + $" AKA {movie.title}", movie.originally_available_at);


if (!string.IsNullOrEmpty(movie.tagline))
{
sb.AppendFormat("<p style=\"font-family: sans-serif; font-size: 15px; font-weight: normal; margin: 0; Margin-bottom: 15px;\">{0}</p>", movie.tagline);
}

if (info.Genres.Any())
if (!string.IsNullOrEmpty(movie.tags_genre))
{
sb.AppendFormat(
"<p style=\"font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; Margin-bottom: 15px;\">Genre: {0}</p>",
string.Join(", ", info.Genres.Select(x => x.Name.ToString()).ToArray()));
sb.AppendFormat("<p style=\"font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; Margin-bottom: 15px;\">Genre: {0}</p>", PlexHelper.FormatGenres(movie.tags_genre));
}
sb.AppendFormat(
"<p style=\"font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; Margin-bottom: 15px;\">{0}</p>",
info.Overview);

sb.AppendFormat("<p style=\"font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; Margin-bottom: 15px;\">{0}</p>", movie.summary);

sb.Append("<td");
sb.Append("<hr>");
Expand Down Expand Up @@ -332,17 +339,23 @@ private void GenerateTvHtml(IEnumerable<RecentlyAddedChild> tv, PlexSettings ple

private void GenerateTvHtml(IEnumerable<MetadataItems> tv, ref StringBuilder sb)
{
var items = tv as MetadataItems[] ?? tv.ToArray();
if (!items.Any())
{
return;
}

// TV
sb.Append("<h1>New Episodes:</h1><br/><br/>");
sb.Append(
"<table border=\"0\" cellpadding=\"0\" align=\"center\" cellspacing=\"0\" style=\"border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%;\" width=\"100%\">");
foreach (var t in tv)
foreach (var t in items)
{
var plexGUID = string.Empty;
try
{

plexGUID = t.Guid;
plexGUID = t.guid;
var seasonInfo = PlexHelper.GetSeasonsAndEpisodesFromPlexGuid(plexGUID);

var info = TvApi.ShowLookupByTheTvDbId(int.Parse(PlexHelper.GetProviderIdFromPlexGuid(plexGUID)));
Expand All @@ -360,8 +373,8 @@ private void GenerateTvHtml(IEnumerable<MetadataItems> tv, ref StringBuilder sb)
sb.Append("<tr>");
sb.Append("<td align=\"center\" style=\"font-family: sans-serif; font-size: 14px; vertical-align: top;\" valign=\"top\">");

sb.AppendFormat("<a href=\"https://www.imdb.com/title/{0}/\"><h3 style=\"font-family: sans-serif; font-weight: normal; margin: 0; Margin-bottom: 15px;\">{1} {2}</p></a>",
info.externals.imdb, info.name, info.premiered.Substring(0, 4)); // Only the year
sb.AppendFormat("<a href=\"https://www.imdb.com/title/{0}/\"><h3 style=\"font-family: sans-serif; font-weight: normal; margin: 0; Margin-bottom: 15px;\">{1} {2:yyyy}</p></a>",
info.externals.imdb, string.IsNullOrEmpty(t.original_title) ? t.title : t.original_title + $" AKA {t.title}", t.originally_available_at); // Only the year

sb.AppendFormat("<p style=\"font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; Margin-bottom: 15px;\">Season: {0}, Episode: {1}</p>", seasonInfo.SeasonNumber, seasonInfo.EpisodeNumber);

Expand All @@ -372,7 +385,7 @@ private void GenerateTvHtml(IEnumerable<MetadataItems> tv, ref StringBuilder sb)
string.Join(", ", info.genres.Select(x => x.ToString()).ToArray()));
}
sb.AppendFormat("<p style=\"font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; Margin-bottom: 15px;\">{0}</p>",
string.IsNullOrEmpty(t.Summary) ? info.summary : t.Summary); // Episode Summary
string.IsNullOrEmpty(t.summary) ? info.summary : t.summary); // Episode Summary

sb.Append("<td");
sb.Append("<hr>");
Expand Down
86 changes: 29 additions & 57 deletions PlexRequests.Store/Models/Plex/MetadataItems.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,70 +26,42 @@
#endregion

using System;
using System.Data.Linq.Mapping;
using Dapper;
using Dapper.Contrib.Extensions;

namespace PlexRequests.Store.Models.Plex
{
[Table(Name = "metadata_items")]
[Table("metadata_items")]
public class MetadataItems
{
[Column(IsPrimaryKey = true)]
public int Id { get; set; }

[Column(Name = "library_section_id")]
public int LibrarySectionId { get; set; }

[Column(Name = "parent_id")]
public int ParentId { get; set; }

[Column(Name = "metadata_type")]
public int MetadataType { get; set; }

[Column(Name = "guid")]
public string Guid { get; set; }

[Column(Name = "media_item_count")]
public int MediaItemCount { get; set; }

[Column(Name = "title")]
public string Title { get; set; }

[Column(Name = "title_sort")]
public string TitleSort { get; set; }

[Column(Name = "OriginalTitle")]
public string OriginalTitle { get; set; }

[Column(Name = "studio")]
public string Studio { get; set; }
[Column(Name = "rating")]
public float Rating { get; set; }
[Column(Name = "rating_count")]
public int RatingCount { get; set; }
[Column(Name = "tagline")]
public string Tagline { get; set; }
[Column(Name = "summary")]
public string Summary { get; set; }
[Column(Name = "trivia")]
public string Trivia { get; set; }
[Column(Name = "quotes")]
public string Quotes { get; set; }
[Column(Name = "content_rating")]
public string ContentRating { get; set; }
[Column(Name = "content_rating_age")]
public int ContentRatingAge { get; set; }
[Column(Name = "Index")]
[Key]
public int id { get; set; }

public int library_section_id { get; set; }
public int parent_id { get; set; }
public int metadata_type { get; set; }
public string guid { get; set; }
public int media_item_count { get; set; }
public string title { get; set; }
public string title_sort { get; set; }
public string original_title { get; set; }
public string studio { get; set; }
public float rating { get; set; }
public int rating_count { get; set; }
public string tagline { get; set; }
public string summary { get; set; }
public string trivia { get; set; }
public string quotes { get; set; }
public string content_rating { get; set; }
public int content_rating_age { get; set; }
public int Index { get; set; }
public string tags_genre { get; set; }
// SKIP Until Date Times

[Column(Name = "originally_available_at")]
public DateTime OriginallyAvailableAt { get; set; }
[Column(Name = "available_at")]
public DateTime AvailableAt { get; set; }
[Column(Name = "expires_at")]
public DateTime ExpiresAt { get; set; }

public DateTime originally_available_at { get; set; }
public DateTime available_at { get; set; }
public DateTime expires_at { get; set; }
// Skip RefreshedAt and Year
[Column(Name = "added_at")]
public DateTime AddedAt { get; set; }
public DateTime added_at { get; set; }
}
}
7 changes: 5 additions & 2 deletions PlexRequests.Store/PlexDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private IDbConnection DbConnection()
{
throw new SqliteException("Factory returned null");
}
fact.ConnectionString = "Data Source=" + "Plex Path";
fact.ConnectionString = "Data Source=" + DbLocation;
return fact;
}

Expand All @@ -84,7 +84,10 @@ public IEnumerable<MetadataItems> QueryMetadataItems(string query, object param)
{
using (var con = DbConnection())
{
return (IEnumerable<MetadataItems>)con.Query(query, param);
con.Open();
var data = con.Query<MetadataItems>(query, param);
con.Close();
return data;
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions PlexRequests.Store/Repository/BaseGenericRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Dapper;
using Dapper.Contrib.Extensions;
using Dapper;

using Mono.Data.Sqlite;

Expand Down

0 comments on commit ec49ab7

Please sign in to comment.