'Set the status of an episode or a season (when no episode is provided)'

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')
    for status in statusStrings:
        if (str(statusStrings[status]).lower() == str(self.status).lower()):
            self.status = status
            break
    else:
        raise ApiError(u'The    status    string    could    not    be    matched    to    a    status.    Report    to    Devs!')
    if self.e:
        ep_obj = show_obj.getEpisode(self.s, self.e)
        if (not ep_obj):
            return _responds(RESULT_FAILURE, msg=u'Episode    not    found')
        ep_list = [ep_obj]
    else:
        ep_list = show_obj.getAllEpisodes(season=self.s)
    def _ep_result(result_code, ep, msg=u''):
        return {u'season': ep.season, u'episode': ep.episode, u'status': _get_status_strings(ep.status), u'result': result_type_map[result_code], u'message': msg}
    ep_results = []
    failure = False
    start_backlog = False
    segments = {}
    sql_l = []
    for ep_obj in ep_list:
        with ep_obj.lock:
            if (self.status == WANTED):
                if (ep_obj.season in segments):
                    segments[ep_obj.season].append(ep_obj)
                else:
                    segments[ep_obj.season] = [ep_obj]
            if (ep_obj.status == UNAIRED):
                if (self.e is not None):
                    ep_results.append(_ep_result(RESULT_FAILURE, ep_obj, u'Refusing    to    change    status    because    it    is    UN-AIRED'))
                    failure = True
                continue
            if ((self.status == FAILED) and (not sickbeard.USE_FAILED_DOWNLOADS)):
                ep_results.append(_ep_result(RESULT_FAILURE, ep_obj, u'Refusing    to    change    status    to    FAILED    because    failed    download    handling    is    disabled'))
                failure = True
                continue
            if ((ep_obj.status in (Quality.DOWNLOADED + Quality.ARCHIVED)) and (not self.force)):
                ep_results.append(_ep_result(RESULT_FAILURE, ep_obj, u'Refusing    to    change    status    because    it    is    already    marked    as    DOWNLOADED'))
                failure = True
                continue
            ep_obj.status = self.status
            sql_l.append(ep_obj.get_sql())
            if (self.status == WANTED):
                start_backlog = True
            ep_results.append(_ep_result(RESULT_SUCCESS, ep_obj))
    if sql_l:
        main_db_con = db.DBConnection()
        main_db_con.mass_action(sql_l)
    extra_msg = u''
    if start_backlog:
        for (season, segment) in six.iteritems(segments):
            cur_backlog_queue_item = search_queue.BacklogQueueItem(show_obj, segment)
            sickbeard.searchQueueScheduler.action.add_item(cur_backlog_queue_item)
            logger.log(((((u'API    ::    Starting    backlog    for    ' + show_obj.name) + u'    season    ') + str(season)) + u'    because    some    episodes    were    set    to    WANTED'))
        extra_msg = u'    Backlog    started'
    if failure:
        return _responds(RESULT_FAILURE, ep_results, (u'Failed    to    set    all    or    some    status.    Check    data.' + extra_msg))
    else:
        return _responds(RESULT_SUCCESS, msg=(u'All    status    set    successfully.' + extra_msg))
