2011-10-28 10:57:00 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Text;
|
|
|
|
using NLog;
|
2013-02-24 09:48:52 +03:00
|
|
|
using NzbDrone.Core.Configuration;
|
2011-10-28 10:57:00 +03:00
|
|
|
using NzbDrone.Core.Model;
|
|
|
|
using NzbDrone.Core.Model.Twitter;
|
|
|
|
using Twitterizer;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Providers
|
|
|
|
{
|
|
|
|
public class TwitterProvider
|
|
|
|
{
|
2013-02-24 09:48:52 +03:00
|
|
|
private readonly IConfigService _configService;
|
2011-10-28 10:57:00 +03:00
|
|
|
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
|
|
private const string ConsumerKey = "umKU6jBWpFbHTuqQbW2VlQ";
|
|
|
|
private const string ConsumerSecret = "e30OXkI6qrZWS35hbUUnrQQ8J2R9XNpccQNWAVK10";
|
|
|
|
|
2013-02-24 09:48:52 +03:00
|
|
|
public TwitterProvider(IConfigService configService)
|
2011-10-28 10:57:00 +03:00
|
|
|
{
|
2013-02-24 09:48:52 +03:00
|
|
|
_configService = configService;
|
2011-10-28 10:57:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public virtual TwitterAuthorizationModel GetAuthorization()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
OAuthTokenResponse requestToken = OAuthUtility.GetRequestToken(ConsumerKey, ConsumerSecret, "oob", null);
|
|
|
|
Uri authorizationUri = OAuthUtility.BuildAuthorizationUri(requestToken.Token);
|
|
|
|
|
|
|
|
return new TwitterAuthorizationModel
|
|
|
|
{
|
|
|
|
Token = requestToken.Token,
|
|
|
|
Url = authorizationUri.ToString()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
Logger.Warn("Failed to get Twitter authorization URL.");
|
|
|
|
Logger.TraceException(ex.Message, ex);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual bool GetAndSaveAccessToken(string authToken, string verifier)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Logger.Debug("Attempting to get the AccessToken from Twitter");
|
|
|
|
|
|
|
|
OAuthTokenResponse accessToken = OAuthUtility.GetAccessToken(ConsumerKey, ConsumerSecret, authToken, verifier);
|
|
|
|
|
2013-02-24 09:48:52 +03:00
|
|
|
_configService.TwitterAccessToken = accessToken.Token;
|
|
|
|
_configService.TwitterAccessTokenSecret = accessToken.TokenSecret;
|
2011-10-28 10:57:00 +03:00
|
|
|
|
|
|
|
//Send a tweet to test!
|
|
|
|
SendTweet("I have just setup tweet notifications for NzbDrone!");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
Logger.TraceException(ex.Message, ex);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual bool SendTweet(string message)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Logger.Trace("Sending status update to twitter: {0}", message);
|
|
|
|
|
2013-02-24 09:48:52 +03:00
|
|
|
var accessToken = _configService.TwitterAccessToken;
|
|
|
|
var accessTokenSecret = _configService.TwitterAccessTokenSecret;
|
2011-10-28 10:57:00 +03:00
|
|
|
|
|
|
|
//If the access token or access token secret are not configured, log an error and return
|
|
|
|
if (String.IsNullOrWhiteSpace(accessToken) || String.IsNullOrWhiteSpace(accessTokenSecret))
|
|
|
|
{
|
|
|
|
Logger.Warn("Twitter Setup is incomplete, please check your settings");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var token = new OAuthTokens
|
|
|
|
{
|
|
|
|
AccessToken = accessToken,
|
|
|
|
AccessTokenSecret = accessTokenSecret,
|
|
|
|
ConsumerKey = ConsumerKey,
|
|
|
|
ConsumerSecret = ConsumerSecret
|
|
|
|
};
|
|
|
|
|
|
|
|
TwitterStatus.Update(token, message + " #NzbDrone");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
Logger.DebugException(ex.Message, ex);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|