# Buggy code
def get_old_torrents(
    self, personal_channel_only=False, batch_size=BATCH_SIZE, offset=0, sign=False
):
    connection = sqlite3.connect(self.tribler_db)
    cursor = connection.cursor()

    personal_channel_filter = ""
    if self.personal_channel_id:
        personal_channel_filter = (
            " AND ct.channel_id "
            + (" == " if personal_channel_only else " != ")
            + (" %i " % self.personal_channel_id)
        )

    torrents = []
    for (
        tracker_url,
        channel_id,
        name,
        infohash,
        length,
        creation_date,
        torrent_id,
        category,
        num_seeders,
        num_leechers,
        last_tracker_check,
    ) in cursor.execute(
        self.select_full
        + personal_channel_filter
        + " group by infohash"
        + (" LIMIT " + str(batch_size) + " OFFSET " + str(offset))
    ):
        # check if name is valid unicode data
        try:
            name = text_type(name)
        except UnicodeDecodeError:
            continue

        try:
            if len(base64.decodestring(infohash)) != 20:
                continue
            if not torrent_id or int(torrent_id) == 0:
                continue

            infohash = base64.decodestring(infohash)

            torrent_dict = {
                "status": NEW,
                "infohash": infohash,
                "size": int(length or 0),
                "torrent_date": datetime.datetime.utcfromtimestamp(creation_date or 0),
                "title": name or "",
                "tags": category or "",
                "tracker_info": tracker_url or "",
                "xxx": int(category == "xxx"),
            }
            if not sign:
                torrent_dict.update(
                    {
                        "origin_id": infohash_to_id(channel_id),
                        "status": COMMITTED,
                        "public_key": NULL_KEY,
                    }
                )
            health_dict = {
                "seeders": int(num_seeders or 0),
                "leechers": int(num_leechers or 0),
                "last_check": int(last_tracker_check or 0),
            }
            torrents.append((torrent_dict, health_dict))
        except:
            continue

    connection.close()
    return torrents


# Traceback (RuntimeError)
Traceback (most recent call last):
File "TriblerGUI\event_request_manager.py", line 114, in on_read_data
RuntimeError: [Failure instance: Traceback: <class 'pony.orm.core.TransactionIntegrityError'>: Object TorrentMetadata[new:47] cannot be stored in the database. IntegrityError: UNIQUE constraint failed: ChannelNode.public_key, ChannelNode.id_
Tribler\Core\Modules\restapi\torrentinfo_endpoint.py:135:render_GET
Tribler\Core\Modules\restapi\torrentinfo_endpoint.py:109:on_file
lib\site-packages\twisted\internet\defer.py:459:callback
lib\site-packages\twisted\internet\defer.py:567:_startRunCallbacks
--- <exception caught here> ---
lib\site-packages\twisted\internet\defer.py:653:_runCallbacks
Tribler\Core\Modules\restapi\torrentinfo_endpoint.py:70:on_got_metainfo
<auto generated wrapper of add_ffa_from_tdef() function>:2:add_ffa_from_tdef
lib\site-packages\pony\orm\core.py:543:new_func
lib\site-packages\pony\orm\core.py:528:new_func
Tribler\Core\Modules\MetadataStore\OrmBindings\torrent_metadata.py:98:add_ffa_from_tdef
Tribler\Core\Modules\MetadataStore\OrmBindings\channel_node.py:213:from_dict
Tribler\Core\Modules\MetadataStore\OrmBindings\torrent_metadata.py:77:__init__
Tribler\Core\Modules\MetadataStore\OrmBindings\torrent_metadata.py:82:add_tracker
lib\site-packages\pony\orm\core.py:3950:get
lib\site-packages\pony\orm\core.py:4053:_find_one_
lib\site-packages\pony\orm\core.py:4112:_find_in_db_
lib\site-packages\pony\orm\core.py:934:_exec_sql
lib\site-packages\pony\orm\core.py:1797:prepare_connection_for_query_execution
lib\site-packages\pony\orm\core.py:1883:flush
lib\site-packages\pony\orm\core.py:5357:_save_
lib\site-packages\pony\orm\core.py:5201:_save_created_
lib\site-packages\pony\utils\utils.py:106:throw
]

# Fixed code
def get_old_torrents(
    self, personal_channel_only=False, batch_size=BATCH_SIZE, offset=0, sign=False
):
    connection = sqlite3.connect(self.tribler_db)
    cursor = connection.cursor()

    personal_channel_filter = ""
    if self.personal_channel_id:
        personal_channel_filter = (
            " AND ct.channel_id "
            + (" == " if personal_channel_only else " != ")
            + (" %i " % self.personal_channel_id)
        )

    torrents = []
    for (
        tracker_url,
        channel_id,
        name,
        infohash,
        length,
        creation_date,
        torrent_id,
        category,
        num_seeders,
        num_leechers,
        last_tracker_check,
    ) in cursor.execute(
        self.select_full
        + personal_channel_filter
        + " group by infohash"
        + (" LIMIT " + str(batch_size) + " OFFSET " + str(offset))
    ):
        # check if name is valid unicode data
        try:
            name = text_type(name)
        except UnicodeDecodeError:
            continue

        try:
            if len(base64.decodestring(infohash)) != 20:
                continue
            if not torrent_id or int(torrent_id) == 0:
                continue

            if not length:
                continue

            infohash = base64.decodestring(infohash)

            torrent_dict = {
                "status": NEW,
                "infohash": infohash,
                "size": int(length),
                "torrent_date": datetime.datetime.utcfromtimestamp(creation_date or 0),
                "title": name or "",
                "tags": category or "",
                "tracker_info": tracker_url or "",
                "xxx": int(category == "xxx"),
            }
            if not sign:
                torrent_dict.update({"origin_id": infohash_to_id(channel_id)})
            health_dict = {
                "seeders": int(num_seeders or 0),
                "leechers": int(num_leechers or 0),
                "last_check": int(last_tracker_check or 0),
            }
            torrents.append((torrent_dict, health_dict))
        except:
            continue

    connection.close()
    return torrents