From 8dd4ca3c1653137de34b362e80a22da83f026a5a Mon Sep 17 00:00:00 2001 From: JackDandy Date: Sun, 12 Jun 2016 16:28:40 +0100 Subject: [PATCH] Add button 'Discover' Emby server to notifications. --- CHANGES.md | 1 + .../default/config_notifications.tmpl | 1 + gui/slick/js/configNotifications.js | 42 +++++++++++++------ sickbeard/notifiers/emby.py | 34 +++++++++++++++ sickbeard/webserve.py | 4 ++ 5 files changed, 69 insertions(+), 13 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index dbcbaa6b..eba00b81 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -77,6 +77,7 @@ * Add FileList torrent provider * Add provider Anizb * 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) diff --git a/gui/slick/interfaces/default/config_notifications.tmpl b/gui/slick/interfaces/default/config_notifications.tmpl index 1d747661..e14a06fd 100644 --- a/gui/slick/interfaces/default/config_notifications.tmpl +++ b/gui/slick/interfaces/default/config_notifications.tmpl @@ -63,6 +63,7 @@ Host(s) running Emby +

IP:Port [, IP:Port] (e.g. 192.168.0.1:8096, 192.168.1.2:8096)

diff --git a/gui/slick/js/configNotifications.js b/gui/slick/js/configNotifications.js index 1e55af94..bb064cc7 100644 --- a/gui/slick/js/configNotifications.js +++ b/gui/slick/js/configNotifications.js @@ -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 () { - var emby_host = $('#emby_host').val(); - var emby_apikey = $('#emby_apikey').val(); - if (!emby_host || !emby_apikey) { + var host$ = $('#emby_host'), host = $.trim(host$.val()); + var apikey$ = $('#emby_apikey'), apikey = $.trim(apikey$.val()); + if (!host || !apikey) { $('#testEMBY-result').html('Please fill out the necessary fields above.'); - if (!emby_host) { - $('#emby_host').addClass('warning'); + if (!host) { + host$.addClass('warning'); } else { - $('#emby_host').removeClass('warning'); + host$.removeClass('warning'); } - if (!emby_apikey) { - $('#emby_apikey').addClass('warning'); + if (!apikey) { + apikey$.addClass('warning'); } else { - $('#emby_apikey').removeClass('warning'); + apikey$.removeClass('warning'); } return; } - $('#emby_host, #emby_apikey').removeClass('warning'); - $(this).prop('disabled', true); + $('#emby_host,#emby_apikey').removeClass('warning'); + $(this).prop('disabled', !0); $('#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) { $('#testEMBY-result').html(data); - $('#testEMBY').prop('disabled', false); + $('#testEMBY').prop('disabled', !1); }); }); diff --git a/sickbeard/notifiers/emby.py b/sickbeard/notifiers/emby.py index 8c9985be..71e279f5 100644 --- a/sickbeard/notifiers/emby.py +++ b/sickbeard/notifiers/emby.py @@ -17,6 +17,8 @@ import sickbeard 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: @@ -114,6 +116,35 @@ class EmbyNotifier: self.response = dict(status_code=r.status_code, ok=r.ok) 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): from sickbeard.helpers import starify @@ -148,6 +179,9 @@ class EmbyNotifier: # Public functions ############################################################################## + def discover_server(self): + return self._discover_server() + def test_notify(self, host, apikey): self.test_mode = True diff --git a/sickbeard/webserve.py b/sickbeard/webserve.py index 744d1600..b69c8a6c 100644 --- a/sickbeard/webserve.py +++ b/sickbeard/webserve.py @@ -776,6 +776,10 @@ class Home(MainHandler): else: return 'Error sending tweet' + @staticmethod + def discover_emby(): + return notifiers.emby_notifier.discover_server() + def testEMBY(self, host=None, apikey=None): self.set_header('Cache-Control', 'max-age=0,no-cache,no-store')