2014-03-10 01:18:05 -04:00
|
|
|
# Author: Nic Wolfe <nic@wolfeden.ca>
|
|
|
|
# URL: http://code.google.com/p/sickbeard/
|
|
|
|
#
|
2014-05-23 08:37:22 -04:00
|
|
|
# This file is part of SickRage.
|
2014-03-10 01:18:05 -04:00
|
|
|
#
|
2014-05-23 08:37:22 -04:00
|
|
|
# SickRage is free software: you can redistribute it and/or modify
|
2014-03-10 01:18:05 -04: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-05-23 08:37:22 -04:00
|
|
|
# SickRage is distributed in the hope that it will be useful,
|
2014-03-10 01:18:05 -04: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-05-23 08:37:22 -04:00
|
|
|
# along with SickRage. If not, see <http://www.gnu.org/licenses/>.
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
import sickbeard
|
|
|
|
|
|
|
|
from sickbeard import logger, common
|
|
|
|
from sickbeard.exceptions import ex
|
|
|
|
|
|
|
|
# parse_qsl moved to urlparse module in v2.6
|
|
|
|
try:
|
2014-03-25 01:57:24 -04:00
|
|
|
from urlparse import parse_qsl #@UnusedImport
|
2014-03-10 01:18:05 -04:00
|
|
|
except:
|
2014-03-25 01:57:24 -04:00
|
|
|
from cgi import parse_qsl #@Reimport
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
import lib.oauth2 as oauth
|
|
|
|
import lib.pythontwitter as twitter
|
|
|
|
|
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
class TwitterNotifier:
|
2014-03-10 01:18:05 -04:00
|
|
|
consumer_key = "vHHtcB6WzpWDG6KYlBMr8g"
|
|
|
|
consumer_secret = "zMqq5CB3f8cWKiRO2KzWPTlBanYmV0VYxSXZ0Pxds0E"
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
REQUEST_TOKEN_URL = 'https://api.twitter.com/oauth/request_token'
|
2014-03-25 01:57:24 -04:00
|
|
|
ACCESS_TOKEN_URL = 'https://api.twitter.com/oauth/access_token'
|
2014-03-10 01:18:05 -04:00
|
|
|
AUTHORIZATION_URL = 'https://api.twitter.com/oauth/authorize'
|
2014-03-25 01:57:24 -04:00
|
|
|
SIGNIN_URL = 'https://api.twitter.com/oauth/authenticate'
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def notify_snatch(self, ep_name):
|
|
|
|
if sickbeard.TWITTER_NOTIFY_ONSNATCH:
|
2014-03-25 01:57:24 -04:00
|
|
|
self._notifyTwitter(common.notifyStrings[common.NOTIFY_SNATCH] + ': ' + ep_name)
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
def notify_download(self, ep_name):
|
|
|
|
if sickbeard.TWITTER_NOTIFY_ONDOWNLOAD:
|
2014-03-25 01:57:24 -04:00
|
|
|
self._notifyTwitter(common.notifyStrings[common.NOTIFY_DOWNLOAD] + ': ' + ep_name)
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def notify_subtitle_download(self, ep_name, lang):
|
|
|
|
if sickbeard.TWITTER_NOTIFY_ONSUBTITLEDOWNLOAD:
|
2014-03-25 01:57:24 -04:00
|
|
|
self._notifyTwitter(common.notifyStrings[common.NOTIFY_SUBTITLE_DOWNLOAD] + ' ' + ep_name + ": " + lang)
|
2014-07-03 02:43:48 -04:00
|
|
|
|
2014-07-03 17:04:26 -04:00
|
|
|
def notify_git_update(self, new_version = "??"):
|
2014-07-03 02:43:48 -04:00
|
|
|
if sickbeard.USE_TWITTER:
|
2014-07-03 17:04:26 -04:00
|
|
|
update_text=common.notifyStrings[common.NOTIFY_GIT_UPDATE_TEXT]
|
|
|
|
title=common.notifyStrings[common.NOTIFY_GIT_UPDATE]
|
2014-07-03 02:43:48 -04:00
|
|
|
self._notifyTwitter(title + " - " + update_text + new_version)
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
def test_notify(self):
|
2014-05-23 08:37:22 -04:00
|
|
|
return self._notifyTwitter("This is a test notification from SickRage", force=True)
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
def _get_authorization(self):
|
2014-03-25 01:57:24 -04:00
|
|
|
|
|
|
|
signature_method_hmac_sha1 = oauth.SignatureMethod_HMAC_SHA1() #@UnusedVariable
|
|
|
|
oauth_consumer = oauth.Consumer(key=self.consumer_key, secret=self.consumer_secret)
|
|
|
|
oauth_client = oauth.Client(oauth_consumer)
|
|
|
|
|
2014-05-28 20:30:38 -04:00
|
|
|
logger.log('Requesting temp token from Twitter', logger.DEBUG)
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
resp, content = oauth_client.request(self.REQUEST_TOKEN_URL, 'GET')
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
if resp['status'] != '200':
|
2014-05-28 20:30:38 -04:00
|
|
|
logger.log('Invalid response from Twitter requesting temp token: %s' % resp['status'], logger.ERROR)
|
2014-03-10 01:18:05 -04:00
|
|
|
else:
|
|
|
|
request_token = dict(parse_qsl(content))
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
sickbeard.TWITTER_USERNAME = request_token['oauth_token']
|
|
|
|
sickbeard.TWITTER_PASSWORD = request_token['oauth_token_secret']
|
2014-03-25 01:57:24 -04:00
|
|
|
|
|
|
|
return self.AUTHORIZATION_URL + "?oauth_token=" + request_token['oauth_token']
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def _get_credentials(self, key):
|
|
|
|
request_token = {}
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
request_token['oauth_token'] = sickbeard.TWITTER_USERNAME
|
|
|
|
request_token['oauth_token_secret'] = sickbeard.TWITTER_PASSWORD
|
|
|
|
request_token['oauth_callback_confirmed'] = 'true'
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
token = oauth.Token(request_token['oauth_token'], request_token['oauth_token_secret'])
|
|
|
|
token.set_verifier(key)
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-05-28 20:30:38 -04:00
|
|
|
logger.log('Generating and signing request for an access token using key ' + key, logger.DEBUG)
|
2014-03-25 01:57:24 -04:00
|
|
|
|
|
|
|
signature_method_hmac_sha1 = oauth.SignatureMethod_HMAC_SHA1() #@UnusedVariable
|
|
|
|
oauth_consumer = oauth.Consumer(key=self.consumer_key, secret=self.consumer_secret)
|
2014-05-28 20:30:38 -04:00
|
|
|
logger.log('oauth_consumer: ' + str(oauth_consumer), logger.DEBUG)
|
2014-03-25 01:57:24 -04:00
|
|
|
oauth_client = oauth.Client(oauth_consumer, token)
|
2014-05-28 20:30:38 -04:00
|
|
|
logger.log('oauth_client: ' + str(oauth_client), logger.DEBUG)
|
2014-03-10 01:18:05 -04:00
|
|
|
resp, content = oauth_client.request(self.ACCESS_TOKEN_URL, method='POST', body='oauth_verifier=%s' % key)
|
2014-05-28 20:30:38 -04:00
|
|
|
logger.log('resp, content: ' + str(resp) + ',' + str(content), logger.DEBUG)
|
2014-03-25 01:57:24 -04:00
|
|
|
|
|
|
|
access_token = dict(parse_qsl(content))
|
2014-05-28 20:30:38 -04:00
|
|
|
logger.log('access_token: ' + str(access_token), logger.DEBUG)
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-05-28 20:30:38 -04:00
|
|
|
logger.log('resp[status] = ' + str(resp['status']), logger.DEBUG)
|
2014-03-10 01:18:05 -04:00
|
|
|
if resp['status'] != '200':
|
2014-03-25 01:57:24 -04:00
|
|
|
logger.log('The request for a token with did not succeed: ' + str(resp['status']), logger.ERROR)
|
2014-03-10 01:18:05 -04:00
|
|
|
return False
|
|
|
|
else:
|
2014-05-28 20:30:38 -04:00
|
|
|
logger.log('Your Twitter Access Token key: %s' % access_token['oauth_token'], logger.DEBUG)
|
|
|
|
logger.log('Access Token secret: %s' % access_token['oauth_token_secret'], logger.DEBUG)
|
2014-03-10 01:18:05 -04:00
|
|
|
sickbeard.TWITTER_USERNAME = access_token['oauth_token']
|
|
|
|
sickbeard.TWITTER_PASSWORD = access_token['oauth_token_secret']
|
|
|
|
return True
|
2014-03-25 01:57:24 -04:00
|
|
|
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def _send_tweet(self, message=None):
|
2014-03-25 01:57:24 -04:00
|
|
|
|
|
|
|
username = self.consumer_key
|
|
|
|
password = self.consumer_secret
|
|
|
|
access_token_key = sickbeard.TWITTER_USERNAME
|
|
|
|
access_token_secret = sickbeard.TWITTER_PASSWORD
|
|
|
|
|
2014-05-28 20:30:38 -04:00
|
|
|
logger.log(u"Sending tweet: " + message, logger.DEBUG)
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
api = twitter.Api(username, password, access_token_key, access_token_secret)
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
try:
|
|
|
|
api.PostUpdate(message.encode('utf8'))
|
|
|
|
except Exception, e:
|
2014-03-25 01:57:24 -04:00
|
|
|
logger.log(u"Error Sending Tweet: " + ex(e), logger.ERROR)
|
2014-03-10 01:18:05 -04:00
|
|
|
return False
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
return True
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def _notifyTwitter(self, message='', force=False):
|
|
|
|
prefix = sickbeard.TWITTER_PREFIX
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
if not sickbeard.USE_TWITTER and not force:
|
|
|
|
return False
|
2014-03-25 01:57:24 -04:00
|
|
|
|
|
|
|
return self._send_tweet(prefix + ": " + message)
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-05-28 20:30:38 -04:00
|
|
|
notifier = TwitterNotifier
|