Пример #1
0
    def get_torrent_info(self, torrent_db):
        '''Returns a Torrent() object containing miscanealous info about the torrent
        State is either: 'Downloading', 'Completed' or 'Error'.'''

        import json
        from datetime import timedelta

        torrent_bt = Torrent()
        handle = self.get_handle_for_hash(torrent_db.hash)
        status = handle.status()

        # Status
        log.debug('Built torrent info for BT hash %s (status = %s, error = %s)', torrent_db.hash, status.state, status.error)
        if 'seeding' in str(status.state):
            torrent_bt.status = 'Completed'
        elif status.error:
            torrent_bt.status = 'Error'
        else:
            torrent_bt.status = 'Downloading'

        # Metadata
        torrent_bt.has_metadata = handle.has_metadata()
        if not torrent_bt.has_metadata:
            return torrent_bt

        info = handle.get_torrent_info()
        torrent_bt.name = info.name()
        torrent_bt.progress = status.progress
        torrent_bt.download_speed = "%.3f MB/s" % (status.download_rate/(1024*1024))
        torrent_bt.upload_speed = "%.3f MB/s" % (status.upload_rate/(1024*1024))
        torrent_bt.active_time = status.active_time
        torrent_bt.seeds = status.list_seeds
        torrent_bt.peers = status.list_peers

        # ETA
        size_left = info.total_size() - status.total_done
        if status.download_rate > 0:
            torrent_bt.eta = size_left / status.download_rate
        else:
            torrent_bt.eta = None

        # Files
        file_list = list()
        for res_file in info.files():
            file_list.append({'path': unicode(res_file.path, 'utf-8'), 'size': res_file.size})
        torrent_bt.file_list = json.dumps(file_list)

        return torrent_bt