pull/591/head^2
Jamie.Rees 9 years ago
parent ec49ab7389
commit 1a0e7cbe7b

@ -28,6 +28,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using PlexRequests.Core.SettingModels; using PlexRequests.Core.SettingModels;
using PlexRequests.Store; using PlexRequests.Store;
using PlexRequests.Store.Models.Plex; using PlexRequests.Store.Models.Plex;
@ -63,11 +64,19 @@ namespace PlexRequests.Core
public IEnumerable<MetadataItems> GetItemsAddedAfterDate(DateTime dateTime) public IEnumerable<MetadataItems> GetItemsAddedAfterDate(DateTime dateTime)
{ {
// type 1 = Movie, type 4 = TV Episode // type 1 = Movie, type 4 = TV Episode
return Plex.QueryMetadataItems(@"SELECT * FROM metadata_items var movies = Plex.QueryMetadataItems(@"SELECT * FROM metadata_items
WHERE added_at > @AddedAt WHERE added_at > @AddedAt
AND metadata_type in (1,4) AND metadata_type = 1
AND title <> ''", AND title <> ''", new { AddedAt = dateTime });
new { AddedAt = dateTime });
// Custom query to include the series title
var tv = Plex.QueryMetadataItems(@"SELECT series.title AS SeriesTitle, mi.* FROM metadata_items mi
INNER JOIN metadata_items season ON mi.parent_id = season.id
INNER JOIN metadata_items series ON series.id = season.parent_id
WHERE mi.added_at > @AddedAt
AND mi.metadata_type = 4", new { AddedAt = dateTime });
return movies.Union(tv);
} }
} }
} }

@ -0,0 +1,71 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: HtmlTemplateGenerator.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.IO;
using System.Text;
using System.Web.UI;
namespace PlexRequests.Services.Jobs
{
public abstract class HtmlTemplateGenerator
{
protected virtual void AddParagraph(ref StringBuilder stringBuilder, string text, int fontSize = 14, string fontWeight = "normal")
{
stringBuilder.AppendFormat("<p style=\"font-family: sans-serif; font-size: {1}px; font-weight: {2}; margin: 0; Margin-bottom: 15px;\">{0}</p>", text, fontSize, fontWeight);
}
protected virtual void AddImageInsideTable(ref StringBuilder sb, string url)
{
sb.Append("<tr>");
sb.Append("<td align=\"center\">");
sb.AppendFormat(
"<img src=\"{0}\" width=\"400px\" text-align=\"center\" />",
url);
sb.Append("</td>");
sb.Append("</tr>");
}
protected virtual void Href(ref StringBuilder sb, string url)
{
sb.AppendFormat("<a href=\"{0}\">", url);
}
protected virtual void EndTag(ref StringBuilder sb, string tag)
{
sb.AppendFormat("</{0}>", tag);
}
protected virtual void Header(ref StringBuilder sb, int size, string text, string fontWeight = "normal")
{
sb.AppendFormat(
"<h{0} style=\"font-family: sans-serif; font-weight: {2}; margin: 0; Margin-bottom: 15px;\">{1}</h{0}>",
size, text, fontWeight);
}
}
}

@ -48,7 +48,7 @@ using Quartz;
namespace PlexRequests.Services.Jobs namespace PlexRequests.Services.Jobs
{ {
public class RecentlyAdded : IJob, IRecentlyAdded public class RecentlyAdded : HtmlTemplateGenerator, IJob, IRecentlyAdded
{ {
public RecentlyAdded(IPlexApi api, ISettingsService<PlexSettings> plexSettings, public RecentlyAdded(IPlexApi api, ISettingsService<PlexSettings> plexSettings,
ISettingsService<EmailNotificationSettings> email, ISettingsService<EmailNotificationSettings> email,
@ -146,7 +146,7 @@ namespace PlexRequests.Services.Jobs
var sb = new StringBuilder(); var sb = new StringBuilder();
var plexSettings = PlexSettings.GetSettings(); var plexSettings = PlexSettings.GetSettings();
var recentlyAdded = PlexDb.GetItemsAddedAfterDate(DateTime.Now.AddDays(-12)).ToList(); var recentlyAdded = PlexDb.GetItemsAddedAfterDate(DateTime.Now.AddDays(-12)).ToList(); // TODO Date configurable
var movies = recentlyAdded.Where(x => x.metadata_type == MetadataTypeMovie); var movies = recentlyAdded.Where(x => x.metadata_type == MetadataTypeMovie);
var tv = recentlyAdded.Where(x => x.metadata_type == MetadataTypeTv); var tv = recentlyAdded.Where(x => x.metadata_type == MetadataTypeTv);
@ -165,7 +165,7 @@ namespace PlexRequests.Services.Jobs
sb.Append("<h1>New Movies:</h1><br/><br/>"); sb.Append("<h1>New Movies:</h1><br/><br/>");
sb.Append( 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%\">"); "<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 movies.OrderByDescending(x => x.addedAt))
{ {
var plexGUID = string.Empty; var plexGUID = string.Empty;
try try
@ -178,36 +178,24 @@ namespace PlexRequests.Services.Jobs
var imdbId = PlexHelper.GetProviderIdFromPlexGuid(plexGUID); var imdbId = PlexHelper.GetProviderIdFromPlexGuid(plexGUID);
var info = _movieApi.GetMovieInformation(imdbId).Result; var info = _movieApi.GetMovieInformation(imdbId).Result;
sb.Append("<tr>"); AddImageInsideTable(ref sb, $"https://image.tmdb.org/t/p/w500{info.BackdropPath}");
sb.Append("<td align=\"center\">");
sb.AppendFormat(
"<img src=\"https://image.tmdb.org/t/p/w500{0}\" width=\"400px\" text-align=\"center\" />",
info.BackdropPath);
sb.Append("</td>");
sb.Append("</tr>");
sb.Append("<tr>"); sb.Append("<tr>");
sb.Append( sb.Append(
"<td align=\"center\" style=\"font-family: sans-serif; font-size: 14px; vertical-align: top;\" valign=\"top\">"); "<td align=\"center\" style=\"font-family: sans-serif; font-size: 14px; vertical-align: top;\" valign=\"top\">");
sb.AppendFormat( Href(ref sb, $"https://www.imdb.com/title/{info.ImdbId}/");
"<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>", Header(ref sb, 3, $"{info.Title} {info.ReleaseDate?.ToString("yyyy") ?? string.Empty}");
info.ImdbId, info.Title, info.ReleaseDate?.ToString("yyyy") ?? string.Empty); EndTag(ref sb, "a");
if (info.Genres.Any()) if (info.Genres.Any())
{ {
sb.AppendFormat( AddParagraph(ref sb, $"Genre: {string.Join(", ", info.Genres.Select(x => x.Name.ToString()).ToArray())}");
"<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;\">{0}</p>",
info.Overview);
sb.Append("<td"); AddParagraph(ref sb, info.Overview);
sb.Append("<hr>");
sb.Append("<br>"); EndLoopHtml(ref sb);
sb.Append("<br>");
sb.Append("</tr>");
} }
catch (Exception e) catch (Exception e)
{ {
@ -229,7 +217,7 @@ namespace PlexRequests.Services.Jobs
sb.Append("<h1>New Movies:</h1><br/><br/>"); sb.Append("<h1>New Movies:</h1><br/><br/>");
sb.Append( 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%\">"); "<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 items) foreach (var movie in items.OrderByDescending(x => x.added_at))
{ {
var plexGUID = string.Empty; var plexGUID = string.Empty;
try try
@ -240,38 +228,32 @@ namespace PlexRequests.Services.Jobs
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 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
AddImageInsideTable(ref sb, $"https://image.tmdb.org/t/p/w500{info.BackdropPath}");
sb.Append("<tr>"); sb.Append("<tr>");
sb.Append("<td align=\"center\">"); sb.Append("<td align=\"center\" style=\"font-family: sans-serif; font-size: 14px; vertical-align: top;\" valign=\"top\">");
sb.AppendFormat(
"<img src=\"https://image.tmdb.org/t/p/w500{0}\" width=\"400px\" text-align=\"center\" />",
info.BackdropPath);
sb.Append("</td>");
sb.Append("</tr>");
sb.Append("<tr>");
sb.Append(
"<td align=\"center\" style=\"font-family: sans-serif; font-size: 14px; vertical-align: top;\" valign=\"top\">");
sb.AppendFormat( Href(ref sb, $"https://www.imdb.com/title/{info.ImdbId}/");
"<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>", var title = string.IsNullOrEmpty(movie.original_title)
imdbId, string.IsNullOrEmpty(movie.original_title) ? movie.title : movie.original_title + $" AKA {movie.title}", movie.originally_available_at); ? $"{movie.title} {movie.originally_available_at:yyyy}"
: $"{movie.original_title} AKA {movie.title} {movie.originally_available_at:yyyy}";
Header(ref sb, 3, title);
EndTag(ref sb, "a");
if (!string.IsNullOrEmpty(movie.tagline)) 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); AddParagraph(ref sb, movie.tagline);
} }
if (!string.IsNullOrEmpty(movie.tags_genre)) 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>", PlexHelper.FormatGenres(movie.tags_genre)); AddParagraph(ref sb, $"Genre: {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>", movie.summary); AddParagraph(ref sb, movie.summary);
sb.Append("<td"); EndLoopHtml(ref sb);
sb.Append("<hr>");
sb.Append("<br>");
sb.Append("<br>");
sb.Append("</tr>");
} }
catch (Exception e) catch (Exception e)
{ {
@ -283,13 +265,14 @@ namespace PlexRequests.Services.Jobs
sb.Append("</table><br/><br/>"); sb.Append("</table><br/><br/>");
} }
[Obsolete("Use the new DB Version")]
private void GenerateTvHtml(IEnumerable<RecentlyAddedChild> tv, PlexSettings plexSettings, ref StringBuilder sb) private void GenerateTvHtml(IEnumerable<RecentlyAddedChild> tv, PlexSettings plexSettings, ref StringBuilder sb)
{ {
// TV // TV
sb.Append("<h1>New Episodes:</h1><br/><br/>"); sb.Append("<h1>New Episodes:</h1><br/><br/>");
sb.Append( 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%\">"); "<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 tv.OrderByDescending(x => x.addedAt))
{ {
var plexGUID = string.Empty; var plexGUID = string.Empty;
try try
@ -349,7 +332,7 @@ namespace PlexRequests.Services.Jobs
sb.Append("<h1>New Episodes:</h1><br/><br/>"); sb.Append("<h1>New Episodes:</h1><br/><br/>");
sb.Append( 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%\">"); "<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 items) foreach (var t in items.OrderByDescending(x => x.added_at))
{ {
var plexGUID = string.Empty; var plexGUID = string.Empty;
try try
@ -373,8 +356,12 @@ namespace PlexRequests.Services.Jobs
sb.Append("<tr>"); sb.Append("<tr>");
sb.Append("<td align=\"center\" style=\"font-family: sans-serif; font-size: 14px; vertical-align: top;\" valign=\"top\">"); 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:yyyy}</p></a>", var title = !string.IsNullOrEmpty(t.SeriesTitle)
info.externals.imdb, string.IsNullOrEmpty(t.original_title) ? t.title : t.original_title + $" AKA {t.title}", t.originally_available_at); // Only the year ? $"{t.SeriesTitle} - {t.title} {t.originally_available_at:yyyy}"
: $"{t.title}";
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}</p></a>",
info.externals.imdb, title);
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); 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);
@ -453,5 +440,15 @@ namespace PlexRequests.Services.Jobs
Log.Error(e); Log.Error(e);
} }
} }
private void EndLoopHtml(ref StringBuilder sb)
{
sb.Append("<td");
sb.Append("<hr>");
sb.Append("<br>");
sb.Append("<br>");
sb.Append("</tr>");
}
} }
} }

@ -80,6 +80,7 @@
<ItemGroup> <ItemGroup>
<Compile Include="Interfaces\IJobRecord.cs" /> <Compile Include="Interfaces\IJobRecord.cs" />
<Compile Include="Interfaces\INotificationEngine.cs" /> <Compile Include="Interfaces\INotificationEngine.cs" />
<Compile Include="Jobs\HtmlTemplateGenerator.cs" />
<Compile Include="Jobs\IRecentlyAdded.cs" /> <Compile Include="Jobs\IRecentlyAdded.cs" />
<Compile Include="Jobs\JobRecord.cs" /> <Compile Include="Jobs\JobRecord.cs" />
<Compile Include="Jobs\JobNames.cs" /> <Compile Include="Jobs\JobNames.cs" />

@ -63,5 +63,6 @@ namespace PlexRequests.Store.Models.Plex
public DateTime expires_at { get; set; } public DateTime expires_at { get; set; }
// Skip RefreshedAt and Year // Skip RefreshedAt and Year
public DateTime added_at { get; set; } public DateTime added_at { get; set; }
public string SeriesTitle { get; set; } // Only used in a custom query for the TV Shows
} }
} }

@ -32,7 +32,7 @@ using Ninject.Planning.Bindings.Resolvers;
using NLog; using NLog;
using Owin; using Owin;
using PlexRequests.Services.Jobs;
using PlexRequests.UI.Helpers; using PlexRequests.UI.Helpers;
using PlexRequests.UI.Jobs; using PlexRequests.UI.Jobs;
using PlexRequests.UI.NinjectModules; using PlexRequests.UI.NinjectModules;
@ -67,6 +67,9 @@ namespace PlexRequests.UI
Debug.WriteLine("Finished bootstrapper"); Debug.WriteLine("Finished bootstrapper");
var scheduler = new Scheduler(); var scheduler = new Scheduler();
scheduler.StartScheduler(); scheduler.StartScheduler();
var r = kernel.Get<IRecentlyAdded>();
r.Test();
} }
catch (Exception exception) catch (Exception exception)
{ {

Loading…
Cancel
Save