Inital work on Twitter notifications
This commit is contained in:
parent
e05365a669
commit
2fbf7a4114
|
@ -11,6 +11,7 @@ using NzbDrone.Common.Reflection;
|
|||
using NzbDrone.Core.ThingiProvider;
|
||||
using NzbDrone.Core.Validation;
|
||||
using Omu.ValueInjecter;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace NzbDrone.Api
|
||||
{
|
||||
|
@ -27,7 +28,8 @@ namespace NzbDrone.Api
|
|||
_providerFactory = providerFactory;
|
||||
|
||||
Get["schema"] = x => GetTemplates();
|
||||
Post["test"] = x => Test(ReadResourceFromRequest());
|
||||
Post["test"] = x => Test(ReadResourceFromRequest(true));
|
||||
Post["connectData/{stage}"] = x => ConnectData(x.stage, ReadResourceFromRequest(true));
|
||||
|
||||
GetResourceAll = GetAll;
|
||||
GetResourceById = GetProviderById;
|
||||
|
@ -98,7 +100,7 @@ namespace NzbDrone.Api
|
|||
_providerFactory.Update(providerDefinition);
|
||||
}
|
||||
|
||||
private TProviderDefinition GetDefinition(TProviderResource providerResource, bool includeWarnings = false)
|
||||
private TProviderDefinition GetDefinition(TProviderResource providerResource, bool includeWarnings = false, bool validate = true)
|
||||
{
|
||||
var definition = new TProviderDefinition();
|
||||
|
||||
|
@ -111,8 +113,10 @@ namespace NzbDrone.Api
|
|||
|
||||
var configContract = ReflectionExtensions.CoreAssembly.FindTypeByName(definition.ConfigContract);
|
||||
definition.Settings = (IProviderConfig)SchemaBuilder.ReadFormSchema(providerResource.Fields, configContract, preset);
|
||||
|
||||
Validate(definition, includeWarnings);
|
||||
if (validate)
|
||||
{
|
||||
Validate(definition, includeWarnings);
|
||||
}
|
||||
|
||||
return definition;
|
||||
}
|
||||
|
@ -163,6 +167,19 @@ namespace NzbDrone.Api
|
|||
return "{}";
|
||||
}
|
||||
|
||||
|
||||
private Response ConnectData(string stage, TProviderResource providerResource)
|
||||
{
|
||||
TProviderDefinition providerDefinition = GetDefinition(providerResource, true, false);
|
||||
|
||||
if (!providerDefinition.Enable) return "{}";
|
||||
|
||||
object data = _providerFactory.ConnectData(providerDefinition, stage, (IDictionary<string, object>) Request.Query.ToDictionary());
|
||||
Response resp = JsonConvert.SerializeObject(data);
|
||||
resp.ContentType = "application/json";
|
||||
return resp;
|
||||
}
|
||||
|
||||
protected virtual void Validate(TProviderDefinition definition, bool includeWarnings)
|
||||
{
|
||||
var validationResult = definition.Settings.Validate();
|
||||
|
|
|
@ -182,7 +182,7 @@ namespace NzbDrone.Api.REST
|
|||
}
|
||||
}
|
||||
|
||||
protected TResource ReadResourceFromRequest()
|
||||
protected TResource ReadResourceFromRequest(bool skipValidate = false)
|
||||
{
|
||||
//TODO: handle when request is null
|
||||
var resource = Request.Body.FromJson<TResource>();
|
||||
|
@ -194,7 +194,7 @@ namespace NzbDrone.Api.REST
|
|||
|
||||
var errors = SharedValidator.Validate(resource).Errors.ToList();
|
||||
|
||||
if (Request.Method.Equals("POST", StringComparison.InvariantCultureIgnoreCase) && !Request.Url.Path.EndsWith("/test", StringComparison.InvariantCultureIgnoreCase))
|
||||
if (Request.Method.Equals("POST", StringComparison.InvariantCultureIgnoreCase) && !skipValidate && !Request.Url.Path.EndsWith("/test", StringComparison.InvariantCultureIgnoreCase))
|
||||
{
|
||||
errors.AddRange(PostValidator.Validate(resource).Errors);
|
||||
}
|
||||
|
|
|
@ -29,5 +29,6 @@ namespace NzbDrone.Core.Annotations
|
|||
Path,
|
||||
Hidden,
|
||||
Tag
|
||||
Action
|
||||
}
|
||||
}
|
|
@ -50,6 +50,8 @@ namespace NzbDrone.Core.Download
|
|||
|
||||
public ProviderDefinition Definition { get; set; }
|
||||
|
||||
public object ConnectData(string stage, IDictionary<string, object> query) { return null; }
|
||||
|
||||
protected TSettings Settings
|
||||
{
|
||||
get
|
||||
|
|
|
@ -65,6 +65,7 @@ namespace NzbDrone.Core.Indexers
|
|||
}
|
||||
|
||||
public virtual ProviderDefinition Definition { get; set; }
|
||||
public object ConnectData(string stage, IDictionary<string, object> query) { return null; }
|
||||
|
||||
protected TSettings Settings
|
||||
{
|
||||
|
|
|
@ -52,6 +52,8 @@ namespace NzbDrone.Core.Metadata
|
|||
public abstract List<ImageFileResult> SeasonImages(Series series, Season season);
|
||||
public abstract List<ImageFileResult> EpisodeImages(Series series, EpisodeFile episodeFile);
|
||||
|
||||
public object ConnectData(string stage, IDictionary<string, object> query) { return null; }
|
||||
|
||||
protected TSettings Settings
|
||||
{
|
||||
get
|
||||
|
|
|
@ -60,5 +60,8 @@ namespace NzbDrone.Core.Notifications
|
|||
{
|
||||
return GetType().Name;
|
||||
}
|
||||
|
||||
public virtual object ConnectData(string stage, IDictionary<string, object> query) { return null; }
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,81 @@
|
|||
using System.Collections.Generic;
|
||||
using FluentValidation.Results;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NzbDrone.Core.Tv;
|
||||
using System;
|
||||
using OAuth;
|
||||
using System.Net;
|
||||
using System.IO;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Twitter
|
||||
{
|
||||
class Twitter : NotificationBase<TwitterSettings>
|
||||
{
|
||||
|
||||
private readonly ITwitterService _TwitterService;
|
||||
|
||||
public Twitter(ITwitterService TwitterService)
|
||||
{
|
||||
_TwitterService = TwitterService;
|
||||
}
|
||||
|
||||
public override string Link
|
||||
{
|
||||
get { return "https://twitter.com/"; }
|
||||
}
|
||||
|
||||
public override void OnGrab(string message)
|
||||
{
|
||||
_TwitterService.SendNotification(message, Settings.AccessToken, Settings.AccessTokenSecret, Settings.ConsumerKey, Settings.ConsumerSecret);
|
||||
}
|
||||
|
||||
public override void OnDownload(DownloadMessage message)
|
||||
{
|
||||
_TwitterService.SendNotification(message.Message, Settings.AccessToken, Settings.AccessTokenSecret, Settings.ConsumerKey, Settings.ConsumerSecret);
|
||||
}
|
||||
|
||||
public override void AfterRename(Series series)
|
||||
{
|
||||
}
|
||||
|
||||
public override object ConnectData(string stage, IDictionary<string, object> query)
|
||||
{
|
||||
if (stage == "step1")
|
||||
{
|
||||
return new
|
||||
{
|
||||
nextStep = "step2",
|
||||
action = "openwindow",
|
||||
url = _TwitterService.GetOAuthRedirect(
|
||||
Settings.ConsumerKey,
|
||||
Settings.ConsumerSecret,
|
||||
"http://localhost:8989/Content/oauthLand.html" /* FIXME - how do I get http host and such */
|
||||
)
|
||||
};
|
||||
}
|
||||
else if (stage == "step2")
|
||||
{
|
||||
return new
|
||||
{
|
||||
action = "updatefields",
|
||||
fields = _TwitterService.GetOAuthToken(
|
||||
Settings.ConsumerKey, Settings.ConsumerSecret,
|
||||
query["oauth_token"].ToString(),
|
||||
query["oauth_verifier"].ToString()
|
||||
)
|
||||
};
|
||||
}
|
||||
return new {};
|
||||
}
|
||||
|
||||
public override ValidationResult Test()
|
||||
{
|
||||
var failures = new List<ValidationFailure>();
|
||||
|
||||
failures.AddIfNotNull(_TwitterService.Test(Settings));
|
||||
|
||||
return new ValidationResult(failures);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,127 @@
|
|||
using FluentValidation.Results;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NLog;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using OAuth;
|
||||
using System.Net;
|
||||
using System.Collections.Specialized;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Twitter
|
||||
{
|
||||
public interface ITwitterService
|
||||
{
|
||||
void SendNotification(string message, String accessToken, String accessTokenSecret, String consumerKey, String consumerSecret);
|
||||
ValidationFailure Test(TwitterSettings settings);
|
||||
string GetOAuthRedirect(string consumerKey, string consumerSecret, string callback);
|
||||
object GetOAuthToken(string consumerKey, string consumerSecret, string oauthToken, string oauthVerifier);
|
||||
}
|
||||
|
||||
public class TwitterService : ITwitterService
|
||||
{
|
||||
private readonly Logger _logger;
|
||||
|
||||
public TwitterService(Logger logger)
|
||||
{
|
||||
_logger = logger;
|
||||
|
||||
var logo = typeof(TwitterService).Assembly.GetManifestResourceBytes("NzbDrone.Core.Resources.Logo.64.png");
|
||||
}
|
||||
|
||||
private NameValueCollection oauthQuery(OAuthRequest client)
|
||||
{
|
||||
// Using HTTP header authorization
|
||||
string auth = client.GetAuthorizationHeader();
|
||||
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(client.RequestUrl);
|
||||
|
||||
request.Headers.Add("Authorization", auth);
|
||||
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
||||
System.Collections.Specialized.NameValueCollection qscoll;
|
||||
using (var reader = new System.IO.StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding("utf-8")))
|
||||
{
|
||||
string responseText = reader.ReadToEnd();
|
||||
return System.Web.HttpUtility.ParseQueryString(responseText);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public object GetOAuthToken(string consumerKey, string consumerSecret, string oauthToken, string oauthVerifier)
|
||||
{
|
||||
// Creating a new instance with a helper method
|
||||
OAuthRequest client = OAuthRequest.ForAccessToken(
|
||||
consumerKey,
|
||||
consumerSecret,
|
||||
oauthToken,
|
||||
"",
|
||||
oauthVerifier
|
||||
);
|
||||
client.RequestUrl = "https://api.twitter.com/oauth/access_token";
|
||||
NameValueCollection qscoll = oauthQuery(client);
|
||||
|
||||
return new
|
||||
{
|
||||
AccessToken = qscoll["oauth_token"],
|
||||
AccessTokenSecret = qscoll["oauth_token_secret"]
|
||||
};
|
||||
}
|
||||
|
||||
public string GetOAuthRedirect(string consumerKey, string consumerSecret, string callback)
|
||||
{
|
||||
// Creating a new instance with a helper method
|
||||
OAuthRequest client = OAuthRequest.ForRequestToken(consumerKey, consumerSecret, callback);
|
||||
client.RequestUrl = "https://api.twitter.com/oauth/request_token";
|
||||
NameValueCollection qscoll = oauthQuery(client);
|
||||
|
||||
return "https://api.twitter.com/oauth/authorize?oauth_token=" + qscoll["oauth_token"];
|
||||
}
|
||||
|
||||
public void SendNotification(string message, String accessToken, String accessTokenSecret, String consumerKey, String consumerSecret)
|
||||
{
|
||||
try
|
||||
{
|
||||
var oauth = new TinyTwitter.OAuthInfo
|
||||
{
|
||||
AccessToken = accessToken,
|
||||
AccessSecret = accessTokenSecret,
|
||||
ConsumerKey = consumerKey,
|
||||
ConsumerSecret = consumerSecret
|
||||
};
|
||||
var twitter = new TinyTwitter.TinyTwitter(oauth);
|
||||
twitter.UpdateStatus(message);
|
||||
}
|
||||
catch (WebException e)
|
||||
{
|
||||
using (WebResponse response = e.Response)
|
||||
{
|
||||
HttpWebResponse httpResponse = (HttpWebResponse)response;
|
||||
Console.WriteLine("Error code: {0}", httpResponse.StatusCode);
|
||||
using (System.IO.Stream data = response.GetResponseStream())
|
||||
using (var reader = new System.IO.StreamReader(data))
|
||||
{
|
||||
string text = reader.ReadToEnd();
|
||||
Console.WriteLine(text);
|
||||
}
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
public ValidationFailure Test(TwitterSettings settings)
|
||||
{
|
||||
try
|
||||
{
|
||||
string body = "This is a test message from Sonarr @ " + DateTime.Now.ToString();
|
||||
SendNotification(body, settings.AccessToken, settings.AccessTokenSecret, settings.ConsumerKey, settings.ConsumerSecret);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.ErrorException("Unable to send test message: " + ex.Message, ex);
|
||||
return new ValidationFailure("Host", "Unable to send test message");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
using System;
|
||||
using FluentValidation;
|
||||
using NzbDrone.Core.Annotations;
|
||||
using NzbDrone.Core.ThingiProvider;
|
||||
using NzbDrone.Core.Validation;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Twitter
|
||||
{
|
||||
public class TwitterSettingsValidator : AbstractValidator<TwitterSettings>
|
||||
{
|
||||
public TwitterSettingsValidator()
|
||||
{
|
||||
RuleFor(c => c.AccessToken).NotEmpty();
|
||||
RuleFor(c => c.AccessTokenSecret).NotEmpty();
|
||||
RuleFor(c => c.ConsumerKey).NotEmpty();
|
||||
RuleFor(c => c.ConsumerSecret).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class TwitterSettings : IProviderConfig
|
||||
{
|
||||
private static readonly TwitterSettingsValidator Validator = new TwitterSettingsValidator();
|
||||
|
||||
public TwitterSettings()
|
||||
{
|
||||
ConsumerKey = "3POVsO3KW90LKZXyzPOjQ"; /* FIXME - Key from Couchpotato so needs to be replaced */
|
||||
ConsumerSecret = "Qprb94hx9ucXvD4Wvg2Ctsk4PDK7CcQAKgCELXoyIjE"; /* FIXME - Key from Couchpotato so needs to be replaced */
|
||||
AuthorizeNotification = "step1";
|
||||
}
|
||||
|
||||
[FieldDefinition(0, Label = "Access Token", Advanced = true)]
|
||||
public String AccessToken { get; set; }
|
||||
|
||||
[FieldDefinition(1, Label = "Access Token Secret", Advanced = true)]
|
||||
public String AccessTokenSecret { get; set; }
|
||||
|
||||
public String ConsumerKey { get; set; }
|
||||
public String ConsumerSecret { get; set; }
|
||||
|
||||
[FieldDefinition(4, Label = "Connect to twitter", Type = FieldType.Action)]
|
||||
public String AuthorizeNotification { get; set; }
|
||||
|
||||
public bool IsValid
|
||||
{
|
||||
get
|
||||
{
|
||||
return !string.IsNullOrWhiteSpace(AccessToken) && !string.IsNullOrWhiteSpace(AccessTokenSecret) &&
|
||||
!string.IsNullOrWhiteSpace(ConsumerKey) && !string.IsNullOrWhiteSpace(ConsumerSecret);
|
||||
}
|
||||
}
|
||||
|
||||
public NzbDroneValidationResult Validate()
|
||||
{
|
||||
return new NzbDroneValidationResult(Validator.Validate(this));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -80,6 +80,9 @@
|
|||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\packages\Newtonsoft.Json.6.0.6\lib\net40\Newtonsoft.Json.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="OAuth">
|
||||
<HintPath>..\packages\OAuth.1.0.3\lib\net40\OAuth.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="RestSharp, Version=105.0.1.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\packages\RestSharp.105.0.1\lib\net4\RestSharp.dll</HintPath>
|
||||
|
@ -93,6 +96,7 @@
|
|||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Drawing" />
|
||||
<Reference Include="System.Web" />
|
||||
<Reference Include="System.Web.Extensions" />
|
||||
<Reference Include="System.Windows.Forms" />
|
||||
<Reference Include="System.Xml" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
|
@ -740,6 +744,9 @@
|
|||
<Compile Include="Notifications\Synology\SynologyIndexerProxy.cs" />
|
||||
<Compile Include="Notifications\Synology\SynologyIndexerSettings.cs" />
|
||||
<Compile Include="Organizer\NamingConfigRepository.cs" />
|
||||
<Compile Include="Notifications\Twitter\Twitter.cs" />
|
||||
<Compile Include="Notifications\Twitter\TwitterService.cs" />
|
||||
<Compile Include="Notifications\Twitter\TwitterSettings.cs" />
|
||||
<Compile Include="Profiles\Delay\DelayProfile.cs" />
|
||||
<Compile Include="Profiles\Delay\DelayProfileService.cs" />
|
||||
<Compile Include="Profiles\Delay\DelayProfileTagInUseValidator.cs" />
|
||||
|
@ -916,6 +923,7 @@
|
|||
<Compile Include="ThingiProvider\ProviderFactory.cs" />
|
||||
<Compile Include="ThingiProvider\ProviderMessage.cs" />
|
||||
<Compile Include="ThingiProvider\ProviderRepository.cs" />
|
||||
<Compile Include="TinyTwitter.cs" />
|
||||
<Compile Include="Tv\Actor.cs" />
|
||||
<Compile Include="Tv\AddSeriesOptions.cs" />
|
||||
<Compile Include="Tv\Commands\MoveSeriesCommand.cs" />
|
||||
|
|
|
@ -12,5 +12,6 @@ namespace NzbDrone.Core.ThingiProvider
|
|||
IEnumerable<ProviderDefinition> DefaultDefinitions { get; }
|
||||
ProviderDefinition Definition { get; set; }
|
||||
ValidationResult Test();
|
||||
object ConnectData(string stage, IDictionary<string, object> query);
|
||||
}
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
using System.Collections.Generic;
|
||||
using FluentValidation.Results;
|
||||
using System;
|
||||
|
||||
namespace NzbDrone.Core.ThingiProvider
|
||||
{
|
||||
|
@ -18,5 +19,6 @@ namespace NzbDrone.Core.ThingiProvider
|
|||
TProviderDefinition GetProviderCharacteristics(TProvider provider, TProviderDefinition definition);
|
||||
TProvider GetInstance(TProviderDefinition definition);
|
||||
ValidationResult Test(TProviderDefinition definition);
|
||||
object ConnectData(TProviderDefinition definition, string stage, IDictionary<string, object> query );
|
||||
}
|
||||
}
|
|
@ -81,6 +81,11 @@ namespace NzbDrone.Core.ThingiProvider
|
|||
return GetInstance(definition).Test();
|
||||
}
|
||||
|
||||
public object ConnectData(TProviderDefinition definition, string stage, IDictionary<string, object> query)
|
||||
{
|
||||
return GetInstance(definition).ConnectData(stage, query);
|
||||
}
|
||||
|
||||
public List<TProvider> GetAvailableProviders()
|
||||
{
|
||||
return Active().Select(GetInstance).ToList();
|
||||
|
|
|
@ -0,0 +1,262 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Web.Script.Serialization;
|
||||
|
||||
namespace TinyTwitter
|
||||
{
|
||||
public class OAuthInfo
|
||||
{
|
||||
public string ConsumerKey { get; set; }
|
||||
public string ConsumerSecret { get; set; }
|
||||
public string AccessToken { get; set; }
|
||||
public string AccessSecret { get; set; }
|
||||
}
|
||||
|
||||
public class Tweet
|
||||
{
|
||||
public long Id { get; set; }
|
||||
public DateTime CreatedAt { get; set; }
|
||||
public string UserName { get; set; }
|
||||
public string ScreenName { get; set; }
|
||||
public string Text { get; set; }
|
||||
}
|
||||
|
||||
public class TinyTwitter
|
||||
{
|
||||
private readonly OAuthInfo oauth;
|
||||
|
||||
public TinyTwitter(OAuthInfo oauth)
|
||||
{
|
||||
this.oauth = oauth;
|
||||
}
|
||||
|
||||
public void UpdateStatus(string message)
|
||||
{
|
||||
new RequestBuilder(oauth, "POST", "https://api.twitter.com/1.1/statuses/update.json")
|
||||
.AddParameter("status", message)
|
||||
.Execute();
|
||||
}
|
||||
|
||||
public IEnumerable<Tweet> GetHomeTimeline(long? sinceId = null, int? count = 20)
|
||||
{
|
||||
return GetTimeline("http://api.twitter.com/1.1/statuses/home_timeline.json", sinceId, count);
|
||||
}
|
||||
|
||||
public IEnumerable<Tweet> GetMentions(long? sinceId = null, int? count = 20)
|
||||
{
|
||||
return GetTimeline("http://api.twitter.com/1.1/statuses/mentions.json", sinceId, count);
|
||||
}
|
||||
|
||||
public IEnumerable<Tweet> GetUserTimeline(long? sinceId = null, int? count = 20)
|
||||
{
|
||||
return GetTimeline("http://api.twitter.com/1.1/statuses/user_timeline.json", sinceId, count);
|
||||
}
|
||||
|
||||
private IEnumerable<Tweet> GetTimeline(string url, long? sinceId, int? count)
|
||||
{
|
||||
var builder = new RequestBuilder(oauth, "GET", url);
|
||||
|
||||
if (sinceId.HasValue)
|
||||
builder.AddParameter("since_id", sinceId.Value.ToString());
|
||||
|
||||
if (count.HasValue)
|
||||
builder.AddParameter("count", count.Value.ToString());
|
||||
|
||||
using (var response = builder.Execute())
|
||||
using (var stream = response.GetResponseStream())
|
||||
using (var reader = new StreamReader(stream))
|
||||
{
|
||||
var content = reader.ReadToEnd();
|
||||
var serializer = new JavaScriptSerializer();
|
||||
|
||||
var tweets = (object[])serializer.DeserializeObject(content);
|
||||
|
||||
return tweets.Cast<Dictionary<string, object>>().Select(tweet =>
|
||||
{
|
||||
var user = ((Dictionary<string, object>)tweet["user"]);
|
||||
var date = DateTime.ParseExact(tweet["created_at"].ToString(),
|
||||
"ddd MMM dd HH:mm:ss zz00 yyyy",
|
||||
CultureInfo.InvariantCulture).ToLocalTime();
|
||||
return new Tweet
|
||||
{
|
||||
Id = (long)tweet["id"],
|
||||
CreatedAt =
|
||||
date,
|
||||
Text = (string)tweet["text"],
|
||||
UserName = (string)user["name"],
|
||||
ScreenName = (string)user["screen_name"]
|
||||
};
|
||||
}).ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
#region RequestBuilder
|
||||
|
||||
public class RequestBuilder
|
||||
{
|
||||
private const string VERSION = "1.0";
|
||||
private const string SIGNATURE_METHOD = "HMAC-SHA1";
|
||||
|
||||
private readonly OAuthInfo oauth;
|
||||
private readonly string method;
|
||||
private readonly IDictionary<string, string> customParameters;
|
||||
private readonly string url;
|
||||
|
||||
public RequestBuilder(OAuthInfo oauth, string method, string url)
|
||||
{
|
||||
this.oauth = oauth;
|
||||
this.method = method;
|
||||
this.url = url;
|
||||
customParameters = new Dictionary<string, string>();
|
||||
}
|
||||
|
||||
public RequestBuilder AddParameter(string name, string value)
|
||||
{
|
||||
customParameters.Add(name, value.EncodeRFC3986());
|
||||
return this;
|
||||
}
|
||||
|
||||
public WebResponse Execute()
|
||||
{
|
||||
var timespan = GetTimestamp();
|
||||
var nonce = CreateNonce();
|
||||
|
||||
var parameters = new Dictionary<string, string>(customParameters);
|
||||
AddOAuthParameters(parameters, timespan, nonce);
|
||||
|
||||
var signature = GenerateSignature(parameters);
|
||||
var headerValue = GenerateAuthorizationHeaderValue(parameters, signature);
|
||||
|
||||
var request = (HttpWebRequest)WebRequest.Create(GetRequestUrl());
|
||||
request.Method = method;
|
||||
request.ContentType = "application/x-www-form-urlencoded";
|
||||
|
||||
request.Headers.Add("Authorization", headerValue);
|
||||
|
||||
WriteRequestBody(request);
|
||||
|
||||
// It looks like a bug in HttpWebRequest. It throws random TimeoutExceptions
|
||||
// after some requests. Abort the request seems to work. More info:
|
||||
// http://stackoverflow.com/questions/2252762/getrequeststream-throws-timeout-exception-randomly
|
||||
|
||||
var response = request.GetResponse();
|
||||
request.Abort();
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
private void WriteRequestBody(HttpWebRequest request)
|
||||
{
|
||||
if (method == "GET")
|
||||
return;
|
||||
|
||||
var requestBody = Encoding.ASCII.GetBytes(GetCustomParametersString());
|
||||
using (var stream = request.GetRequestStream())
|
||||
stream.Write(requestBody, 0, requestBody.Length);
|
||||
}
|
||||
|
||||
private string GetRequestUrl()
|
||||
{
|
||||
if (method != "GET" || customParameters.Count == 0)
|
||||
return url;
|
||||
|
||||
return string.Format("{0}?{1}", url, GetCustomParametersString());
|
||||
}
|
||||
|
||||
private string GetCustomParametersString()
|
||||
{
|
||||
return customParameters.Select(x => string.Format("{0}={1}", x.Key, x.Value)).Join("&");
|
||||
}
|
||||
|
||||
private string GenerateAuthorizationHeaderValue(IEnumerable<KeyValuePair<string, string>> parameters, string signature)
|
||||
{
|
||||
return new StringBuilder("OAuth ")
|
||||
.Append(parameters.Concat(new KeyValuePair<string, string>("oauth_signature", signature))
|
||||
.Where(x => x.Key.StartsWith("oauth_"))
|
||||
.Select(x => string.Format("{0}=\"{1}\"", x.Key, x.Value.EncodeRFC3986()))
|
||||
.Join(","))
|
||||
.ToString();
|
||||
}
|
||||
|
||||
private string GenerateSignature(IEnumerable<KeyValuePair<string, string>> parameters)
|
||||
{
|
||||
var dataToSign = new StringBuilder()
|
||||
.Append(method).Append("&")
|
||||
.Append(url.EncodeRFC3986()).Append("&")
|
||||
.Append(parameters
|
||||
.OrderBy(x => x.Key)
|
||||
.Select(x => string.Format("{0}={1}", x.Key, x.Value))
|
||||
.Join("&")
|
||||
.EncodeRFC3986());
|
||||
|
||||
var signatureKey = string.Format("{0}&{1}", oauth.ConsumerSecret.EncodeRFC3986(), oauth.AccessSecret.EncodeRFC3986());
|
||||
var sha1 = new HMACSHA1(Encoding.ASCII.GetBytes(signatureKey));
|
||||
|
||||
var signatureBytes = sha1.ComputeHash(Encoding.ASCII.GetBytes(dataToSign.ToString()));
|
||||
return Convert.ToBase64String(signatureBytes);
|
||||
}
|
||||
|
||||
private void AddOAuthParameters(IDictionary<string, string> parameters, string timestamp, string nonce)
|
||||
{
|
||||
parameters.Add("oauth_version", VERSION);
|
||||
parameters.Add("oauth_consumer_key", oauth.ConsumerKey);
|
||||
parameters.Add("oauth_nonce", nonce);
|
||||
parameters.Add("oauth_signature_method", SIGNATURE_METHOD);
|
||||
parameters.Add("oauth_timestamp", timestamp);
|
||||
parameters.Add("oauth_token", oauth.AccessToken);
|
||||
}
|
||||
|
||||
private static string GetTimestamp()
|
||||
{
|
||||
return ((int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds).ToString();
|
||||
}
|
||||
|
||||
private static string CreateNonce()
|
||||
{
|
||||
return new Random().Next(0x0000000, 0x7fffffff).ToString("X8");
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
public static class TinyTwitterHelperExtensions
|
||||
{
|
||||
public static string Join<T>(this IEnumerable<T> items, string separator)
|
||||
{
|
||||
return string.Join(separator, items.ToArray());
|
||||
}
|
||||
|
||||
public static IEnumerable<T> Concat<T>(this IEnumerable<T> items, T value)
|
||||
{
|
||||
return items.Concat(new[] { value });
|
||||
}
|
||||
|
||||
public static string EncodeRFC3986(this string value)
|
||||
{
|
||||
// From Twitterizer http://www.twitterizer.net/
|
||||
|
||||
if (string.IsNullOrEmpty(value))
|
||||
return string.Empty;
|
||||
|
||||
var encoded = Uri.EscapeDataString(value);
|
||||
|
||||
return Regex
|
||||
.Replace(encoded, "(%[0-9a-f][0-9a-f])", c => c.Value.ToUpper())
|
||||
.Replace("(", "%28")
|
||||
.Replace(")", "%29")
|
||||
.Replace("$", "%24")
|
||||
.Replace("!", "%21")
|
||||
.Replace("*", "%2A")
|
||||
.Replace("'", "%27")
|
||||
.Replace("%7E", "~");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -7,7 +7,9 @@
|
|||
<package id="ImageResizer" version="3.4.3" targetFramework="net40" />
|
||||
<package id="Newtonsoft.Json" version="6.0.6" targetFramework="net40" />
|
||||
<package id="NLog" version="2.1.0" targetFramework="net40" />
|
||||
<package id="OAuth" version="1.0.3" targetFramework="net40" />
|
||||
<package id="Prowlin" version="0.9.4456.26422" targetFramework="net40" />
|
||||
<package id="RestSharp" version="105.0.1" targetFramework="net40" />
|
||||
<package id="ValueInjecter" version="2.3.3" targetFramework="net40" />
|
||||
<package id="TinyTwitter" version="1.1.1" targetFramework="net40" />
|
||||
</packages>
|
|
@ -0,0 +1,13 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>oauth landing page</title>
|
||||
<script><!--
|
||||
window.opener.onCompleteOauth(window.location.search, function() { window.close(); });
|
||||
--></script>
|
||||
</head>
|
||||
<body>
|
||||
Shouldn't see this
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,7 @@
|
|||
<div class="form-group {{#if advanced}}advanced-setting{{/if}}">
|
||||
<label class="col-sm-3 control-label"></label>
|
||||
|
||||
<div class="col-sm-5">
|
||||
<button class="form-control x-path {{name}}" data-value="{{value}}">{{label}}</button>
|
||||
</div>
|
||||
</div>
|
|
@ -29,6 +29,10 @@ var _fieldBuilder = function(field) {
|
|||
return _templateRenderer.call(field, 'Form/SelectTemplate');
|
||||
}
|
||||
|
||||
if (field.type === 'hidden') {
|
||||
return _templateRenderer.call(field, 'Form/HiddenTemplate');
|
||||
}
|
||||
|
||||
if (field.type === 'path') {
|
||||
return _templateRenderer.call(field, 'Form/PathTemplate');
|
||||
}
|
||||
|
@ -37,6 +41,11 @@ var _fieldBuilder = function(field) {
|
|||
return _templateRenderer.call(field, 'Form/TagTemplate');
|
||||
}
|
||||
|
||||
if (field.type === 'action') {
|
||||
return _templateRenderer.call(field, 'Form/ActionTemplate');
|
||||
}
|
||||
|
||||
|
||||
return _templateRenderer.call(field, 'Form/TextboxTemplate');
|
||||
};
|
||||
|
||||
|
|
|
@ -1,8 +1 @@
|
|||
<div class="form-group hidden">
|
||||
<label class="col-sm-3 control-label">{{label}}</label>
|
||||
|
||||
<div class="col-sm-5">
|
||||
<input type="text" name="fields.{{order}}.value" validation-name="{{name}}" spellcheck="false" class="form-control"/>
|
||||
</div>
|
||||
{{> FormHelpPartial}}
|
||||
</div>
|
||||
<input type="hidden" name="fields.{{order}}.value" validation-name="{{name}}" spellcheck="false"/>
|
|
@ -13,8 +13,11 @@ var view = Marionette.ItemView.extend({
|
|||
template : 'Settings/Notifications/Edit/NotificationEditViewTemplate',
|
||||
|
||||
ui : {
|
||||
onDownloadToggle : '.x-on-download',
|
||||
onUpgradeSection : '.x-on-upgrade',
|
||||
onDownloadToggle : '.x-on-download',
|
||||
onUpgradeSection : '.x-on-upgrade',
|
||||
tags : '.x-tags',
|
||||
indicator : '.x-indicator',
|
||||
authorizedNotificationButton : '.AuthorizeNotification'
|
||||
tags : '.x-tags',
|
||||
modalBody : '.modal-body',
|
||||
formTag : '.x-form-tag',
|
||||
|
@ -23,7 +26,8 @@ var view = Marionette.ItemView.extend({
|
|||
|
||||
events : {
|
||||
'click .x-back' : '_back',
|
||||
'change .x-on-download' : '_onDownloadChanged'
|
||||
'change .x-on-download' : '_onDownloadChanged',
|
||||
'click .AuthorizeNotification' : '_onAuthorizeNotification'
|
||||
},
|
||||
|
||||
_deleteView : DeleteView,
|
||||
|
@ -81,7 +85,15 @@ var view = Marionette.ItemView.extend({
|
|||
} else {
|
||||
this.ui.onUpgradeSection.hide();
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
_onAuthorizeNotification : function(e) {
|
||||
var self = this;
|
||||
self.ui.indicator.show();
|
||||
this.model.connectData(this.ui.authorizedNotificationButton.data('value')).always(function(newValues) {
|
||||
self.ui.indicator.hide();
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
AsModelBoundView.call(view);
|
||||
|
|
|
@ -3,6 +3,66 @@ var DeepModel = require('backbone.deepmodel');
|
|||
var Messenger = require('../Shared/Messenger');
|
||||
|
||||
module.exports = DeepModel.extend({
|
||||
connectData : function(action) {
|
||||
var self = this;
|
||||
|
||||
this.trigger('connect:sync');
|
||||
|
||||
var promise = $.Deferred();
|
||||
|
||||
var callAction = function(action) {
|
||||
var params = {};
|
||||
params.url = self.collection.url + '/connectData/' + action;
|
||||
params.contentType = 'application/json';
|
||||
params.data = JSON.stringify(self.toJSON());
|
||||
params.type = 'POST';
|
||||
params.isValidatedCall = true;
|
||||
|
||||
$.ajax(params).fail(promise.reject).success(function(response) {
|
||||
if (response.action)
|
||||
{
|
||||
|
||||
if (response.action === "openwindow")
|
||||
{
|
||||
var connectResponseWindow = window.open(response.url);
|
||||
var selfWindow = window;
|
||||
selfWindow.onCompleteOauth = function(query, callback) {
|
||||
delete selfWindow.onCompleteOauth;
|
||||
if (response.nextStep) { callAction(response.nextStep + query); }
|
||||
else { promise.resolve(response); }
|
||||
callback();
|
||||
};
|
||||
return;
|
||||
}
|
||||
else if (response.action === "updatefields")
|
||||
{
|
||||
Object.keys(response.fields).forEach(function(field) {
|
||||
self.set(field, response.fields[field]);
|
||||
self.attributes.fields.forEach(function(fieldDef) {
|
||||
if (fieldDef.name === field) { fieldDef.value = response.fields[field]; }
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
if (response.nextStep) { callAction(response.nextStep); }
|
||||
else { promise.resolve(response); }
|
||||
});
|
||||
};
|
||||
|
||||
callAction(action);
|
||||
|
||||
Messenger.monitor({
|
||||
promise : promise,
|
||||
successMessage : 'Connecting for \'{0}\' completed'.format(this.get('name')),
|
||||
errorMessage : 'Connecting for \'{0}\' failed'.format(this.get('name'))
|
||||
});
|
||||
|
||||
promise.fail(function(response) {
|
||||
self.trigger('connect:failed', response);
|
||||
});
|
||||
|
||||
return promise;
|
||||
},
|
||||
test : function() {
|
||||
var self = this;
|
||||
|
||||
|
|
Loading…
Reference in New Issue