2014-03-10 01:18:05 -04:00
|
|
|
# Author: Marvin Pinto <me@marvinp.ca>
|
|
|
|
# Author: Dennis Lutter <lad1337@gmail.com>
|
|
|
|
# Author: Aaron Bieber <deftly@gmail.com>
|
|
|
|
# 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
|
2014-06-19 12:42:20 -04:00
|
|
|
# GNU General Public License for more details.
|
2014-03-10 01:18:05 -04:00
|
|
|
#
|
|
|
|
# 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-06-19 12:42:20 -04:00
|
|
|
import httplib
|
2014-03-10 01:18:05 -04:00
|
|
|
import urllib, urllib2
|
|
|
|
import time
|
|
|
|
|
|
|
|
import sickbeard
|
|
|
|
from sickbeard import logger
|
2014-07-03 17:04:26 -04:00
|
|
|
from sickbeard.common import notifyStrings, NOTIFY_SNATCH, NOTIFY_DOWNLOAD, NOTIFY_SUBTITLE_DOWNLOAD, NOTIFY_GIT_UPDATE, NOTIFY_GIT_UPDATE_TEXT
|
2014-03-10 01:18:05 -04:00
|
|
|
from sickbeard.exceptions import ex
|
|
|
|
|
|
|
|
API_URL = "https://api.pushover.net/1/messages.json"
|
|
|
|
|
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
class PushoverNotifier:
|
2014-06-17 16:37:09 -04:00
|
|
|
def test_notify(self, userKey=None, apiKey=None):
|
2014-06-18 19:11:20 -04:00
|
|
|
return self._notifyPushover("This is a test notification from SickRage", 'Test', userKey, apiKey, force=True)
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-06-17 16:37:09 -04:00
|
|
|
def _sendPushover(self, msg, title, userKey=None, apiKey=None):
|
2014-03-10 01:18:05 -04:00
|
|
|
"""
|
|
|
|
Sends a pushover notification to the address provided
|
|
|
|
|
|
|
|
msg: The message to send (unicode)
|
|
|
|
title: The title of the message
|
|
|
|
userKey: The pushover user id to send the message to (or to subscribe with)
|
|
|
|
|
|
|
|
returns: True if the message succeeded, False otherwise
|
|
|
|
"""
|
|
|
|
|
2014-06-18 19:13:16 -04:00
|
|
|
if userKey == None:
|
2014-03-10 01:18:05 -04:00
|
|
|
userKey = sickbeard.PUSHOVER_USERKEY
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-06-18 19:13:16 -04:00
|
|
|
if apiKey == None:
|
|
|
|
apiKey = sickbeard.PUSHOVER_APIKEY
|
2014-06-17 16:37:09 -04:00
|
|
|
|
|
|
|
logger.log("Pushover API KEY in use: " + apiKey, logger.DEBUG)
|
2014-06-19 12:42:20 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
# build up the URL and parameters
|
|
|
|
msg = msg.strip()
|
|
|
|
|
|
|
|
# send the request to pushover
|
|
|
|
try:
|
2014-06-19 12:42:20 -04:00
|
|
|
conn = httplib.HTTPSConnection("api.pushover.net:443")
|
|
|
|
conn.request("POST", "/1/messages.json",
|
|
|
|
urllib.urlencode({
|
|
|
|
"token": apiKey,
|
|
|
|
"user": userKey,
|
|
|
|
"title": title.encode('utf-8'),
|
|
|
|
"message": msg.encode('utf-8'),
|
|
|
|
'timestamp': int(time.time()),
|
|
|
|
"retry": 60,
|
|
|
|
"expire": 3600,
|
|
|
|
}), {"Content-type": "application/x-www-form-urlencoded"})
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-05-26 02:29:22 -04:00
|
|
|
except urllib2.HTTPError, e:
|
2014-03-10 01:18:05 -04:00
|
|
|
# if we get an error back that doesn't have an error code then who knows what's really happening
|
|
|
|
if not hasattr(e, 'code'):
|
|
|
|
logger.log("Pushover notification failed." + ex(e), logger.ERROR)
|
|
|
|
return False
|
|
|
|
else:
|
2014-05-28 20:30:38 -04:00
|
|
|
logger.log("Pushover notification failed. Error code: " + str(e.code), logger.ERROR)
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
# HTTP status 404 if the provided email address isn't a Pushover user.
|
|
|
|
if e.code == 404:
|
|
|
|
logger.log("Username is wrong/not a pushover email. Pushover will send an email to it", logger.WARNING)
|
|
|
|
return False
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
# For HTTP status code 401's, it is because you are passing in either an invalid token, or the user has not added your service.
|
|
|
|
elif e.code == 401:
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
#HTTP status 401 if the user doesn't have the service added
|
2014-06-17 16:37:09 -04:00
|
|
|
subscribeNote = self._sendPushover(msg, title, userKey, apiKey)
|
2014-03-10 01:18:05 -04:00
|
|
|
if subscribeNote:
|
|
|
|
logger.log("Subscription send", logger.DEBUG)
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
logger.log("Subscription could not be send", logger.ERROR)
|
|
|
|
return False
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
# If you receive an HTTP status code of 400, it is because you failed to send the proper parameters
|
|
|
|
elif e.code == 400:
|
|
|
|
logger.log("Wrong data sent to pushover", logger.ERROR)
|
|
|
|
return False
|
|
|
|
|
2014-06-18 16:50:48 -04:00
|
|
|
# If you receive a HTTP status code of 429, it is because the message limit has been reached (free limit is 7,500)
|
|
|
|
elif e.code == 429:
|
|
|
|
logger.log("Pushover API message limit reached - try a different API key", logger.ERROR)
|
|
|
|
return False
|
|
|
|
|
2014-12-16 05:24:06 -05:00
|
|
|
logger.log("Pushover notification successful.", logger.INFO)
|
2014-03-10 01:18:05 -04:00
|
|
|
return True
|
|
|
|
|
|
|
|
def notify_snatch(self, ep_name, title=notifyStrings[NOTIFY_SNATCH]):
|
|
|
|
if sickbeard.PUSHOVER_NOTIFY_ONSNATCH:
|
|
|
|
self._notifyPushover(title, ep_name)
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
def notify_download(self, ep_name, title=notifyStrings[NOTIFY_DOWNLOAD]):
|
|
|
|
if sickbeard.PUSHOVER_NOTIFY_ONDOWNLOAD:
|
|
|
|
self._notifyPushover(title, ep_name)
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def notify_subtitle_download(self, ep_name, lang, title=notifyStrings[NOTIFY_SUBTITLE_DOWNLOAD]):
|
|
|
|
if sickbeard.PUSHOVER_NOTIFY_ONSUBTITLEDOWNLOAD:
|
|
|
|
self._notifyPushover(title, 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_PUSHOVER:
|
2014-07-03 17:04:26 -04:00
|
|
|
update_text=notifyStrings[NOTIFY_GIT_UPDATE_TEXT]
|
|
|
|
title=notifyStrings[NOTIFY_GIT_UPDATE]
|
2014-07-03 02:43:48 -04:00
|
|
|
self._notifyPushover(title, update_text + new_version)
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-06-17 16:37:09 -04:00
|
|
|
def _notifyPushover(self, title, message, userKey=None, apiKey=None, force=False):
|
2014-03-10 01:18:05 -04:00
|
|
|
"""
|
|
|
|
Sends a pushover notification based on the provided info or SB config
|
|
|
|
|
|
|
|
title: The title of the notification to send
|
|
|
|
message: The message string to send
|
|
|
|
userKey: The userKey to send the notification to
|
2014-05-28 20:30:38 -04:00
|
|
|
force: Enforce sending, for instance for testing
|
2014-03-10 01:18:05 -04:00
|
|
|
"""
|
|
|
|
|
2014-05-28 20:30:38 -04:00
|
|
|
if not sickbeard.USE_PUSHOVER and not force:
|
2014-03-10 01:18:05 -04:00
|
|
|
logger.log("Notification for Pushover not enabled, skipping this notification", logger.DEBUG)
|
|
|
|
return False
|
|
|
|
|
|
|
|
logger.log("Sending notification for " + message, logger.DEBUG)
|
|
|
|
|
2014-06-17 16:37:09 -04:00
|
|
|
# self._sendPushover(message, title, userKey, apiKey)
|
|
|
|
return self._sendPushover(message, title, userKey, apiKey)
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
notifier = PushoverNotifier
|