You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
4.2 KiB

14 years ago
from couchpotato.api import addApiView
from couchpotato.core.event import addEvent
from couchpotato.core.helpers.encoding import tryUrlencode
14 years ago
from couchpotato.core.logger import CPLog
from couchpotato.core.notifications.base import Notification
import re
import telnetlib
try:
import xml.etree.cElementTree as etree
except ImportError:
import xml.etree.ElementTree as etree
log = CPLog(__name__)
class NMJ(Notification):
def __init__(self):
addEvent('renamer.after', self.addToLibrary)
13 years ago
addApiView(self.testNotifyName(), self.test)
14 years ago
addApiView('notify.nmj.auto_config', self.autoConfig)
def autoConfig(self, host = 'localhost', **kwargs):
14 years ago
mount = ''
try:
terminal = telnetlib.Telnet(host)
except Exception:
12 years ago
log.error('Warning: unable to get a telnet session to %s', host)
14 years ago
return self.failed()
12 years ago
log.debug('Connected to %s via telnet', host)
14 years ago
terminal.read_until('sh-3.00# ')
terminal.write('cat /tmp/source\n')
terminal.write('cat /tmp/netshare\n')
terminal.write('exit\n')
tnoutput = terminal.read_all()
match = re.search(r'(.+\.db)\r\n?(.+)(?=sh-3.00# cat /tmp/netshare)', tnoutput)
if match:
database = match.group(1)
device = match.group(2)
log.info('Found NMJ database %s on device %s', (database, device))
14 years ago
else:
12 years ago
log.error('Could not get current NMJ database on %s, NMJ is probably not running!', host)
14 years ago
return self.failed()
if device.startswith('NETWORK_SHARE/'):
match = re.search('.*(?=\r\n?%s)' % (re.escape(device[14:])), tnoutput)
if match:
mount = match.group().replace('127.0.0.1', host)
12 years ago
log.info('Found mounting url on the Popcorn Hour in configuration: %s', mount)
14 years ago
else:
log.error('Detected a network share on the Popcorn Hour, but could not get the mounting url')
return self.failed()
return {
14 years ago
'success': True,
'database': database,
'mount': mount,
}
14 years ago
def addToLibrary(self, message = None, group = None):
if self.isDisabled(): return
if not group: group = {}
14 years ago
host = self.conf('host')
mount = self.conf('mount')
database = self.conf('database')
13 years ago
if mount:
12 years ago
log.debug('Try to mount network drive via url: %s', mount)
try:
12 years ago
self.urlopen(mount)
except:
14 years ago
return False
params = {
'arg0': 'scanner_start',
'arg1': database,
'arg2': 'background',
'arg3': '',
}
params = tryUrlencode(params)
11 years ago
update_url = 'http://%(host)s:8008/metadata_database?%(params)s' % {'host': host, 'params': params}
14 years ago
try:
11 years ago
response = self.urlopen(update_url)
except:
14 years ago
return False
try:
et = etree.fromstring(response)
result = et.findtext('returnValue')
11 years ago
except SyntaxError as e:
12 years ago
log.error('Unable to parse XML returned from the Popcorn Hour: %s', e)
14 years ago
return False
if int(result) > 0:
12 years ago
log.error('Popcorn Hour returned an errorcode: %s', result)
14 years ago
return False
else:
log.info('NMJ started background scan')
return True
def failed(self):
return {
'success': False
}
def test(self, **kwargs):
return {
'success': self.addToLibrary()
}
config = [{
'name': 'nmj',
'groups': [
{
'tab': 'notifications',
'list': 'notification_providers',
'name': 'nmj',
'label': 'NMJ',
'options': [
{
'name': 'enabled',
'default': 0,
'type': 'enabler',
},
{
'name': 'host',
'default': 'localhost',
},
{
'name': 'database',
},
{
'name': 'mount',
},
],
}
],
}]