mirror of
https://github.com/Readarr/Readarr.git
synced 2026-04-27 22:56:45 -04:00
@@ -28,59 +28,56 @@ namespace TinyTwitter
|
||||
|
||||
public class TinyTwitter
|
||||
{
|
||||
private readonly OAuthInfo oauth;
|
||||
private readonly OAuthInfo _oauth;
|
||||
|
||||
public TinyTwitter(OAuthInfo oauth)
|
||||
{
|
||||
this.oauth = oauth;
|
||||
_oauth = oauth;
|
||||
}
|
||||
|
||||
public void UpdateStatus(string message)
|
||||
{
|
||||
new RequestBuilder(oauth, "POST", "https://api.twitter.com/1.1/statuses/update.json")
|
||||
new RequestBuilder(_oauth, "POST", "https://api.twitter.com/1.1/statuses/update.json")
|
||||
.AddParameter("status", message)
|
||||
.Execute();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* As of June 26th 2015 Direct Messaging is not part of TinyTwitter.
|
||||
* I have added it to Lidarr's copy to make our implementation easier
|
||||
* and added this banner so it's not blindly updated.
|
||||
*
|
||||
*
|
||||
**/
|
||||
|
||||
public void DirectMessage(string message, string screenName)
|
||||
{
|
||||
new RequestBuilder(oauth, "POST", "https://api.twitter.com/1.1/direct_messages/new.json")
|
||||
new RequestBuilder(_oauth, "POST", "https://api.twitter.com/1.1/direct_messages/new.json")
|
||||
.AddParameter("text", message)
|
||||
.AddParameter("screen_name", screenName)
|
||||
.Execute();
|
||||
}
|
||||
|
||||
#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;
|
||||
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>();
|
||||
_oauth = oauth;
|
||||
_method = method;
|
||||
_url = url;
|
||||
_customParameters = new Dictionary<string, string>();
|
||||
}
|
||||
|
||||
public RequestBuilder AddParameter(string name, string value)
|
||||
{
|
||||
customParameters.Add(name, value.EncodeRFC3986());
|
||||
_customParameters.Add(name, value.EncodeRFC3986());
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -89,14 +86,14 @@ namespace TinyTwitter
|
||||
var timespan = GetTimestamp();
|
||||
var nonce = CreateNonce();
|
||||
|
||||
var parameters = new Dictionary<string, string>(customParameters);
|
||||
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.Method = _method;
|
||||
request.ContentType = "application/x-www-form-urlencoded";
|
||||
|
||||
request.Headers.Add("Authorization", headerValue);
|
||||
@@ -104,9 +101,8 @@ namespace TinyTwitter
|
||||
WriteRequestBody(request);
|
||||
|
||||
// It looks like a bug in HttpWebRequest. It throws random TimeoutExceptions
|
||||
// after some requests. Abort the request seems to work. More info:
|
||||
// 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();
|
||||
|
||||
string content;
|
||||
@@ -126,25 +122,31 @@ namespace TinyTwitter
|
||||
|
||||
private void WriteRequestBody(HttpWebRequest request)
|
||||
{
|
||||
if (method == "GET")
|
||||
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;
|
||||
if (_method != "GET" || _customParameters.Count == 0)
|
||||
{
|
||||
return _url;
|
||||
}
|
||||
|
||||
return string.Format("{0}?{1}", url, GetCustomParametersString());
|
||||
return string.Format("{0}?{1}", _url, GetCustomParametersString());
|
||||
}
|
||||
|
||||
private string GetCustomParametersString()
|
||||
{
|
||||
return customParameters.Select(x => string.Format("{0}={1}", x.Key, x.Value)).Join("&");
|
||||
return _customParameters.Select(x => string.Format("{0}={1}", x.Key, x.Value)).Join("&");
|
||||
}
|
||||
|
||||
private string GenerateAuthorizationHeaderValue(IEnumerable<KeyValuePair<string, string>> parameters, string signature)
|
||||
@@ -160,15 +162,15 @@ namespace TinyTwitter
|
||||
private string GenerateSignature(IEnumerable<KeyValuePair<string, string>> parameters)
|
||||
{
|
||||
var dataToSign = new StringBuilder()
|
||||
.Append(method).Append("&")
|
||||
.Append(url.EncodeRFC3986()).Append("&")
|
||||
.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 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()));
|
||||
@@ -178,11 +180,11 @@ namespace TinyTwitter
|
||||
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_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);
|
||||
parameters.Add("oauth_token", _oauth.AccessToken);
|
||||
}
|
||||
|
||||
private static string GetTimestamp()
|
||||
@@ -195,8 +197,6 @@ namespace TinyTwitter
|
||||
return new Random().Next(0x0000000, 0x7fffffff).ToString("X8");
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
public static class TinyTwitterHelperExtensions
|
||||
@@ -214,9 +214,10 @@ namespace TinyTwitter
|
||||
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);
|
||||
|
||||
@@ -231,4 +232,4 @@ namespace TinyTwitter
|
||||
.Replace("%7E", "~");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user