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.

118 lines
3.9 KiB

11 years ago
import random as rndm
import time
from CodernityDB.database import RecordDeleted, RecordNotFound
11 years ago
11 years ago
from couchpotato import get_db
12 years ago
from couchpotato.api import addApiView
from couchpotato.core.event import fireEvent
from couchpotato.core.helpers.variable import splitString, tryInt
from couchpotato.core.logger import CPLog
from couchpotato.core.plugins.base import Plugin
11 years ago
12 years ago
log = CPLog(__name__)
autoload = 'Dashboard'
12 years ago
class Dashboard(Plugin):
def __init__(self):
addApiView('dashboard.soon', self.getSoonView)
def getSoonView(self, limit_offset = None, random = False, late = False, **kwargs):
12 years ago
11 years ago
db = get_db()
now = time.time()
12 years ago
# Get profiles first, determine pre or post theater
profiles = fireEvent('profile.all', single = True)
pre_releases = fireEvent('quality.pre_releases', single = True)
# See what the profile contain and cache it
profile_pre = {}
for profile in profiles:
contains = {}
11 years ago
for q_identifier in profile.get('qualities', []):
contains['theater' if q_identifier in pre_releases else 'dvd'] = True
12 years ago
11 years ago
profile_pre[profile.get('_id')] = contains
12 years ago
# Add limit
limit = 12
if limit_offset:
splt = splitString(limit_offset) if isinstance(limit_offset, (str, unicode)) else limit_offset
limit = tryInt(splt[0])
11 years ago
# Get all active medias
active_ids = [x['_id'] for x in fireEvent('media.with_status', 'active', with_doc = False, single = True)]
11 years ago
medias = []
11 years ago
if len(active_ids) > 0:
11 years ago
# Order by title or randomize
if not random:
orders_ids = db.all('media_title')
active_ids = [x['_id'] for x in orders_ids if x['_id'] in active_ids]
else:
rndm.shuffle(active_ids)
11 years ago
for media_id in active_ids:
try:
media = db.get('id', media_id)
except RecordDeleted:
log.debug('Record already deleted: %s', media_id)
continue
except RecordNotFound:
log.debug('Record not found: %s', media_id)
continue
pp = profile_pre.get(media.get('profile_id'))
if not pp: continue
11 years ago
eta = media['info'].get('release_date', {}) or {}
coming_soon = False
# Theater quality
event = '%s.searcher.could_be_released' % (media.get('type'))
if pp.get('theater') and fireEvent(event, True, eta, media, single = True):
coming_soon = 'theater'
elif pp.get('dvd') and fireEvent(event, False, eta, media, single = True):
coming_soon = 'dvd'
if coming_soon:
# Don't list older movies
eta_date = eta.get(coming_soon)
eta_3month_passed = eta_date < (now - 7862400) # Release was more than 3 months ago
if (not late and not eta_3month_passed) or \
(late and eta_3month_passed):
add = True
# Check if it doesn't have any releases
if late:
media['releases'] = fireEvent('release.for_media', media['_id'], single = True)
for release in media.get('releases', []):
if release.get('status') in ['snatched', 'available', 'seeding', 'downloaded']:
add = False
break
if add:
medias.append(media)
11 years ago
if len(medias) >= limit:
break
return {
12 years ago
'success': True,
11 years ago
'empty': len(medias) == 0,
'movies': medias,
}
getLateView = getSoonView