# Buggy code
def run(self):
    """Search for a show with a given name on all the indexers, in a specific language"""

    results = []
    lang_id = self.valid_languages[self.lang]

    if self.name and not self.indexerid:  # only name was given
        search_results = sickchill.indexer.search_indexers_for_series_name(
            self.name, self.lang
        )
        for indexer, indexer_results in search_results.items():
            for result in indexer_results:
                # Skip it if it's in our show list already, and we only want new shows
                in_show_list = sickchill.show.Show.Show.find(
                    settings.showList, int(result["id"])
                )
                if in_show_list and self.only_new:
                    continue

                results.append(
                    {
                        indexer_ids[indexer]: result["id"],
                        "name": result["seriesName"],
                        "first_aired": result["firstAired"],
                        "indexer": indexer,
                        "in_show_list": in_show_list,
                    }
                )

            return _responds(RESULT_SUCCESS, {"results": results, "langid": lang_id})

    elif self.indexerid:
        indexer, result = sickchill.indexer.search_indexers_for_series_id(
            indexerid=self.indexerid, language=self.lang
        )
        if not indexer:
            logger.warning("API :: Unable to find show with id " + str(self.indexerid))
            return _responds(RESULT_SUCCESS, {"results": [], "langid": lang_id})

        if not result.seriesName:
            logger.debug(
                "API :: Found show with indexerid: "
                + str(self.indexerid)
                + ", however it contained no show name"
            )
            return _responds(
                RESULT_FAILURE, msg="Show contains no name, invalid result"
            )

        results = [
            {
                indexer_ids[indexer]: result.id,
                "name": str(result.seriesName),
                "first_aired": result.firstAired,
                "indexer": indexer,
            }
        ]

        return _responds(RESULT_SUCCESS, {"results": results, "langid": lang_id})
    else:
        return _responds(RESULT_FAILURE, msg="Either a unique id or name is required!")


# Traceback (TypeError)
2020-09-22 22:35:33 DEBUG :: WEBSERVER :: API :: 192.168.175.25 - gave correct API KEY. ACCESS GRANTED
2020-09-22 22:35:33 DEBUG :: WEBSERVER :: API :: all args: '()'
2020-09-22 22:35:33 DEBUG :: WEBSERVER :: API :: all kwargs: '{'cmd': 'sb.searchtvdb', 'lang': 'en', 'only_new': '0', 'name': 'game of thrones'}'
2020-09-22 22:35:33 DEBUG :: WEBSERVER :: API :: sb.searchtvdb: cur_kwargs {'lang': 'en', 'only_new': '0', 'name': 'game of thrones'}
2020-09-22 22:35:33 DEBUG :: WEBSERVER :: API :: Traceback (most recent call last):
File "/opt/sickchill/sickchill/views/api/webapi.py", line 123, in _out_as_json
out = json.dumps(_dict, ensure_ascii=False, sort_keys=True)
File "/usr/lib/python3.6/json/__init__.py", line 238, in dumps
**kw).encode(obj)
File "/usr/lib/python3.6/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python3.6/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/usr/lib/python3.6/json/encoder.py", line 180, in default
o.__class__.__name__)
TypeError: Object of type 'TVShow' is not JSON serializable

# Fixed code
def run(self):
    """Search for a show with a given name on all the indexers, in a specific language"""

    results = []
    lang_id = self.valid_languages[self.lang]

    if self.name and not self.indexerid:  # only name was given
        search_results = sickchill.indexer.search_indexers_for_series_name(
            self.name, self.lang
        )
        for indexer, indexer_results in search_results.items():
            for result in indexer_results:
                # Skip it if it's in our show list already, and we only want new shows
                in_show_list = (
                    sickchill.show.Show.Show.find(settings.showList, int(result["id"]))
                    is not None
                )
                if in_show_list and self.only_new:
                    continue

                results.append(
                    {
                        indexer_ids[indexer]: result["id"],
                        "name": result["seriesName"],
                        "first_aired": result["firstAired"],
                        "indexer": indexer,
                        "in_show_list": in_show_list,
                    }
                )

            return _responds(RESULT_SUCCESS, {"results": results, "langid": lang_id})

    elif self.indexerid:
        indexer, result = sickchill.indexer.search_indexers_for_series_id(
            indexerid=self.indexerid, language=self.lang
        )
        if not indexer:
            logger.warning("API :: Unable to find show with id " + str(self.indexerid))
            return _responds(RESULT_SUCCESS, {"results": [], "langid": lang_id})

        if not result.seriesName:
            logger.debug(
                "API :: Found show with indexerid: "
                + str(self.indexerid)
                + ", however it contained no show name"
            )
            return _responds(
                RESULT_FAILURE, msg="Show contains no name, invalid result"
            )

        results = [
            {
                indexer_ids[indexer]: result.id,
                "name": str(result.seriesName),
                "first_aired": result.firstAired,
                "indexer": indexer,
            }
        ]

        return _responds(RESULT_SUCCESS, {"results": results, "langid": lang_id})
    else:
        return _responds(RESULT_FAILURE, msg="Either a unique id or name is required!")