2014-03-10 05:18:05 +00:00
|
|
|
# Author: Nic Wolfe <nic@wolfeden.ca>
|
|
|
|
# URL: http://code.google.com/p/sickbeard/
|
|
|
|
#
|
2014-11-12 16:43:14 +00:00
|
|
|
# This file is part of SickGear.
|
2014-03-10 05:18:05 +00:00
|
|
|
#
|
2014-11-12 16:43:14 +00:00
|
|
|
# SickGear is free software: you can redistribute it and/or modify
|
2014-03-10 05:18:05 +00:00
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
2014-11-12 16:43:14 +00:00
|
|
|
# SickGear is distributed in the hope that it will be useful,
|
2014-03-10 05:18:05 +00:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2014-11-12 16:43:14 +00:00
|
|
|
# along with SickGear. If not, see <http://www.gnu.org/licenses/>.
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2017-10-17 15:43:28 +00:00
|
|
|
from urlparse import parse_qsl
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2017-10-17 15:43:28 +00:00
|
|
|
import sickbeard
|
2014-03-10 05:18:05 +00:00
|
|
|
from sickbeard.exceptions import ex
|
2017-10-17 15:43:28 +00:00
|
|
|
from sickbeard.notifiers.generic import Notifier
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
import lib.oauth2 as oauth
|
|
|
|
import lib.pythontwitter as twitter
|
|
|
|
|
|
|
|
|
2017-10-17 15:43:28 +00:00
|
|
|
class TwitterNotifier(Notifier):
|
|
|
|
|
2015-08-16 03:23:56 +00:00
|
|
|
consumer_key = 'vHHtcB6WzpWDG6KYlBMr8g'
|
|
|
|
consumer_secret = 'zMqq5CB3f8cWKiRO2KzWPTlBanYmV0VYxSXZ0Pxds0E'
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
REQUEST_TOKEN_URL = 'https://api.twitter.com/oauth/request_token'
|
2014-03-25 05:57:24 +00:00
|
|
|
ACCESS_TOKEN_URL = 'https://api.twitter.com/oauth/access_token'
|
2014-03-10 05:18:05 +00:00
|
|
|
AUTHORIZATION_URL = 'https://api.twitter.com/oauth/authorize'
|
2014-03-25 05:57:24 +00:00
|
|
|
SIGNIN_URL = 'https://api.twitter.com/oauth/authenticate'
|
|
|
|
|
2017-10-17 15:43:28 +00:00
|
|
|
def get_authorization(self):
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2017-10-17 15:43:28 +00:00
|
|
|
# noinspection PyUnusedLocal
|
|
|
|
signature_method_hmac_sha1 = oauth.SignatureMethod_HMAC_SHA1()
|
2014-03-25 05:57:24 +00:00
|
|
|
oauth_consumer = oauth.Consumer(key=self.consumer_key, secret=self.consumer_secret)
|
|
|
|
oauth_client = oauth.Client(oauth_consumer)
|
|
|
|
|
2017-10-17 15:43:28 +00:00
|
|
|
self._log_debug('Requesting temp token from Twitter')
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
resp, content = oauth_client.request(self.REQUEST_TOKEN_URL, 'GET')
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2017-10-17 15:43:28 +00:00
|
|
|
if '200' != resp['status']:
|
|
|
|
self._log_error('Invalid response from Twitter requesting temp token: %s' % resp['status'])
|
2014-03-10 05:18:05 +00:00
|
|
|
else:
|
|
|
|
request_token = dict(parse_qsl(content))
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
sickbeard.TWITTER_USERNAME = request_token['oauth_token']
|
|
|
|
sickbeard.TWITTER_PASSWORD = request_token['oauth_token_secret']
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2015-08-16 03:23:56 +00:00
|
|
|
return self.AUTHORIZATION_URL + '?oauth_token=' + request_token['oauth_token']
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2017-10-17 15:43:28 +00:00
|
|
|
def get_credentials(self, key):
|
|
|
|
request_token = dict(oauth_token=sickbeard.TWITTER_USERNAME, oauth_token_secret=sickbeard.TWITTER_PASSWORD,
|
|
|
|
oauth_callback_confirmed='true')
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
token = oauth.Token(request_token['oauth_token'], request_token['oauth_token_secret'])
|
|
|
|
token.set_verifier(key)
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2017-10-17 15:43:28 +00:00
|
|
|
self._log_debug('Generating and signing request for an access token using key ' + key)
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2017-10-17 15:43:28 +00:00
|
|
|
# noinspection PyUnusedLocal
|
|
|
|
signature_method_hmac_sha1 = oauth.SignatureMethod_HMAC_SHA1()
|
2014-03-25 05:57:24 +00:00
|
|
|
oauth_consumer = oauth.Consumer(key=self.consumer_key, secret=self.consumer_secret)
|
2017-10-17 15:43:28 +00:00
|
|
|
self._log_debug('oauth_consumer: ' + str(oauth_consumer))
|
2014-03-25 05:57:24 +00:00
|
|
|
oauth_client = oauth.Client(oauth_consumer, token)
|
2017-10-17 15:43:28 +00:00
|
|
|
self._log_debug('oauth_client: ' + str(oauth_client))
|
2014-03-10 05:18:05 +00:00
|
|
|
resp, content = oauth_client.request(self.ACCESS_TOKEN_URL, method='POST', body='oauth_verifier=%s' % key)
|
2017-10-17 15:43:28 +00:00
|
|
|
self._log_debug('resp, content: ' + str(resp) + ',' + str(content))
|
2014-03-25 05:57:24 +00:00
|
|
|
|
|
|
|
access_token = dict(parse_qsl(content))
|
2017-10-17 15:43:28 +00:00
|
|
|
self._log_debug('access_token: ' + str(access_token))
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2017-10-17 15:43:28 +00:00
|
|
|
self._log_debug('resp[status] = ' + str(resp['status']))
|
|
|
|
if '200' != resp['status']:
|
|
|
|
self._log_error('The request for a token with did not succeed: ' + str(resp['status']))
|
|
|
|
result = False
|
2014-03-10 05:18:05 +00:00
|
|
|
else:
|
2017-10-17 15:43:28 +00:00
|
|
|
self._log_debug('Your Twitter Access Token key: %s' % access_token['oauth_token'])
|
|
|
|
self._log_debug('Access Token secret: %s' % access_token['oauth_token_secret'])
|
2014-03-10 05:18:05 +00:00
|
|
|
sickbeard.TWITTER_USERNAME = access_token['oauth_token']
|
|
|
|
sickbeard.TWITTER_PASSWORD = access_token['oauth_token_secret']
|
2017-10-17 15:43:28 +00:00
|
|
|
result = True
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2017-10-17 15:43:28 +00:00
|
|
|
message = ('Key verification successful', 'Unable to verify key')[not result]
|
|
|
|
logger.log(u'%s result: %s' % (self.name, message))
|
|
|
|
return self._choose(message, result)
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2017-10-17 15:43:28 +00:00
|
|
|
def _notify(self, title, body, **kwargs):
|
|
|
|
|
|
|
|
# don't use title with updates or testing, as only one str is used
|
|
|
|
body = '::'.join(([], [sickbeard.TWITTER_PREFIX])[bool(sickbeard.TWITTER_PREFIX)]
|
|
|
|
+ [body.replace('#: ', ': ') if 'SickGear' in title else body])
|
2014-03-25 05:57:24 +00:00
|
|
|
|
|
|
|
username = self.consumer_key
|
|
|
|
password = self.consumer_secret
|
|
|
|
access_token_key = sickbeard.TWITTER_USERNAME
|
|
|
|
access_token_secret = sickbeard.TWITTER_PASSWORD
|
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
api = twitter.Api(username, password, access_token_key, access_token_secret)
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
try:
|
2017-10-17 15:43:28 +00:00
|
|
|
api.PostUpdate(body.encode('utf8'))
|
2015-06-08 12:47:01 +00:00
|
|
|
except Exception as e:
|
2017-10-17 15:43:28 +00:00
|
|
|
self._log_error(u'Error sending Tweet: ' + ex(e))
|
2014-03-10 05:18:05 +00:00
|
|
|
return False
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
return True
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-05-29 00:30:38 +00:00
|
|
|
notifier = TwitterNotifier
|