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
|
|
|
|
|
|
|
import socket
|
|
|
|
|
|
|
|
import sickbeard
|
|
|
|
|
|
|
|
from sickbeard import logger, common
|
|
|
|
from sickbeard.exceptions import ex
|
|
|
|
|
|
|
|
from lib.growl import gntp
|
|
|
|
|
|
|
|
|
2014-03-25 05:57:24 +00:00
|
|
|
class GrowlNotifier:
|
2014-03-10 05:18:05 +00:00
|
|
|
def test_notify(self, host, password):
|
|
|
|
self._sendRegistration(host, password, 'Test')
|
2014-11-12 16:43:14 +00:00
|
|
|
return self._sendGrowl("Test Growl", "Testing Growl settings from SickGear", "Test", host, password,
|
2014-03-25 05:57:24 +00:00
|
|
|
force=True)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
def notify_snatch(self, ep_name):
|
|
|
|
if sickbeard.GROWL_NOTIFY_ONSNATCH:
|
|
|
|
self._sendGrowl(common.notifyStrings[common.NOTIFY_SNATCH], ep_name)
|
|
|
|
|
|
|
|
def notify_download(self, ep_name):
|
|
|
|
if sickbeard.GROWL_NOTIFY_ONDOWNLOAD:
|
|
|
|
self._sendGrowl(common.notifyStrings[common.NOTIFY_DOWNLOAD], ep_name)
|
|
|
|
|
|
|
|
def notify_subtitle_download(self, ep_name, lang):
|
|
|
|
if sickbeard.GROWL_NOTIFY_ONSUBTITLEDOWNLOAD:
|
|
|
|
self._sendGrowl(common.notifyStrings[common.NOTIFY_SUBTITLE_DOWNLOAD], ep_name + ": " + lang)
|
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_GROWL:
|
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._sendGrowl(title, update_text + new_version)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-03-25 05:57:24 +00:00
|
|
|
def _send_growl(self, options, message=None):
|
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
#Send Notification
|
|
|
|
notice = gntp.GNTPNotice()
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
#Required
|
2014-03-25 05:57:24 +00:00
|
|
|
notice.add_header('Application-Name', options['app'])
|
|
|
|
notice.add_header('Notification-Name', options['name'])
|
|
|
|
notice.add_header('Notification-Title', options['title'])
|
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
if options['password']:
|
|
|
|
notice.set_password(options['password'])
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
#Optional
|
|
|
|
if options['sticky']:
|
2014-03-25 05:57:24 +00:00
|
|
|
notice.add_header('Notification-Sticky', options['sticky'])
|
2014-03-10 05:18:05 +00:00
|
|
|
if options['priority']:
|
2014-03-25 05:57:24 +00:00
|
|
|
notice.add_header('Notification-Priority', options['priority'])
|
2014-03-10 05:18:05 +00:00
|
|
|
if options['icon']:
|
2014-03-25 05:57:24 +00:00
|
|
|
notice.add_header('Notification-Icon',
|
2014-11-12 16:43:14 +00:00
|
|
|
'https://raw.github.com/SickGear/SickGear/master/gui/slick/images/sickgear.png')
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
if message:
|
2014-03-25 05:57:24 +00:00
|
|
|
notice.add_header('Notification-Text', message)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-03-25 05:57:24 +00:00
|
|
|
response = self._send(options['host'], options['port'], notice.encode(), options['debug'])
|
|
|
|
if isinstance(response, gntp.GNTPOK): return True
|
2014-03-10 05:18:05 +00:00
|
|
|
return False
|
|
|
|
|
2014-03-25 05:57:24 +00:00
|
|
|
def _send(self, host, port, data, debug=False):
|
|
|
|
if debug: print '<Sending>\n', data, '\n</Sending>'
|
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
2014-03-25 05:57:24 +00:00
|
|
|
s.connect((host, port))
|
2014-03-10 05:18:05 +00:00
|
|
|
s.send(data)
|
|
|
|
response = gntp.parse_gntp(s.recv(1024))
|
|
|
|
s.close()
|
2014-03-25 05:57:24 +00:00
|
|
|
|
|
|
|
if debug: print '<Recieved>\n', response, '\n</Recieved>'
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
return response
|
|
|
|
|
2014-11-12 16:43:14 +00:00
|
|
|
def _sendGrowl(self, title="SickGear Notification", message=None, name=None, host=None, password=None,
|
2014-03-25 05:57:24 +00:00
|
|
|
force=False):
|
2014-03-10 05:18:05 +00:00
|
|
|
if not sickbeard.USE_GROWL and not force:
|
|
|
|
return False
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-20 18:03:22 +00:00
|
|
|
if name == None:
|
2014-03-10 05:18:05 +00:00
|
|
|
name = title
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-20 18:03:22 +00:00
|
|
|
if host == None:
|
2014-03-10 05:18:05 +00:00
|
|
|
hostParts = sickbeard.GROWL_HOST.split(':')
|
|
|
|
else:
|
|
|
|
hostParts = host.split(':')
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
if len(hostParts) != 2 or hostParts[1] == '':
|
|
|
|
port = 23053
|
|
|
|
else:
|
|
|
|
port = int(hostParts[1])
|
2014-03-25 05:57:24 +00:00
|
|
|
|
|
|
|
growlHosts = [(hostParts[0], port)]
|
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
opts = {}
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
opts['name'] = name
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
opts['title'] = title
|
2014-11-12 16:43:14 +00:00
|
|
|
opts['app'] = 'SickGear'
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
opts['sticky'] = None
|
|
|
|
opts['priority'] = None
|
|
|
|
opts['debug'] = False
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-20 18:03:22 +00:00
|
|
|
if password == None:
|
2014-03-10 05:18:05 +00:00
|
|
|
opts['password'] = sickbeard.GROWL_PASSWORD
|
|
|
|
else:
|
|
|
|
opts['password'] = password
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
opts['icon'] = True
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
for pc in growlHosts:
|
|
|
|
opts['host'] = pc[0]
|
|
|
|
opts['port'] = pc[1]
|
2014-05-29 00:30:38 +00:00
|
|
|
logger.log(u"GROWL: Sending message '" + message + "' to " + opts['host'] + ":" + str(opts['port']), logger.DEBUG)
|
2014-03-10 05:18:05 +00:00
|
|
|
try:
|
|
|
|
if self._send_growl(opts, message):
|
|
|
|
return True
|
2014-03-25 05:57:24 +00:00
|
|
|
else:
|
2014-03-10 05:18:05 +00:00
|
|
|
if self._sendRegistration(host, password, 'Sickbeard'):
|
|
|
|
return self._send_growl(opts, message)
|
|
|
|
else:
|
|
|
|
return False
|
2015-06-08 12:47:01 +00:00
|
|
|
except Exception as e:
|
2014-05-29 00:30:38 +00:00
|
|
|
logger.log(u"GROWL: Unable to send growl to " + opts['host'] + ":" + str(opts['port']) + " - " + ex(e), logger.WARNING)
|
2014-03-10 05:18:05 +00:00
|
|
|
return False
|
|
|
|
|
2014-11-12 16:43:14 +00:00
|
|
|
def _sendRegistration(self, host=None, password=None, name='SickGear Notification'):
|
2014-03-10 05:18:05 +00:00
|
|
|
opts = {}
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-20 18:03:22 +00:00
|
|
|
if host == None:
|
2014-03-10 05:18:05 +00:00
|
|
|
hostParts = sickbeard.GROWL_HOST.split(':')
|
|
|
|
else:
|
|
|
|
hostParts = host.split(':')
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
if len(hostParts) != 2 or hostParts[1] == '':
|
|
|
|
port = 23053
|
|
|
|
else:
|
|
|
|
port = int(hostParts[1])
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
opts['host'] = hostParts[0]
|
|
|
|
opts['port'] = port
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-20 18:03:22 +00:00
|
|
|
if password == None:
|
2014-03-10 05:18:05 +00:00
|
|
|
opts['password'] = sickbeard.GROWL_PASSWORD
|
|
|
|
else:
|
|
|
|
opts['password'] = password
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-11-12 16:43:14 +00:00
|
|
|
opts['app'] = 'SickGear'
|
2014-03-10 05:18:05 +00:00
|
|
|
opts['debug'] = False
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
#Send Registration
|
|
|
|
register = gntp.GNTPRegister()
|
|
|
|
register.add_header('Application-Name', opts['app'])
|
2014-03-25 05:57:24 +00:00
|
|
|
register.add_header('Application-Icon',
|
2014-11-12 16:43:14 +00:00
|
|
|
'https://raw.githubusercontent.com/SickGear/SickGear/master/gui/slick/images/ico/apple-touch-icon-72x72.png')
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
register.add_notification('Test', True)
|
|
|
|
register.add_notification(common.notifyStrings[common.NOTIFY_SNATCH], True)
|
|
|
|
register.add_notification(common.notifyStrings[common.NOTIFY_DOWNLOAD], True)
|
2014-11-08 12:55:51 +00:00
|
|
|
register.add_notification(common.notifyStrings[common.NOTIFY_GIT_UPDATE], True)
|
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
if opts['password']:
|
|
|
|
register.set_password(opts['password'])
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
try:
|
2014-03-25 05:57:24 +00:00
|
|
|
return self._send(opts['host'], opts['port'], register.encode(), opts['debug'])
|
2015-06-08 12:47:01 +00:00
|
|
|
except Exception as e:
|
2014-05-29 00:30:38 +00:00
|
|
|
logger.log(u"GROWL: Unable to send growl to " + opts['host'] + ":" + str(opts['port']) + " - " + ex(e), logger.WARNING)
|
2014-03-10 05:18:05 +00:00
|
|
|
return False
|
2014-03-25 05:57:24 +00:00
|
|
|
|
|
|
|
|
2014-05-29 00:30:38 +00:00
|
|
|
notifier = GrowlNotifier
|