2014-03-10 01:18:05 -04:00
|
|
|
# Author: Pedro Correia (http://github.com/pedrocorreia/)
|
|
|
|
# Based on pushalot.py by 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 base64
|
|
|
|
from httplib import HTTPSConnection, HTTPException
|
|
|
|
from urllib import urlencode
|
|
|
|
from ssl import SSLError
|
|
|
|
import sickbeard
|
|
|
|
from sickbeard import logger, common
|
|
|
|
|
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
class PushbulletNotifier:
|
2014-03-10 01:18:05 -04:00
|
|
|
def test_notify(self, pushbullet_api):
|
2014-05-23 08:37:22 -04:00
|
|
|
return self._sendPushbullet(pushbullet_api, event="Test", message="Testing Pushbullet settings from SickRage",
|
2014-03-25 01:57:24 -04:00
|
|
|
method="POST", notificationType="note", force=True)
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
def get_devices(self, pushbullet_api):
|
|
|
|
return self._sendPushbullet(pushbullet_api, method="GET", force=True)
|
|
|
|
|
|
|
|
def notify_snatch(self, ep_name):
|
|
|
|
if sickbeard.PUSHBULLET_NOTIFY_ONSNATCH:
|
2014-05-18 07:39:30 -04:00
|
|
|
self._sendPushbullet(pushbullet_api=None, event=common.notifyStrings[common.NOTIFY_SNATCH] + " : " + ep_name, message=ep_name,
|
2014-03-25 01:57:24 -04:00
|
|
|
notificationType="note", method="POST")
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
def notify_download(self, ep_name):
|
|
|
|
if sickbeard.PUSHBULLET_NOTIFY_ONDOWNLOAD:
|
2014-05-18 07:39:30 -04:00
|
|
|
self._sendPushbullet(pushbullet_api=None, event=common.notifyStrings[common.NOTIFY_DOWNLOAD] + " : " + ep_name,
|
2014-03-25 01:57:24 -04:00
|
|
|
message=ep_name, notificationType="note", method="POST")
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
def notify_subtitle_download(self, ep_name, lang):
|
|
|
|
if sickbeard.PUSHBULLET_NOTIFY_ONSUBTITLEDOWNLOAD:
|
2014-05-18 07:39:30 -04:00
|
|
|
self._sendPushbullet(pushbullet_api=None, event=common.notifyStrings[common.NOTIFY_SUBTITLE_DOWNLOAD] + " : " + ep_name + " : " + lang,
|
2014-03-25 01:57:24 -04:00
|
|
|
message=ep_name + ": " + lang, notificationType="note", method="POST")
|
|
|
|
|
|
|
|
def _sendPushbullet(self, pushbullet_api=None, pushbullet_device=None, event=None, message=None,
|
|
|
|
notificationType=None, method=None, force=False):
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
if not sickbeard.USE_PUSHBULLET and not force:
|
2014-03-25 01:57:24 -04:00
|
|
|
return False
|
|
|
|
|
2014-03-20 14:03:22 -04:00
|
|
|
if pushbullet_api == None:
|
2014-03-10 01:18:05 -04:00
|
|
|
pushbullet_api = sickbeard.PUSHBULLET_API
|
2014-03-20 14:03:22 -04:00
|
|
|
if pushbullet_device == None:
|
2014-03-10 01:18:05 -04:00
|
|
|
pushbullet_device = sickbeard.PUSHBULLET_DEVICE
|
|
|
|
|
|
|
|
if method == 'POST':
|
|
|
|
uri = '/api/pushes'
|
|
|
|
else:
|
|
|
|
uri = '/api/devices'
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
logger.log(u"Pushbullet event: " + str(event), logger.DEBUG)
|
|
|
|
logger.log(u"Pushbullet message: " + str(message), logger.DEBUG)
|
|
|
|
logger.log(u"Pushbullet api: " + str(pushbullet_api), logger.DEBUG)
|
|
|
|
logger.log(u"Pushbullet devices: " + str(pushbullet_device), logger.DEBUG)
|
|
|
|
logger.log(u"Pushbullet notification type: " + str(notificationType), logger.DEBUG)
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
http_handler = HTTPSConnection("api.pushbullet.com")
|
|
|
|
|
|
|
|
authString = base64.encodestring('%s:' % (pushbullet_api)).replace('\n', '')
|
|
|
|
|
2014-03-20 14:03:22 -04:00
|
|
|
if notificationType == None:
|
2014-03-10 01:18:05 -04:00
|
|
|
testMessage = True
|
|
|
|
try:
|
|
|
|
logger.log(u"Testing Pushbullet authentication and retrieving the device list.", logger.DEBUG)
|
2014-03-25 01:57:24 -04:00
|
|
|
http_handler.request(method, uri, None, headers={'Authorization': 'Basic %s:' % authString})
|
2014-03-10 01:18:05 -04:00
|
|
|
except (SSLError, HTTPException):
|
|
|
|
logger.log(u"Pushbullet notification failed.", logger.ERROR)
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
testMessage = False
|
|
|
|
try:
|
|
|
|
data = {
|
|
|
|
'title': event.encode('utf-8'),
|
|
|
|
'body': message.encode('utf-8'),
|
|
|
|
'device_iden': pushbullet_device,
|
|
|
|
'type': notificationType}
|
2014-03-25 01:57:24 -04:00
|
|
|
http_handler.request(method, uri, body=urlencode(data),
|
|
|
|
headers={'Authorization': 'Basic %s' % authString})
|
2014-03-10 01:18:05 -04:00
|
|
|
pass
|
|
|
|
except (SSLError, HTTPException):
|
|
|
|
return False
|
|
|
|
|
|
|
|
response = http_handler.getresponse()
|
|
|
|
request_body = response.read()
|
|
|
|
request_status = response.status
|
|
|
|
|
|
|
|
if request_status == 200:
|
2014-03-25 01:57:24 -04:00
|
|
|
if testMessage:
|
|
|
|
return request_body
|
|
|
|
else:
|
|
|
|
logger.log(u"Pushbullet notifications sent.", logger.DEBUG)
|
|
|
|
return True
|
2014-03-10 01:18:05 -04:00
|
|
|
elif request_status == 410:
|
2014-03-25 01:57:24 -04:00
|
|
|
logger.log(u"Pushbullet auth failed: %s" % response.reason, logger.ERROR)
|
|
|
|
return False
|
2014-03-10 01:18:05 -04:00
|
|
|
else:
|
2014-03-25 01:57:24 -04:00
|
|
|
logger.log(u"Pushbullet notification failed.", logger.ERROR)
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
notifier = PushbulletNotifier
|
|
|
|
|