1
0
mirror of https://github.com/moparisthebest/SickRage synced 2024-12-14 20:12:19 -05:00
SickRage/sickbeard/notifiers/boxcar2.py

124 lines
4.5 KiB
Python
Raw Normal View History

2014-05-07 10:23:06 -04:00
# Author: Rafael Silva <rpluto@gmail.com>
# Author: Marvin Pinto <me@marvinp.ca>
# Author: Dennis Lutter <lad1337@gmail.com>
# URL: http://code.google.com/p/sickbeard/
#
2014-05-23 08:37:22 -04:00
# This file is part of SickRage.
2014-05-07 10:23:06 -04:00
#
2014-05-23 08:37:22 -04:00
# SickRage is free software: you can redistribute it and/or modify
2014-05-07 10:23:06 -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-05-07 10:23:06 -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-05-07 10:23:06 -04:00
import urllib, urllib2
import time
import sickbeard
from sickbeard import logger
from sickbeard.common import notifyStrings, NOTIFY_SNATCH, NOTIFY_DOWNLOAD, NOTIFY_SUBTITLE_DOWNLOAD
from sickbeard.exceptions import ex
API_URL = "https://new.boxcar.io/api/notifications"
class Boxcar2Notifier:
2014-05-07 14:44:43 -04:00
def test_notify(self, accesstoken, title="SickRage : Test"):
2014-05-23 08:37:22 -04:00
return self._sendBoxcar2("This is a test notification from SickRage", title, accesstoken)
2014-05-07 10:23:06 -04:00
def _sendBoxcar2(self, msg, title, accesstoken):
"""
Sends a boxcar2 notification to the address provided
msg: The message to send
title: The title of the message
accesstoken: to send to this device
returns: True if the message succeeded, False otherwise
"""
# build up the URL and parameters
2014-05-07 14:44:43 -04:00
#more info goes here - https://boxcar.uservoice.com/knowledgebase/articles/306788-how-to-send-your-boxcar-account-a-notification
2014-05-07 10:23:06 -04:00
msg = msg.strip()
curUrl = API_URL
data = urllib.urlencode({
'user_credentials': accesstoken,
'notification[title]': "SickRage : " + title + ' : ' + msg,
2014-05-07 10:23:06 -04:00
'notification[long_message]': msg,
'notification[sound]': "notifier-2"
2014-05-07 10:23:06 -04:00
})
# send the request to boxcar2
try:
req = urllib2.Request(curUrl)
handle = urllib2.urlopen(req, data)
handle.close()
except urllib2.HTTPError, e:
2014-05-07 10:23:06 -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("Boxcar2 notification failed." + ex(e), logger.ERROR)
return False
else:
logger.log("Boxcar2 notification failed. Error code: " + str(e.code), logger.WARNING)
# HTTP status 404
if e.code == 404:
logger.log("Access token is invalid. Check it.", logger.WARNING)
return False
# 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 send to boxcar2", logger.ERROR)
return False
logger.log("Boxcar2 notification successful.", logger.DEBUG)
return True
def notify_snatch(self, ep_name, title=notifyStrings[NOTIFY_SNATCH]):
if sickbeard.BOXCAR2_NOTIFY_ONSNATCH:
self._notifyBoxcar2(title, ep_name)
def notify_download(self, ep_name, title=notifyStrings[NOTIFY_DOWNLOAD]):
if sickbeard.BOXCAR2_NOTIFY_ONDOWNLOAD:
self._notifyBoxcar2(title, ep_name)
def notify_subtitle_download(self, ep_name, lang, title=notifyStrings[NOTIFY_SUBTITLE_DOWNLOAD]):
if sickbeard.BOXCAR2_NOTIFY_ONSUBTITLEDOWNLOAD:
self._notifyBoxcar2(title, ep_name + ": " + lang)
def _notifyBoxcar2(self, title, message, accesstoken=None):
"""
Sends a boxcar2 notification based on the provided info or SB config
title: The title of the notification to send
message: The message string to send
accesstoken: to send to this device
"""
if not sickbeard.USE_BOXCAR2:
logger.log("Notification for Boxcar2 not enabled, skipping this notification", logger.DEBUG)
return False
# if no username was given then use the one from the config
if not accesstoken:
accesstoken = sickbeard.BOXCAR2_ACCESSTOKEN
logger.log("Sending notification for " + message, logger.DEBUG)
self._sendBoxcar2(message, title, accesstoken)
return True
notifier = Boxcar2Notifier