Skip to content

Commit

Permalink
Start adding a Simkl integration. Copied Trakt integration and starte…
Browse files Browse the repository at this point in the history
…d modifying it...
  • Loading branch information
Freaksed committed Oct 21, 2020
1 parent d835358 commit 5397722
Show file tree
Hide file tree
Showing 30 changed files with 1,387 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/NzbDrone.Core/ImportLists/ImportListType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public enum ImportListType
Program,
TMDB,
Trakt,
Simkl,
Other,
Advanced
}
Expand Down
34 changes: 34 additions & 0 deletions src/NzbDrone.Core/ImportLists/Simkl/List/SimklListImport.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using NLog;
using NzbDrone.Common.Http;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.Notifications.Simkl;
using NzbDrone.Core.Parser;

namespace NzbDrone.Core.ImportLists.Simkl.List
{
public class SimklListImport : SimklImportBase<SimklListSettings>
{
public SimklListImport(IImportListRepository importListRepository,
ISimklProxy simklProxy,
IHttpClient httpClient,
IImportListStatusService importListStatusService,
IConfigService configService,
IParsingService parsingService,
Logger logger)
: base(importListRepository, simklProxy, httpClient, importListStatusService, configService, parsingService, logger)
{
}

public override string Name => "Simkl List";
public override bool Enabled => true;
public override bool EnableAuto => false;

public override IImportListRequestGenerator GetRequestGenerator()
{
return new SimklListRequestGenerator(_SimklProxy)
{
Settings = Settings
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Collections.Generic;
using NzbDrone.Common.Http;
using NzbDrone.Core.Notifications.Simkl;

namespace NzbDrone.Core.ImportLists.Simkl.List
{
public class SimklListRequestGenerator : IImportListRequestGenerator
{
private readonly ISimklProxy _simklProxy;
public SimklListSettings Settings { get; set; }

public SimklListRequestGenerator(ISimklProxy simklProxy)
{
_simklProxy = simklProxy;
}

public virtual ImportListPageableRequestChain GetMovies()
{
var pageableRequests = new ImportListPageableRequestChain();

pageableRequests.Add(GetMoviesRequest());

return pageableRequests;
}

private IEnumerable<ImportListRequest> GetMoviesRequest()
{
var link = string.Empty;

var listName = Parser.Parser.ToUrlSlug(Settings.Listname.Trim());
link += $"users/{Settings.Username.Trim()}/lists/{listName}/items/movies?limit={Settings.Limit}";

var request = new ImportListRequest(_simklProxy.BuildSimklRequest(link, HttpMethod.GET, Settings.AccessToken));

yield return request;
}
}
}
28 changes: 28 additions & 0 deletions src/NzbDrone.Core/ImportLists/Simkl/List/SimklListSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using FluentValidation;
using NzbDrone.Core.Annotations;

namespace NzbDrone.Core.ImportLists.Simkl.List
{
public class SimklListSettingsValidator : SimklSettingsBaseValidator<SimklListSettings>
{
public SimklListSettingsValidator()
: base()
{
}
}

public class SimklListSettings : SimklSettingsBase<SimklListSettings>
{
protected override AbstractValidator<SimklListSettings> Validator => new SimklListSettingsValidator();

public SimklListSettings()
{
}

[FieldDefinition(1, Label = "Username", Privacy = PrivacyLevel.UserName, HelpText = "Username for the List to import from")]
public string Username { get; set; }

[FieldDefinition(2, Label = "List Name", HelpText = "List name for import, list must be public or you must have access to the list")]
public string Listname { get; set; }
}
}
39 changes: 39 additions & 0 deletions src/NzbDrone.Core/ImportLists/Simkl/Popular/SimklPopularImport.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using NLog;
using NzbDrone.Common.Http;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.Notifications.Simkl;
using NzbDrone.Core.Parser;

namespace NzbDrone.Core.ImportLists.Simkl.Popular
{
public class SimklPopularImport : SimklImportBase<SimklPopularSettings>
{
public SimklPopularImport(IImportListRepository importListRepository,
ISimklProxy simklProxy,
IHttpClient httpClient,
IImportListStatusService importListStatusService,
IConfigService configService,
IParsingService parsingService,
Logger logger)
: base(importListRepository, simklProxy, httpClient, importListStatusService, configService, parsingService, logger)
{
}

public override string Name => "Simkl Popular List";
public override bool Enabled => true;
public override bool EnableAuto => false;

public override IParseImportListResponse GetParser()
{
return new SimklPopularParser(Settings);
}

public override IImportListRequestGenerator GetRequestGenerator()
{
return new SimklPopularRequestGenerator(_SimklProxy)
{
Settings = Settings
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Runtime.Serialization;

namespace NzbDrone.Core.ImportLists.Simkl.Popular
{
public enum SimklPopularListType
{
[EnumMember(Value = "Trending Movies")]
Trending = 0,
[EnumMember(Value = "Popular Movies")]
Popular = 1,
[EnumMember(Value = "Top Anticipated Movies")]
Anticipated = 2,
[EnumMember(Value = "Top Box Office Movies")]
BoxOffice = 3,

[EnumMember(Value = "Top Watched Movies By Week")]
TopWatchedByWeek = 4,
[EnumMember(Value = "Top Watched Movies By Month")]
TopWatchedByMonth = 5,
[EnumMember(Value = "Top Watched Movies By Year")]
TopWatchedByYear = 6,
[EnumMember(Value = "Top Watched Movies Of All Time")]
TopWatchedByAllTime = 7
}
}
61 changes: 61 additions & 0 deletions src/NzbDrone.Core/ImportLists/Simkl/Popular/SimklPopularParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System.Collections.Generic;
using Newtonsoft.Json;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.ImportLists.ImportListMovies;
using NzbDrone.Core.Notifications.Simkl.Resource;

namespace NzbDrone.Core.ImportLists.Simkl.Popular
{
public class SimklPopularParser : SimklParser
{
private readonly SimklPopularSettings _settings;
private ImportListResponse _importResponse;

public SimklPopularParser(SimklPopularSettings settings)
{
_settings = settings;
}

public override IList<ImportListMovie> ParseResponse(ImportListResponse importResponse)
{
_importResponse = importResponse;

var movies = new List<ImportListMovie>();

if (!PreProcess(_importResponse))
{
return movies;
}

var jsonResponse = new List<SimklMovieResource>();

if (_settings.SimklListType == (int)SimklPopularListType.Popular)
{
jsonResponse = JsonConvert.DeserializeObject<List<SimklMovieResource>>(_importResponse.Content);
}
else
{
jsonResponse = JsonConvert.DeserializeObject<List<SimklListResource>>(_importResponse.Content).SelectList(c => c.Movie);
}

// no movies were return
if (jsonResponse == null)
{
return movies;
}

foreach (var movie in jsonResponse)
{
movies.AddIfNotNull(new ImportListMovie()
{
Title = movie.Title,
ImdbId = movie.Ids.Imdb,
TmdbId = movie.Ids.Tmdb,
Year = movie.Year ?? 0
});
}

return movies;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System.Collections.Generic;
using NzbDrone.Common.Http;
using NzbDrone.Core.Notifications.Simkl;

namespace NzbDrone.Core.ImportLists.Simkl.Popular
{
public class SimklPopularRequestGenerator : IImportListRequestGenerator
{
private readonly ISimklProxy _simklProxy;
public SimklPopularSettings Settings { get; set; }

public SimklPopularRequestGenerator(ISimklProxy simklProxy)
{
_simklProxy = simklProxy;
}

public virtual ImportListPageableRequestChain GetMovies()
{
var pageableRequests = new ImportListPageableRequestChain();

pageableRequests.Add(GetMoviesRequest());

return pageableRequests;
}

private IEnumerable<ImportListRequest> GetMoviesRequest()
{
var link = string.Empty;

var filtersAndLimit = $"?years={Settings.Years}&genres={Settings.Genres.ToLower()}&ratings={Settings.Rating}&certifications={Settings.Certification.ToLower()}&limit={Settings.Limit}{Settings.SimklAdditionalParameters}";

switch (Settings.SimklListType)
{
case (int)SimklPopularListType.Trending:
link += "movies/trending" + filtersAndLimit;
break;
case (int)SimklPopularListType.Popular:
link += "movies/popular" + filtersAndLimit;
break;
case (int)SimklPopularListType.Anticipated:
link += "movies/anticipated" + filtersAndLimit;
break;
case (int)SimklPopularListType.BoxOffice:
link += "movies/boxoffice" + filtersAndLimit;
break;
case (int)SimklPopularListType.TopWatchedByWeek:
link += "movies/watched/weekly" + filtersAndLimit;
break;
case (int)SimklPopularListType.TopWatchedByMonth:
link += "movies/watched/monthly" + filtersAndLimit;
break;
case (int)SimklPopularListType.TopWatchedByYear:
link += "movies/watched/yearly" + filtersAndLimit;
break;
case (int)SimklPopularListType.TopWatchedByAllTime:
link += "movies/watched/all" + filtersAndLimit;
break;
}

var request = new ImportListRequest(_simklProxy.BuildSimklRequest(link, HttpMethod.GET, Settings.AccessToken));

yield return request;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using FluentValidation;
using NzbDrone.Core.Annotations;

namespace NzbDrone.Core.ImportLists.Simkl.Popular
{
public class SimklPopularSettingsValidator : SimklSettingsBaseValidator<SimklPopularSettings>
{
public SimklPopularSettingsValidator()
: base()
{
RuleFor(c => c.SimklListType).NotNull();
}
}

public class SimklPopularSettings : SimklSettingsBase<SimklPopularSettings>
{
protected override AbstractValidator<SimklPopularSettings> Validator => new SimklPopularSettingsValidator();

public SimklPopularSettings()
{
SimklListType = (int)SimklPopularListType.Popular;
}

[FieldDefinition(1, Label = "List Type", Type = FieldType.Select, SelectOptions = typeof(SimklPopularListType), HelpText = "Type of list you're seeking to import from")]
public int SimklListType { get; set; }
}
}
Loading

0 comments on commit 5397722

Please sign in to comment.