2014-03-10 05:18:05 +00: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-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
|
|
|
|
2014-09-07 00:51:44 +00:00
|
|
|
import socket
|
2014-03-10 05:18:05 +00:00
|
|
|
import base64
|
|
|
|
from httplib import HTTPSConnection, HTTPException
|
2014-08-31 16:10:00 +00:00
|
|
|
import json
|
2014-03-10 05:18:05 +00:00
|
|
|
from ssl import SSLError
|
|
|
|
import sickbeard
|
|
|
|
from sickbeard import logger, common
|
|
|
|
|
|
|
|
|
2014-03-25 05:57:24 +00:00
|
|
|
class PushbulletNotifier:
|
2014-03-10 05:18:05 +00:00
|
|
|
def test_notify(self, pushbullet_api):
|
2014-11-12 16:43:14 +00:00
|
|
|
return self._sendPushbullet(pushbullet_api, event="Test", message="Testing Pushbullet settings from SickGear",
|
2014-03-25 05:57:24 +00:00
|
|
|
method="POST", notificationType="note", force=True)
|
2014-03-10 05:18:05 +00: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 11:39:30 +00:00
|
|
|
self._sendPushbullet(pushbullet_api=None, event=common.notifyStrings[common.NOTIFY_SNATCH] + " : " + ep_name, message=ep_name,
|
2014-03-25 05:57:24 +00:00
|
|
|
notificationType="note", method="POST")
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
def notify_download(self, ep_name):
|
|
|
|
if sickbeard.PUSHBULLET_NOTIFY_ONDOWNLOAD:
|
2014-05-18 11:39:30 +00:00
|
|
|
self._sendPushbullet(pushbullet_api=None, event=common.notifyStrings[common.NOTIFY_DOWNLOAD] + " : " + ep_name,
|
2014-03-25 05:57:24 +00:00
|
|
|
message=ep_name, notificationType="note", method="POST")
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
def notify_subtitle_download(self, ep_name, lang):
|
|
|
|
if sickbeard.PUSHBULLET_NOTIFY_ONSUBTITLEDOWNLOAD:
|
2014-05-18 11:39:30 +00:00
|
|
|
self._sendPushbullet(pushbullet_api=None, event=common.notifyStrings[common.NOTIFY_SUBTITLE_DOWNLOAD] + " : " + ep_name + " : " + lang,
|
2014-03-25 05:57:24 +00:00
|
|
|
message=ep_name + ": " + lang, notificationType="note", method="POST")
|
2014-07-03 06:43:48 +00:00
|
|
|
|
2014-07-03 21:04:26 +00:00
|
|
|
def notify_git_update(self, new_version = "??"):
|
2014-07-03 06:43:48 +00:00
|
|
|
if sickbeard.USE_PUSHBULLET:
|
2014-07-03 21:04:26 +00:00
|
|
|
update_text=common.notifyStrings[common.NOTIFY_GIT_UPDATE_TEXT]
|
|
|
|
title=common.notifyStrings[common.NOTIFY_GIT_UPDATE]
|
2014-07-03 06:43:48 +00:00
|
|
|
self._sendPushbullet(pushbullet_api=None, event=title, message=update_text + new_version, method="POST")
|
2014-03-25 05:57:24 +00:00
|
|
|
|
|
|
|
def _sendPushbullet(self, pushbullet_api=None, pushbullet_device=None, event=None, message=None,
|
|
|
|
notificationType=None, method=None, force=False):
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
if not sickbeard.USE_PUSHBULLET and not force:
|
2014-03-25 05:57:24 +00:00
|
|
|
return False
|
|
|
|
|
2014-03-20 18:03:22 +00:00
|
|
|
if pushbullet_api == None:
|
2014-03-10 05:18:05 +00:00
|
|
|
pushbullet_api = sickbeard.PUSHBULLET_API
|
2014-03-20 18:03:22 +00:00
|
|
|
if pushbullet_device == None:
|
2014-03-10 05:18:05 +00:00
|
|
|
pushbullet_device = sickbeard.PUSHBULLET_DEVICE
|
|
|
|
|
|
|
|
if method == 'POST':
|
2014-07-26 19:46:00 +00:00
|
|
|
uri = '/v2/pushes'
|
2014-03-10 05:18:05 +00:00
|
|
|
else:
|
2014-07-28 05:30:13 +00:00
|
|
|
uri = '/v2/devices'
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00: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 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
http_handler = HTTPSConnection("api.pushbullet.com")
|
|
|
|
|
2014-03-20 18:03:22 +00:00
|
|
|
if notificationType == None:
|
2014-03-10 05:18:05 +00:00
|
|
|
testMessage = True
|
|
|
|
try:
|
|
|
|
logger.log(u"Testing Pushbullet authentication and retrieving the device list.", logger.DEBUG)
|
2014-09-18 03:24:27 +00:00
|
|
|
http_handler.request(method, uri, None, headers={'Authorization': 'Bearer %s' % pushbullet_api})
|
2014-09-07 00:51:44 +00:00
|
|
|
except (SSLError, HTTPException, socket.error):
|
2014-03-10 05:18:05 +00:00
|
|
|
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-08-31 16:10:00 +00:00
|
|
|
data = json.dumps(data)
|
|
|
|
http_handler.request(method, uri, body=data,
|
2014-09-18 03:24:27 +00:00
|
|
|
headers={'Content-Type': 'application/json', 'Authorization': 'Bearer %s' % pushbullet_api})
|
2014-03-10 05:18:05 +00:00
|
|
|
pass
|
2014-09-07 00:51:44 +00:00
|
|
|
except (SSLError, HTTPException, socket.error):
|
2014-03-10 05:18:05 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
response = http_handler.getresponse()
|
|
|
|
request_body = response.read()
|
|
|
|
request_status = response.status
|
2014-07-28 05:30:13 +00:00
|
|
|
logger.log(u"Pushbullet response: %s" % request_body, logger.DEBUG)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
if request_status == 200:
|
2014-03-25 05:57:24 +00:00
|
|
|
if testMessage:
|
|
|
|
return request_body
|
|
|
|
else:
|
|
|
|
logger.log(u"Pushbullet notifications sent.", logger.DEBUG)
|
|
|
|
return True
|
2014-03-10 05:18:05 +00:00
|
|
|
elif request_status == 410:
|
2015-02-08 02:59:10 +00:00
|
|
|
logger.log(u"Pushbullet authentication failed: %s" % response.reason, logger.ERROR)
|
2014-03-25 05:57:24 +00:00
|
|
|
return False
|
2014-03-10 05:18:05 +00:00
|
|
|
else:
|
2014-03-25 05:57:24 +00:00
|
|
|
logger.log(u"Pushbullet notification failed.", logger.ERROR)
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
notifier = PushbulletNotifier
|
|
|
|
|