mirror of
https://github.com/SickGear/SickGear.git
synced 2025-01-22 09:33:37 +00:00
Merge pull request #424 from adam111316/feature/ChangePushbullet
Change pushbullet from urllib2 to requests
This commit is contained in:
commit
5a36d261df
4 changed files with 32 additions and 52 deletions
|
@ -91,6 +91,8 @@
|
||||||
* Update jsonrpclib library r20 to (b59217c)
|
* Update jsonrpclib library r20 to (b59217c)
|
||||||
* Change cachecontrol library to ensure cache file exists before attempting delete
|
* Change cachecontrol library to ensure cache file exists before attempting delete
|
||||||
* Fix saving root dirs
|
* Fix saving root dirs
|
||||||
|
* Change pushbullet from urllib2 to requests
|
||||||
|
* Change to make pushbullet error messages clearer
|
||||||
|
|
||||||
[develop changelog]
|
[develop changelog]
|
||||||
* Update Requests library 2.7.0 (ab1f493) to 2.7.0 (8b5e457)
|
* Update Requests library 2.7.0 (ab1f493) to 2.7.0 (8b5e457)
|
||||||
|
|
|
@ -489,10 +489,11 @@ $(document).ready(function(){
|
||||||
$.get(sbRoot + '/home/getPushbulletDevices', {'accessToken': pushbullet_access_token})
|
$.get(sbRoot + '/home/getPushbulletDevices', {'accessToken': pushbullet_access_token})
|
||||||
.done(function (data) {
|
.done(function (data) {
|
||||||
var devices = jQuery.parseJSON(data || '{}').devices;
|
var devices = jQuery.parseJSON(data || '{}').devices;
|
||||||
|
var error = jQuery.parseJSON(data || '{}').error;
|
||||||
$('#pushbullet_device_list').html('');
|
$('#pushbullet_device_list').html('');
|
||||||
|
if (devices) {
|
||||||
// add default option to send to all devices
|
// add default option to send to all devices
|
||||||
$('#pushbullet_device_list').append('<option value="" selected="selected">-- All Devices --</option>');
|
$('#pushbullet_device_list').append('<option value="" selected="selected">-- All Devices --</option>');
|
||||||
if (devices) {
|
|
||||||
for (var i = 0; i < devices.length; i++) {
|
for (var i = 0; i < devices.length; i++) {
|
||||||
// only list active device targets
|
// only list active device targets
|
||||||
if (devices[i].active == true) {
|
if (devices[i].active == true) {
|
||||||
|
@ -507,7 +508,11 @@ $(document).ready(function(){
|
||||||
}
|
}
|
||||||
$('#getPushbulletDevices').prop('disabled', false);
|
$('#getPushbulletDevices').prop('disabled', false);
|
||||||
if (msg) {
|
if (msg) {
|
||||||
$('#testPushbullet-result').html(msg);
|
if (error.message) {
|
||||||
|
$('#testPushbullet-result').html(error.message);
|
||||||
|
} else {
|
||||||
|
$('#testPushbullet-result').html(msg);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -16,16 +16,13 @@
|
||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with SickGear. If not, see <http://www.gnu.org/licenses/>.
|
# along with SickGear. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
import urllib
|
|
||||||
import urllib2
|
|
||||||
import socket
|
|
||||||
import base64
|
import base64
|
||||||
|
import simplejson as json
|
||||||
import sickbeard
|
import sickbeard
|
||||||
|
|
||||||
from sickbeard import logger
|
from sickbeard import logger
|
||||||
from sickbeard.common import notifyStrings, NOTIFY_SNATCH, NOTIFY_DOWNLOAD, NOTIFY_SUBTITLE_DOWNLOAD, NOTIFY_GIT_UPDATE, NOTIFY_GIT_UPDATE_TEXT
|
from sickbeard.common import notifyStrings, NOTIFY_SNATCH, NOTIFY_DOWNLOAD, NOTIFY_SUBTITLE_DOWNLOAD, NOTIFY_GIT_UPDATE, NOTIFY_GIT_UPDATE_TEXT
|
||||||
from sickbeard.exceptions import ex
|
import requests
|
||||||
|
|
||||||
PUSHAPI_ENDPOINT = 'https://api.pushbullet.com/v2/pushes'
|
PUSHAPI_ENDPOINT = 'https://api.pushbullet.com/v2/pushes'
|
||||||
DEVICEAPI_ENDPOINT = 'https://api.pushbullet.com/v2/devices'
|
DEVICEAPI_ENDPOINT = 'https://api.pushbullet.com/v2/devices'
|
||||||
|
@ -40,54 +37,38 @@ class PushbulletNotifier:
|
||||||
|
|
||||||
# get devices from pushbullet
|
# get devices from pushbullet
|
||||||
try:
|
try:
|
||||||
req = urllib2.Request(DEVICEAPI_ENDPOINT)
|
|
||||||
base64string = base64.encodestring('%s:%s' % (accessToken, ''))[:-1]
|
base64string = base64.encodestring('%s:%s' % (accessToken, ''))[:-1]
|
||||||
req.add_header('Authorization', 'Basic %s' % base64string)
|
headers = {'Authorization': 'Basic %s' % base64string}
|
||||||
handle = urllib2.urlopen(req)
|
return requests.get(DEVICEAPI_ENDPOINT, headers=headers).text
|
||||||
if handle:
|
except Exception as e:
|
||||||
result = handle.read()
|
return json.dumps({'error': {'message': 'Error failed to connect: %s' % e}})
|
||||||
handle.close()
|
|
||||||
return result
|
|
||||||
except urllib2.URLError:
|
|
||||||
return None
|
|
||||||
except socket.timeout:
|
|
||||||
return None
|
|
||||||
|
|
||||||
def _sendPushbullet(self, title, body, accessToken, device_iden):
|
def _sendPushbullet(self, title, body, accessToken, device_iden):
|
||||||
|
|
||||||
# build up the URL and parameters
|
# build up the URL and parameters
|
||||||
body = body.strip().encode('utf-8')
|
payload = {
|
||||||
|
|
||||||
data = urllib.urlencode({
|
|
||||||
'type': 'note',
|
'type': 'note',
|
||||||
'title': title,
|
'title': title,
|
||||||
'body': body,
|
'body': body.strip().encode('utf-8'),
|
||||||
'device_iden': device_iden
|
'device_iden': device_iden
|
||||||
})
|
}
|
||||||
|
|
||||||
# send the request to pushbullet
|
# send the request to pushbullet
|
||||||
try:
|
try:
|
||||||
req = urllib2.Request(PUSHAPI_ENDPOINT)
|
|
||||||
base64string = base64.encodestring('%s:%s' % (accessToken, ''))[:-1]
|
base64string = base64.encodestring('%s:%s' % (accessToken, ''))[:-1]
|
||||||
req.add_header('Authorization', 'Basic %s' % base64string)
|
headers = {'Authorization': 'Basic %s' % base64string, 'Content-Type': 'application/json'}
|
||||||
handle = urllib2.urlopen(req, data)
|
result = requests.post(PUSHAPI_ENDPOINT, headers=headers, data=json.dumps(payload))
|
||||||
handle.close()
|
result.raise_for_status()
|
||||||
except socket.timeout:
|
except Exception as e:
|
||||||
return False
|
try:
|
||||||
except urllib2.URLError as e:
|
e = result.json()['error']['message']
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
logger.log(u'PUSHBULLET: %s' % e, logger.WARNING)
|
||||||
|
return 'Error sending Pushbullet notification: %s' % e
|
||||||
|
|
||||||
if e.code == 404:
|
logger.log(u'PUSHBULLET: Pushbullet notification succeeded', logger.MESSAGE)
|
||||||
logger.log(u'PUSHBULLET: Access token is wrong/not associated to a device.', logger.ERROR)
|
return 'Pushbullet notification succeeded'
|
||||||
elif e.code == 401:
|
|
||||||
logger.log(u'PUSHBULLET: Unauthorized, not a valid access token.', logger.ERROR)
|
|
||||||
elif e.code == 400:
|
|
||||||
logger.log(u'PUSHBULLET: Bad request, missing required parameter.', logger.ERROR)
|
|
||||||
elif e.code == 503:
|
|
||||||
logger.log(u'PUSHBULLET: Pushbullet server to busy to handle the request at this time.', logger.WARNING)
|
|
||||||
return False
|
|
||||||
|
|
||||||
logger.log(u'PUSHBULLET: Notification successful.', logger.MESSAGE)
|
|
||||||
return True
|
|
||||||
|
|
||||||
def _notifyPushbullet(self, title, body, accessToken=None, device_iden=None, force=False):
|
def _notifyPushbullet(self, title, body, accessToken=None, device_iden=None, force=False):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -953,11 +953,7 @@ class Home(MainHandler):
|
||||||
if None is not accessToken and starify(accessToken, True):
|
if None is not accessToken and starify(accessToken, True):
|
||||||
accessToken = sickbeard.PUSHBULLET_ACCESS_TOKEN
|
accessToken = sickbeard.PUSHBULLET_ACCESS_TOKEN
|
||||||
|
|
||||||
result = notifiers.pushbullet_notifier.test_notify(accessToken, device_iden)
|
return notifiers.pushbullet_notifier.test_notify(accessToken, device_iden)
|
||||||
if result:
|
|
||||||
return 'Pushbullet notification succeeded. Check your device to make sure it worked'
|
|
||||||
else:
|
|
||||||
return 'Error sending Pushbullet notification'
|
|
||||||
|
|
||||||
def getPushbulletDevices(self, accessToken=None):
|
def getPushbulletDevices(self, accessToken=None):
|
||||||
self.set_header('Cache-Control', 'max-age=0,no-cache,no-store')
|
self.set_header('Cache-Control', 'max-age=0,no-cache,no-store')
|
||||||
|
@ -965,11 +961,7 @@ class Home(MainHandler):
|
||||||
if None is not accessToken and starify(accessToken, True):
|
if None is not accessToken and starify(accessToken, True):
|
||||||
accessToken = sickbeard.PUSHBULLET_ACCESS_TOKEN
|
accessToken = sickbeard.PUSHBULLET_ACCESS_TOKEN
|
||||||
|
|
||||||
result = notifiers.pushbullet_notifier.get_devices(accessToken)
|
return notifiers.pushbullet_notifier.get_devices(accessToken)
|
||||||
if result:
|
|
||||||
return result
|
|
||||||
else:
|
|
||||||
return 'Error sending Pushbullet notification'
|
|
||||||
|
|
||||||
def shutdown(self, pid=None):
|
def shutdown(self, pid=None):
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue