Skip to content

Commit

Permalink
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 ec49ab7 commit 1a0e7cb
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 57 deletions.
17 changes: 13 additions & 4 deletions PlexRequests.Core/PlexReadOnlyDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using PlexRequests.Core.SettingModels;
using PlexRequests.Store;
using PlexRequests.Store.Models.Plex;
Expand Down Expand Up @@ -63,11 +64,19 @@ public PlexReadOnlyDatabase(IPlexDatabase plexDatabase, ISettingsService<PlexSet
public IEnumerable<MetadataItems> GetItemsAddedAfterDate(DateTime dateTime)
{
// 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
AND metadata_type in (1,4)
AND title <> ''",
new { AddedAt = dateTime });
AND metadata_type = 1
AND title <> ''", 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);
}
}
}
71 changes: 71 additions & 0 deletions PlexRequests.Services/Jobs/HtmlTemplateGenerator.cs
Original file line number Diff line number Diff line change
@@ -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);
}


}
}
101 changes: 49 additions & 52 deletions PlexRequests.Services/Jobs/RecentlyAdded.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

namespace PlexRequests.Services.Jobs
{
public class RecentlyAdded : IJob, IRecentlyAdded
public class RecentlyAdded : HtmlTemplateGenerator, IJob, IRecentlyAdded
{
public RecentlyAdded(IPlexApi api, ISettingsService<PlexSettings> plexSettings,
ISettingsService<EmailNotificationSettings> email,
Expand Down Expand Up @@ -146,7 +146,7 @@ private void StartDb(bool testEmail = false)
var sb = new StringBuilder();
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 tv = recentlyAdded.Where(x => x.metadata_type == MetadataTypeTv);
Expand All @@ -160,12 +160,12 @@ private void StartDb(bool testEmail = false)
Send(html, plexSettings, testEmail);
}

private void GenerateMovieHtml(IEnumerable<RecentlyAddedChild> movies, PlexSettings plexSettings,ref StringBuilder sb)
private void GenerateMovieHtml(IEnumerable<RecentlyAddedChild> movies, PlexSettings plexSettings, ref StringBuilder sb)
{
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 movies.OrderByDescending(x => x.addedAt))
{
var plexGUID = string.Empty;
try
Expand All @@ -178,36 +178,24 @@ private void GenerateMovieHtml(IEnumerable<RecentlyAddedChild> movies, PlexSetti
var imdbId = PlexHelper.GetProviderIdFromPlexGuid(plexGUID);
var info = _movieApi.GetMovieInformation(imdbId).Result;

sb.Append("<tr>");
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>");
AddImageInsideTable(ref sb, $"https://image.tmdb.org/t/p/w500{info.BackdropPath}");

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.ImdbId, info.Title, info.ReleaseDate?.ToString("yyyy") ?? string.Empty);
Href(ref sb, $"https://www.imdb.com/title/{info.ImdbId}/");
Header(ref sb, 3, $"{info.Title} {info.ReleaseDate?.ToString("yyyy") ?? string.Empty}");
EndTag(ref sb, "a");

if (info.Genres.Any())
{
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()));
AddParagraph(ref sb, $"Genre: {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");
sb.Append("<hr>");
sb.Append("<br>");
sb.Append("<br>");
sb.Append("</tr>");
AddParagraph(ref sb, info.Overview);

EndLoopHtml(ref sb);
}
catch (Exception e)
{
Expand All @@ -229,49 +217,43 @@ private void GenerateMovieHtml(IEnumerable<MetadataItems> movies, ref StringBuil
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 items)
foreach (var movie in items.OrderByDescending(x => x.added_at))
{
var plexGUID = string.Empty;
try
{
plexGUID = movie.guid;

var imdbId = PlexHelper.GetProviderIdFromPlexGuid(plexGUID);

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("<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(
"<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>",
imdbId, string.IsNullOrEmpty(movie.original_title) ? movie.title : movie.original_title + $" AKA {movie.title}", movie.originally_available_at);
Href(ref sb, $"https://www.imdb.com/title/{info.ImdbId}/");
var title = string.IsNullOrEmpty(movie.original_title)
? $"{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))
{
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))
{
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");
sb.Append("<hr>");
sb.Append("<br>");
sb.Append("<br>");
sb.Append("</tr>");
EndLoopHtml(ref sb);
}
catch (Exception e)
{
Expand All @@ -283,13 +265,14 @@ private void GenerateMovieHtml(IEnumerable<MetadataItems> movies, ref StringBuil
sb.Append("</table><br/><br/>");
}

[Obsolete("Use the new DB Version")]
private void GenerateTvHtml(IEnumerable<RecentlyAddedChild> tv, PlexSettings plexSettings, ref StringBuilder sb)
{
// 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 tv.OrderByDescending(x => x.addedAt))
{
var plexGUID = string.Empty;
try
Expand Down Expand Up @@ -349,12 +332,12 @@ private void GenerateTvHtml(IEnumerable<MetadataItems> tv, ref StringBuilder sb)
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 items)
foreach (var t in items.OrderByDescending(x => x.added_at))
{
var plexGUID = string.Empty;
try
{

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

Expand All @@ -373,8 +356,12 @@ 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: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
var title = !string.IsNullOrEmpty(t.SeriesTitle)
? $"{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);

Expand Down Expand Up @@ -453,5 +440,15 @@ private void Send(string html, PlexSettings plexSettings, bool testEmail = false
Log.Error(e);
}
}

private void EndLoopHtml(ref StringBuilder sb)
{
sb.Append("<td");
sb.Append("<hr>");
sb.Append("<br>");
sb.Append("<br>");
sb.Append("</tr>");
}

}
}
1 change: 1 addition & 0 deletions PlexRequests.Services/PlexRequests.Services.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
<ItemGroup>
<Compile Include="Interfaces\IJobRecord.cs" />
<Compile Include="Interfaces\INotificationEngine.cs" />
<Compile Include="Jobs\HtmlTemplateGenerator.cs" />
<Compile Include="Jobs\IRecentlyAdded.cs" />
<Compile Include="Jobs\JobRecord.cs" />
<Compile Include="Jobs\JobNames.cs" />
Expand Down
1 change: 1 addition & 0 deletions PlexRequests.Store/Models/Plex/MetadataItems.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,6 @@ public class MetadataItems
public DateTime expires_at { get; set; }
// Skip RefreshedAt and Year
public DateTime added_at { get; set; }
public string SeriesTitle { get; set; } // Only used in a custom query for the TV Shows
}
}
5 changes: 4 additions & 1 deletion PlexRequests.UI/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
using NLog;

using Owin;

using PlexRequests.Services.Jobs;
using PlexRequests.UI.Helpers;
using PlexRequests.UI.Jobs;
using PlexRequests.UI.NinjectModules;
Expand Down Expand Up @@ -67,6 +67,9 @@ public void Configuration(IAppBuilder app)
Debug.WriteLine("Finished bootstrapper");
var scheduler = new Scheduler();
scheduler.StartScheduler();

var r = kernel.Get<IRecentlyAdded>();
r.Test();
}
catch (Exception exception)
{
Expand Down

0 comments on commit 1a0e7cb

Please sign in to comment.