def get_coming_episodes(categories, sort, group, paused=sickbeard.COMING_EPS_DISPLAY_PAUSED):
        """
        :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
        """

        categories = ComingEpisodes._get_categories(categories)
        sort = ComingEpisodes._get_sort(sort)

        today = date.today().toordinal()
        next_week = (date.today() + timedelta(days=7)).toordinal()
        recently = (date.today() - timedelta(days=sickbeard.COMING_EPS_MISSED_RANGE)).toordinal()
        qualities_list = Quality.DOWNLOADED + Quality.SNATCHED + Quality.SNATCHED_BEST + Quality.SNATCHED_PROPER + Quality.ARCHIVED + [IGNORED]

        db = DBConnection()
        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 = db.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['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 += db.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 += db.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]['localtime'] = sbdatetime.convert_to_setting(
                parse_date_time(item['airdate'], item['airs'], item['network']))

        results.sort(ComingEpisodes.sorts[sort])

        if not group:
            return results

        grouped_results = ComingEpisodes._get_categories_map(categories)

        for result in results:
            if result['paused'] and not paused:
                continue

            result['airs'] = str(result['airs']).replace('am', ' AM').replace('pm', ' PM').replace('  ', ' ')
            result['airdate'] = result['localtime'].toordinal()

            if result['airdate'] < today:
                category = 'missed'
            elif result['airdate'] >= next_week:
                category = 'later'
            elif result['airdate'] == today:
                category = 'today'
            else:
                category = 'soon'

            if len(categories) > 0 and category not in categories:
                continue

            if not result['network']:
                result['network'] = ''

            result['quality'] = get_quality_string(result['quality'])
            result['airs'] = sbdatetime.sbftime(result['localtime'], t_preset=timeFormat).lstrip('0').replace(' 0', ' ')
            result['weekday'] = 1 + date.fromordinal(result['airdate']).weekday()
            result['tvdbid'] = result['indexer_id']
            result['airdate'] = sbdatetime.sbfdate(result['localtime'], d_preset=dateFormat)
            result['localtime'] = result['localtime'].toordinal()

            grouped_results[category].append(result)

        return grouped_results
Exemple #2
0
    def get_coming_episodes(categories,
                            sort,
                            group,
                            paused=sickbeard.COMING_EPS_DISPLAY_PAUSED):
        """
        :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
        """

        categories = ComingEpisodes._get_categories(categories)
        sort = ComingEpisodes._get_sort(sort)

        today = date.today().toordinal()
        next_week = (date.today() + timedelta(days=7)).toordinal()
        recently = (
            date.today() -
            timedelta(days=sickbeard.COMING_EPS_MISSED_RANGE)).toordinal()
        qualities_list = Quality.DOWNLOADED + Quality.SNATCHED + Quality.SNATCHED_BEST + Quality.SNATCHED_PROPER + Quality.ARCHIVED + [
            IGNORED
        ]

        db = DBConnection()
        fields_to_select = ', '.join([
            'airdate', 'airs', 'e.description as description', 'episode',
            'imdb_id', 'e.indexer', 'indexer_id', 'name', 'network', 'paused',
            'quality', 'runtime', 'season', 'show_name', 'showid', 's.status'
        ])
        results = db.select(
            'SELECT {0} '.format(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['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 += db.select(
            'SELECT {0} '.format(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 += db.select(
            'SELECT {0} '.format(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]['localtime'] = sbdatetime.convert_to_setting(
                parse_date_time(item['airdate'], item['airs'],
                                item['network']))

        results.sort(ComingEpisodes.sorts[sort])

        if not group:
            return results

        grouped_results = ComingEpisodes._get_categories_map(categories)

        for result in results:
            if result['paused'] and not paused:
                continue

            result['airs'] = str(result['airs']).replace('am', ' AM').replace(
                'pm', ' PM').replace('  ', ' ')
            result['airdate'] = result['localtime'].toordinal()

            if result['airdate'] < today:
                category = 'missed'
            elif result['airdate'] >= next_week:
                category = 'later'
            elif result['airdate'] == today:
                category = 'today'
            else:
                category = 'soon'

            if len(categories) > 0 and category not in categories:
                continue

            if not result['network']:
                result['network'] = ''

            result['quality'] = get_quality_string(result['quality'])
            result['airs'] = sbdatetime.sbftime(
                result['localtime'],
                t_preset=timeFormat).lstrip('0').replace(' 0', ' ')
            result['weekday'] = 1 + date.fromordinal(
                result['airdate']).weekday()
            result['tvdbid'] = result['indexer_id']
            result['airdate'] = sbdatetime.sbfdate(result['localtime'],
                                                   d_preset=dateFormat)
            result['localtime'] = result['localtime'].toordinal()

            grouped_results[category].append(result)

        return grouped_results
Exemple #3
0
    def get_coming_episodes(categories, sort, group, paused=sickbeard.COMING_EPS_DISPLAY_PAUSED):
        """
        :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
        """

        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=sickbeard.COMING_EPS_MISSED_RANGE)).toordinal()
        qualities_list = (
            Quality.DOWNLOADED
            + Quality.SNATCHED
            + Quality.SNATCHED_BEST
            + Quality.SNATCHED_PROPER
            + Quality.ARCHIVED
            + [IGNORED]
        )

        db = DBConnection()
        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 = db.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 += db.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 += db.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"] = sbdatetime.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"] = (
                sbdatetime.sbftime(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"] = sbdatetime.sbfdate(result[b"localtime"], d_preset=dateFormat)
            result[b"localtime"] = result[b"localtime"].toordinal()

            grouped_results[category].append(result)

        return grouped_results
Exemple #4
0
    def get_coming_episodes(categories, sort, group, paused=sickbeard.COMING_EPS_DISPLAY_PAUSED):
        """
        :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
        """

        categories = ComingEpisodes._get_categories(categories)
        sort = ComingEpisodes._get_sort(sort)

        today = date.today().toordinal()
        recently = (date.today() - timedelta(days=sickbeard.COMING_EPS_MISSED_RANGE)).toordinal()
        next_week = (date.today() + timedelta(days=7)).toordinal()

        db = DBConnection(row_type='dict')
        fields_to_select = ', '.join(
            ['airdate', 'airs', 'e.description as description', 'episode', 'imdb_id', 'e.indexer', 'indexer_id', 'name', 'network',
             'paused', 'quality', 'runtime', 'season', 'show_name', 'showid', 's.status']
        )

        sql_l = []
        for show_obj in sickbeard.showList:
            next_air_date = show_obj.nextEpisode()
            if next_air_date:
                sql_l.append(
                    [
                        'SELECT DISTINCT {0} '.format(fields_to_select) +
                        'FROM tv_episodes e, tv_shows s '
                        'WHERE showid = ? '
                        'AND airdate <= ? '
                        'AND airdate >= ? '
                        'AND s.indexer_id = e.showid '
                        'AND e.status IN (' + ','.join(['?'] * 2) + ')',
                        [show_obj.indexerid, next_air_date, recently, WANTED, UNAIRED]
                    ]
                )

        results = []
        for sql_i in sql_l:
            if results:
                results += db.select(*sql_i)
            else:
                results = db.select(*sql_i)

        for index, item in enumerate(results):
            results[index][b'localtime'] = sbdatetime.convert_to_setting(
                parse_date_time(item[b'airdate'], item[b'airs'], item[b'network']))

        results.sort(key=itemgetter(b'localtime'))
        results.sort(ComingEpisodes.sorts[sort])

        if not group:
            return results

        grouped_results = ComingEpisodes._get_categories_map(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'] = sbdatetime.sbftime(result[b'localtime'], t_preset=timeFormat).lstrip('0').replace(' 0', ' ')
            result[b'weekday'] = 1 + result[b'localtime'].weekday()
            result[b'tvdbid'] = result[b'indexer_id']
            result[b'airdate'] = sbdatetime.sbfdate(result[b'localtime'], d_preset=dateFormat)
            result[b'localtime'] = result[b'localtime'].toordinal()

            grouped_results[category].append(result)

        return grouped_results