Skip to content
Fetching contributors…
Cannot retrieve contributors at this time
143 lines (118 sloc) 5.21 KB
# Author: Nico Berlee
# URL:
# This file is part of Sick Beard.
# Sick Beard is free software: you can redistribute it and/or modify
# 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.
# Sick Beard is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with Sick Beard. If not, see <>.
import urllib, urllib2
import sickbeard
import telnetlib
import re
from sickbeard import logger
import xml.etree.cElementTree as etree
except ImportError:
import xml.etree.ElementTree as etree
class NMJNotifier:
def notify_settings(self, host):
terminal = False
terminal = telnetlib.Telnet(host)
except Exception:
logger.log(u"Warning: unable to get a telnet session to %s" % (host), logger.ERROR)
return False
logger.log(u"Connected to %s via telnet" % (host), logger.DEBUG)
terminal.read_until("sh-3.00# ")
terminal.write("cat /tmp/source\n")
terminal.write("cat /tmp/netshare\n")
tnoutput = terminal.read_all()
database = ""
device = ""
match ="(.+\.db)\r\n?(.+)(?=sh-3.00# cat /tmp/netshare)", tnoutput)
if match:
database =
device =
logger.log(u"Found NMJ database %s on device %s" % (database, device), logger.DEBUG)
sickbeard.NMJ_DATABASE = database
logger.log(u"Could not get current NMJ database on %s, NMJ is probably not running!" % (host), logger.ERROR)
return False
if device.startswith("NETWORK_SHARE/"):
match =".*(?=\r\n?%s)" % (re.escape(device[14:])), tnoutput)
if match:
mount ="", host)
logger.log(u"Found mounting url on the Popcorn Hour in configuration: %s" % (mount), logger.DEBUG)
sickbeard.NMJ_MOUNT = mount
logger.log(u"Detected a network share on the Popcorn Hour, but could not get the mounting url", logger.DEBUG)
return False
return True
def notify_snatch(self, ep_name):
return False
#Not implemented: Start the scanner when snatched does not make any sense
def notify_download(self, ep_name):
def test_notify(self, host, database, mount):
return self._sendNMJ(host, database, mount)
def _sendNMJ(self, host, database, mount=None):
if mount:
req = urllib2.Request(mount)
logger.log(u"Try to mount network drive via url: %s" % (mount), logger.DEBUG)
handle = urllib2.urlopen(req)
except IOError, e:
logger.log(u"Warning: Couldn't contact popcorn hour on host %s: %s" % (host, e))
return False
UPDATE_URL = "http://%(host)s:8008/metadata_database?%(params)s"
params = {
"arg0": "scanner_start",
"arg1": database,
"arg2": "background",
"arg3": ""}
params = urllib.urlencode(params)
updateUrl = UPDATE_URL % {"host": host, "params": params}
req = urllib2.Request(updateUrl)
logger.log(u"Sending NMJ scan update command via url: %s" % (updateUrl), logger.DEBUG)
handle = urllib2.urlopen(req)
response =
except IOError, e:
logger.log(u"Warning: Couldn't contact Popcorn Hour on host %s: %s" % (host, e))
return False
et = etree.fromstring(response)
result = et.findtext("returnValue")
except SyntaxError, e:
logger.log(u"Unable to parse XML returned from the Popcorn Hour: %s" % (e), logger.ERROR)
return False
if int(result) > 0:
logger.log(u"Popcorn Hour returned an errorcode: %s" % (result))
return False
logger.log(u"NMJ started background scan")
return True
def _notifyNMJ(self, host=None, database=None, mount=None, force=False):
if not sickbeard.USE_NMJ and not force:
logger.log("Notification for NMJ scan update not enabled, skipping this notification", logger.DEBUG)
return False
if not host:
host = sickbeard.NMJ_HOST
if not database:
database = sickbeard.NMJ_DATABASE
if not mount:
mount = sickbeard.NMJ_MOUNT
logger.log(u"Sending scan command for NMJ ", logger.DEBUG)
return self._sendNMJ(host, database, mount)
notifier = NMJNotifier
Jump to Line
Something went wrong with that request. Please try again.