# Buggy code
def parse(self, data, mode):
    """Parse search results for items.

    :param data: The raw response from a search
    :param mode: The current mode used to search, e.g. RSS

    :return: A list of items found
    """
    items = []

    unsorted_torrent_rows = data.get("torrents") if mode != "RSS" else data
    torrent_rows = sorted(unsorted_torrent_rows, key=itemgetter("added"), reverse=True)

    if not torrent_rows or not isinstance(torrent_rows, list):
        logger.log(
            "Data returned from provider does not contain any {0}torrents".format(
                "confirmed " if self.confirmed else ""
            ),
            logger.DEBUG,
        )
        return items

    for row in torrent_rows:
        if not isinstance(row, dict):
            logger.log("Invalid data returned from provider", logger.WARNING)
            continue

        if (
            mode == "RSS"
            and "category" in row
            and try_int(row["category"], 0) not in self.subcategories
        ):
            continue

        try:
            title = row["name"]
            torrent_id = row["id"]
            download_url = self.urls["download"] % torrent_id
            if not all([title, download_url]):
                continue

            seeders = try_int(row["seeders"])
            leechers = try_int(row["leechers"])
            verified = bool(row["isVerified"])

            # Filter unseeded torrent
            if seeders < min(self.minseed, 1):
                if mode != "RSS":
                    logger.log(
                        "Discarding torrent because it doesn't meet the "
                        "minimum seeders: {0}. Seeders: {1}".format(title, seeders),
                        logger.DEBUG,
                    )
                continue

            if self.confirmed and not verified and mode != "RSS":
                logger.log(
                    "Found result {0} but that doesn't seem like a verified"
                    " result so I'm ignoring it".format(title),
                    logger.DEBUG,
                )
                continue

            torrent_size = row["size"]
            size = convert_size(torrent_size) or -1

            item = {
                "title": title,
                "link": download_url,
                "size": size,
                "seeders": seeders,
                "leechers": leechers,
                "pubdate": None,
                "torrent_hash": None,
            }
            if mode != "RSS":
                logger.log(
                    "Found result: {0} with {1} seeders and {2} leechers".format(
                        title, seeders, leechers
                    ),
                    logger.DEBUG,
                )

            items.append(item)
        except (AttributeError, TypeError, KeyError, ValueError, IndexError):
            logger.log(
                "Failed parsing provider. Traceback: {0!r}".format(
                    traceback.format_exc()
                ),
                logger.ERROR,
            )

    return items


# Traceback (TypeError)
2016-10-31 12:46:27 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [NZBGeek] :: [ff8dcfa] Added RSS item: [Once.Upon.a.Time.S06E06.HDTV.x264-KILLERS] to cache: [nzbgeek]
2016-10-31 12:46:27 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [NZBGeek] :: [ff8dcfa] Mass updating cache table with manual results for provider: NZBGeek
2016-10-31 12:46:27 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [NZBGeek] :: [ff8dcfa] Transaction with 9 queries executed
2016-10-31 12:46:27 INFO     SEARCHQUEUE-MANUAL-248835 :: [HDTorrents] :: [ff8dcfa] Performing episode search for Once Upon a Time (2011)
2016-10-31 12:46:27 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [HDTorrents] :: [ff8dcfa] Search mode: Episode
2016-10-31 12:46:27 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [HDTorrents] :: [ff8dcfa] Search string: Once Upon a Time (2011) S06E06
2016-10-31 12:46:29 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [HDTorrents] :: [ff8dcfa] GET URL: https://hd-torrents.org/torrents.php?category%5B3%5D=38&search=Once+Upon+a+Time+%282011%29+S06E06&category%5B4%5D=65&category%5B2%5D=30&active=1&options=0&category%5B1%5D=60&category%5B0%5D=59 [Status: 200]
2016-10-31 12:46:29 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [HDTorrents] :: [ff8dcfa] Data returned from provider does not contain any torrents
2016-10-31 12:46:29 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [HDTorrents] :: [ff8dcfa] Search string: Once Upon a Time S06E06
2016-10-31 12:46:30 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [HDTorrents] :: [ff8dcfa] GET URL: https://hd-torrents.org/torrents.php?category%5B3%5D=38&search=Once+Upon+a+Time+S06E06&category%5B4%5D=65&category%5B2%5D=30&active=1&options=0&category%5B1%5D=60&category%5B0%5D=59 [Status: 200]
2016-10-31 12:46:31 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [HDTorrents] :: [ff8dcfa] Found result: Once Upon a Time S06E06 720p HDTV x264-AVS with 60 seeders and 0 leechers
2016-10-31 12:46:31 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [HDTorrents] :: [ff8dcfa] Using cached parse result for 'Once.Upon.a.Time.S06E06.720p.HDTV.x264-AVS'
2016-10-31 12:46:31 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [HDTorrents] :: [ff8dcfa] Found result Once.Upon.a.Time.S06E06.720p.HDTV.x264-AVS at https://hd-torrents.org/download.php?id=9c60e52da795903dc3753814c822ef34fa549e79&f=Once+Upon+a+Time+S06E06+720p+HDTV+x264-AVS.torrent
2016-10-31 12:46:31 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [HDTorrents] :: [ff8dcfa] Single episode result.
2016-10-31 12:46:31 INFO     SEARCHQUEUE-MANUAL-248835 :: [HDTorrents] :: [ff8dcfa] Removing cache entries older than 3 days from hdtorrents
2016-10-31 12:46:31 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [HDTorrents] :: [ff8dcfa] Creating UNIQUE URL index for hdtorrents
2016-10-31 12:46:31 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [HDTorrents] :: [ff8dcfa] Adding to cache item found in manual search: Once.Upon.a.Time.S06E06.720p.HDTV.x264-AVS
2016-10-31 12:46:31 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [HDTorrents] :: [ff8dcfa] Using cached parse result for 'Once.Upon.a.Time.S06E06.720p.HDTV.x264-AVS'
2016-10-31 12:46:31 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [HDTorrents] :: [ff8dcfa] Added RSS item: [Once.Upon.a.Time.S06E06.720p.HDTV.x264-AVS] to cache: [hdtorrents]
2016-10-31 12:46:31 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [HDTorrents] :: [ff8dcfa] Mass updating cache table with manual results for provider: HDTorrents
2016-10-31 12:46:31 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [HDTorrents] :: [ff8dcfa] Transaction with 1 queries executed
2016-10-31 12:46:31 INFO     SEARCHQUEUE-MANUAL-248835 :: [Speedcd] :: [ff8dcfa] Performing episode search for Once Upon a Time (2011)
2016-10-31 12:46:32 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [Speedcd] :: [ff8dcfa] POST URL: https://speed.cd/takeElogin.php [Status: 302]
2016-10-31 12:46:32 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [Speedcd] :: [ff8dcfa] With post data: username=**********&password=**********
2016-10-31 12:46:33 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [Speedcd] :: [ff8dcfa] GET URL: http://speed.cd [Status: 200]
2016-10-31 12:46:33 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [Speedcd] :: [ff8dcfa] Search mode: Episode
2016-10-31 12:46:33 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [Speedcd] :: [ff8dcfa] Search string: Once Upon a Time (2011) S06E06
2016-10-31 12:46:35 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [Speedcd] :: [ff8dcfa] GET URL: https://speed.cd/browse.php?c55=1&c52=1&search=Once+Upon+a+Time+%282011%29+S06E06&c41=1&c50=1&c2=1&c30=1&c49=1 [Status: 200]
2016-10-31 12:46:36 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [Speedcd] :: [ff8dcfa] Data returned from provider does not contain any torrents
2016-10-31 12:46:36 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [Speedcd] :: [ff8dcfa] Search string: Once Upon a Time S06E06
2016-10-31 12:46:37 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [Speedcd] :: [ff8dcfa] GET URL: https://speed.cd/browse.php?c55=1&c52=1&search=Once+Upon+a+Time+S06E06&c41=1&c50=1&c2=1&c30=1&c49=1 [Status: 200]
2016-10-31 12:46:38 INFO     SEARCHQUEUE-MANUAL-248835 :: [TorrentBytes] :: [ff8dcfa] Performing episode search for Once Upon a Time (2011)
2016-10-31 12:46:40 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [TorrentBytes] :: [ff8dcfa] POST URL: https://www.torrentbytes.net/takelogin.php [Status: 302]
2016-10-31 12:46:40 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [TorrentBytes] :: [ff8dcfa] With post data: username=**********&login=Log+in%21&password=**********
2016-10-31 12:46:40 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [TorrentBytes] :: [ff8dcfa] GET URL: https://www.torrentbytes.net/my.php [Status: 200]
2016-10-31 12:46:40 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [TorrentBytes] :: [ff8dcfa] Search mode: Episode
2016-10-31 12:46:40 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [TorrentBytes] :: [ff8dcfa] Search string: Once Upon a Time (2011) S06E06
2016-10-31 12:46:43 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [TorrentBytes] :: [ff8dcfa] GET URL: https://www.torrentbytes.net/browse.php?search=Once+Upon+a+Time+%282011%29+S06E06&c41=1&c38=1&c37=1&c33=1&c32=1 [Status: 200]
2016-10-31 12:46:43 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [TorrentBytes] :: [ff8dcfa] Data returned from provider does not contain any torrents
2016-10-31 12:46:43 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [TorrentBytes] :: [ff8dcfa] Search string: Once Upon a Time S06E06
2016-10-31 12:46:46 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [TorrentBytes] :: [ff8dcfa] GET URL: https://www.torrentbytes.net/browse.php?search=Once+Upon+a+Time+S06E06&c41=1&c38=1&c37=1&c33=1&c32=1 [Status: 200]
2016-10-31 12:46:46 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [TorrentBytes] :: [ff8dcfa] Found result: Once.Upon.a.Time.S06E06.HDTV.x264-KILLERS with 40 seeders and 1 leechers
2016-10-31 12:46:46 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [TorrentBytes] :: [ff8dcfa] Found result: Once.Upon.a.Time.S06E06.720p.HDTV.x264-AVS with 54 seeders and 2 leechers
2016-10-31 12:46:46 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [TorrentBytes] :: [ff8dcfa] Using cached parse result for 'Once.Upon.a.Time.S06E06.720p.HDTV.x264-AVS'
2016-10-31 12:46:46 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [TorrentBytes] :: [ff8dcfa] Found result Once.Upon.a.Time.S06E06.720p.HDTV.x264-AVS at https://www.torrentbytes.net/download.php?id=1301570&SSL=1
2016-10-31 12:46:46 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [TorrentBytes] :: [ff8dcfa] Single episode result.
2016-10-31 12:46:46 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [TorrentBytes] :: [ff8dcfa] Using cached parse result for 'Once.Upon.a.Time.S06E06.HDTV.x264-KILLERS'
2016-10-31 12:46:46 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [TorrentBytes] :: [ff8dcfa] Found result Once.Upon.a.Time.S06E06.HDTV.x264-KILLERS at https://www.torrentbytes.net/download.php?id=1301595&SSL=1
2016-10-31 12:46:46 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [TorrentBytes] :: [ff8dcfa] Single episode result.
2016-10-31 12:46:46 INFO     SEARCHQUEUE-MANUAL-248835 :: [TorrentBytes] :: [ff8dcfa] Removing cache entries older than 3 days from torrentbytes
2016-10-31 12:46:46 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [TorrentBytes] :: [ff8dcfa] Creating UNIQUE URL index for torrentbytes
2016-10-31 12:46:47 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [TorrentBytes] :: [ff8dcfa] Adding to cache item found in manual search: Once.Upon.a.Time.S06E06.720p.HDTV.x264-AVS
2016-10-31 12:46:47 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [TorrentBytes] :: [ff8dcfa] Using cached parse result for 'Once.Upon.a.Time.S06E06.720p.HDTV.x264-AVS'
2016-10-31 12:46:47 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [TorrentBytes] :: [ff8dcfa] Added RSS item: [Once.Upon.a.Time.S06E06.720p.HDTV.x264-AVS] to cache: [torrentbytes]
2016-10-31 12:46:47 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [TorrentBytes] :: [ff8dcfa] Adding to cache item found in manual search: Once.Upon.a.Time.S06E06.HDTV.x264-KILLERS
2016-10-31 12:46:47 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [TorrentBytes] :: [ff8dcfa] Using cached parse result for 'Once.Upon.a.Time.S06E06.HDTV.x264-KILLERS'
2016-10-31 12:46:47 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [TorrentBytes] :: [ff8dcfa] Added RSS item: [Once.Upon.a.Time.S06E06.HDTV.x264-KILLERS] to cache: [torrentbytes]
2016-10-31 12:46:47 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [TorrentBytes] :: [ff8dcfa] Mass updating cache table with manual results for provider: TorrentBytes
2016-10-31 12:46:47 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [TorrentBytes] :: [ff8dcfa] Transaction with 2 queries executed
2016-10-31 12:46:47 INFO     SEARCHQUEUE-MANUAL-248835 :: [T411] :: [ff8dcfa] Performing episode search for Once Upon a Time (2011)
2016-10-31 12:46:48 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [T411] :: [ff8dcfa] POST URL: https://api.t411.ch/auth [Status: 200]
2016-10-31 12:46:48 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [T411] :: [ff8dcfa] With post data: username=**********&password=**********
2016-10-31 12:46:48 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [T411] :: [ff8dcfa] Search mode: Episode
2016-10-31 12:46:48 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [T411] :: [ff8dcfa] Search string: Once Upon a Time (2011) S06E06
2016-10-31 12:46:48 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [T411] :: [ff8dcfa] GET URL: https://api.t411.ch/torrents/search/Once%20Upon%20a%20Time%20(2011)%20S06E06*?cid=433&limit=100 [Status: 200]
2016-10-31 12:46:48 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [T411] :: [ff8dcfa] Data returned from provider does not contain any torrents
2016-10-31 12:46:49 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [T411] :: [ff8dcfa] GET URL: https://api.t411.ch/torrents/search/Once%20Upon%20a%20Time%20(2011)%20S06E06*?cid=637&limit=100 [Status: 200]
2016-10-31 12:46:49 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [T411] :: [ff8dcfa] Data returned from provider does not contain any torrents
2016-10-31 12:46:49 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [T411] :: [ff8dcfa] GET URL: https://api.t411.ch/torrents/search/Once%20Upon%20a%20Time%20(2011)%20S06E06*?cid=455&limit=100 [Status: 200]
2016-10-31 12:46:49 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [T411] :: [ff8dcfa] Data returned from provider does not contain any torrents
2016-10-31 12:46:50 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [T411] :: [ff8dcfa] GET URL: https://api.t411.ch/torrents/search/Once%20Upon%20a%20Time%20(2011)%20S06E06*?cid=639&limit=100 [Status: 200]
2016-10-31 12:46:50 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [T411] :: [ff8dcfa] Data returned from provider does not contain any torrents
2016-10-31 12:46:50 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [T411] :: [ff8dcfa] Search string: Once Upon a Time S06E06
2016-10-31 12:46:51 DEBUG    SEARCHQUEUE-MANUAL-248835 :: [T411] :: [ff8dcfa] GET URL: https://api.t411.ch/torrents/search/Once%20Upon%20a%20Time%20S06E06*?cid=433&limit=100 [Status: 200]
2016-10-31 12:46:51 ERROR    SEARCHQUEUE-MANUAL-248835 :: [T411] :: [ff8dcfa] Unknown exception while searching T411. Error: "'int' object has no attribute '__getitem__'"
Traceback (most recent call last):
  File "/home/osmc/Medusa/medusa/search/core.py", line 621, in searchProviders
    downCurQuality, manual_search, manual_search_type)
  File "/home/osmc/Medusa/medusa/providers/generic_provider.py", line 182, in find_search_results
    items_list += self.search(search_string, ep_obj=episode)
  File "/home/osmc/Medusa/medusa/providers/torrent/json/t411.py", line 106, in search
    results += self.parse(jdata, mode)
  File "/home/osmc/Medusa/medusa/providers/torrent/json/t411.py", line 121, in parse
    torrent_rows = sorted(unsorted_torrent_rows, key=itemgetter('added'), reverse=True)
TypeError: 'int' object has no attribute '__getitem__'

# Fixed code
def parse(self, data, mode):
    """Parse search results for items.

    :param data: The raw response from a search
    :param mode: The current mode used to search, e.g. RSS

    :return: A list of items found
    """
    items = []

    unsorted_torrent_rows = data.get("torrents") if mode != "RSS" else data

    if not unsorted_torrent_rows or not isinstance(unsorted_torrent_rows, dict):
        logger.log(
            "Data returned from provider does not contain any {0}torrents".format(
                "confirmed " if self.confirmed else ""
            ),
            logger.DEBUG,
        )
        return items

    torrent_rows = sorted(unsorted_torrent_rows, key=itemgetter("added"), reverse=True)

    for row in torrent_rows:
        if not isinstance(row, dict):
            logger.log("Invalid data returned from provider", logger.WARNING)
            continue

        if (
            mode == "RSS"
            and "category" in row
            and try_int(row["category"], 0) not in self.subcategories
        ):
            continue

        try:
            title = row["name"]
            torrent_id = row["id"]
            download_url = self.urls["download"] % torrent_id
            if not all([title, download_url]):
                continue

            seeders = try_int(row["seeders"])
            leechers = try_int(row["leechers"])
            verified = bool(row["isVerified"])

            # Filter unseeded torrent
            if seeders < min(self.minseed, 1):
                if mode != "RSS":
                    logger.log(
                        "Discarding torrent because it doesn't meet the "
                        "minimum seeders: {0}. Seeders: {1}".format(title, seeders),
                        logger.DEBUG,
                    )
                continue

            if self.confirmed and not verified and mode != "RSS":
                logger.log(
                    "Found result {0} but that doesn't seem like a verified"
                    " result so I'm ignoring it".format(title),
                    logger.DEBUG,
                )
                continue

            torrent_size = row["size"]
            size = convert_size(torrent_size) or -1

            item = {
                "title": title,
                "link": download_url,
                "size": size,
                "seeders": seeders,
                "leechers": leechers,
                "pubdate": None,
                "torrent_hash": None,
            }
            if mode != "RSS":
                logger.log(
                    "Found result: {0} with {1} seeders and {2} leechers".format(
                        title, seeders, leechers
                    ),
                    logger.DEBUG,
                )

            items.append(item)
        except (AttributeError, TypeError, KeyError, ValueError, IndexError):
            logger.log(
                "Failed parsing provider. Traceback: {0!r}".format(
                    traceback.format_exc()
                ),
                logger.ERROR,
            )

    return items