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.

95 lines
2.7 KiB

11 years ago
from couchpotato import get_db
from couchpotato.core.event import addEvent, fireEvent, fireEventAsync
11 years ago
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 = 'Season'
11 years ago
11 years ago
class Season(MediaBase):
11 years ago
def __init__(self):
11 years ago
addEvent('show.season.add', self.add)
addEvent('show.season.update_info', self.updateInfo)
11 years ago
def add(self, parent_id, info = None, update_after = True, status = None):
11 years ago
if not info: info = {}
identifiers = info.get('identifiers')
try: del info['identifiers']
except: pass
try: del info['episodes']
except: pass
11 years ago
# Add Season
11 years ago
season_info = {
11 years ago
'_t': 'media',
'type': 'show.season',
11 years ago
'identifiers': identifiers,
'status': status if status else 'active',
'parent_id': parent_id,
'info': info, # Returned dict by providers
11 years ago
}
# Check if season already exists
11 years ago
existing_season = 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_season:
s = existing_season['doc']
s.update(season_info)
season = db.update(s)
else:
season = db.insert(season_info)
11 years ago
# Update library info
if update_after is not False:
handle = fireEventAsync if update_after is 'async' else fireEvent
handle('show.season.update_info', season.get('_id'), identifiers, info, single = True)
11 years ago
return season
def updateInfo(self, media_id = None, identifiers = None, info = None):
11 years ago
if not info: info = {}
11 years ago
identifiers = info.get('identifiers') or identifiers
try: del info['identifiers']
except: pass
try: del info['episodes']
except: pass
11 years ago
if self.shuttingDown():
return
11 years ago
db = get_db()
if media_id:
season = db.get('id', media_id)
else:
season = db.get('media', identifiers, with_doc = True)['doc']
show = db.get('id', season['parent_id'])
11 years ago
11 years ago
# Get new info
11 years ago
if not info:
info = fireEvent('season.info', show.get('identifiers'), {
'season_number': season.get('info', {}).get('number', 0)
}, merge = True)
11 years ago
# Update/create media
season['identifiers'].update(identifiers)
season.update({'info': info})
11 years ago
11 years ago
# Get images
image_urls = info.get('images', [])
existing_files = season.get('files', {})
self.getPoster(image_urls, existing_files)
db.update(season)
11 years ago
return season