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.

224 lines
6.0 KiB

import traceback
11 years ago
from couchpotato import get_session, get_db, tryInt
from couchpotato.api import addApiView
11 years ago
from couchpotato.core.event import addEvent
from couchpotato.core.helpers.encoding import toUnicode
11 years ago
from couchpotato.core.helpers.variable import splitString
14 years ago
from couchpotato.core.logger import CPLog
from couchpotato.core.plugins.base import Plugin
11 years ago
from .index import ProfileIndex
11 years ago
from couchpotato.core.settings.model import Profile, ProfileType
14 years ago
log = CPLog(__name__)
class ProfilePlugin(Plugin):
to_dict = {'types': {}}
def __init__(self):
14 years ago
addEvent('profile.all', self.all)
addEvent('profile.default', self.default)
addApiView('profile.save', self.save)
14 years ago
addApiView('profile.save_order', self.saveOrder)
addApiView('profile.delete', self.delete)
addApiView('profile.list', self.allView, docs = {
'desc': 'List all available profiles',
'return': {'type': 'object', 'example': """{
'success': True,
'list': array, profiles
}"""}
})
11 years ago
addEvent('database.setup', self.databaseSetup)
addEvent('app.initialize', self.fill, priority = 90)
11 years ago
addEvent('app.load', self.forceDefaults)
11 years ago
def databaseSetup(self):
db = get_db()
try:
db.add_index(ProfileIndex(db.path, 'profile'))
except:
log.debug('Index already exists')
db.edit_index(ProfileIndex(db.path, 'profile'))
def forceDefaults(self):
# Get all active movies without profile
try:
11 years ago
db = get_db()
medias = db.run('media', 'with_status', ['active'])
11 years ago
profile_ids = [x.get('_id') for x in self.all()]
for media in medias:
11 years ago
if media.get('profile_id') not in profile_ids:
11 years ago
media['profile_id'] = profile_ids[0].get('_id')
11 years ago
db.update(media)
except:
log.error('Failed: %s', traceback.format_exc())
def allView(self, **kwargs):
return {
'success': True,
'list': self.all()
}
14 years ago
def all(self):
11 years ago
db = get_db()
profiles = db.all('profile', with_doc = True)
14 years ago
11 years ago
return [x['doc'] for x in profiles]
def save(self, **kwargs):
14 years ago
try:
11 years ago
db = get_db()
11 years ago
profile = {
'label': toUnicode(kwargs.get('label')),
'order': tryInt(kwargs.get('order', 999)),
'core': kwargs.get('core', False),
'qualities': [],
'wait_for': [],
'finish': []
}
11 years ago
# Update types
order = 0
for type in kwargs.get('types', []):
11 years ago
profile['qualities'].append(type.get('quality'))
profile['wait_for'].append(tryInt(type.get('wait_for')))
profile['finish'].append((tryInt(type.get('finish')) == 1) if order > 0 else True)
order += 1
14 years ago
11 years ago
id = kwargs.get('id')
try:
p = db.get('id', id)
except:
p = db.insert(profile)
p.update(profile)
14 years ago
11 years ago
p['order'] = tryInt(kwargs.get('order', p.get('order', 999)))
db.update(p)
return {
'success': True,
11 years ago
'profile': p
}
except:
log.error('Failed: %s', traceback.format_exc())
return {
'success': False
}
def default(self):
11 years ago
db = get_db()
11 years ago
return list(db.all('profile', limit = 1, with_doc = True))[0]['doc']
def saveOrder(self, **kwargs):
14 years ago
try:
11 years ago
db = get_db()
14 years ago
order = 0
14 years ago
11 years ago
for profile_id in kwargs.get('ids', []):
p = db.get('id', profile_id)
p['hide'] = tryInt(kwargs.get('hidden')[order]) == 1
p['order'] = order
db.update(p)
14 years ago
11 years ago
order += 1
return {
'success': True
}
except:
log.error('Failed: %s', traceback.format_exc())
14 years ago
return {
'success': False
}
14 years ago
def delete(self, id = None, **kwargs):
14 years ago
try:
db = get_session()
14 years ago
success = False
message = ''
try:
p = db.query(Profile).filter_by(id = id).first()
db.delete(p)
db.commit()
# Force defaults on all empty profile movies
self.forceDefaults()
14 years ago
success = True
except Exception as e:
message = log.error('Failed deleting Profile: %s', e)
return {
'success': success,
'message': message
}
except:
log.error('Failed: %s', traceback.format_exc())
14 years ago
return {
'success': False
}
def fill(self):
try:
11 years ago
db = get_db()
profiles = [{
'label': 'Best',
'qualities': ['720p', '1080p', 'brrip', 'dvdrip']
}, {
'label': 'HD',
'qualities': ['720p', '1080p']
}, {
'label': 'SD',
'qualities': ['dvdrip', 'dvdr']
}]
# Create default quality profile
11 years ago
order = 0
for profile in profiles:
log.info('Creating default profile: %s', profile.get('label'))
11 years ago
pro = {
11 years ago
'_t': 'profile',
11 years ago
'label': toUnicode(profile.get('label')),
'order': order,
'qualities': profile.get('qualities'),
'finish': [],
'wait_for': []
}
for q in profile.get('qualities'):
pro['finish'].append(True)
pro['wait_for'].append(0)
db.insert(pro)
order += 1
return True
except:
log.error('Failed: %s', traceback.format_exc())
return False