mirror of
https://github.com/lidarr/lidarr.git
synced 2025-08-23 14:55:20 -07:00
Merge pull request #1018 from geogolem/useRequestBuilderTrakt
use http request builder (aided by onedrop)
This commit is contained in:
commit
f3b5d9a1d6
2 changed files with 63 additions and 44 deletions
|
@ -10,15 +10,19 @@ namespace NzbDrone.Core.NetImport.Trakt
|
||||||
public override string Name => "Trakt List";
|
public override string Name => "Trakt List";
|
||||||
public override bool Enabled => true;
|
public override bool Enabled => true;
|
||||||
public override bool EnableAuto => false;
|
public override bool EnableAuto => false;
|
||||||
|
private readonly IHttpClient _httpClient;
|
||||||
public IConfigService _configService;
|
public IConfigService _configService;
|
||||||
|
|
||||||
public TraktImport(IHttpClient httpClient, IConfigService configService, IParsingService parsingService, Logger logger)
|
public TraktImport(IHttpClient httpClient, IConfigService configService, IParsingService parsingService, Logger logger)
|
||||||
: base(httpClient, configService, parsingService, logger)
|
: base(httpClient, configService, parsingService, logger)
|
||||||
{ _configService = configService; }
|
{
|
||||||
|
_configService = configService;
|
||||||
|
_httpClient = httpClient;
|
||||||
|
}
|
||||||
|
|
||||||
public override INetImportRequestGenerator GetRequestGenerator()
|
public override INetImportRequestGenerator GetRequestGenerator()
|
||||||
{
|
{
|
||||||
return new TraktRequestGenerator() { Settings = Settings, _configService=_configService };
|
return new TraktRequestGenerator() { Settings = Settings, _configService=_configService, HttpClient = _httpClient, };
|
||||||
}
|
}
|
||||||
|
|
||||||
public override IParseNetImportResponse GetParser()
|
public override IParseNetImportResponse GetParser()
|
||||||
|
|
|
@ -1,15 +1,13 @@
|
||||||
using NzbDrone.Common.Http;
|
using NzbDrone.Common.Http;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Net;
|
using NzbDrone.Common.Serializer;
|
||||||
using System.IO;
|
|
||||||
using Newtonsoft.Json.Linq;
|
|
||||||
using NzbDrone.Core.Configuration;
|
using NzbDrone.Core.Configuration;
|
||||||
|
|
||||||
|
|
||||||
namespace NzbDrone.Core.NetImport.Trakt
|
namespace NzbDrone.Core.NetImport.Trakt
|
||||||
{
|
{
|
||||||
public class refreshRequestResponse
|
public class RefreshRequestResponse
|
||||||
{
|
{
|
||||||
public string access_token { get; set; }
|
public string access_token { get; set; }
|
||||||
public string token_type { get; set; }
|
public string token_type { get; set; }
|
||||||
|
@ -21,8 +19,15 @@ namespace NzbDrone.Core.NetImport.Trakt
|
||||||
public class TraktRequestGenerator : INetImportRequestGenerator
|
public class TraktRequestGenerator : INetImportRequestGenerator
|
||||||
{
|
{
|
||||||
public IConfigService _configService;
|
public IConfigService _configService;
|
||||||
|
public IHttpClient HttpClient { get; set; }
|
||||||
public TraktSettings Settings { get; set; }
|
public TraktSettings Settings { get; set; }
|
||||||
|
|
||||||
|
public string RadarrTraktUrl { get; set; }
|
||||||
|
|
||||||
|
public TraktRequestGenerator()
|
||||||
|
{
|
||||||
|
RadarrTraktUrl = "http://radarr.aeonlucid.com/v1/trakt/refresh?refresh=";
|
||||||
|
}
|
||||||
public virtual NetImportPageableRequestChain GetMovies()
|
public virtual NetImportPageableRequestChain GetMovies()
|
||||||
{
|
{
|
||||||
var pageableRequests = new NetImportPageableRequestChain();
|
var pageableRequests = new NetImportPageableRequestChain();
|
||||||
|
@ -32,6 +37,52 @@ namespace NzbDrone.Core.NetImport.Trakt
|
||||||
return pageableRequests;
|
return pageableRequests;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void Authenticate()
|
||||||
|
{
|
||||||
|
if (_configService.TraktRefreshToken != string.Empty)
|
||||||
|
{
|
||||||
|
//tokens were overwritten with something other than nothing
|
||||||
|
if (_configService.NewTraktTokenExpiry > _configService.TraktTokenExpiry)
|
||||||
|
{
|
||||||
|
//but our refreshedTokens are more current
|
||||||
|
_configService.TraktAuthToken = _configService.NewTraktAuthToken;
|
||||||
|
_configService.TraktRefreshToken = _configService.NewTraktRefreshToken;
|
||||||
|
_configService.TraktTokenExpiry = _configService.NewTraktTokenExpiry;
|
||||||
|
}
|
||||||
|
|
||||||
|
var unixTime = (int)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
|
||||||
|
|
||||||
|
if (unixTime > _configService.TraktTokenExpiry)
|
||||||
|
{
|
||||||
|
var requestBuilder = new HttpRequestBuilder($"{RadarrTraktUrl + _configService.TraktRefreshToken}")
|
||||||
|
{
|
||||||
|
LogResponseContent = true
|
||||||
|
};
|
||||||
|
|
||||||
|
requestBuilder.Method = HttpMethod.GET;
|
||||||
|
|
||||||
|
var authLoginRequest = requestBuilder
|
||||||
|
.SetHeader("Content-Type", "application/json")
|
||||||
|
.Accept(HttpAccept.Json)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
var response = HttpClient.Execute(authLoginRequest);
|
||||||
|
var result = Json.Deserialize<RefreshRequestResponse>(response.Content);
|
||||||
|
|
||||||
|
_configService.TraktAuthToken = result.access_token;
|
||||||
|
_configService.TraktRefreshToken = result.refresh_token;
|
||||||
|
|
||||||
|
//lets have it expire in 8 weeks (4838400 seconds)
|
||||||
|
_configService.TraktTokenExpiry = unixTime + 4838400;
|
||||||
|
|
||||||
|
//store the refreshed tokens in case they get overwritten by an old set of tokens
|
||||||
|
_configService.NewTraktAuthToken = _configService.TraktAuthToken;
|
||||||
|
_configService.NewTraktRefreshToken = _configService.TraktRefreshToken;
|
||||||
|
_configService.NewTraktTokenExpiry = _configService.TraktTokenExpiry;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private IEnumerable<NetImportRequest> GetMovies(string searchParameters)
|
private IEnumerable<NetImportRequest> GetMovies(string searchParameters)
|
||||||
{
|
{
|
||||||
var link = Settings.Link.Trim();
|
var link = Settings.Link.Trim();
|
||||||
|
@ -74,44 +125,8 @@ namespace NzbDrone.Core.NetImport.Trakt
|
||||||
link = link + "/movies/watched/all" + filters;
|
link = link + "/movies/watched/all" + filters;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (_configService.TraktRefreshToken != string.Empty)
|
|
||||||
{
|
|
||||||
//tokens were overwritten with something other than nothing
|
|
||||||
if (_configService.NewTraktTokenExpiry > _configService.TraktTokenExpiry)
|
|
||||||
{
|
|
||||||
//but our refreshedTokens are more current
|
|
||||||
_configService.TraktAuthToken = _configService.NewTraktAuthToken;
|
|
||||||
_configService.TraktRefreshToken = _configService.NewTraktRefreshToken;
|
|
||||||
_configService.TraktTokenExpiry = _configService.NewTraktTokenExpiry;
|
|
||||||
}
|
|
||||||
|
|
||||||
Int32 unixTime= (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
|
Authenticate();
|
||||||
|
|
||||||
if ( unixTime > _configService.TraktTokenExpiry)
|
|
||||||
{
|
|
||||||
var url = "http://radarr.aeonlucid.com/v1/trakt/refresh?refresh="+_configService.TraktRefreshToken;
|
|
||||||
|
|
||||||
HttpWebRequest rquest = (HttpWebRequest)WebRequest.Create(url);
|
|
||||||
string rsponseString = string.Empty;
|
|
||||||
using (HttpWebResponse rsponse = (HttpWebResponse)rquest.GetResponse())
|
|
||||||
using (Stream stream = rsponse.GetResponseStream())
|
|
||||||
using (StreamReader reader = new StreamReader(stream))
|
|
||||||
{
|
|
||||||
rsponseString = reader.ReadToEnd();
|
|
||||||
}
|
|
||||||
refreshRequestResponse j1 = Newtonsoft.Json.JsonConvert.DeserializeObject<refreshRequestResponse>(rsponseString);
|
|
||||||
_configService.TraktAuthToken = j1.access_token;
|
|
||||||
_configService.TraktRefreshToken = j1.refresh_token;
|
|
||||||
|
|
||||||
//lets have it expire in 8 weeks (4838400 seconds)
|
|
||||||
_configService.TraktTokenExpiry = unixTime + 4838400;
|
|
||||||
|
|
||||||
//store the refreshed tokens in case they get overwritten by an old set of tokens
|
|
||||||
_configService.NewTraktAuthToken = _configService.TraktAuthToken;
|
|
||||||
_configService.NewTraktRefreshToken = _configService.TraktRefreshToken;
|
|
||||||
_configService.NewTraktTokenExpiry = _configService.TraktTokenExpiry;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var request = new NetImportRequest($"{link}", HttpAccept.Json);
|
var request = new NetImportRequest($"{link}", HttpAccept.Json);
|
||||||
request.HttpRequest.Headers.Add("trakt-api-version", "2");
|
request.HttpRequest.Headers.Add("trakt-api-version", "2");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue