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.

107 lines
3.8 KiB

from datetime import date
11 years ago
import random as rndm
import time
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 = []
now_year = date.today().year
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:
media = db.get('id', media_id)
11 years ago
pp = profile_pre.get(media['profile_id'])
if not pp: continue
11 years ago
eta = media['info'].get('release_date', {}) or {}
coming_soon = False
# Theater quality
11 years ago
if pp.get('theater') and fireEvent('movie.searcher.could_be_released', True, eta, media['info']['year'], single = True):
coming_soon = True
11 years ago
elif pp.get('dvd') and fireEvent('movie.searcher.could_be_released', False, eta, media['info']['year'], single = True):
coming_soon = True
if coming_soon:
# Don't list older movies
11 years ago
if ((not late and (media['info']['year'] >= now_year - 1) and (not eta.get('dvd') and not eta.get('theater') or eta.get('dvd') and eta.get('dvd') > (now - 2419200))) or
(late and (media['info']['year'] < now_year - 1 or (eta.get('dvd', 0) > 0 or eta.get('theater')) and eta.get('dvd') < (now - 2419200)))):
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