Exemplo n.º 1
0
    def retrieve_from_magnet(url,
                             callback,
                             timeout=30.0,
                             max_connections=30.0):
        """
        If the URL conforms to a magnet link, the .torrent info is
        downloaded and converted into a TorrentDef.  The resulting
        TorrentDef is provided through CALLBACK.

        Returns True when attempting to obtain the TorrentDef, in this
        case CALLBACK will always be called.  Otherwise False is
        returned, in this case CALLBACK will not be called.

        The thread making the callback should be used very briefly.
        """
        assert isinstance(url, str), "URL has invalid type: %s" % type(url)
        assert callable(callback), "CALLBACK must be callable"

        def metainfo_retrieved(metadata):
            tdef = TorrentDef.load_from_dict(metadata)
            callback(tdef)

        try:
            magnet_link = MagnetLink(url, metainfo_retrieved, timeout,
                                     max_connections)
            return magnet_link.retrieve()
        except Exception, e:
            print >> sys.stderr, "Exception within magnet link"
            print >> sys.stderr, e
            return False
Exemplo n.º 2
0
    def retrieve_from_magnet(url, callback, timeout=30.0):
        """
        If the URL conforms to a magnet link, the .torrent info is
        downloaded and converted into a TorrentDef.  The resulting
        TorrentDef is provided through CALLBACK.

        Returns True when attempting to obtain the TorrentDef, in this
        case CALLBACK will always be called.  Otherwise False is
        returned, in this case CALLBACK will not be called.
        
        The thread making the callback should be used very briefly. 
        """
        assert isinstance(url, str), "URL has invalid type: %s" % type(url)
        assert callable(callback), "CALLBACK must be callable"
        def metainfo_retrieved(metadata):
            tdef = TorrentDef.load_from_dict(metadata)
            callback(tdef)

        try:
            magnet_link = MagnetLink(url, metainfo_retrieved, timeout)
            return magnet_link.retrieve()
        except Exception, e:
            print >> sys.stderr, "Exception within magnet link"
            print >> sys.stderr, e
            return False
Exemplo n.º 3
0
    def readTrackers(self, torrent):
        torrent = self.readTorrent(torrent)

        if not self.hasTrackers(torrent):
            #try using magnet torrentcollecting url
            sources = self.torrentdb.getTorrentCollecting(
                torrent['torrent_id'])
            for source, in sources:
                if source.startswith('magnet'):
                    dn, xt, trs = MagnetLink.parse_url(source)

                    if len(trs) > 0:
                        if 'info' not in torrent:
                            torrent["info"] = {}

                        torrent["info"]["announce"] = trs[0]
                        torrent["info"]["announce-list"] = [trs]
                    break

        if not self.hasTrackers(torrent):
            #see if we have a TorrentTracker entry
            trackers = self.torrentdb.getTracker(torrent['infohash'])

            if trackers and len(trackers) > 0:
                if 'info' not in torrent:
                    torrent["info"] = {}
                    torrent["info"]["announce"] = ''

                for tracker, tier in trackers:
                    if tier == 0:
                        torrent["info"]["announce"] = tracker
                    else:
                        #tier 1 is actually first in announce-list

                        tier = max(tier - 1, 0)
                        if "announce-list" not in torrent['info']:
                            torrent['info']["announce-list"] = []

                        while len(torrent["info"]["announce-list"]) <= tier:
                            torrent['info']["announce-list"].append([])

                        torrent['info']["announce-list"][tier].append(tracker)

        return torrent
Exemplo n.º 4
0
    def dbreadTrackers(self, torrent):
        announce = announce_list = None
        
        #try using magnet torrentcollecting url
        sources = self.torrentdb.getTorrentCollecting(torrent['torrent_id'])
        for source, in sources:
            if source.startswith('magnet'):
                dn, xt, trs = MagnetLink.parse_url(source)
                
                if len(trs) > 0:
                    if 'info' not in torrent:
                        torrent["info"] = {}
                    
                    announce = trs[0]
                    announce_list = [trs]
                break
        
        if not (announce or announce_list):
            #see if we have a TorrentTracker entry
            trackers = self.torrentdb.getTracker(torrent['infohash'])
        
            if trackers and len(trackers) > 0:
                announce_list = []
                
                for tracker, tier in trackers:
                    if tier == 0:
                        announce = tracker
                    else:
                        #tier 1 is actually first in announce-list
                        tier = max(tier-1, 0)
                        while len(announce_list) <= tier:
                            announce_list.append([])
                        
                        announce_list[tier].append(tracker)
            
        if announce and announce_list:   
            if 'info' not in torrent:
                torrent["info"] = {}

            torrent["info"]["announce"] = announce
            torrent['info']["announce-list"] = announce_list
            
        return torrent
 def readTrackers(self, torrent):
     torrent = self.readTorrent(torrent)
     
     if not self.hasTrackers(torrent):
         #try using magnet torrentcollecting url
         sources = self.torrentdb.getTorrentCollecting(torrent['torrent_id'])
         for source, in sources:
             if source.startswith('magnet'):
                 dn, xt, trs = MagnetLink.parse_url(source)
                 
                 if len(trs) > 0:
                     if 'info' not in torrent:
                         torrent["info"] = {}
                     
                     torrent["info"]["announce"] = trs[0]
                     torrent["info"]["announce-list"] = [trs]
                 break
             
     if not self.hasTrackers(torrent):
         #see if we have a TorrentTracker entry
         trackers = self.torrentdb.getTracker(torrent['infohash'])
         
         if trackers and len(trackers) > 0:
             if 'info' not in torrent:
                 torrent["info"] = {}
                 torrent["info"]["announce"] = ''
         
             for tracker, tier in trackers:
                 if tier == 0:
                     torrent["info"]["announce"] = tracker
                 else:
                     #tier 1 is actually first in announce-list
                     
                     tier = max(tier-1, 0)
                     if "announce-list" not in torrent['info']:
                         torrent['info']["announce-list"] = []
                     
                     while len(torrent["info"]["announce-list"]) <= tier:
                         torrent['info']["announce-list"].append([])
                     
                     torrent['info']["announce-list"][tier].append(tracker)
     
     return torrent