'Get episode statistics for a given show'

def run(self):

    show_obj = Show.find(sickbeard.showList, int(self.indexerid))
    if (not show_obj):
        return _responds(RESULT_FAILURE, msg=u'Show    not    found')
    episode_status_counts_total = {u'total': 0}
    for status in statusStrings:
        if (status in [UNKNOWN, DOWNLOADED, SNATCHED, SNATCHED_PROPER, ARCHIVED]):
            continue
        episode_status_counts_total[status] = 0
    episode_qualities_counts_download = {u'total': 0}
    for statusCode in (Quality.DOWNLOADED + Quality.ARCHIVED):
        (status, quality) = Quality.splitCompositeStatus(statusCode)
        if (quality in [Quality.NONE]):
            continue
        episode_qualities_counts_download[statusCode] = 0
    episode_qualities_counts_snatch = {u'total': 0}
    for statusCode in (Quality.SNATCHED + Quality.SNATCHED_PROPER):
        (status, quality) = Quality.splitCompositeStatus(statusCode)
        if (quality in [Quality.NONE]):
            continue
        episode_qualities_counts_snatch[statusCode] = 0
    main_db_con = db.DBConnection(row_type=u'dict')
    sql_results = main_db_con.select(u'SELECT    status,    season    FROM    tv_episodes    WHERE    season    !=    0    AND    showid    =    ?', [self.indexerid])
    for row in sql_results:
        (status, quality) = Quality.splitCompositeStatus(int(row['status']))
        episode_status_counts_total[u'total'] += 1
        if (status in (Quality.DOWNLOADED + Quality.ARCHIVED)):
            episode_qualities_counts_download[u'total'] += 1
            episode_qualities_counts_download[int(row['status'])] += 1
        elif (status in (Quality.SNATCHED + Quality.SNATCHED_PROPER)):
            episode_qualities_counts_snatch[u'total'] += 1
            episode_qualities_counts_snatch[int(row['status'])] += 1
        elif (status > 0):
            episode_status_counts_total[status] += 1
    episodes_stats = {u'downloaded': {}}
    for statusCode in episode_qualities_counts_download:
        if (statusCode == u'total'):
            episodes_stats[u'downloaded'][u'total'] = episode_qualities_counts_download[statusCode]
            continue
        (status, quality) = Quality.splitCompositeStatus(int(statusCode))
        status_string = Quality.qualityStrings[quality].lower().replace(u'    ', u'_').replace(u'(', u'').replace(u')', u'')
        episodes_stats[u'downloaded'][status_string] = episode_qualities_counts_download[statusCode]
    episodes_stats[u'snatched'] = {}
    for statusCode in episode_qualities_counts_snatch:
        if (statusCode == u'total'):
            episodes_stats[u'snatched'][u'total'] = episode_qualities_counts_snatch[statusCode]
            continue
        (status, quality) = Quality.splitCompositeStatus(int(statusCode))
        status_string = Quality.qualityStrings[quality].lower().replace(u'    ', u'_').replace(u'(', u'').replace(u')', u'')
        if (Quality.qualityStrings[quality] in episodes_stats[u'snatched']):
            episodes_stats[u'snatched'][status_string] += episode_qualities_counts_snatch[statusCode]
        else:
            episodes_stats[u'snatched'][status_string] = episode_qualities_counts_snatch[statusCode]
    for statusCode in episode_status_counts_total:
        if (statusCode == u'total'):
            episodes_stats[u'total'] = episode_status_counts_total[statusCode]
            continue
        status_string = statusStrings[statusCode].lower().replace(u'    ', u'_').replace(u'(', u'').replace(u')', u'')
        episodes_stats[status_string] = episode_status_counts_total[statusCode]
    return _responds(RESULT_SUCCESS, episodes_stats)
