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.

148 lines
4.4 KiB

11 years ago
from couchpotato import get_db
from couchpotato.core.event import addEvent, fireEvent, fireEventAsync
11 years ago
from couchpotato.core.helpers.encoding import toUnicode
from couchpotato.core.logger import CPLog
from couchpotato.core.helpers.variable import tryInt
11 years ago
from couchpotato.core.media import MediaBase
11 years ago
log = CPLog(__name__)
11 years ago
autoload = 'Episode'
11 years ago
11 years ago
class Episode(MediaBase):
11 years ago
def __init__(self):
addEvent('media.search_query', self.query)
addEvent('media.identifier', self.identifier)
addEvent('show.episode.add', self.add)
11 years ago
addEvent('show.episode.update_info', self.updateInfo)
11 years ago
def add(self, parent_id, info = None, update_after = True):
if not info: info = {}
identifiers = info.get('identifiers')
try: del info['identifiers']
except: pass
11 years ago
# Add Season
11 years ago
episode_info = {
11 years ago
'_t': 'media',
'type': 'episode',
11 years ago
'identifiers': identifiers,
'parent': parent_id,
'info': info, # Returned dict by providers
11 years ago
}
11 years ago
# Check if season already exists
existing_episode = fireEvent('media.with_identifiers', identifiers, with_doc = True, single = True)
11 years ago
11 years ago
db = get_db()
11 years ago
11 years ago
if existing_episode:
s = existing_episode['doc']
s.update(episode_info)
episode = db.update(s)
else:
episode = db.insert(episode_info)
11 years ago
# Update library info
if update_after is not False:
handle = fireEventAsync if update_after is 'async' else fireEvent
11 years ago
handle('show.season.update_info', episode.get('_id'), info = info, single = True)
11 years ago
11 years ago
return episode
11 years ago
11 years ago
def update_info(self, media_id = None, info = None, force = False):
if not info: info = {}
11 years ago
if self.shuttingDown():
return
11 years ago
db = get_db()
episode = db.get('id', media_id)
11 years ago
# Get new info
11 years ago
if not info:
info = fireEvent('episode.info', episode.get('identifiers'), merge = True)
11 years ago
# Update/create media
11 years ago
if force:
11 years ago
11 years ago
episode['identifiers'].update(info['identifiers'])
if 'identifiers' in info:
del info['identifiers']
11 years ago
11 years ago
episode.update({'info': info})
e = db.update(episode)
episode.update(e)
11 years ago
11 years ago
# Get images
image_urls = info.get('images', [])
existing_files = episode.get('files', {})
self.getPoster(image_urls, existing_files)
11 years ago
11 years ago
return episode
11 years ago
def query(self, library, first = True, condense = True, include_identifier = True, **kwargs):
if library is list or library.get('type') != 'episode':
return
# Get the titles of the season
if not library.get('related_libraries', {}).get('season', []):
log.warning('Invalid library, unable to determine title.')
return
titles = fireEvent(
'media.search_query',
library['related_libraries']['season'][0],
first=False,
include_identifier=include_identifier,
condense=condense,
single=True
)
identifier = fireEvent('media.identifier', library, single = True)
# Add episode identifier to titles
if include_identifier and identifier.get('episode'):
titles = [title + ('E%02d' % identifier['episode']) for title in titles]
if first:
return titles[0] if titles else None
return titles
def identifier(self, library):
if library.get('type') != 'episode':
return
identifier = {
'season': None,
'episode': None
}
scene_map = library['info'].get('map_episode', {}).get('scene')
if scene_map:
# Use scene mappings if they are available
identifier['season'] = scene_map.get('season')
identifier['episode'] = scene_map.get('episode')
else:
# Fallback to normal season/episode numbers
identifier['season'] = library.get('season_number')
identifier['episode'] = library.get('episode_number')
# Cast identifiers to integers
# TODO this will need changing to support identifiers with trailing 'a', 'b' characters
identifier['season'] = tryInt(identifier['season'], None)
identifier['episode'] = tryInt(identifier['episode'], None)
return identifier