示例#1
0
 def _get_http_info(self, url):
     url += '?info_hash=' + ''.join('%' + c.encode('hex') for c in self.torrent_hash)
     resp = urlopen(url)
     if resp.getcode() != 200:
         # TODO: issue warning here
         print resp.getcode()
         print resp.read()
         return {}
     data = lt.bdecode(resp.read())
     stats = data['files'][self.torrent_hash]
     info = {'seeds': stats['complete'], 'leeches': stats['incomplete'], 'complete': stats['downloaded']}
     return info
示例#2
0
    def __init__(self, torrent_file, request_timeout=5):
        # parse .torrent file
        if isinstance(torrent_file, file):
            data = lt.bdecode(torrent_file.read())
            torrent_file.close()
        else:
            data = lt.bdecode(torrent_file)
        info_obj = lt.torrent_info(data)
        self.trackers = data['announce-list'][0]  # use only IPv4 trackers

        # add torrent to session
        # noinspection PyArgumentList
        session = lt.session()
        self.torrent_handle = session.add_torrent({
            'save_path': '/tmp',
            'storage_mode': lt.storage_mode_t.storage_mode_sparse,
            'paused': True,
            'ti': info_obj
        })
        self.torrent_hash = self.torrent_handle.info_hash().to_bytes()

        # create UDP socket
        self._socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self._socket.settimeout(request_timeout)