Ejemplo n.º 1
0
    def send_metadata_request(self, permid, infohash, selversion = -1, caller = 'BC'):
        if DEBUG:
            print >> sys.stderr, 'metadata: Connect to send GET_METADATA to', show_permid_short(permid)
        if not isValidInfohash(infohash):
            return False
        filename, metadata = self.torrent_exists(infohash)
        if filename is not None:
            if DEBUG:
                print >> sys.stderr, 'metadata: send_meta_req: Already on disk??!'
            self.notify_torrent_is_in(infohash, metadata, filename)
            return True
        if caller == 'dlhelp':
            self.requested_torrents.add(infohash)
        if self.min_free_space != 0 and self.free_space - self.avg_torrent_size < self.min_free_space:
            self.free_space = self.get_free_space()
            if self.free_space - self.avg_torrent_size < self.min_free_space:
                self.warn_disk_full()
                return True
        try:
            if selversion == -1:
                self.overlay_bridge.connect(permid, lambda e, d, p, s: self.get_metadata_connect_callback(e, d, p, s, infohash))
            else:
                self.get_metadata_connect_callback(None, None, permid, selversion, infohash)
        except:
            print_exc()
            return False

        return True
Ejemplo n.º 2
0
    def send_metadata(self, permid, message, selversion):
        try:
            infohash = bdecode(message[1:])
        except:
            print_exc()
            if DEBUG:
                print >> sys.stderr, 'metadata: GET_METADATA: error becoding'
            return False

        if not isValidInfohash(infohash):
            if DEBUG:
                print >> sys.stderr, 'metadata: GET_METADATA: invalid hash'
            return False
        res = self.torrent_db.getOne(('torrent_file_name', 'status_id'),
                                     infohash=bin2str(infohash))
        if not res:
            if DEBUG:
                print >> sys.stderr, 'metadata: GET_METADATA: not in database', infohash
            return True
        torrent_file_name, status_id = res
        if status_id == self.torrent_db._getStatusID('dead'):
            if DEBUG:
                print >> sys.stderr, 'metadata: GET_METADATA: Torrent was dead'
            return True
        if not torrent_file_name:
            if DEBUG:
                print >> sys.stderr, 'metadata: GET_METADATA: no torrent file name'
            return True
        torrent_path = os.path.join(self.torrent_dir, torrent_file_name)
        if not os.path.isfile(torrent_path):
            if DEBUG:
                print >> sys.stderr, 'metadata: GET_METADATA: not existing', res, torrent_path
            return True
        task = {
            'permid': permid,
            'infohash': infohash,
            'torrent_path': torrent_path,
            'selversion': selversion
        }
        self.upload_queue.append(task)
        if int(time()) >= self.next_upload_time:
            self.checking_upload_queue()
        return True
Ejemplo n.º 3
0
    def send_metadata(self, permid, message, selversion):
        try:
            infohash = bdecode(message[1:])
        except:
            print_exc()
            if DEBUG:
                print >> sys.stderr, 'metadata: GET_METADATA: error becoding'
            return False

        if not isValidInfohash(infohash):
            if DEBUG:
                print >> sys.stderr, 'metadata: GET_METADATA: invalid hash'
            return False
        res = self.torrent_db.getOne(('torrent_file_name', 'status_id'), infohash=bin2str(infohash))
        if not res:
            if DEBUG:
                print >> sys.stderr, 'metadata: GET_METADATA: not in database', infohash
            return True
        torrent_file_name, status_id = res
        if status_id == self.torrent_db._getStatusID('dead'):
            if DEBUG:
                print >> sys.stderr, 'metadata: GET_METADATA: Torrent was dead'
            return True
        if not torrent_file_name:
            if DEBUG:
                print >> sys.stderr, 'metadata: GET_METADATA: no torrent file name'
            return True
        torrent_path = os.path.join(self.torrent_dir, torrent_file_name)
        if not os.path.isfile(torrent_path):
            if DEBUG:
                print >> sys.stderr, 'metadata: GET_METADATA: not existing', res, torrent_path
            return True
        task = {'permid': permid,
         'infohash': infohash,
         'torrent_path': torrent_path,
         'selversion': selversion}
        self.upload_queue.append(task)
        if int(time()) >= self.next_upload_time:
            self.checking_upload_queue()
        return True
Ejemplo n.º 4
0
    def send_metadata_request(self,
                              permid,
                              infohash,
                              selversion=-1,
                              caller='BC'):
        if DEBUG:
            print >> sys.stderr, 'metadata: Connect to send GET_METADATA to', show_permid_short(
                permid)
        if not isValidInfohash(infohash):
            return False
        filename, metadata = self.torrent_exists(infohash)
        if filename is not None:
            if DEBUG:
                print >> sys.stderr, 'metadata: send_meta_req: Already on disk??!'
            self.notify_torrent_is_in(infohash, metadata, filename)
            return True
        if caller == 'dlhelp':
            self.requested_torrents.add(infohash)
        if self.min_free_space != 0 and self.free_space - self.avg_torrent_size < self.min_free_space:
            self.free_space = self.get_free_space()
            if self.free_space - self.avg_torrent_size < self.min_free_space:
                self.warn_disk_full()
                return True
        try:
            if selversion == -1:
                self.overlay_bridge.connect(
                    permid,
                    lambda e, d, p, s: self.get_metadata_connect_callback(
                        e, d, p, s, infohash))
            else:
                self.get_metadata_connect_callback(None, None, permid,
                                                   selversion, infohash)
        except:
            print_exc()
            return False

        return True
Ejemplo n.º 5
0
    def got_metadata(self, permid, message, selversion):
        try:
            message = bdecode(message[1:])
        except:
            print_exc()
            return False

        if not isinstance(message, dict):
            return False
        try:
            infohash = message['torrent_hash']
            if not isValidInfohash(infohash):
                return False
            if infohash not in self.requested_torrents:
                return True
            if self.torrent_db.hasMetaData(infohash):
                return True
            goturl = False
            if selversion >= OLPROTO_VER_ELEVENTH:
                if 'metatype' in message and message[
                        'metatype'] == URL_MIME_TYPE:
                    try:
                        tdef = TorrentDef.load_from_url(message['metadata'])
                        metainfo = tdef.get_metainfo()
                        metadata = bencode(metainfo)
                        goturl = True
                    except:
                        print_exc()
                        return False

                else:
                    metadata = message['metadata']
            else:
                metadata = message['metadata']
            if not self.valid_metadata(infohash, metadata):
                return False
            if DEBUG:
                torrent_size = len(metadata)
                if goturl:
                    mdt = 'URL'
                else:
                    mdt = 'torrent'
                print >> sys.stderr, 'metadata: Recvd', mdt, ` infohash `, sha(
                    infohash).hexdigest(), torrent_size
            extra_info = {}
            if selversion >= OLPROTO_VER_FOURTH:
                try:
                    extra_info = {
                        'leecher': message.get('leecher', -1),
                        'seeder': message.get('seeder', -1),
                        'last_check_time': message.get('last_check_time', -1),
                        'status': message.get('status', 'unknown')
                    }
                except Exception as msg:
                    print_exc()
                    print >> sys.stderr, 'metadata: wrong extra info in msg - ', message
                    extra_info = {}

            filename = self.save_torrent(infohash,
                                         metadata,
                                         extra_info=extra_info)
            self.requested_torrents.remove(infohash)
            if filename is not None:
                self.notify_torrent_is_in(infohash, metadata, filename)
            if permid is not None and BARTERCAST_TORRENTS:
                self.overlay_bridge.add_task(
                    lambda: self.olthread_bartercast_torrentexchange(
                        permid, 'downloaded'), 0)
        except Exception as e:
            print_exc()
            print >> sys.stderr, 'metadata: Received metadata is broken', e, message.keys(
            )
            return False

        return True
Ejemplo n.º 6
0
    def got_metadata(self, permid, message, selversion):
        try:
            message = bdecode(message[1:])
        except:
            print_exc()
            return False

        if not isinstance(message, dict):
            return False
        try:
            infohash = message['torrent_hash']
            if not isValidInfohash(infohash):
                return False
            if infohash not in self.requested_torrents:
                return True
            if self.torrent_db.hasMetaData(infohash):
                return True
            goturl = False
            if selversion >= OLPROTO_VER_ELEVENTH:
                if 'metatype' in message and message['metatype'] == URL_MIME_TYPE:
                    try:
                        tdef = TorrentDef.load_from_url(message['metadata'])
                        metainfo = tdef.get_metainfo()
                        metadata = bencode(metainfo)
                        goturl = True
                    except:
                        print_exc()
                        return False

                else:
                    metadata = message['metadata']
            else:
                metadata = message['metadata']
            if not self.valid_metadata(infohash, metadata):
                return False
            if DEBUG:
                torrent_size = len(metadata)
                if goturl:
                    mdt = 'URL'
                else:
                    mdt = 'torrent'
                print >> sys.stderr, 'metadata: Recvd', mdt, `infohash`, sha(infohash).hexdigest(), torrent_size
            extra_info = {}
            if selversion >= OLPROTO_VER_FOURTH:
                try:
                    extra_info = {'leecher': message.get('leecher', -1),
                     'seeder': message.get('seeder', -1),
                     'last_check_time': message.get('last_check_time', -1),
                     'status': message.get('status', 'unknown')}
                except Exception as msg:
                    print_exc()
                    print >> sys.stderr, 'metadata: wrong extra info in msg - ', message
                    extra_info = {}

            filename = self.save_torrent(infohash, metadata, extra_info=extra_info)
            self.requested_torrents.remove(infohash)
            if filename is not None:
                self.notify_torrent_is_in(infohash, metadata, filename)
            if permid is not None and BARTERCAST_TORRENTS:
                self.overlay_bridge.add_task(lambda : self.olthread_bartercast_torrentexchange(permid, 'downloaded'), 0)
        except Exception as e:
            print_exc()
            print >> sys.stderr, 'metadata: Received metadata is broken', e, message.keys()
            return False

        return True