Moved library and refactored to its now location. Modified anything firing libray.add/update/_release date to now fire library.add.movie...
This commit is contained in:
+16
-11
@@ -10,9 +10,19 @@ class Loader(object):
|
|||||||
|
|
||||||
plugins = {}
|
plugins = {}
|
||||||
providers = {}
|
providers = {}
|
||||||
|
|
||||||
modules = {}
|
modules = {}
|
||||||
|
|
||||||
|
def addPath(self, root, base_path, priority, recursive=False):
|
||||||
|
for filename in os.listdir(os.path.join(root, *base_path)):
|
||||||
|
path = os.path.join(os.path.join(root, *base_path), filename)
|
||||||
|
if os.path.isdir(path) and filename[:2] != '__':
|
||||||
|
if not u'__init__.py' in os.listdir(path):
|
||||||
|
return
|
||||||
|
new_base_path = ''.join(s + '.' for s in base_path) + filename
|
||||||
|
self.paths[new_base_path.replace('.', '_')] = (priority, new_base_path, path)
|
||||||
|
if recursive:
|
||||||
|
self.addPath(root, base_path + [filename], priority, recursive=True)
|
||||||
|
|
||||||
def preload(self, root = ''):
|
def preload(self, root = ''):
|
||||||
|
|
||||||
core = os.path.join(root, 'couchpotato', 'core')
|
core = os.path.join(root, 'couchpotato', 'core')
|
||||||
@@ -25,19 +35,14 @@ class Loader(object):
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Add providers to loader
|
# Add providers to loader
|
||||||
provider_dir = os.path.join(root, 'couchpotato', 'core', 'providers')
|
self.addPath(root, ['couchpotato', 'core', 'providers'], 25, recursive=False)
|
||||||
for provider in os.listdir(provider_dir):
|
|
||||||
path = os.path.join(provider_dir, provider)
|
|
||||||
if os.path.isdir(path) and provider[:2] != '__':
|
|
||||||
self.paths[provider + '_provider'] = (25, 'couchpotato.core.providers.' + provider, path)
|
|
||||||
|
|
||||||
# Add media to loader
|
# Add media to loader
|
||||||
media_dir = os.path.join(root, 'couchpotato', 'core', 'media')
|
self.addPath(root, ['couchpotato', 'core', 'media'], 25, recursive=False)
|
||||||
for media in os.listdir(media_dir):
|
|
||||||
path = os.path.join(media_dir, media)
|
|
||||||
if os.path.isdir(path) and media[:2] != '__':
|
|
||||||
self.paths[media + '_media'] = (25, 'couchpotato.core.media.' + media, path)
|
|
||||||
|
|
||||||
|
# Add Libraries to loader
|
||||||
|
self.addPath(root, ['couchpotato', 'core', 'media', 'movie'], 1, recursive=False)
|
||||||
|
self.addPath(root, ['couchpotato', 'core', 'media', 'show'], 1, recursive=False)
|
||||||
|
|
||||||
for plugin_type, plugin_tuple in self.paths.iteritems():
|
for plugin_type, plugin_tuple in self.paths.iteritems():
|
||||||
priority, module, dir_name = plugin_tuple
|
priority, module, dir_name = plugin_tuple
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
from couchpotato.core.event import addEvent
|
||||||
|
from couchpotato.core.logger import CPLog
|
||||||
|
from couchpotato.core.plugins.base import Plugin
|
||||||
|
|
||||||
|
|
||||||
|
class LibraryBase(Plugin):
|
||||||
|
|
||||||
|
_type = None
|
||||||
|
|
||||||
|
def initType(self):
|
||||||
|
addEvent('library.types', self.getType)
|
||||||
|
|
||||||
|
def getType(self):
|
||||||
|
return self._type
|
||||||
@@ -319,7 +319,7 @@ class MovieBase(MovieTypeBase):
|
|||||||
if title.default: default_title = title.title
|
if title.default: default_title = title.title
|
||||||
|
|
||||||
fireEvent('notify.frontend', type = 'movie.busy.%s' % x, data = True)
|
fireEvent('notify.frontend', type = 'movie.busy.%s' % x, data = True)
|
||||||
fireEventAsync('library.update', identifier = movie.library.identifier, default_title = default_title, force = True, on_complete = self.createOnComplete(x))
|
fireEventAsync('library.update.movie', identifier = movie.library.identifier, default_title = default_title, force = True, on_complete = self.createOnComplete(x))
|
||||||
|
|
||||||
db.expire_all()
|
db.expire_all()
|
||||||
return {
|
return {
|
||||||
@@ -363,7 +363,7 @@ class MovieBase(MovieTypeBase):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
library = fireEvent('library.add', single = True, attrs = params, update_after = update_library)
|
library = fireEvent('library.add.movie', single = True, attrs = params, update_after = update_library)
|
||||||
|
|
||||||
# Status
|
# Status
|
||||||
status_active, snatched_status, ignored_status, done_status, downloaded_status = \
|
status_active, snatched_status, ignored_status, done_status, downloaded_status = \
|
||||||
@@ -390,7 +390,7 @@ class MovieBase(MovieTypeBase):
|
|||||||
if search_after:
|
if search_after:
|
||||||
onComplete = self.createOnComplete(m.id)
|
onComplete = self.createOnComplete(m.id)
|
||||||
|
|
||||||
fireEventAsync('library.update', params.get('identifier'), default_title = params.get('title', ''), on_complete = onComplete)
|
fireEventAsync('library.update.movie', params.get('identifier'), default_title = params.get('title', ''), on_complete = onComplete)
|
||||||
search_after = False
|
search_after = False
|
||||||
elif force_readd:
|
elif force_readd:
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
from .main import MovieLibraryPlugin
|
||||||
|
|
||||||
|
def start():
|
||||||
|
return MovieLibraryPlugin()
|
||||||
|
|
||||||
|
config = []
|
||||||
@@ -0,0 +1,177 @@
|
|||||||
|
from couchpotato import get_session
|
||||||
|
from couchpotato.core.event import addEvent, fireEventAsync, fireEvent
|
||||||
|
from couchpotato.core.helpers.encoding import toUnicode, simplifyString
|
||||||
|
from couchpotato.core.logger import CPLog
|
||||||
|
from couchpotato.core.settings.model import Library, LibraryTitle, File
|
||||||
|
from couchpotato.core.media._base.library import LibraryBase
|
||||||
|
from string import ascii_letters
|
||||||
|
import time
|
||||||
|
import traceback
|
||||||
|
|
||||||
|
log = CPLog(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class MovieLibraryPlugin(LibraryBase):
|
||||||
|
|
||||||
|
default_dict = {'titles': {}, 'files':{}}
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
addEvent('library.add.movie', self.add)
|
||||||
|
addEvent('library.update.movie', self.update)
|
||||||
|
addEvent('library.update.movie_release_date', self.updateReleaseDate)
|
||||||
|
|
||||||
|
def add(self, attrs = {}, update_after = True):
|
||||||
|
# movies don't yet contain these, so lets make sure to set defaults
|
||||||
|
type = attrs.get('type', 'movie')
|
||||||
|
primary_provider = attrs.get('primary_provider', 'imdb')
|
||||||
|
|
||||||
|
db = get_session()
|
||||||
|
|
||||||
|
l = db.query(Library).filter_by(type = type, identifier = attrs.get('identifier')).first()
|
||||||
|
if not l:
|
||||||
|
status = fireEvent('status.get', 'needs_update', single = True)
|
||||||
|
l = Library(
|
||||||
|
type = type,
|
||||||
|
primary_provider = primary_provider,
|
||||||
|
year = attrs.get('year'),
|
||||||
|
identifier = attrs.get('identifier'),
|
||||||
|
plot = toUnicode(attrs.get('plot')),
|
||||||
|
tagline = toUnicode(attrs.get('tagline')),
|
||||||
|
status_id = status.get('id'),
|
||||||
|
info = {},
|
||||||
|
parent = None,
|
||||||
|
)
|
||||||
|
|
||||||
|
title = LibraryTitle(
|
||||||
|
title = toUnicode(attrs.get('title')),
|
||||||
|
simple_title = self.simplifyTitle(attrs.get('title')),
|
||||||
|
)
|
||||||
|
|
||||||
|
l.titles.append(title)
|
||||||
|
|
||||||
|
db.add(l)
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
# Update library info
|
||||||
|
if update_after is not False:
|
||||||
|
handle = fireEventAsync if update_after is 'async' else fireEvent
|
||||||
|
handle('library.update.movie', identifier = l.identifier, default_title = toUnicode(attrs.get('title', '')))
|
||||||
|
|
||||||
|
library_dict = l.to_dict(self.default_dict)
|
||||||
|
|
||||||
|
db.expire_all()
|
||||||
|
return library_dict
|
||||||
|
|
||||||
|
def update(self, identifier, default_title = '', force = False):
|
||||||
|
|
||||||
|
if self.shuttingDown():
|
||||||
|
return
|
||||||
|
|
||||||
|
db = get_session()
|
||||||
|
library = db.query(Library).filter_by(identifier = identifier).first()
|
||||||
|
done_status = fireEvent('status.get', 'done', single = True)
|
||||||
|
|
||||||
|
if library:
|
||||||
|
library_dict = library.to_dict(self.default_dict)
|
||||||
|
|
||||||
|
do_update = True
|
||||||
|
|
||||||
|
info = fireEvent('movie.info', merge = True, identifier = identifier)
|
||||||
|
|
||||||
|
# Don't need those here
|
||||||
|
try: del info['in_wanted']
|
||||||
|
except: pass
|
||||||
|
try: del info['in_library']
|
||||||
|
except: pass
|
||||||
|
|
||||||
|
if not info or len(info) == 0:
|
||||||
|
log.error('Could not update, no movie info to work with: %s', identifier)
|
||||||
|
return False
|
||||||
|
|
||||||
|
# Main info
|
||||||
|
if do_update:
|
||||||
|
library.plot = toUnicode(info.get('plot', ''))
|
||||||
|
library.tagline = toUnicode(info.get('tagline', ''))
|
||||||
|
library.year = info.get('year', 0)
|
||||||
|
library.status_id = done_status.get('id')
|
||||||
|
library.info.update(info)
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
# Titles
|
||||||
|
[db.delete(title) for title in library.titles]
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
titles = info.get('titles', [])
|
||||||
|
log.debug('Adding titles: %s', titles)
|
||||||
|
counter = 0
|
||||||
|
for title in titles:
|
||||||
|
if not title:
|
||||||
|
continue
|
||||||
|
title = toUnicode(title)
|
||||||
|
t = LibraryTitle(
|
||||||
|
title = title,
|
||||||
|
simple_title = self.simplifyTitle(title),
|
||||||
|
default = (len(default_title) == 0 and counter == 0) or len(titles) == 1 or title.lower() == toUnicode(default_title.lower()) or (toUnicode(default_title) == u'' and toUnicode(titles[0]) == title)
|
||||||
|
)
|
||||||
|
library.titles.append(t)
|
||||||
|
counter += 1
|
||||||
|
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
# Files
|
||||||
|
images = info.get('images', [])
|
||||||
|
for image_type in ['poster']:
|
||||||
|
for image in images.get(image_type, []):
|
||||||
|
if not isinstance(image, (str, unicode)):
|
||||||
|
continue
|
||||||
|
|
||||||
|
file_path = fireEvent('file.download', url = image, single = True)
|
||||||
|
if file_path:
|
||||||
|
file_obj = fireEvent('file.add', path = file_path, type_tuple = ('image', image_type), single = True)
|
||||||
|
try:
|
||||||
|
file_obj = db.query(File).filter_by(id = file_obj.get('id')).one()
|
||||||
|
library.files.append(file_obj)
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
break
|
||||||
|
except:
|
||||||
|
log.debug('Failed to attach to library: %s', traceback.format_exc())
|
||||||
|
|
||||||
|
library_dict = library.to_dict(self.default_dict)
|
||||||
|
|
||||||
|
db.expire_all()
|
||||||
|
return library_dict
|
||||||
|
|
||||||
|
def updateReleaseDate(self, identifier):
|
||||||
|
|
||||||
|
db = get_session()
|
||||||
|
library = db.query(Library).filter_by(identifier = identifier).first()
|
||||||
|
|
||||||
|
if not library.info:
|
||||||
|
library_dict = self.update(identifier, force = True)
|
||||||
|
dates = library_dict.get('info', {}).get('release_date')
|
||||||
|
else:
|
||||||
|
dates = library.info.get('release_date')
|
||||||
|
|
||||||
|
if dates and dates.get('expires', 0) < time.time() or not dates:
|
||||||
|
dates = fireEvent('movie.release_date', identifier = identifier, merge = True)
|
||||||
|
library.info.update({'release_date': dates })
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
db.expire_all()
|
||||||
|
return dates
|
||||||
|
|
||||||
|
|
||||||
|
def simplifyTitle(self, title):
|
||||||
|
|
||||||
|
title = toUnicode(title)
|
||||||
|
|
||||||
|
nr_prefix = '' if title[0] in ascii_letters else '#'
|
||||||
|
title = simplifyString(title)
|
||||||
|
|
||||||
|
for prefix in ['the ']:
|
||||||
|
if prefix == title[:len(prefix)]:
|
||||||
|
title = title[len(prefix):]
|
||||||
|
break
|
||||||
|
|
||||||
|
return nr_prefix + title
|
||||||
@@ -100,7 +100,7 @@ class MovieSearcher(SearcherBase, MovieTypeBase):
|
|||||||
self.single(movie_dict, search_types)
|
self.single(movie_dict, search_types)
|
||||||
except IndexError:
|
except IndexError:
|
||||||
log.error('Forcing library update for %s, if you see this often, please report: %s', (movie_dict['library']['identifier'], traceback.format_exc()))
|
log.error('Forcing library update for %s, if you see this often, please report: %s', (movie_dict['library']['identifier'], traceback.format_exc()))
|
||||||
fireEvent('library.update', movie_dict['library']['identifier'], force = True)
|
fireEvent('library.update.movie', movie_dict['library']['identifier'], force = True)
|
||||||
except:
|
except:
|
||||||
log.error('Search failed for %s: %s', (movie_dict['library']['identifier'], traceback.format_exc()))
|
log.error('Search failed for %s: %s', (movie_dict['library']['identifier'], traceback.format_exc()))
|
||||||
|
|
||||||
@@ -133,7 +133,7 @@ class MovieSearcher(SearcherBase, MovieTypeBase):
|
|||||||
db = get_session()
|
db = get_session()
|
||||||
|
|
||||||
pre_releases = fireEvent('quality.pre_releases', single = True)
|
pre_releases = fireEvent('quality.pre_releases', single = True)
|
||||||
release_dates = fireEvent('library.update_release_date', identifier = movie['library']['identifier'], merge = True)
|
release_dates = fireEvent('library.update.movie_release_date', identifier = movie['library']['identifier'], merge = True)
|
||||||
available_status, ignored_status, failed_status = fireEvent('status.get', ['available', 'ignored', 'failed'], single = True)
|
available_status, ignored_status, failed_status = fireEvent('status.get', ['available', 'ignored', 'failed'], single = True)
|
||||||
|
|
||||||
found_releases = []
|
found_releases = []
|
||||||
|
|||||||
@@ -124,7 +124,7 @@ class ShowBase(MediaBase):
|
|||||||
log.debug("show.add")
|
log.debug("show.add")
|
||||||
|
|
||||||
# Add show parent to db first
|
# Add show parent to db first
|
||||||
parent = self.addToDatabase(params = params)
|
parent = self.addToDatabase(params = params, type = 'show')
|
||||||
|
|
||||||
skip = False # XXX: For debugging
|
skip = False # XXX: For debugging
|
||||||
identifier = params.get('id')
|
identifier = params.get('id')
|
||||||
@@ -136,7 +136,7 @@ class ShowBase(MediaBase):
|
|||||||
episode['title'] = episode.get('titles', None)[0]
|
episode['title'] = episode.get('titles', None)[0]
|
||||||
episode['identifier'] = episode.get('id', None)
|
episode['identifier'] = episode.get('id', None)
|
||||||
episode['parent_identifier'] = identifier
|
episode['parent_identifier'] = identifier
|
||||||
self.addToDatabase(params=episode, type="episode")
|
self.addToDatabase(params=episode, type = "episode")
|
||||||
|
|
||||||
return parent
|
return parent
|
||||||
|
|
||||||
@@ -159,7 +159,9 @@ class ShowBase(MediaBase):
|
|||||||
#except:
|
#except:
|
||||||
#pass
|
#pass
|
||||||
|
|
||||||
library = fireEvent('library.add', single = True, attrs = params, update_after = update_library)
|
library = fireEvent('library.add.%s' % type, single = True, attrs = params, update_after = update_library)
|
||||||
|
if not library:
|
||||||
|
return False
|
||||||
|
|
||||||
# Status
|
# Status
|
||||||
status_active, snatched_status, ignored_status, done_status, downloaded_status = \
|
status_active, snatched_status, ignored_status, done_status, downloaded_status = \
|
||||||
@@ -187,7 +189,7 @@ class ShowBase(MediaBase):
|
|||||||
if search_after:
|
if search_after:
|
||||||
onComplete = self.createOnComplete(m.id)
|
onComplete = self.createOnComplete(m.id)
|
||||||
|
|
||||||
fireEventAsync('library.update', params.get('identifier'), default_title = params.get('title', ''), on_complete = onComplete)
|
fireEventAsync('library.update.%s' % type, params.get('identifier'), default_title = params.get('title', ''), on_complete = onComplete)
|
||||||
search_after = False
|
search_after = False
|
||||||
elif force_readd:
|
elif force_readd:
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
from .main import EpisodeLibraryPlugin
|
||||||
|
|
||||||
|
def start():
|
||||||
|
return EpisodeLibraryPlugin()
|
||||||
|
|
||||||
|
config = []
|
||||||
+19
-28
@@ -2,8 +2,8 @@ from couchpotato import get_session
|
|||||||
from couchpotato.core.event import addEvent, fireEventAsync, fireEvent
|
from couchpotato.core.event import addEvent, fireEventAsync, fireEvent
|
||||||
from couchpotato.core.helpers.encoding import toUnicode, simplifyString
|
from couchpotato.core.helpers.encoding import toUnicode, simplifyString
|
||||||
from couchpotato.core.logger import CPLog
|
from couchpotato.core.logger import CPLog
|
||||||
from couchpotato.core.plugins.base import Plugin
|
|
||||||
from couchpotato.core.settings.model import Library, LibraryTitle, File
|
from couchpotato.core.settings.model import Library, LibraryTitle, File
|
||||||
|
from couchpotato.core.media._base.library import LibraryBase
|
||||||
from string import ascii_letters
|
from string import ascii_letters
|
||||||
import time
|
import time
|
||||||
import traceback
|
import traceback
|
||||||
@@ -11,28 +11,24 @@ import traceback
|
|||||||
log = CPLog(__name__)
|
log = CPLog(__name__)
|
||||||
|
|
||||||
|
|
||||||
class LibraryPlugin(Plugin):
|
class EpisodeLibraryPlugin(LibraryBase):
|
||||||
|
|
||||||
default_dict = {'titles': {}, 'files':{}}
|
default_dict = {'titles': {}, 'files':{}}
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
addEvent('library.add', self.add)
|
addEvent('library.add.episode', self.add)
|
||||||
addEvent('library.update', self.update)
|
addEvent('library.update.episode', self.update)
|
||||||
addEvent('library.update_release_date', self.updateReleaseDate)
|
addEvent('library.update.episode_release_date', self.updateReleaseDate)
|
||||||
|
|
||||||
def add(self, attrs = {}, update_after = True):
|
def add(self, attrs = {}, update_after = True):
|
||||||
|
type = attrs.get('type', 'episode')
|
||||||
# movies don't yet contain these, so lets make sure to set defaults
|
primary_provider = attrs.get('primary_provider', 'thetvdb')
|
||||||
type = attrs.get('type', 'movie')
|
|
||||||
primary_provider = attrs.get('primary_provider', 'imdb')
|
|
||||||
|
|
||||||
db = get_session()
|
db = get_session()
|
||||||
parent_identifier = attrs.get('parent_identifier', None)
|
parent_identifier = attrs.get('parent_identifier', None)
|
||||||
|
|
||||||
# XXX: add type (somehow? it will be show when episode id the type) so we dont get conflicts
|
|
||||||
parent = None
|
parent = None
|
||||||
if parent_identifier:
|
if parent_identifier:
|
||||||
#parent = db.query(Library).filter_by(identifier = attrs.get('parent_identifier')).first()
|
|
||||||
parent = db.query(Library).filter_by(primary_provider = primary_provider, identifier = attrs.get('parent_identifier')).first()
|
parent = db.query(Library).filter_by(primary_provider = primary_provider, identifier = attrs.get('parent_identifier')).first()
|
||||||
|
|
||||||
l = db.query(Library).filter_by(type = type, identifier = attrs.get('identifier')).first()
|
l = db.query(Library).filter_by(type = type, identifier = attrs.get('identifier')).first()
|
||||||
@@ -49,7 +45,6 @@ class LibraryPlugin(Plugin):
|
|||||||
info = {},
|
info = {},
|
||||||
parent = parent,
|
parent = parent,
|
||||||
)
|
)
|
||||||
# children = [],
|
|
||||||
|
|
||||||
title = LibraryTitle(
|
title = LibraryTitle(
|
||||||
title = toUnicode(attrs.get('title')),
|
title = toUnicode(attrs.get('title')),
|
||||||
@@ -64,7 +59,7 @@ class LibraryPlugin(Plugin):
|
|||||||
# Update library info
|
# Update library info
|
||||||
if update_after is not False:
|
if update_after is not False:
|
||||||
handle = fireEventAsync if update_after is 'async' else fireEvent
|
handle = fireEventAsync if update_after is 'async' else fireEvent
|
||||||
handle('library.update', identifier = l.identifier, default_title = toUnicode(attrs.get('title', '')))
|
handle('library.update.episode', identifier = l.identifier, default_title = toUnicode(attrs.get('title', '')))
|
||||||
|
|
||||||
library_dict = l.to_dict(self.default_dict)
|
library_dict = l.to_dict(self.default_dict)
|
||||||
|
|
||||||
@@ -92,23 +87,19 @@ class LibraryPlugin(Plugin):
|
|||||||
|
|
||||||
if library.status_id == done_status.get('id') and not force:
|
if library.status_id == done_status.get('id') and not force:
|
||||||
do_update = False
|
do_update = False
|
||||||
# XXX: do this a better way. we need to pass parent_identifier to things like episode.info
|
|
||||||
# maybe just make all .info (movie, show, season and esisode requre parent var and not use it)
|
|
||||||
elif parent_identifier:
|
|
||||||
info = fireEvent('%s.info' % library.type, merge = True, identifier = identifier, \
|
|
||||||
parent_identifier = parent_identifier)
|
|
||||||
else:
|
|
||||||
info = fireEvent('%s.info' % library.type, merge = True, identifier = identifier)
|
|
||||||
|
|
||||||
# Don't need those here
|
info = fireEvent('episode.info', merge = True, identifier = identifier, \
|
||||||
try: del info['in_wanted']
|
parent_identifier = parent_identifier)
|
||||||
except: pass
|
|
||||||
try: del info['in_library']
|
|
||||||
except: pass
|
|
||||||
|
|
||||||
if not info or len(info) == 0:
|
# Don't need those here
|
||||||
log.error('Could not update, no movie info to work with: %s', identifier)
|
try: del info['in_wanted']
|
||||||
return False
|
except: pass
|
||||||
|
try: del info['in_library']
|
||||||
|
except: pass
|
||||||
|
|
||||||
|
if not info or len(info) == 0:
|
||||||
|
log.error('Could not update, no movie info to work with: %s', identifier)
|
||||||
|
return False
|
||||||
|
|
||||||
# Main info
|
# Main info
|
||||||
if do_update:
|
if do_update:
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
from .main import SeasonLibraryPlugin
|
||||||
|
|
||||||
|
def start():
|
||||||
|
return SeasonLibraryPlugin()
|
||||||
|
|
||||||
|
config = []
|
||||||
@@ -0,0 +1,190 @@
|
|||||||
|
from couchpotato import get_session
|
||||||
|
from couchpotato.core.event import addEvent, fireEventAsync, fireEvent
|
||||||
|
from couchpotato.core.helpers.encoding import toUnicode, simplifyString
|
||||||
|
from couchpotato.core.logger import CPLog
|
||||||
|
from couchpotato.core.settings.model import Library, LibraryTitle, File
|
||||||
|
from couchpotato.core.media._base.library import LibraryBase
|
||||||
|
from string import ascii_letters
|
||||||
|
import time
|
||||||
|
import traceback
|
||||||
|
|
||||||
|
log = CPLog(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class SeasonLibraryPlugin(LibraryBase):
|
||||||
|
|
||||||
|
default_dict = {'titles': {}, 'files':{}}
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
addEvent('library.add.season', self.add)
|
||||||
|
addEvent('library.update.season', self.update)
|
||||||
|
addEvent('library.update.season_release_date', self.updateReleaseDate)
|
||||||
|
|
||||||
|
def add(self, attrs = {}, update_after = True):
|
||||||
|
type = attrs.get('type', 'season')
|
||||||
|
primary_provider = attrs.get('primary_provider', 'thetvdb')
|
||||||
|
|
||||||
|
db = get_session()
|
||||||
|
parent_identifier = attrs.get('parent_identifier', None)
|
||||||
|
|
||||||
|
parent = None
|
||||||
|
if parent_identifier:
|
||||||
|
parent = db.query(Library).filter_by(primary_provider = primary_provider, identifier = attrs.get('parent_identifier')).first()
|
||||||
|
|
||||||
|
l = db.query(Library).filter_by(type = type, identifier = attrs.get('identifier')).first()
|
||||||
|
if not l:
|
||||||
|
status = fireEvent('status.get', 'needs_update', single = True)
|
||||||
|
l = Library(
|
||||||
|
type = type,
|
||||||
|
primary_provider = primary_provider,
|
||||||
|
year = attrs.get('year'),
|
||||||
|
identifier = attrs.get('identifier'),
|
||||||
|
plot = toUnicode(attrs.get('plot')),
|
||||||
|
tagline = toUnicode(attrs.get('tagline')),
|
||||||
|
status_id = status.get('id'),
|
||||||
|
info = {},
|
||||||
|
parent = parent,
|
||||||
|
)
|
||||||
|
|
||||||
|
title = LibraryTitle(
|
||||||
|
title = toUnicode(attrs.get('title')),
|
||||||
|
simple_title = self.simplifyTitle(attrs.get('title')),
|
||||||
|
)
|
||||||
|
|
||||||
|
l.titles.append(title)
|
||||||
|
|
||||||
|
db.add(l)
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
# Update library info
|
||||||
|
if update_after is not False:
|
||||||
|
handle = fireEventAsync if update_after is 'async' else fireEvent
|
||||||
|
handle('library.update.season', identifier = l.identifier, default_title = toUnicode(attrs.get('title', '')))
|
||||||
|
|
||||||
|
library_dict = l.to_dict(self.default_dict)
|
||||||
|
|
||||||
|
db.expire_all()
|
||||||
|
return library_dict
|
||||||
|
|
||||||
|
def update(self, identifier, default_title = '', force = False):
|
||||||
|
|
||||||
|
if self.shuttingDown():
|
||||||
|
return
|
||||||
|
|
||||||
|
db = get_session()
|
||||||
|
library = db.query(Library).filter_by(identifier = identifier).first()
|
||||||
|
done_status = fireEvent('status.get', 'done', single = True)
|
||||||
|
|
||||||
|
if library:
|
||||||
|
library_dict = library.to_dict(self.default_dict)
|
||||||
|
|
||||||
|
do_update = True
|
||||||
|
|
||||||
|
# XXX: Fix to be pretty
|
||||||
|
parent_identifier = None
|
||||||
|
if library.parent:
|
||||||
|
parent_identifier = library.parent.identifier
|
||||||
|
|
||||||
|
if library.status_id == done_status.get('id') and not force:
|
||||||
|
do_update = False
|
||||||
|
|
||||||
|
info = fireEvent('season.info', merge = True, identifier = identifier, \
|
||||||
|
parent_identifier = parent_identifier)
|
||||||
|
|
||||||
|
# Don't need those here
|
||||||
|
try: del info['in_wanted']
|
||||||
|
except: pass
|
||||||
|
try: del info['in_library']
|
||||||
|
except: pass
|
||||||
|
|
||||||
|
if not info or len(info) == 0:
|
||||||
|
log.error('Could not update, no movie info to work with: %s', identifier)
|
||||||
|
return False
|
||||||
|
|
||||||
|
# Main info
|
||||||
|
if do_update:
|
||||||
|
library.plot = toUnicode(info.get('plot', ''))
|
||||||
|
library.tagline = toUnicode(info.get('tagline', ''))
|
||||||
|
library.year = info.get('year', 0)
|
||||||
|
library.status_id = done_status.get('id')
|
||||||
|
library.info.update(info)
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
# Titles
|
||||||
|
[db.delete(title) for title in library.titles]
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
titles = info.get('titles', [])
|
||||||
|
log.debug('Adding titles: %s', titles)
|
||||||
|
counter = 0
|
||||||
|
for title in titles:
|
||||||
|
if not title:
|
||||||
|
continue
|
||||||
|
title = toUnicode(title)
|
||||||
|
t = LibraryTitle(
|
||||||
|
title = title,
|
||||||
|
simple_title = self.simplifyTitle(title),
|
||||||
|
default = (len(default_title) == 0 and counter == 0) or len(titles) == 1 or title.lower() == toUnicode(default_title.lower()) or (toUnicode(default_title) == u'' and toUnicode(titles[0]) == title)
|
||||||
|
)
|
||||||
|
library.titles.append(t)
|
||||||
|
counter += 1
|
||||||
|
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
# Files
|
||||||
|
images = info.get('images', [])
|
||||||
|
for image_type in ['poster']:
|
||||||
|
for image in images.get(image_type, []):
|
||||||
|
if not isinstance(image, (str, unicode)):
|
||||||
|
continue
|
||||||
|
|
||||||
|
file_path = fireEvent('file.download', url = image, single = True)
|
||||||
|
if file_path:
|
||||||
|
file_obj = fireEvent('file.add', path = file_path, type_tuple = ('image', image_type), single = True)
|
||||||
|
try:
|
||||||
|
file_obj = db.query(File).filter_by(id = file_obj.get('id')).one()
|
||||||
|
library.files.append(file_obj)
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
break
|
||||||
|
except:
|
||||||
|
log.debug('Failed to attach to library: %s', traceback.format_exc())
|
||||||
|
|
||||||
|
library_dict = library.to_dict(self.default_dict)
|
||||||
|
|
||||||
|
db.expire_all()
|
||||||
|
return library_dict
|
||||||
|
|
||||||
|
def updateReleaseDate(self, identifier):
|
||||||
|
|
||||||
|
db = get_session()
|
||||||
|
library = db.query(Library).filter_by(identifier = identifier).first()
|
||||||
|
|
||||||
|
if not library.info:
|
||||||
|
library_dict = self.update(identifier, force = True)
|
||||||
|
dates = library_dict.get('info', {}).get('release_date')
|
||||||
|
else:
|
||||||
|
dates = library.info.get('release_date')
|
||||||
|
|
||||||
|
if dates and dates.get('expires', 0) < time.time() or not dates:
|
||||||
|
dates = fireEvent('movie.release_date', identifier = identifier, merge = True)
|
||||||
|
library.info.update({'release_date': dates })
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
db.expire_all()
|
||||||
|
return dates
|
||||||
|
|
||||||
|
|
||||||
|
def simplifyTitle(self, title):
|
||||||
|
|
||||||
|
title = toUnicode(title)
|
||||||
|
|
||||||
|
nr_prefix = '' if title[0] in ascii_letters else '#'
|
||||||
|
title = simplifyString(title)
|
||||||
|
|
||||||
|
for prefix in ['the ']:
|
||||||
|
if prefix == title[:len(prefix)]:
|
||||||
|
title = title[len(prefix):]
|
||||||
|
break
|
||||||
|
|
||||||
|
return nr_prefix + title
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
from .main import ShowLibraryPlugin
|
||||||
|
|
||||||
|
def start():
|
||||||
|
return ShowLibraryPlugin()
|
||||||
|
|
||||||
|
config = []
|
||||||
@@ -0,0 +1,176 @@
|
|||||||
|
from couchpotato import get_session
|
||||||
|
from couchpotato.core.event import addEvent, fireEventAsync, fireEvent
|
||||||
|
from couchpotato.core.helpers.encoding import toUnicode, simplifyString
|
||||||
|
from couchpotato.core.logger import CPLog
|
||||||
|
from couchpotato.core.settings.model import Library, LibraryTitle, File
|
||||||
|
from couchpotato.core.media._base.library import LibraryBase
|
||||||
|
from string import ascii_letters
|
||||||
|
import time
|
||||||
|
import traceback
|
||||||
|
|
||||||
|
log = CPLog(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class ShowLibraryPlugin(LibraryBase):
|
||||||
|
|
||||||
|
default_dict = {'titles': {}, 'files':{}}
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
addEvent('library.add.show', self.add)
|
||||||
|
addEvent('library.update.show', self.update)
|
||||||
|
addEvent('library.update.show_release_date', self.updateReleaseDate)
|
||||||
|
|
||||||
|
def add(self, attrs = {}, update_after = True):
|
||||||
|
type = attrs.get('type', 'show')
|
||||||
|
primary_provider = attrs.get('primary_provider', 'thetvdb')
|
||||||
|
|
||||||
|
db = get_session()
|
||||||
|
|
||||||
|
l = db.query(Library).filter_by(type = type, identifier = attrs.get('identifier')).first()
|
||||||
|
if not l:
|
||||||
|
status = fireEvent('status.get', 'needs_update', single = True)
|
||||||
|
l = Library(
|
||||||
|
type = type,
|
||||||
|
primary_provider = primary_provider,
|
||||||
|
year = attrs.get('year'),
|
||||||
|
identifier = attrs.get('identifier'),
|
||||||
|
plot = toUnicode(attrs.get('plot')),
|
||||||
|
tagline = toUnicode(attrs.get('tagline')),
|
||||||
|
status_id = status.get('id'),
|
||||||
|
info = {},
|
||||||
|
parent = None,
|
||||||
|
)
|
||||||
|
|
||||||
|
title = LibraryTitle(
|
||||||
|
title = toUnicode(attrs.get('title')),
|
||||||
|
simple_title = self.simplifyTitle(attrs.get('title')),
|
||||||
|
)
|
||||||
|
|
||||||
|
l.titles.append(title)
|
||||||
|
|
||||||
|
db.add(l)
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
# Update library info
|
||||||
|
if update_after is not False:
|
||||||
|
handle = fireEventAsync if update_after is 'async' else fireEvent
|
||||||
|
handle('library.update.show', identifier = l.identifier, default_title = toUnicode(attrs.get('title', '')))
|
||||||
|
|
||||||
|
library_dict = l.to_dict(self.default_dict)
|
||||||
|
|
||||||
|
db.expire_all()
|
||||||
|
return library_dict
|
||||||
|
|
||||||
|
def update(self, identifier, default_title = '', force = False):
|
||||||
|
|
||||||
|
if self.shuttingDown():
|
||||||
|
return
|
||||||
|
|
||||||
|
db = get_session()
|
||||||
|
library = db.query(Library).filter_by(identifier = identifier).first()
|
||||||
|
done_status = fireEvent('status.get', 'done', single = True)
|
||||||
|
|
||||||
|
if library:
|
||||||
|
library_dict = library.to_dict(self.default_dict)
|
||||||
|
|
||||||
|
do_update = True
|
||||||
|
|
||||||
|
info = fireEvent('show.info' % library.type, merge = True, identifier = identifier)
|
||||||
|
|
||||||
|
# Don't need those here
|
||||||
|
try: del info['in_wanted']
|
||||||
|
except: pass
|
||||||
|
try: del info['in_library']
|
||||||
|
except: pass
|
||||||
|
|
||||||
|
if not info or len(info) == 0:
|
||||||
|
log.error('Could not update, no movie info to work with: %s', identifier)
|
||||||
|
return False
|
||||||
|
|
||||||
|
# Main info
|
||||||
|
if do_update:
|
||||||
|
library.plot = toUnicode(info.get('plot', ''))
|
||||||
|
library.tagline = toUnicode(info.get('tagline', ''))
|
||||||
|
library.year = info.get('year', 0)
|
||||||
|
library.status_id = done_status.get('id')
|
||||||
|
library.info.update(info)
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
# Titles
|
||||||
|
[db.delete(title) for title in library.titles]
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
titles = info.get('titles', [])
|
||||||
|
log.debug('Adding titles: %s', titles)
|
||||||
|
counter = 0
|
||||||
|
for title in titles:
|
||||||
|
if not title:
|
||||||
|
continue
|
||||||
|
title = toUnicode(title)
|
||||||
|
t = LibraryTitle(
|
||||||
|
title = title,
|
||||||
|
simple_title = self.simplifyTitle(title),
|
||||||
|
default = (len(default_title) == 0 and counter == 0) or len(titles) == 1 or title.lower() == toUnicode(default_title.lower()) or (toUnicode(default_title) == u'' and toUnicode(titles[0]) == title)
|
||||||
|
)
|
||||||
|
library.titles.append(t)
|
||||||
|
counter += 1
|
||||||
|
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
# Files
|
||||||
|
images = info.get('images', [])
|
||||||
|
for image_type in ['poster']:
|
||||||
|
for image in images.get(image_type, []):
|
||||||
|
if not isinstance(image, (str, unicode)):
|
||||||
|
continue
|
||||||
|
|
||||||
|
file_path = fireEvent('file.download', url = image, single = True)
|
||||||
|
if file_path:
|
||||||
|
file_obj = fireEvent('file.add', path = file_path, type_tuple = ('image', image_type), single = True)
|
||||||
|
try:
|
||||||
|
file_obj = db.query(File).filter_by(id = file_obj.get('id')).one()
|
||||||
|
library.files.append(file_obj)
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
break
|
||||||
|
except:
|
||||||
|
log.debug('Failed to attach to library: %s', traceback.format_exc())
|
||||||
|
|
||||||
|
library_dict = library.to_dict(self.default_dict)
|
||||||
|
|
||||||
|
db.expire_all()
|
||||||
|
return library_dict
|
||||||
|
|
||||||
|
def updateReleaseDate(self, identifier):
|
||||||
|
|
||||||
|
db = get_session()
|
||||||
|
library = db.query(Library).filter_by(identifier = identifier).first()
|
||||||
|
|
||||||
|
if not library.info:
|
||||||
|
library_dict = self.update(identifier, force = True)
|
||||||
|
dates = library_dict.get('info', {}).get('release_date')
|
||||||
|
else:
|
||||||
|
dates = library.info.get('release_date')
|
||||||
|
|
||||||
|
if dates and dates.get('expires', 0) < time.time() or not dates:
|
||||||
|
dates = fireEvent('movie.release_date', identifier = identifier, merge = True)
|
||||||
|
library.info.update({'release_date': dates })
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
db.expire_all()
|
||||||
|
return dates
|
||||||
|
|
||||||
|
|
||||||
|
def simplifyTitle(self, title):
|
||||||
|
|
||||||
|
title = toUnicode(title)
|
||||||
|
|
||||||
|
nr_prefix = '' if title[0] in ascii_letters else '#'
|
||||||
|
title = simplifyString(title)
|
||||||
|
|
||||||
|
for prefix in ['the ']:
|
||||||
|
if prefix == title[:len(prefix)]:
|
||||||
|
title = title[len(prefix):]
|
||||||
|
break
|
||||||
|
|
||||||
|
return nr_prefix + title
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
from .main import LibraryPlugin
|
|
||||||
|
|
||||||
def start():
|
|
||||||
return LibraryPlugin()
|
|
||||||
|
|
||||||
config = []
|
|
||||||
@@ -182,7 +182,7 @@ class Manage(Plugin):
|
|||||||
|
|
||||||
# Add it to release and update the info
|
# Add it to release and update the info
|
||||||
fireEvent('release.add', group = group)
|
fireEvent('release.add', group = group)
|
||||||
fireEventAsync('library.update', identifier = identifier, on_complete = self.createAfterUpdate(folder, identifier))
|
fireEventAsync('library.update.movie', identifier = identifier, on_complete = self.createAfterUpdate(folder, identifier))
|
||||||
else:
|
else:
|
||||||
self.in_progress[folder]['to_go'] = self.in_progress[folder]['to_go'] - 1
|
self.in_progress[folder]['to_go'] = self.in_progress[folder]['to_go'] - 1
|
||||||
|
|
||||||
|
|||||||
@@ -154,7 +154,7 @@ class Renamer(Plugin):
|
|||||||
continue
|
continue
|
||||||
# Rename the files using the library data
|
# Rename the files using the library data
|
||||||
else:
|
else:
|
||||||
group['library'] = fireEvent('library.update', identifier = group['library']['identifier'], single = True)
|
group['library'] = fireEvent('library.update.movie', identifier = group['library']['identifier'], single = True)
|
||||||
if not group['library']:
|
if not group['library']:
|
||||||
log.error('Could not rename, no library item to work with: %s', group_identifier)
|
log.error('Could not rename, no library item to work with: %s', group_identifier)
|
||||||
continue
|
continue
|
||||||
|
|||||||
@@ -617,7 +617,7 @@ class Scanner(Plugin):
|
|||||||
log.debug('Identifier to short to use for search: %s', identifier)
|
log.debug('Identifier to short to use for search: %s', identifier)
|
||||||
|
|
||||||
if imdb_id:
|
if imdb_id:
|
||||||
return fireEvent('library.add', attrs = {
|
return fireEvent('library.add.movie', attrs = {
|
||||||
'identifier': imdb_id
|
'identifier': imdb_id
|
||||||
}, update_after = False, single = True)
|
}, update_after = False, single = True)
|
||||||
|
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ class MetaDataBase(Plugin):
|
|||||||
|
|
||||||
# Update library to get latest info
|
# Update library to get latest info
|
||||||
try:
|
try:
|
||||||
updated_library = fireEvent('library.update', group['library']['identifier'], force = True, single = True)
|
updated_library = fireEvent('library.update.movie', group['library']['identifier'], force = True, single = True)
|
||||||
group['library'] = mergeDicts(group['library'], updated_library)
|
group['library'] = mergeDicts(group['library'], updated_library)
|
||||||
except:
|
except:
|
||||||
log.error('Failed to update movie, before creating metadata: %s', traceback.format_exc())
|
log.error('Failed to update movie, before creating metadata: %s', traceback.format_exc())
|
||||||
|
|||||||
Reference in New Issue
Block a user