1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-14 11:23:42 +02:00
Sonarr/NzbDrone.Core/Providers/TwitterProvider.cs

112 lines
3.7 KiB
C#
Raw Normal View History

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;
using NzbDrone.Core.Model;
using NzbDrone.Core.Model.Twitter;
using NzbDrone.Core.Providers.Core;
using Twitterizer;
namespace NzbDrone.Core.Providers
{
public class TwitterProvider
{
2013-02-24 09:48:52 +03:00
private readonly IConfigService _configService;
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)
{
2013-02-24 09:48:52 +03:00
_configService = configService;
}
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;
//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;
//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;
}
}
}
}