Add button 'Discover' Emby server to notifications.

This commit is contained in:
JackDandy 2016-06-12 16:28:40 +01:00
parent a12a99fcb2
commit 8dd4ca3c16
5 changed files with 69 additions and 13 deletions

View file

@ -77,6 +77,7 @@
* Add FileList torrent provider * Add FileList torrent provider
* Add provider Anizb * Add provider Anizb
* Change TorrentDay to use its 2.x interface * Change TorrentDay to use its 2.x interface
* Add button 'Discover' Emby server to notifications
### 0.11.11 (2016-04-05 19:20:00 UTC) ### 0.11.11 (2016-04-05 19:20:00 UTC)

View file

@ -63,6 +63,7 @@
<span class="component-title">Host(s) running Emby</span> <span class="component-title">Host(s) running Emby</span>
<span class="component-desc"> <span class="component-desc">
<input type="text" name="emby_host" id="emby_host" value="$sickbeard.EMBY_HOST" class="form-control input-sm input250"> <input type="text" name="emby_host" id="emby_host" value="$sickbeard.EMBY_HOST" class="form-control input-sm input250">
<input type="button" class="btn" value="Discover" id="discover-emby">
<div class="clear-left"><p>IP:Port [, IP:Port] (e.g. 192.168.0.1:8096, 192.168.1.2:8096)</p></div> <div class="clear-left"><p>IP:Port [, IP:Port] (e.g. 192.168.0.1:8096, 192.168.1.2:8096)</p></div>
</span> </span>
</label> </label>

View file

@ -37,30 +37,46 @@
}); });
}); });
$('#discover-emby').click(function () {
$(this).prop('disabled', !0);
$('#emby_host,#emby_apikey').removeClass('warning');
$('#testEMBY-result').html(loading);
$.get(sbRoot + '/home/discover_emby')
.done(function (data) {
var result = 'Unable to discover a server, is one running?';
if ('' != data) {
$('#emby_host').val(data);
result = 'Server found.';
}
$('#testEMBY-result').html(result);
$('#discover-emby').prop('disabled', !1);
});
});
$('#testEMBY').click(function () { $('#testEMBY').click(function () {
var emby_host = $('#emby_host').val(); var host$ = $('#emby_host'), host = $.trim(host$.val());
var emby_apikey = $('#emby_apikey').val(); var apikey$ = $('#emby_apikey'), apikey = $.trim(apikey$.val());
if (!emby_host || !emby_apikey) { if (!host || !apikey) {
$('#testEMBY-result').html('Please fill out the necessary fields above.'); $('#testEMBY-result').html('Please fill out the necessary fields above.');
if (!emby_host) { if (!host) {
$('#emby_host').addClass('warning'); host$.addClass('warning');
} else { } else {
$('#emby_host').removeClass('warning'); host$.removeClass('warning');
} }
if (!emby_apikey) { if (!apikey) {
$('#emby_apikey').addClass('warning'); apikey$.addClass('warning');
} else { } else {
$('#emby_apikey').removeClass('warning'); apikey$.removeClass('warning');
} }
return; return;
} }
$('#emby_host, #emby_apikey').removeClass('warning'); $('#emby_host,#emby_apikey').removeClass('warning');
$(this).prop('disabled', true); $(this).prop('disabled', !0);
$('#testEMBY-result').html(loading); $('#testEMBY-result').html(loading);
$.get(sbRoot + '/home/testEMBY', {'host': emby_host, 'apikey': emby_apikey}) $.get(sbRoot + '/home/testEMBY', {'host': host, 'apikey': apikey})
.done(function (data) { .done(function (data) {
$('#testEMBY-result').html(data); $('#testEMBY-result').html(data);
$('#testEMBY').prop('disabled', false); $('#testEMBY').prop('disabled', !1);
}); });
}); });

View file

@ -17,6 +17,8 @@
import sickbeard import sickbeard
from sickbeard import logger from sickbeard import logger
from socket import socket, AF_INET, SOCK_DGRAM, SOL_SOCKET, SO_REUSEADDR, SO_BROADCAST, SHUT_RDWR
from lib import simplejson as json
class EmbyNotifier: class EmbyNotifier:
@ -114,6 +116,35 @@ class EmbyNotifier:
self.response = dict(status_code=r.status_code, ok=r.ok) self.response = dict(status_code=r.status_code, ok=r.ok)
return r return r
@staticmethod
def _discover_server():
cs = socket(AF_INET, SOCK_DGRAM)
mb_listen_port = 7359
cs.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
cs.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
cs.settimeout(10)
result, sock_issue = '', None
for server in ('EmbyServer', 'MediaBrowserServer'):
bufr = 'who is %s?' % server
try:
assert len(bufr) == cs.sendto(bufr, ('255.255.255.255', mb_listen_port)), \
'Not all data sent through the socket'
message, host = cs.recvfrom(1024)
if message:
logger.log('%s found at %s: udp query response (%s)' % (server, host[0], message))
result = ('{"Address":' not in message and message.split('|')[1] or
json.loads(message).get('Address', ''))
if result:
break
except AssertionError:
sock_issue = True
except Exception:
pass
if not sock_issue:
cs.shutdown(SHUT_RDWR)
return result
def _check_config(self, hosts=None, apikeys=None): def _check_config(self, hosts=None, apikeys=None):
from sickbeard.helpers import starify from sickbeard.helpers import starify
@ -148,6 +179,9 @@ class EmbyNotifier:
# Public functions # Public functions
############################################################################## ##############################################################################
def discover_server(self):
return self._discover_server()
def test_notify(self, host, apikey): def test_notify(self, host, apikey):
self.test_mode = True self.test_mode = True

View file

@ -776,6 +776,10 @@ class Home(MainHandler):
else: else:
return 'Error sending tweet' return 'Error sending tweet'
@staticmethod
def discover_emby():
return notifiers.emby_notifier.discover_server()
def testEMBY(self, host=None, apikey=None): def testEMBY(self, host=None, apikey=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')