# Buggy code
def loadEpisodesFromDB(self):  # pylint: disable=too-many-locals
    logger.log("Loading all episodes from the DB", logger.DEBUG)
    scannedEps = {}

    try:
        main_db_con = db.DBConnection()
        sql = "SELECT season, episode, showid, show_name FROM tv_episodes JOIN tv_shows WHERE showid = indexer_id and showid = ?"
        sql_results = main_db_con.select(sql, [self.indexerid])
    except Exception as error:
        logger.log(
            "Could not load episodes from the DB. Error: %s" % error, logger.ERROR
        )
        return scannedEps

    lINDEXER_API_PARMS = sickbeard.indexerApi(self.indexer).api_params.copy()

    if self.lang:
        lINDEXER_API_PARMS["language"] = self.lang
        logger.log("Using language: " + str(self.lang), logger.DEBUG)

    if self.dvdorder != 0:
        lINDEXER_API_PARMS["dvdorder"] = True

    # logger.log(u"lINDEXER_API_PARMS: " + str(lINDEXER_API_PARMS), logger.DEBUG)
    # Spamming log
    t = sickbeard.indexerApi(self.indexer).indexer(**lINDEXER_API_PARMS)

    cachedShow = t[self.indexerid]
    cachedSeasons = {}

    for curResult in sql_results:
        curSeason = int(curResult["season"])
        curEpisode = int(curResult["episode"])
        curShowid = int(curResult["showid"])
        curShowName = str(curResult["show_name"])

        logger.log(
            "%s: Loading %s episodes from DB" % (curShowid, curShowName), logger.DEBUG
        )
        deleteEp = False

        if curSeason not in cachedSeasons:
            try:
                cachedSeasons[curSeason] = cachedShow[curSeason]
            except sickbeard.indexer_seasonnotfound as error:
                logger.log(
                    "%s: %s (unaired/deleted) in the indexer %s for %s. Removing existing records from database"
                    % (
                        curShowid,
                        error.message,
                        sickbeard.indexerApi(self.indexer).name,
                        curShowName,
                    ),
                    logger.DEBUG,
                )
                deleteEp = True

        if curSeason not in scannedEps:
            logger.log(
                "{id}: Not curSeason in scannedEps".format(id=curShowid), logger.DEBUG
            )
            scannedEps[curSeason] = {}

        logger.log(
            "{id}: Loading {show} {ep} from the DB".format(
                id=curShowid, show=curShowName, ep=episode_num(curSeason, curEpisode)
            ),
            logger.DEBUG,
        )

        try:
            curEp = self.getEpisode(curSeason, curEpisode)
            if not curEp:
                raise EpisodeNotFoundException

            # if we found out that the ep is no longer on TVDB then delete it from our database too
            if deleteEp:
                curEp.deleteEpisode()

            curEp.loadFromDB(curSeason, curEpisode)
            curEp.loadFromIndexer(tvapi=t, cachedSeason=cachedSeasons[curSeason])
            scannedEps[curSeason][curEpisode] = True
        except EpisodeDeletedException:
            logger.log(
                "{id}: Tried loading {show} {ep} from the DB that should have been deleted, skipping it".format(
                    id=curShowid,
                    show=curShowName,
                    ep=episode_num(curSeason, curEpisode),
                ),
                logger.DEBUG,
            )
            continue

    logger.log(
        "{id}: Finished loading all episodes for {show} from the DB".format(
            show=curShowName, id=curShowid
        ),
        logger.DEBUG,
    )

    return scannedEps


# Traceback (KeyError)
EVENT-QUEUE :: [316d27c] Exception generated in thread EVENT-QUEUE: year
Traceback (most recent call last):
File "/usr/local/Sickrage/sickbeard/event_queue.py", line 45, in run
self.callback(event_type)
File "/usr/local/Sickrage/SickBeard.py", line 519, in shutdown
sickbeard.saveAll()  # save all shows to DB
File "/usr/local/Sickrage/sickbeard/__init__.py", line 1655, in saveAll
show.saveToDB()
File "/usr/local/Sickrage/sickbeard/tv.py", line 1171, in saveToDB
if self.imdbid and self.imdb_info['year']:
KeyError: 'year'

# Fixed code
def loadEpisodesFromDB(self):  # pylint: disable=too-many-locals
    logger.log("Loading all episodes from the DB", logger.DEBUG)
    scannedEps = {}

    try:
        main_db_con = db.DBConnection()
        sql = "SELECT season, episode, showid, show_name FROM tv_episodes JOIN tv_shows WHERE showid = indexer_id and showid = ?"
        sql_results = main_db_con.select(sql, [self.indexerid])
    except Exception as error:
        logger.log(
            "Could not load episodes from the DB. Error: %s" % error, logger.ERROR
        )
        return scannedEps

    lINDEXER_API_PARMS = sickbeard.indexerApi(self.indexer).api_params.copy()

    if self.lang:
        lINDEXER_API_PARMS["language"] = self.lang
        logger.log("Using language: " + str(self.lang), logger.DEBUG)

    if self.dvdorder != 0:
        lINDEXER_API_PARMS["dvdorder"] = True

    # logger.log(u"lINDEXER_API_PARMS: " + str(lINDEXER_API_PARMS), logger.DEBUG)
    # Spamming log
    t = sickbeard.indexerApi(self.indexer).indexer(**lINDEXER_API_PARMS)

    cachedShow = t[self.indexerid]
    cachedSeasons = {}

    for curResult in sql_results:
        curSeason = int(curResult["season"])
        curEpisode = int(curResult["episode"])
        curShowid = int(curResult["showid"])
        curShowName = unicode(curResult["show_name"])

        logger.log(
            "%s: Loading %s episodes from DB" % (curShowid, curShowName), logger.DEBUG
        )
        deleteEp = False

        if curSeason not in cachedSeasons:
            try:
                cachedSeasons[curSeason] = cachedShow[curSeason]
            except sickbeard.indexer_seasonnotfound as error:
                logger.log(
                    "%s: %s (unaired/deleted) in the indexer %s for %s. Removing existing records from database"
                    % (
                        curShowid,
                        error.message,
                        sickbeard.indexerApi(self.indexer).name,
                        curShowName,
                    ),
                    logger.DEBUG,
                )
                deleteEp = True

        if curSeason not in scannedEps:
            logger.log(
                "{id}: Not curSeason in scannedEps".format(id=curShowid), logger.DEBUG
            )
            scannedEps[curSeason] = {}

        logger.log(
            "{id}: Loading {show} {ep} from the DB".format(
                id=curShowid, show=curShowName, ep=episode_num(curSeason, curEpisode)
            ),
            logger.DEBUG,
        )

        try:
            curEp = self.getEpisode(curSeason, curEpisode)
            if not curEp:
                raise EpisodeNotFoundException

            # if we found out that the ep is no longer on TVDB then delete it from our database too
            if deleteEp:
                curEp.deleteEpisode()

            curEp.loadFromDB(curSeason, curEpisode)
            curEp.loadFromIndexer(tvapi=t, cachedSeason=cachedSeasons[curSeason])
            scannedEps[curSeason][curEpisode] = True
        except EpisodeDeletedException:
            logger.log(
                "{id}: Tried loading {show} {ep} from the DB that should have been deleted, skipping it".format(
                    id=curShowid,
                    show=curShowName,
                    ep=episode_num(curSeason, curEpisode),
                ),
                logger.DEBUG,
            )
            continue

    logger.log(
        "{id}: Finished loading all episodes for {show} from the DB".format(
            show=curShowName, id=curShowid
        ),
        logger.DEBUG,
    )

    return scannedEps