def get_coming_episodes(categories, sort, group, paused=False): """ :param categories: The categories of coming episodes. See ``ComingEpisodes.categories`` :param sort: The sort to apply to the coming episodes. See ``ComingEpisodes.sorts`` :param group: ``True`` to group the coming episodes by category, ``False`` otherwise :param paused: ``True`` to include paused shows, ``False`` otherwise :return: The list of coming episodes """ paused = sickrage.srConfig.COMING_EPS_DISPLAY_PAUSED or paused if not isinstance(categories, list): categories = categories.split('|') if sort not in ComingEpisodes.sorts.keys(): sort = 'date' today = date.today().toordinal() next_week = (date.today() + timedelta(days=7)).toordinal() recently = (date.today() - timedelta( days=sickrage.srConfig.COMING_EPS_MISSED_RANGE)).toordinal() qualities_list = Quality.DOWNLOADED + \ Quality.SNATCHED + \ Quality.SNATCHED_BEST + \ Quality.SNATCHED_PROPER + \ Quality.ARCHIVED + \ Quality.IGNORED fields_to_select = ', '.join([ 'airdate', 'airs', 'description', 'episode', 'imdb_id', 'e.indexer', 'indexer_id', 'name', 'network', 'paused', 'quality', 'runtime', 'season', 'show_name', 'showid', 's.status' ]) results = main_db.MainDB().select( 'SELECT %s ' % fields_to_select + 'FROM tv_episodes e, tv_shows s ' 'WHERE season != 0 ' 'AND airdate >= ? ' 'AND airdate < ? ' 'AND s.indexer_id = e.showid ' 'AND e.status NOT IN (' + ','.join(['?'] * len(qualities_list)) + ')', [today, next_week] + qualities_list) done_shows_list = [int(result[b'showid']) for result in results] placeholder = ','.join(['?'] * len(done_shows_list)) placeholder2 = ','.join( ['?'] * len(Quality.DOWNLOADED + Quality.SNATCHED + Quality.SNATCHED_BEST + Quality.SNATCHED_PROPER)) results += main_db.MainDB().select( 'SELECT %s ' % fields_to_select + 'FROM tv_episodes e, tv_shows s ' 'WHERE season != 0 ' 'AND showid NOT IN (' + placeholder + ') ' 'AND s.indexer_id = e.showid ' 'AND airdate = (SELECT airdate ' 'FROM tv_episodes inner_e ' 'WHERE inner_e.season != 0 ' 'AND inner_e.showid = e.showid ' 'AND inner_e.airdate >= ? ' 'ORDER BY inner_e.airdate ASC LIMIT 1) ' 'AND e.status NOT IN (' + placeholder2 + ')', done_shows_list + [next_week] + Quality.DOWNLOADED + Quality.SNATCHED + Quality.SNATCHED_BEST + Quality.SNATCHED_PROPER) results += main_db.MainDB().select( 'SELECT %s ' % fields_to_select + 'FROM tv_episodes e, tv_shows s ' 'WHERE season != 0 ' 'AND s.indexer_id = e.showid ' 'AND airdate < ? ' 'AND airdate >= ? ' 'AND e.status IN (?,?) ' 'AND e.status NOT IN (' + ','.join(['?'] * len(qualities_list)) + ')', [today, recently, WANTED, UNAIRED] + qualities_list) results = [dict(result) for result in results] for index, item in enumerate(results): results[index][b'localtime'] = srDateTime.convert_to_setting( parse_date_time(item[b'airdate'], item[b'airs'], item[b'network'])) results.sort(ComingEpisodes.sorts[sort]) if not group: return results grouped_results = {category: [] for category in categories} for result in results: if result[b'paused'] and not paused: continue result[b'airs'] = str(result[b'airs']).replace( 'am', ' AM').replace('pm', ' PM').replace(' ', ' ') result[b'airdate'] = result[b'localtime'].toordinal() if result[b'airdate'] < today: category = 'missed' elif result[b'airdate'] >= next_week: category = 'later' elif result[b'airdate'] == today: category = 'today' else: category = 'soon' if len(categories) > 0 and category not in categories: continue if not result[b'network']: result[b'network'] = '' result[b'quality'] = get_quality_string(result[b'quality']) result[b'airs'] = srDateTime.srftime( result[b'localtime'], t_preset=timeFormat).lstrip('0').replace(' 0', ' ') result[b'weekday'] = 1 + date.fromordinal( result[b'airdate']).weekday() result[b'tvdbid'] = result[b'indexer_id'] result[b'airdate'] = srDateTime.srfdate(result[b'localtime'], d_preset=dateFormat) result[b'localtime'] = result[b'localtime'].toordinal() grouped_results[category].append(result) return grouped_results
def run(self, force=False): """ Runs the daily searcher, queuing selected episodes for search :param force: Force search """ if self.amActive: return self.amActive = True # trim failed download history if sickrage.srConfig.USE_FAILED_DOWNLOADS: FailedHistory.trimHistory() sickrage.srLogger.info("Searching for new released episodes ...") if tz_updater.load_network_dict(): curDate = (date.today() + timedelta(days=1)).toordinal() else: curDate = (date.today() + timedelta(days=2)).toordinal() curTime = datetime.now(tz_updater.sr_timezone) sqlResults = main_db.MainDB().select( "SELECT * FROM tv_episodes WHERE status = ? AND season > 0 AND (airdate <= ? AND airdate > 1)", [UNAIRED, curDate]) sql_l = [] show = None for sqlEp in sqlResults: try: if not show or int(sqlEp[b"showid"]) != show.indexerid: show = findCertainShow(sickrage.srCore.SHOWLIST, int(sqlEp[b"showid"])) # for when there is orphaned series in the database but not loaded into our showlist if not show or show.paused: continue except MultipleShowObjectsException: sickrage.srLogger.info("ERROR: expected to find a single show matching " + str(sqlEp[b'showid'])) continue if show.airs and show.network: # This is how you assure it is always converted to local time air_time = tz_updater.parse_date_time(sqlEp[b'airdate'], show.airs, show.network).astimezone( tz_updater.sr_timezone) # filter out any episodes that haven't started airing yet, # but set them to the default status while they are airing # so they are snatched faster if air_time > curTime: continue ep = show.getEpisode(int(sqlEp[b"season"]), int(sqlEp[b"episode"])) with ep.lock: if ep.season == 0: sickrage.srLogger.info( "New episode " + ep.prettyName() + " airs today, setting status to SKIPPED because is a special season") ep.status = SKIPPED else: sickrage.srLogger.info("New episode %s airs today, setting to default episode status for this show: %s" % ( ep.prettyName(), statusStrings[ep.show.default_ep_status])) ep.status = ep.show.default_ep_status sql_q = ep.saveToDB(False) if sql_q: sql_l.append(sql_q) del sql_q if len(sql_l) > 0: main_db.MainDB().mass_upsert(sql_l) del sql_l else: sickrage.srLogger.info("No new released episodes found ...") # queue episode for daily search sickrage.srCore.SEARCHQUEUE.add_item(DailySearchQueueItem()) self.amActive = False
def get_coming_episodes(categories, sort, group, paused=False): """ :param categories: The categories of coming episodes. See ``ComingEpisodes.categories`` :param sort: The sort to apply to the coming episodes. See ``ComingEpisodes.sorts`` :param group: ``True`` to group the coming episodes by category, ``False`` otherwise :param paused: ``True`` to include paused shows, ``False`` otherwise :return: The list of coming episodes """ paused = sickrage.srConfig.COMING_EPS_DISPLAY_PAUSED or paused if not isinstance(categories, list): categories = categories.split('|') if sort not in ComingEpisodes.sorts.keys(): sort = 'date' today = date.today().toordinal() next_week = (date.today() + timedelta(days=7)).toordinal() recently = (date.today() - timedelta(days=sickrage.srConfig.COMING_EPS_MISSED_RANGE)).toordinal() qualities_list = Quality.DOWNLOADED + \ Quality.SNATCHED + \ Quality.SNATCHED_BEST + \ Quality.SNATCHED_PROPER + \ Quality.ARCHIVED + \ Quality.IGNORED fields_to_select = ', '.join( ['airdate', 'airs', 'description', 'episode', 'imdb_id', 'e.indexer', 'indexer_id', 'name', 'network', 'paused', 'quality', 'runtime', 'season', 'show_name', 'showid', 's.status'] ) results = main_db.MainDB().select( 'SELECT %s ' % fields_to_select + 'FROM tv_episodes e, tv_shows s ' 'WHERE season != 0 ' 'AND airdate >= ? ' 'AND airdate < ? ' 'AND s.indexer_id = e.showid ' 'AND e.status NOT IN (' + ','.join(['?'] * len(qualities_list)) + ')', [today, next_week] + qualities_list ) done_shows_list = [int(result[b'showid']) for result in results] placeholder = ','.join(['?'] * len(done_shows_list)) placeholder2 = ','.join( ['?'] * len(Quality.DOWNLOADED + Quality.SNATCHED + Quality.SNATCHED_BEST + Quality.SNATCHED_PROPER)) results += main_db.MainDB().select( 'SELECT %s ' % fields_to_select + 'FROM tv_episodes e, tv_shows s ' 'WHERE season != 0 ' 'AND showid NOT IN (' + placeholder + ') ' 'AND s.indexer_id = e.showid ' 'AND airdate = (SELECT airdate ' 'FROM tv_episodes inner_e ' 'WHERE inner_e.season != 0 ' 'AND inner_e.showid = e.showid ' 'AND inner_e.airdate >= ? ' 'ORDER BY inner_e.airdate ASC LIMIT 1) ' 'AND e.status NOT IN (' + placeholder2 + ')', done_shows_list + [ next_week] + Quality.DOWNLOADED + Quality.SNATCHED + Quality.SNATCHED_BEST + Quality.SNATCHED_PROPER ) results += main_db.MainDB().select( 'SELECT %s ' % fields_to_select + 'FROM tv_episodes e, tv_shows s ' 'WHERE season != 0 ' 'AND s.indexer_id = e.showid ' 'AND airdate < ? ' 'AND airdate >= ? ' 'AND e.status IN (?,?) ' 'AND e.status NOT IN (' + ','.join(['?'] * len(qualities_list)) + ')', [today, recently, WANTED, UNAIRED] + qualities_list ) results = [dict(result) for result in results] for index, item in enumerate(results): results[index][b'localtime'] = srDateTime.convert_to_setting( parse_date_time(item[b'airdate'], item[b'airs'], item[b'network'])) results.sort(ComingEpisodes.sorts[sort]) if not group: return results grouped_results = {category: [] for category in categories} for result in results: if result[b'paused'] and not paused: continue result[b'airs'] = str(result[b'airs']).replace('am', ' AM').replace('pm', ' PM').replace(' ', ' ') result[b'airdate'] = result[b'localtime'].toordinal() if result[b'airdate'] < today: category = 'missed' elif result[b'airdate'] >= next_week: category = 'later' elif result[b'airdate'] == today: category = 'today' else: category = 'soon' if len(categories) > 0 and category not in categories: continue if not result[b'network']: result[b'network'] = '' result[b'quality'] = get_quality_string(result[b'quality']) result[b'airs'] = srDateTime.srftime(result[b'localtime'], t_preset=timeFormat).lstrip('0').replace(' 0', ' ') result[b'weekday'] = 1 + date.fromordinal(result[b'airdate']).weekday() result[b'tvdbid'] = result[b'indexer_id'] result[b'airdate'] = srDateTime.srfdate(result[b'localtime'], d_preset=dateFormat) result[b'localtime'] = result[b'localtime'].toordinal() grouped_results[category].append(result) return grouped_results