Example #1
0
 def valid_metadata(self, infohash, metadata):
     try:
         metainfo = bdecode(metadata)
         tdef = TorrentDef.load_from_dict(metainfo)
         got_infohash = tdef.get_infohash()
         if infohash != got_infohash:
             print >> sys.stderr, "metadata: infohash doesn't match the torrent " + 'hash. Required: ' + ` infohash ` + ', but got: ' + ` got_infohash `
             return False
         return True
     except:
         print_exc()
         return False
Example #2
0
 def valid_metadata(self, infohash, metadata):
     try:
         metainfo = bdecode(metadata)
         tdef = TorrentDef.load_from_dict(metainfo)
         got_infohash = tdef.get_infohash()
         if infohash != got_infohash:
             print >> sys.stderr, "metadata: infohash doesn't match the torrent " + 'hash. Required: ' + `infohash` + ', but got: ' + `got_infohash`
             return False
         return True
     except:
         print_exc()
         return False
Example #3
0
    def read_and_send_metadata(self, permid, infohash, torrent_path,
                               selversion):
        torrent_data = self.read_torrent(torrent_path)
        if torrent_data:
            try:
                metainfo = bdecode(torrent_data)
                if 'info' in metainfo and 'private' in metainfo[
                        'info'] and metainfo['info']['private']:
                    if DEBUG:
                        print >> sys.stderr, 'metadata: Not sending torrent', ` torrent_path `, 'because it is private'
                    return 0
            except:
                print_exc()
                return 0

            if DEBUG:
                print >> sys.stderr, 'metadata: sending torrent', ` torrent_path `, len(
                    torrent_data)
            torrent = {}
            torrent['torrent_hash'] = infohash
            tdef = TorrentDef.load_from_dict(metainfo)
            if selversion >= OLPROTO_VER_ELEVENTH and tdef.get_url_compat():
                torrent['metatype'] = URL_MIME_TYPE
                torrent['metadata'] = tdef.get_url()
            else:
                torrent['metatype'] = TSTREAM_MIME_TYPE
                torrent['metadata'] = torrent_data
            if selversion >= OLPROTO_VER_FOURTH:
                data = self.torrent_db.getTorrent(infohash)
                if data is None:
                    return 0
                nleechers = data.get('leecher', -1)
                nseeders = data.get('seeder', -1)
                last_check_ago = int(time()) - data.get('last_check_time', 0)
                if last_check_ago < 0:
                    last_check_ago = 0
                status = data.get('status', 'unknown')
                torrent.update({
                    'leecher': nleechers,
                    'seeder': nseeders,
                    'last_check_time': last_check_ago,
                    'status': status
                })
            return self.do_send_metadata(permid, torrent, selversion)
        else:
            self.torrent_db.deleteTorrent(infohash,
                                          delete_file=True,
                                          commit=True)
            if DEBUG:
                print >> sys.stderr, 'metadata: GET_METADATA: no torrent data to send'
            return 0
Example #4
0
def isValidRemoteVal(d, selversion):
    if not isinstance(d, dict):
        if DEBUG:
            print >> sys.stderr, 'rqmh: reply: a: value not dict'
        return False
    if selversion >= OLPROTO_VER_TWELFTH:
        if not ('content_name' in d and 'length' in d and 'leecher' in d
                and 'seeder' in d and 'category' in d and 'torrent_size' in d
                and 'channel_permid' in d and 'channel_name' in d):
            if DEBUG:
                print >> sys.stderr, 'rqmh: reply: torrentrec12: key missing, got', d.keys(
                )
            return False
        if 'metatype' in d and 'metadata' in d:
            try:
                metatype = d['metatype']
                metadata = d['metadata']
                if metatype == URL_MIME_TYPE:
                    tdef = TorrentDef.load_from_url(metadata)
                else:
                    metainfo = bdecode(metadata)
                    tdef = TorrentDef.load_from_dict(metainfo)
            except:
                if DEBUG:
                    print >> sys.stderr, 'rqmh: reply: torrentrec12: metadata invalid'
                    print_exc()
                return False

    elif selversion >= OLPROTO_VER_ELEVENTH:
        if not ('content_name' in d and 'length' in d and 'leecher' in d
                and 'seeder' in d and 'category' in d and 'torrent_size' in d
                and 'channel_permid' in d and 'channel_name' in d):
            if DEBUG:
                print >> sys.stderr, 'rqmh: reply: torrentrec11: key missing, got', d.keys(
                )
            return False
    elif selversion >= OLPROTO_VER_NINETH:
        if not ('content_name' in d and 'length' in d and 'leecher' in d
                and 'seeder' in d and 'category' in d and 'torrent_size' in d):
            if DEBUG:
                print >> sys.stderr, 'rqmh: reply: torrentrec9: key missing, got', d.keys(
                )
            return False
    elif not ('content_name' in d and 'length' in d and 'leecher' in d
              and 'seeder' in d and 'category' in d):
        if DEBUG:
            print >> sys.stderr, 'rqmh: reply: torrentrec6: key missing, got', d.keys(
            )
        return False
    return True
Example #5
0
    def recv_query_reply(self, permid, message, selversion):
        if selversion < OLPROTO_VER_SIXTH:
            return False
        try:
            d = bdecode(message[1:])
        except:
            if DEBUG:
                print >> sys.stderr, 'rquery: Cannot bdecode QUERY_REPLY message', selversion
            return False

        if not isValidQueryReply(d, selversion):
            if DEBUG:
                print >> sys.stderr, 'rquery: not valid QUERY_REPLY message', selversion
            return False
        queryrec = self.is_registered_query_id(d['id'])
        if not queryrec:
            if DEBUG:
                print >> sys.stderr, 'rquery: QUERY_REPLY has unknown query ID', selversion
            return False
        if selversion >= OLPROTO_VER_TWELFTH:
            if queryrec['query'].startswith('SIMPLE+METADATA'):
                for infohash, torrentrec in d['a'].iteritems():
                    if 'metatype' not in torrentrec:
                        if DEBUG:
                            print >> sys.stderr, 'rquery: QUERY_REPLY has no metatype field', selversion
                        return False
                    if 'metadata' not in torrentrec:
                        if DEBUG:
                            print >> sys.stderr, 'rquery: QUERY_REPLY has no metadata field', selversion
                        return False
                    if torrentrec['torrent_size'] != len(
                            torrentrec['metadata']):
                        if DEBUG:
                            print >> sys.stderr, 'rquery: QUERY_REPLY torrent_size != len metadata', selversion
                        return False
                    try:
                        if torrentrec['metatype'] == URL_MIME_TYPE:
                            tdef = TorrentDef.load_from_url(
                                torrentrec['metadata'])
                        else:
                            metainfo = bdecode(torrentrec['metadata'])
                            tdef = TorrentDef.load_from_dict(metainfo)
                    except:
                        if DEBUG:
                            print_exc()
                        return False

        self.process_query_reply(permid, queryrec['query'],
                                 queryrec['usercallback'], d)
        return True
    def recv_query_reply(self, permid, message, selversion):
        if selversion < OLPROTO_VER_SIXTH:
            return False
        try:
            d = bdecode(message[1:])
        except:
            if DEBUG:
                print >> sys.stderr, 'rquery: Cannot bdecode QUERY_REPLY message', selversion
            return False

        if not isValidQueryReply(d, selversion):
            if DEBUG:
                print >> sys.stderr, 'rquery: not valid QUERY_REPLY message', selversion
            return False
        queryrec = self.is_registered_query_id(d['id'])
        if not queryrec:
            if DEBUG:
                print >> sys.stderr, 'rquery: QUERY_REPLY has unknown query ID', selversion
            return False
        if selversion >= OLPROTO_VER_TWELFTH:
            if queryrec['query'].startswith('SIMPLE+METADATA'):
                for infohash, torrentrec in d['a'].iteritems():
                    if 'metatype' not in torrentrec:
                        if DEBUG:
                            print >> sys.stderr, 'rquery: QUERY_REPLY has no metatype field', selversion
                        return False
                    if 'metadata' not in torrentrec:
                        if DEBUG:
                            print >> sys.stderr, 'rquery: QUERY_REPLY has no metadata field', selversion
                        return False
                    if torrentrec['torrent_size'] != len(torrentrec['metadata']):
                        if DEBUG:
                            print >> sys.stderr, 'rquery: QUERY_REPLY torrent_size != len metadata', selversion
                        return False
                    try:
                        if torrentrec['metatype'] == URL_MIME_TYPE:
                            tdef = TorrentDef.load_from_url(torrentrec['metadata'])
                        else:
                            metainfo = bdecode(torrentrec['metadata'])
                            tdef = TorrentDef.load_from_dict(metainfo)
                    except:
                        if DEBUG:
                            print_exc()
                        return False

        self.process_query_reply(permid, queryrec['query'], queryrec['usercallback'], d)
        return True
Example #7
0
    def read_and_send_metadata(self, permid, infohash, torrent_path, selversion):
        torrent_data = self.read_torrent(torrent_path)
        if torrent_data:
            try:
                metainfo = bdecode(torrent_data)
                if 'info' in metainfo and 'private' in metainfo['info'] and metainfo['info']['private']:
                    if DEBUG:
                        print >> sys.stderr, 'metadata: Not sending torrent', `torrent_path`, 'because it is private'
                    return 0
            except:
                print_exc()
                return 0

            if DEBUG:
                print >> sys.stderr, 'metadata: sending torrent', `torrent_path`, len(torrent_data)
            torrent = {}
            torrent['torrent_hash'] = infohash
            tdef = TorrentDef.load_from_dict(metainfo)
            if selversion >= OLPROTO_VER_ELEVENTH and tdef.get_url_compat():
                torrent['metatype'] = URL_MIME_TYPE
                torrent['metadata'] = tdef.get_url()
            else:
                torrent['metatype'] = TSTREAM_MIME_TYPE
                torrent['metadata'] = torrent_data
            if selversion >= OLPROTO_VER_FOURTH:
                data = self.torrent_db.getTorrent(infohash)
                if data is None:
                    return 0
                nleechers = data.get('leecher', -1)
                nseeders = data.get('seeder', -1)
                last_check_ago = int(time()) - data.get('last_check_time', 0)
                if last_check_ago < 0:
                    last_check_ago = 0
                status = data.get('status', 'unknown')
                torrent.update({'leecher': nleechers,
                 'seeder': nseeders,
                 'last_check_time': last_check_ago,
                 'status': status})
            return self.do_send_metadata(permid, torrent, selversion)
        else:
            self.torrent_db.deleteTorrent(infohash, delete_file=True, commit=True)
            if DEBUG:
                print >> sys.stderr, 'metadata: GET_METADATA: no torrent data to send'
            return 0
Example #8
0
    def resume_download(self, dltype, filename, initialdlstatus = None):
        try:
            pstate = self.load_download_pstate(filename)
            if DEBUG:
                log('lm::resume_download: dltype', dltype, 'filename', filename, 'dlconfig', pstate['dlconfig'])
            if DEBUG:
                log('lm::resume_download: status', dlstatus_strings[pstate['dlstate']['status']], 'progress', pstate['dlstate']['progress'])
                if pstate['engineresumedata'] is None:
                    log('lm::resume_download: resumedata None')
                else:
                    log('lm::resume_download: resumedata len', len(pstate['engineresumedata']))
            dscfg = DownloadStartupConfig(dlconfig=pstate['dlconfig'])
            if dltype == DLTYPE_TORRENT:
                tdef = TorrentDef.load_from_dict(pstate['metainfo'])
                d = self.add(tdef, dscfg, pstate, initialdlstatus)
            elif dltype == DLTYPE_DIRECT:
                main_url = pstate['url']
                d = self.add_direct_download(main_url, dscfg, pstate, initialdlstatus)
            if initialdlstatus == DLSTATUS_STOPPED:
                dest_files = d.get_dest_files(get_all=True)
                if DEBUG:
                    log('lm::resume_download: check dest files: dest_files', dest_files)
                got_existing_file = False
                for filename, savepath in dest_files:
                    if os.path.exists(savepath):
                        got_existing_file = True
                        break

                if not got_existing_file:
                    if DEBUG:
                        log('lm::resume_download: none of the files exists, remove this download')
                    self.remove(d, removecontent=True)
        except Exception as e:
            if DEBUG:
                log('lm::resume_download: failed to load checkpoint: filename', filename)
                print_exc()
            try:
                if os.access(filename, os.F_OK):
                    os.remove(filename)
            except:
                print_exc()
def isValidRemoteVal(d, selversion):
    if not isinstance(d, dict):
        if DEBUG:
            print >> sys.stderr, 'rqmh: reply: a: value not dict'
        return False
    if selversion >= OLPROTO_VER_TWELFTH:
        if not ('content_name' in d and 'length' in d and 'leecher' in d and 'seeder' in d and 'category' in d and 'torrent_size' in d and 'channel_permid' in d and 'channel_name' in d):
            if DEBUG:
                print >> sys.stderr, 'rqmh: reply: torrentrec12: key missing, got', d.keys()
            return False
        if 'metatype' in d and 'metadata' in d:
            try:
                metatype = d['metatype']
                metadata = d['metadata']
                if metatype == URL_MIME_TYPE:
                    tdef = TorrentDef.load_from_url(metadata)
                else:
                    metainfo = bdecode(metadata)
                    tdef = TorrentDef.load_from_dict(metainfo)
            except:
                if DEBUG:
                    print >> sys.stderr, 'rqmh: reply: torrentrec12: metadata invalid'
                    print_exc()
                return False

    elif selversion >= OLPROTO_VER_ELEVENTH:
        if not ('content_name' in d and 'length' in d and 'leecher' in d and 'seeder' in d and 'category' in d and 'torrent_size' in d and 'channel_permid' in d and 'channel_name' in d):
            if DEBUG:
                print >> sys.stderr, 'rqmh: reply: torrentrec11: key missing, got', d.keys()
            return False
    elif selversion >= OLPROTO_VER_NINETH:
        if not ('content_name' in d and 'length' in d and 'leecher' in d and 'seeder' in d and 'category' in d and 'torrent_size' in d):
            if DEBUG:
                print >> sys.stderr, 'rqmh: reply: torrentrec9: key missing, got', d.keys()
            return False
    elif not ('content_name' in d and 'length' in d and 'leecher' in d and 'seeder' in d and 'category' in d):
        if DEBUG:
            print >> sys.stderr, 'rqmh: reply: torrentrec6: key missing, got', d.keys()
        return False
    return True