Skip to content

Commit

Permalink
Merge pull request #1362 from tidusjar/EAP
Browse files Browse the repository at this point in the history
Rework sonarr and newsletter fixes
  • Loading branch information
tidusjar authored Apr 9, 2017
2 parents 240ae62 + 2a48ffb commit 574fc38
Show file tree
Hide file tree
Showing 22 changed files with 647 additions and 87 deletions.
3 changes: 3 additions & 0 deletions Ombi.Api.Interfaces/ISonarrApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,8 @@ SonarrAddSeries AddSeriesNew(int tvdbId, string title, int qualityId, bool seaso
Series UpdateSeries(Series series, string apiKey, Uri baseUrl);
SonarrSeasonSearchResult SearchForSeason(int seriesId, int seasonNumber, string apiKey, Uri baseUrl);
SonarrSeriesSearchResult SearchForSeries(int seriesId, string apiKey, Uri baseUrl);


SonarrAddSeries AddSeries(SonarrAddSeries series, string apiKey, Uri baseUrl);
}
}
59 changes: 57 additions & 2 deletions Ombi.Api/SonarrApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,42 @@ public SonarrAddSeries AddSeries(int tvdbId, string title, int qualityId, bool s
return result;
}

public SonarrAddSeries AddSeries(SonarrAddSeries series,string apiKey, Uri baseUrl)
{

var request = new RestRequest
{
Resource = "/api/Series?",
Method = Method.POST
};

Log.Debug("Sonarr API Options:");
Log.Debug(series.DumpJson());

request.AddHeader("X-Api-Key", apiKey);
request.AddJsonBody(series);

SonarrAddSeries result;
try
{
var policy = RetryHandler.RetryAndWaitPolicy((exception, timespan) => Log.Error(exception, "Exception when calling AddSeries for Sonarr, Retrying {0}", timespan), new TimeSpan[] {
TimeSpan.FromSeconds (2)
});

result = policy.Execute(() => Api.ExecuteJson<SonarrAddSeries>(request, baseUrl));
}
catch (JsonSerializationException jse)
{
Log.Error(jse);
var error = Api.ExecuteJson<List<SonarrError>>(request, baseUrl);
var messages = error?.Select(x => x.errorMessage).ToList();
messages?.ForEach(x => Log.Error(x));
result = new SonarrAddSeries { ErrorMessages = messages };
}

return result;
}

public SonarrAddSeries AddSeriesNew(int tvdbId, string title, int qualityId, bool seasonFolders, string rootPath, int[] seasons, string apiKey, Uri baseUrl, bool monitor = true, bool searchForMissingEpisodes = false)
{
var request = new RestRequest
Expand Down Expand Up @@ -244,7 +280,18 @@ public List<Series> GetSeries(string apiKey, Uri baseUrl)
TimeSpan.FromSeconds(5)
});

return policy.Execute(() => Api.ExecuteJson<List<Series>>(request, baseUrl));
var series = policy.Execute(() => Api.ExecuteJson<List<Series>>(request, baseUrl));

// Remove the 'specials from the object'
foreach (var s in series)
{
var seasonToRemove = s.seasons.FirstOrDefault(x => x.seasonNumber == 0);
if (seasonToRemove != null)
{
s.seasons.Remove(seasonToRemove);
}
}
return series;
}
catch (Exception e)
{
Expand All @@ -266,7 +313,15 @@ public Series GetSeries(string seriesId, string apiKey, Uri baseUrl)
Log.Error(exception, "Exception when calling GetSeries by ID for Sonarr, Retrying {0}",
timespan));

return policy.Execute(() => Api.ExecuteJson<Series>(request, baseUrl));
var series = policy.Execute(() => Api.ExecuteJson<Series>(request, baseUrl));

// Remove the specials season
var toRemove = series.seasons.FirstOrDefault(x => x.seasonNumber == 0);
if (toRemove != null)
{
series.seasons.Remove(toRemove);
}
return series;
}
catch (Exception e)
{
Expand Down
2 changes: 1 addition & 1 deletion Ombi.Core.Migration/Migrations/Version2200.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private void UpdateRecentlyAdded(IDbConnection con)
content.Add(new RecentlyAddedLog
{
AddedAt = DateTime.UtcNow,
ProviderId = ep.ProviderId
ProviderId = ep.RatingKey
});
}

Expand Down
134 changes: 134 additions & 0 deletions Ombi.Core.Migration/Migrations/Version2210.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#region Copyright

// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: Version1100.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.Data;
using NLog;
using Ombi.Core.SettingModels;
using Ombi.Store;
using Ombi.Store.Models;
using Ombi.Store.Models.Emby;
using Ombi.Store.Models.Plex;
using Ombi.Store.Repository;
using Quartz.Collection;

namespace Ombi.Core.Migration.Migrations
{
[Migration(22100, "v2.21.0.0")]
public class Version2210 : BaseMigration, IMigration
{
public Version2210(IRepository<RecentlyAddedLog> log,
IRepository<PlexContent> content, IRepository<PlexEpisodes> plexEp, IRepository<EmbyContent> embyContent, IRepository<EmbyEpisodes> embyEp)
{
Log = log;
PlexContent = content;
PlexEpisodes = plexEp;
EmbyContent = embyContent;
EmbyEpisodes = embyEp;
}

public int Version => 22100;
private IRepository<RecentlyAddedLog> Log { get; }
private IRepository<PlexContent> PlexContent { get; }
private IRepository<PlexEpisodes> PlexEpisodes { get; }
private IRepository<EmbyContent> EmbyContent { get; }
private IRepository<EmbyEpisodes> EmbyEpisodes { get; }

public void Start(IDbConnection con)
{
UpdateRecentlyAdded(con);
UpdateSchema(con, Version);

}

private void UpdateRecentlyAdded(IDbConnection con)
{

//Delete the recently added table, lets start again
Log.DeleteAll("RecentlyAddedLog");



// Plex
var plexAllContent = PlexContent.GetAll();
var content = new HashSet<RecentlyAddedLog>();
foreach (var plexContent in plexAllContent)
{
if(plexContent.Type == PlexMediaType.Artist) continue;
content.Add(new RecentlyAddedLog
{
AddedAt = DateTime.UtcNow,
ProviderId = plexContent.ProviderId
});
}
Log.BatchInsert(content, "RecentlyAddedLog");

var plexEpisodeses = PlexEpisodes.GetAll();
content.Clear();
foreach (var ep in plexEpisodeses)
{
content.Add(new RecentlyAddedLog
{
AddedAt = DateTime.UtcNow,
ProviderId = ep.RatingKey
});
}
Log.BatchInsert(content, "RecentlyAddedLog");

// Emby
content.Clear();
var embyContent = EmbyContent.GetAll();
foreach (var plexContent in embyContent)
{
content.Add(new RecentlyAddedLog
{
AddedAt = DateTime.UtcNow,
ProviderId = plexContent.EmbyId
});
}
Log.BatchInsert(content, "RecentlyAddedLog");

var embyEpisodes = EmbyEpisodes.GetAll();
content.Clear();
foreach (var ep in embyEpisodes)
{
content.Add(new RecentlyAddedLog
{
AddedAt = DateTime.UtcNow,
ProviderId = ep.EmbyId
});
}
Log.BatchInsert(content, "RecentlyAddedLog");


}


}
}
1 change: 1 addition & 0 deletions Ombi.Core.Migration/Ombi.Core.Migration.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<Compile Include="MigrationAttribute.cs" />
<Compile Include="MigrationRunner.cs" />
<Compile Include="Migrations\BaseMigration.cs" />
<Compile Include="Migrations\Version2210.cs" />
<Compile Include="Migrations\Version2200.cs" />
<Compile Include="Migrations\Version1100.cs" />
<Compile Include="Migrations\Version195.cs" />
Expand Down
5 changes: 3 additions & 2 deletions Ombi.Core/Ombi.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,9 @@
<Compile Include="StatusChecker\AppveyorArtifactResult.cs" />
<Compile Include="StatusChecker\StatusChecker.cs" />
<Compile Include="StatusChecker\AppveyorBranchResult.cs" />
<Compile Include="TvSender.cs" />
<Compile Include="TvSenderOld.cs" />
<Compile Include="Tv\TvSender.cs" />
<Compile Include="Tv\TvSenderOld.cs" />
<Compile Include="Tv\TvSenderV2.cs" />
<Compile Include="Users\IUserHelper.cs" />
<Compile Include="Users\UserHelper.cs" />
<Compile Include="UserIdentity.cs" />
Expand Down
2 changes: 2 additions & 0 deletions Ombi.Core/TvSender.cs → Ombi.Core/Tv/TvSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public async Task<SonarrAddSeries> SendToSonarr(SonarrSettings sonarrSettings, R

var rootFolderPath = model.RootFolderSelected <= 0 ? sonarrSettings.FullRootPath : await GetRootPath(model.RootFolderSelected, sonarrSettings);



if (episodeRequest)
{
// Does series exist?
Expand Down
File renamed without changes.
Loading

0 comments on commit 574fc38

Please sign in to comment.