Example #1
0
    def _add_torrent(self, torrent):
        if torrent in self.files:
            # silently return if already downloading
            return

        if torrent.url:
            # download torrent file
            handle, path = tempfile.mkstemp('.torrent')
            request = urllib2.Request(torrent.url)
            request.add_header('Accept-encoding', 'gzip')
            response = urllib2.urlopen(request)
            if response.info().get('Content-encoding') == 'gzip':
                buf = StringIO(response.read())
                f = gzip.GzipFile(fileobj=buf)
            os.write(handle, f.read())
            os.close(handle)
            ti = lt.torrent_info(path)
        else:
            # use magnet link
            ti = lt.torrent_info(torrent.magnet)
        
        handle = self.session.add_torrent({
            'save_path': self.folder, 'ti': ti})

        if torrent.url:
            # delete torrent file
            os.remove(path)

        self.downloads[handle] = torrent
        self.files[torrent] = handle
        self.upload_total[torrent] = 0
        self.download_total[torrent] = 0
Example #2
0
def upload_torrent(session_id, save_path,
                   file=None, magnet=None, url=None,
                   autostart='1', storage_mode='sparse', memory_only=None):

    if not get_session(session_id):
        raise APIException('session.notfound')

    if [file, magnet, url].count(None) != 2:
        raise APIException('upload.target.selectone')

    session = get_session_raw(session_id)

    if file:
        e = libtorrent.bdecode(file.read())
        info = libtorrent.torrent_info(e)
    elif magnet:
        info = libtorrent.torrent_info(magnet)
    else:
        raise APIException('upload.notsupported')

    save_path = os.path.normpath(os.path.join(ROOT_DIR, save_path))
    if not save_path.startswith(ROOT_DIR):
        raise APIException('access denied')

    params = {
        'save_path': save_path,
        'storage_mode': libtorrent.storage_mode_t.storage_mode_sparse,
        'ti': info
    }

    torrent_handle = session.add_torrent(params)
    return get_torrent_info(torrent_handle)
Example #3
0
    def _add_torrent(self, torrent, save_path, resume_data=None, paused=False, cookies=None):
        """
        Add a torrent to the pool.

        @param torrent: str - the path to a .torrent file or a magnet link
        @param save_path: str - torrent save path
        @param resume_data: str - bencoded torrent resume data
        @return: object - torr_handle
        """
        add_torrent_params = {'save_path': os.path.abspath(save_path),
                              'storage_mode': libtorrent.storage_mode_t.storage_mode_sparse,
                              'paused': paused}
        if resume_data is not None:
            add_torrent_params['resume_data'] = resume_data
        if isinstance(torrent, dict):
            add_torrent_params['ti'] = libtorrent.torrent_info(torrent)
        elif torrent[:7] == 'magnet:':
            add_torrent_params['url'] = str(torrent)  # libtorrent doesn't like unicode objects here
        elif torrent[:7] in ('http://', 'https:/'):
            # Here external http/https client is used in case if libtorrent module is compiled without OpenSSL
            add_torrent_params['ti'] = libtorrent.torrent_info(libtorrent.bdecode(self.load_torrent(torrent, cookies)))
        else:
            try:
                add_torrent_params['ti'] = libtorrent.torrent_info(os.path.abspath(torrent))
            except RuntimeError:
                raise TorrenterError('Invalid path to the .torrent file!')
        torr_handle = self._session.add_torrent(add_torrent_params)
        while not torr_handle.has_metadata():  # Wait until torrent metadata are populated
            time.sleep(0.1)
        torr_handle.auto_managed(False)
        self._torrents_pool[str(torr_handle.info_hash())] = torr_handle
        return torr_handle
 def download(self, url=None, user_data=None, resume_data=None):
     atp = {
         "save_path": self.download_dir.encode("utf-8"),
         "storage_mode": lt.storage_mode_t.storage_mode_sparse, #lt.storage_mode_t.storage_mode_allocate,
         "paused": False,
         "auto_managed": True,
         "duplicate_is_error": False,
         "override_resume_data": True, # for manual pause handling
         }
     if resume_data:
         atp["save_path"] = resume_data.get("download_dir", old_download_dir).encode("utf-8")
         if "resume_data" in resume_data:
             atp["resume_data"] = resume_data["resume_data"]
         if "url" in resume_data:
             atp["url"] = resume_data["url"]
         if "torrent" in resume_data:
             atp["ti"] = lt.torrent_info(lt.bdecode(resume_data["torrent"]))
         if "paused" in resume_data:
             atp["paused"] = resume_data["paused"]
             atp["auto_managed"] = not resume_data["paused"]
         if resume_data.get("finished", False) or resume_data.get("hidden", False):
             atp["upload_mode"] = True
             atp["auto_managed"] = False # prevents libtorrent from change upload_mode
             atp["storage_mode"] = lt.storage_mode_t.storage_mode_sparse # otherwise libtorrent generates null files
             resume_data["hidden"] = True
     elif self.can_download(url):
         if not url.startswith("magnet:?"):
             if not check_if_torrent_url(url):
                 return False
         for k, v in self._html_url_unescape.iteritems():
             url = url.replace(k, v)
         atp["url"] = str(url)
         resume_data = {"url": url.encode("utf-8"), "user_data": user_data}
     else:
         if url.startswith("file://"):
             urlp = urlparse.urlparse(url).path
             path = os.path.join(urlp.netloc, urlp.path)
         else:
             path = url
         path = os.path.abspath(path)
         if os.path.isfile(path):
             f = open(path, "rb")
             data = f.read()
             f.close()
             try:
                 atp["ti"] = lt.torrent_info(lt.bdecode(data))
             except BaseException:
                 return False
             resume_data = {"torrent": data, "user_data": user_data}
     if "url" in atp or "ti" in atp: # is atp valid?
         try:
             resume_data_id = str(atp["ti"].info_hash()) if "ti" in atp else atp["url"]
             self.tmp_resume_data[resume_data_id] = resume_data
             self.session.async_add_torrent(atp)
             return True
         except RuntimeError as e:
             # Torrent already in session
             logger.warn(e)
     return False
    def start_url(self, uri):
        """
        FunciĆ³n encargada iniciar la descarga del torrent desde la url, permite:
          - Url apuntando a un .torrent
          - Url magnet
          - Archivo .torrent local
        """

        if self._th:
            raise Exception('Torrent is already started')

        if uri.startswith('http://') or uri.startswith('https://'):
            torrent_data = self.download_torrent(uri)
            info = lt.torrent_info(lt.bdecode(torrent_data))
            tp = {'ti':info}
            resume_data= self._cache.get_resume(info_hash=str(info.info_hash()))
            if resume_data:
                tp['resume_data']=resume_data

        elif uri.startswith('magnet:'):
            tp={'url':uri}
            resume_data=self._cache.get_resume(info_hash=Cache.hash_from_magnet(uri))
            if resume_data:
                tp['resume_data']=resume_data

        elif os.path.isfile(uri):
            if os.access(uri,os.R_OK):
                info = lt.torrent_info(uri)
                tp= {'ti':info}
                resume_data= self._cache.get_resume(info_hash=str(info.info_hash()))
                if resume_data:
                    tp['resume_data']=resume_data
            else:
                raise ValueError('Invalid torrent path %s' % uri)
        else:
            raise ValueError("Invalid torrent %s" %uri)
        
        tp.update(self.torrent_paramss)
        self._th = self._ses.add_torrent(tp)
        

        for tr in self.INITIAL_TRACKERS:
            self._th.add_tracker({'url':tr})

        self._th.set_sequential_download(True)
        self._th.force_reannounce()
        self._th.force_dht_announce()

        self._monitor.start()
        self._dispatcher.do_start(self._th, self._ses)
        self.server.run()
Example #6
0
def get_file_names(torrent_params):
    if "save_path" not in torrent_params:
        raise InvalidTorrentParamsException(
            "Invalid torrent params passed to torrent.get_file_names(params")

    if "ti" in torrent_params and torrent_params["ti"] is not None:
        info = lt.torrent_info(torrent_params["ti"])
    elif "url" in torrent_params and torrent_params["url"] is not "":
        info = lt.torrent_info(torrent_params["url"])
    elif "info_hash" in torrent_params and torrent_params["info_hash"] is not None:
        info = lt.torrent_info(torrent_params["info_hash"])
    else:
        raise InvalidTorrentParamsException(
            "Invalid torrent params passed to torrent.get_file_names(params")
    return info.files()
Example #7
0
    def start(self, torrent_file, port=6881):
        if not os.path.isdir('log'):
            os.mkdir('log')

        self.ses = lt.session()
        self.ses.set_alert_mask(lt.alert.category_t.status_notification | 0x400) # lt.alert.category_t.dht_notification

        self.ses.listen_on(port, port)
        self.ses.start_dht()
        self.ses.add_dht_router("router.bittorrent.com", 6881)
        self.ses.add_dht_router("router.utorrent.com", 6881)
        self.ses.add_dht_router("router.bitcomet.com", 6881)

        e = lt.bdecode(open(torrent_file, 'rb').read())
        info = lt.torrent_info(e)

        # info = lt.torrent_info(torrent_file)
        h = self.ses.add_torrent({'ti': info, 'save_path': './'})

        while not h.is_seed():
            alert = self.ses.pop_alert()
            while alert is not None:
                self.handle_alert(alert)
                alert = self.ses.pop_alert()
            time.sleep(0.1)

        self.ses.remove_torrent(h)

        while True:
            alert = self.ses.pop_alert()
            while alert is not None:
                self.handle_alert(alert)
                alert = self.ses.pop_alert()
            time.sleep(0.1)
Example #8
0
 def setUp(self):
     # need to keep reference to session() to prevent GC picking it up
     self.session = session = libtorrent.session()
     session.listen_on(6881, 6882)
     torrent = libtorrent.bdecode(open(self.torrent, 'rb').read())
     info = libtorrent.torrent_info(torrent)
     self.transfer = session.add_torrent(info, tempfile.gettempdir())
Example #9
0
File: test.py Project: krattai/AEBL
    def test_cache_info(self):
        ses = lt.session({"alert_mask": lt.alert.category_t.all_categories, "enable_dht": False})
        ti = lt.torrent_info("url_seed_multi.torrent")
        h = ses.add_torrent({"ti": ti, "save_path": os.getcwd()})

        cs = ses.get_cache_info(h)
        self.assertEqual(cs.pieces, [])
Example #10
0
 def start_real(self):
     if self.is_finished() and self.seed_requirement_met():
         # Restarting a finished torrent, don't autostop immediately
         self.autostop = False
     self.download.status = Status.STARTING
     self.download.status_message = None
     if not 'state_changed_alert' in self.dfm:
         self.dfm['state_changed_alert'] = defer.Deferred()
     if not self.torrent_info:
         self.torrent_info = lt.torrent_info(lt.bdecode(self.download.metadata))
         self.download.description = unicode(self.torrent_info.name())
     try:
         if not self.torrent:
             resdata = None
             if self.download.resume_data:
                 resdata = marshal.loads(self.download.resume_data)
             self.torrent = lt_manager.add_torrent(self, self.torrent_info, self.directory, resume_data=resdata)
             self.torrent.auto_managed(True)
         self.rebind()
     except Exception as e:
         dfr = self.dfm['state_changed_alert']
         del self.dfm['state_changed_alert']
         if self.torrent:
             self.torrent.auto_managed(False)
             self.torrent.pause()
             self.torrent.save_resume_data()
         dfr.errback(failure.Failure(e))
         return dfr
     self.torrent.resume()
     return self.dfm['state_changed_alert']
Example #11
0
File: test.py Project: krattai/AEBL
 def test_scrape(self):
     ses = lt.session({"alert_mask": lt.alert.category_t.all_categories, "enable_dht": False})
     ti = lt.torrent_info("url_seed_multi.torrent")
     h = ses.add_torrent({"ti": ti, "save_path": os.getcwd()})
     # this is just to make sure this function can be called like this
     # from python
     h.scrape_tracker()
Example #12
0
	def test_alert(self):

		ses = lt.session({'alert_mask': lt.alert.category_t.all_categories})
		ti = lt.torrent_info('base.torrent');
		h = ses.add_torrent({'ti': ti, 'save_path': os.getcwd()})
		st = h.status()
		time.sleep(1)
		ses.remove_torrent(h)
		ses.wait_for_alert(1000) # milliseconds
		alerts = ses.pop_alerts()
		for a in alerts:
			print(a.message())

		print(st.next_announce)
		self.assertEqual(st.name, 'temp')
		print(st.errc.message())
		print(st.pieces)
		print(st.last_seen_complete)
		print(st.completed_time)
		print(st.progress)
		print(st.num_pieces)
		print(st.distributed_copies)
		print(st.paused)
		print(st.info_hash)
		self.assertEqual(st.save_path, os.getcwd())
Example #13
0
def ActionDNLDTorrent(logger, args):

    ses = lt.session()
    ses.listen_on(args["portStart"],
                  args["portEnd"])

    info = lt.torrent_info(args["torrentFile"])
    h = ses.add_torrent({'ti':           info,
                         'save_path':    args["destPath"],
                         'storage_mode': (lt.storage_mode_t.storage_mode_allocate
                                          if args["allocateStorage"]
                                          else lt.storage_mode_t.storage_mode_sparse)
                         })

    logger.info("Starting [%s]" % h.name())
    while (not h.is_seed()):
       s = h.status()

       state_str = ['queued', 'checking', 'downloading metadata', \
          'downloading', 'finished', 'seeding', 'allocating', 'checking fastresume']
       print '\r%.2f%% complete (down: %.1f kb/s up: %.1f kB/s peers: %d) %s' % \
          (s.progress * 100, s.download_rate / 1000, s.upload_rate / 1000, \
          s.num_peers, state_str[s.state]),
       sys.stdout.flush()

       time.sleep(1)

    logger.info("Completed [%s]" % h.name())
Example #14
0
	def make_torrent(self, tracker_url, torrent_name, dir_name):
		mkdir_p('torrent_files')
		
		fs = lt.file_storage()
		lt.add_files(fs, dir_name)
		t = lt.create_torrent(fs)
		t.add_tracker(tracker_url)
		lt.set_piece_hashes(t, './torrent_data')
		
		f = open(torrent_name, "wb")
		f.write(lt.bencode(t.generate()))
		f.close()

		e = lt.bdecode(open(torrent_name, 'rb').read())
		info = lt.torrent_info(e)
		
		params = { 
			'save_path': './torrent_data',
		    'ti': info,
			'seed_mode': True
		}
		
		h = self.ses.add_torrent(params)
		
		# Wait a bit for the tracker
		sleep(5)
Example #15
0
    def add_torrent(self, session, params):
        info_hash = params['info_hash']
        # convert params into torrent_info
        e = lt.bdecode(params['torrent_file'])
        torrent_info = lt.torrent_info(e)

        # sanity check: info_hash values must match
        if str(torrent_info.info_hash()) != info_hash:
            raise Exception('Error adding torrent (%s != %s)' % (torrent_info.info_hash(), info_hash))

        # add torrent to session
        add_torrent_params = {
            'ti': torrent_info,
            'save_path': str(params['torrent_root']) # cast to regular string; the libtorrent c++ bindings don't like unicode
            }

        torrent = session._ses.add_torrent(add_torrent_params)
        if torrent:
            if self._torrents.has_key(info_hash):
                session._log.error('Cannot add duplicate torrent to session (%s)' % (info_hash))
            else:
                self._torrents[info_hash] = torrent
                session._log.info('Added torrent to session for upload: %s (%s)' % (torrent.name(), info_hash))
        else:
            session._log.error('Error adding torrent to libtorrent session')
Example #16
0
    def torrentInfo(self, donotload=False):
        if not self.session:
            raise Exception("Need session first")
        # URL:
        # http://piratebaytorrents.info/5579280/Big_Buck_Bunny_(1080p).ogv.5579280.TPB.torrent
        if not donotload and len(self.location)>4 and self.location[0:4] == 'http':
            # Create tmp torrent file in the tmp dir
            dest = self.tmpdir + '/' + hashlib.md5(self.location).hexdigest() + '.torrent'
            urllib.urlretrieve(self.location, dest)
            # Set the location to this file
            self.location = dest
        # Magnet: 
        # magnet:?xt=urn:btih:e541adf64e5d10c0827579447948aefba651e4f4&dn=Big+Buck+Bunny+%281080p%29.ogv&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80&tr=udp%3A%2F%2Ftracker.istole.it%3A6969&tr=udp%3A%2F%2Ftracker.ccc.de%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337
        if len(self.location)>6 and self.location[0:6] == 'magnet':
            if donotload and not self.torrent_handle:
                print >> sys.stderr, "NOT Loading"
                return None

            if not self.torrent_handle or not hasattr(self,"magnet"):
                self.magnet = lt.add_magnet_uri(self.session, self.location, self.torrent_params)
                print >> sys.stderr, 'Downloading metadata...'
                while (not self.magnet.has_metadata()): time.sleep(1)
                self.torrent_handle = self.magnet
            # Return info
            return self.magnet.get_torrent_info()
        elif self.location:
            try:
                return lt.torrent_info(self.location)
            except:
                pass
        return None
Example #17
0
def scandir(path, level):
    torrent = 0 
    if os.path.exists(path):
        for (path, dirs, files) in walklevel(path,level):
            for item in files:
                # if archive (dump) file
                if re.search(r'.xml.bz2$', item):
                    # add this file into our index
                    # Building a list of all files which we later use to
                    # build the index: itemname, filename, httplink,
                    #   torrentlink
                    regex = re.compile(r"([a-z]+)-\d+(-pages-meta-current).*"); 
                    entries.append([regex.sub(r"\1\2", item), item, os.stat(path+'/'+item).st_size, webpath+item, webpath+item+'.torrent'])
                    # create torrent file for package if it doesn't already exist
                    if not os.path.isfile(path+"/"+item+".torrent"):
                        with open(os.devnull, 'wb') as devnull:
                            debugmsg("Create torrent file for "+path+"/"+item)
                            subprocess.check_call(mktorrent_bin+' -a '+announce+' -o '+path+'/'+item+'.torrent '+path+'/'+item, shell=True, stdout=devnull, stderr=subprocess.STDOUT)
                        # get torrent info hash and append it to opentracker whitelist
                        info = lt.torrent_info(path+"/"+item+".torrent")
                        info_hash = info.info_hash()
                        hexadecimal = str(info_hash)
                        os.system("echo "+hexadecimal+" >> "+torrenthashlist_file)
                # if torrent file
                if re.search(r'.xml.bz2.torrent$', item):
                    # remove torrent file if the inherent package doesn't exist anymore
                    if not os.path.isfile(path+"/"+item):
                        os.system("rm "+path+"/"+item+".torrent")
    else:
        print("File or directory does not exists")
    return 0
Example #18
0
    def __add_torrent(self, filename):
        self.l.log("TorrentAgent:adding torrent " + filename)

        if self.__check_dups(filename):
            self.l.log("TorrentAgent:skipping, already added " + filename)
            return 

        torrent_data = None
        try:
            f = open(filename, 'rb')
            torrent_data = LT.bdecode(f.read())
            f.close()
        except:
            self.l.log("TorrentAgent:error reading from " + filename, L.ERR)
            return

        try:
            info = LT.torrent_info(torrent_data)
        except:
            self.l.log("TorrentAgent:error adding torrent, corrupt file? " + filename, L.ERR)
            return

        resume_data = self.__get_resume_data(str(info.info_hash()))
        
        h = self.session.add_torrent(info, self.c["save_path"],
                                     resume_data=resume_data,
                                     storage_mode=self.c["storage_mode"])

        self.__setup_handle(h)
        self.handle_by_name[filename] = (h, False)
Example #19
0
 def __init__(self, torr_save_path, file_save_path, tracker, backup_file = None, torr_file = None):
     """
     You must define a path where the torrent is saved, where the data files are saved, a tracker,
     and an optional torrent file.  The torrent file is given if we are getting files from the
     tracker.  If we are putting files, this should be None.
     """
     
     log.debug("Initializing TorrentMetaInfo: %s %s %s %s %s" %(torr_save_path, file_save_path, tracker, backup_file, torr_file))
     
     # values defined by this class
     self.torr_path = self.valid_dir(torr_save_path)
     self.file_path = self.valid_dir(file_save_path)
     self.torr_name = self.valid_file(str(self.torr_path) + "/" + str(torr_file))
     self.file_name = self.valid_file(str(self.file_path) + "/" + str(backup_file))
     
     # Values that are filled in later, or that are optional.
     self.piece_size = 1048576                       # Default piece size.  Set this higher.
     self.priv = False                               # Private torrent?
     self.peer_id = ""
     self.comments = ""
     self.creator = ""
     
     # set the info_hash if we're given a torrent to start with
     if self.torr_name:
         self.info = lt.torrent_info(self.torr_name)
         self.info_hash = self.info.info_hash() 
     
     # set tracker
     self.tracker = tracker 
def downloadTorrent (torrent ):
    torrent="test.torrent"


    ses = lt.session()
    ses.listen_on(TORRENT_PORT1, TORRENT_PORT2)

    e = lt.bdecode(open(torrent, 'rb').read())
    info = lt.torrent_info(e)

    params = { "save_path": '.', \
               "storage_mode": lt.storage_mode_t.storage_mode_sparse, \
               "ti": info }
    h = ses.add_torrent(params)

    s = h.status()
    while (not s.is_seeding):
        s = h.status()

        state_str = ['queued', 'checking', 'downloading metadata', \
                     'downloading', 'finished', 'seeding', 'allocating']
        print( '%.2f%% complete (down: %.1f kb/s up: %.1f kB/s peers: %d) %s' % \
              (s.progress * 100, s.download_rate / 1000, s.upload_rate / 1000, \
               s.num_peers, state_str[s.state]))

        time.sleep(1)
Example #21
0
 def _download_from_torrent(self, torrent_id, binary_file, destination_path):
     e = lt.bdecode(binary_file)
     params = {'save_path': destination_path,
               'storage_mode': lt.storage_mode_t.storage_mode_sparse,
               'ti': lt.torrent_info(e)}
     torrent_handler = self._session.add_torrent(params)
     self._torrent_handlers[torrent_id] = torrent_handler
Example #22
0
    def _init(self, data, magnet):
        self.magnet = magnet

        if data is not None:
            self._info = lt.torrent_info(lt.bdecode(data))
            self.options['ti'] = self._info
            self.id = str(self._info.info_hash())
            self._id_event.set()

            resume_data = self.load_resume_data()
            if resume_data:
                self.options["resume_data"] = resume_data

            self.handle = session.add_torrent(self.options)
        else:
            def _on_metadata_received_alert(alert):
                self._info = self.handle.get_torrent_info()
                self.options['ti'] = self.info
                self.unregister_alert('metadata_received_alert', _on_metadata_received_alert)

            self.register_alert('metadata_received_alert', _on_metadata_received_alert)

            self._info = None
            self.handle = session.add_magnet(magnet, self.options)
            self.id = str(self.handle.info_hash())
            self._id_event.set()

        if self.id == '0000000000000000000000000000000000000000':
            self.remove()
            raise ValueError('error getting torrent id. torrent broken?')

        session.torrents[self.id] = self
def construct_handler(ses, torrent, destination):
    """ helper for handling magnet link, http links and torrent files"""
    tinfo = None
    params = {
        'save_path': destination,
        'storage_mode': lt.storage_mode_t(2),
        'auto_managed': True,
    }
    # if case if ULR was provided download it to file
    # TODO: add nice REGXP URL validator, the 'http' can be a folder name
    if torrent.startswith('http'):
        torrent_body = requests.get(torrent)
        torrent = os.path.join(location_context, 'torrent_file')
        tmp_torrent = torrent+'.tmp'
        with open(tmp_torrent, 'wb') as dist:
            dist.write(torrent_body.text.encode('utf-8'))
        os.rename(tmp_torrent, torrent)

    # check if file contain magnet link
    # XXX: shitty magnet support from file
    torrent_data = open(torrent).read().strip(" \n\t")
    if torrent_data.startswith("magnet:"):
        torrent_handler = lt.add_magnet_uri(ses, torrent_data, params)
    else:
        tinfo = lt.torrent_info(torrent)
        params['ti'] = tinfo
        torrent_handler = ses.add_torrent(params)
    # waiting for metadata to be downloaded
    while (not torrent_handler.has_metadata()):
        time.sleep(0.1)
    return torrent_handler
Example #24
0
def show_torrent_file(torrent_file):
    print torrent_file
    e =  lt.bdecode(open(torrent_file, 'rb').read())
    torrent_file = lt.torrent_info(e)
    name = torrent_file.name()
    files = torrent_file.files()
    show_content = []
    if os.name == 'nt':
        try:
            name = name.decode('utf-8').encode('gbk')
        except Exception as err:
            name = 'unknown'
        for file_item in files:
            try:
                file_item.path = \
                        file_item.path.decode('utf-8').encode('gbk')
            except Exception as err:
                pass

    show_content.append('  idx: name %s\n' % name)
    for file_item in files:
        if (file_item.size / (1024*1024.0)) > 50:
            show_content.append('       files(%.3f MB): %s\n' % 
                    (file_item.size/(1024*1024.0), file_item.path))
    show_content.append('-' * 70)
    show_content.append('\n')
    print '\n'.join(show_content)
Example #25
0
 def _create_torrent(self, resource, fs, root='.', use_sudo=False):
     t = lt.create_torrent(fs)
     transports = resource.transports()
     torrent_transport = next(
         (x for x in transports if x['name'] == 'torrent'))
     trackers = torrent_transport['trackers']
     for tracker in trackers:
         t.add_tracker(tracker)
     lt.set_piece_hashes(t, os.path.join(root, '..'))
     torrent = t.generate()
     torrent['priv'] = True  # private torrent, no DHT, only trackers
     name = self._create_torrent_name()
     try:
         # not checking for path existence
         with open(name, 'wb') as f:
             f.write(lt.bencode(torrent))
     except IOError as e:
         if e.errno != errno.ENOENT:
             raise
         os.makedirs(self._torrent_path)
         with open(name, 'wb') as f:
             f.write(lt.bencode(torrent))
     log.debug("Created torrent file %s", name)
     magnet_uri = lt.make_magnet_uri(lt.torrent_info(name))
     # self._torrents[root] = (name, magnet_uri)
     if not use_sudo:
         self._torrents.append((name, magnet_uri, root))
     else:
         self._sudo_torrents.append((name, magnet_uri, root))
     return name
	def getTorrentContents (self, torrentFile):
		ses = lt.session()
		#ses.listen_on(6881, 6891)

		info = lt.torrent_info(torrentFile)
		files = []
		shouldDownload = False
		for file in info.files():
			if(self.extensionMatch(file.path)):
				shouldDownload=True
				files.append(file.path)

		if(shouldDownload):
			h = ses.add_torrent({'ti': info, 'save_path': './'})
			h.set_upload_limit(self.maxUploadSpeed*1024)
			h.set_download_limit(self.maxDownloadSpeed*1024)

			while (not h.is_seed()):
				s = h.status()
				state_str = ['queued', 'checking', 'downloading metadata', \
					'downloading', 'finished', 'seeding', 'allocating', 'checking fastresume']
				#print '\r          %.2f%% complete (down: %.1f kb/s up: %.1f kB/s peers: %d) %s' % \
				#	(s.progress * 100, s.download_rate / 1000, s.upload_rate / 1000, \
				#	s.num_peers, state_str[s.state]),
				sys.stdout.flush()
				time.sleep(1)
		else:
			del info
			del ses
		return files
def download(clientType):
    if clientType == "server":
        dest = ""
    else:
        dest = expanduser("~") + "\Downloads"

    ses = lt.session()
    ses.listen_on(6881, 6901) #keep trying to bind to a port till set value

    torrentContent = lt.bdecode(open("t.torrent", 'rb').read())
    info = lt.torrent_info(torrentContent)
    if clientType == "server":
        h = ses.add_torrent({'ti': info, 'save_path': dest, 'seed_mode': True})
    else:
        h = ses.add_torrent({'ti': info, 'save_path': dest})

    while (not h.is_seed()):
       s = h.status()
       state_str = ['queued', 'checking', 'downloading metadata', \
                     'downloading', 'finished', 'seeding', 'allocating', 'checking fastresume']
       #print '\r%.2f%% complete (down: %.1f kb/s up: %.1f kB/s peers: %d) %s' % \
       #   (s.progress * 100, s.download_rate / 1000, s.upload_rate / 1000, \
       #   s.num_peers, state_str[s.state]),
       #sys.stdout.flush()
       if clientType != "server":
        progressSender(str(s.progress * 100))
       time.sleep(1)

    if clientType != "server":
        print "Download completed"
        progressSender("Done")
    print "Seeding...\nCtrl+C to stop"

    while h.is_seed():
        time.sleep(1) #keep process from getting garbage collected?
Example #28
0
    def test_alert(self):

        ses = lt.session({"alert_mask": lt.alert.category_t.all_categories})
        shutil.copy(os.path.join("..", "..", "test", "test_torrents", "base.torrent"), ".")
        ti = lt.torrent_info("base.torrent")
        h = ses.add_torrent({"ti": ti, "save_path": "."})
        time.sleep(1)
        ses.remove_torrent(h)
        ses.wait_for_alert(1000)  # milliseconds
        alerts = ses.pop_alerts()
        for a in alerts:
            print(a.message())

        st = h.status()
        print(st.next_announce)
        print(st.name)
        print(st.errc.message())
        print(st.pieces)
        print(st.last_seen_complete)
        print(st.completed_time)
        print(st.progress)
        print(st.num_pieces)
        print(st.distributed_copies)
        print(st.paused)
        print(st.info_hash)
Example #29
0
    def torrentUpdate(self):
        # exit if disabled
        if ( not self.torrent_enabled ): return

        for torrent in self.torrent_queue:
            if ( torrent.removed or torrent.completed ): continue
            try:
                info = None
                
                # process torrent.
                if ( torrent.lt_entry == None ):    
                    if torrent.filename.lower().endswith(".magnet"):
                        torrent.lt_entry = self.startMagnet(torrent)
                    else:
                        info = lt.torrent_info(torrent.filename)
                        torrent.lt_entry = self.torrent_engine.add_torrent({'ti': info, 'save_path': torrent.save_to})

                else:
                    status = torrent.lt_entry.status()
                    if ( int(status.state) == 5 ):
                        self.torrent_engine.remove_torrent(torrent.lt_entry)
                        torrent.lt_entry = None
                        torrent.completed = True

            except:
                mt.log.error("Could not add torrent: " + torrent.filename)
                pass
    def _setup(self):
        self.params = {}
        metainfo_file = open(self.metainfo_file_path, 'rb')
        metainfo = lt.bdecode(metainfo_file.read())
        metainfo_file.close()
        self.torrent_info = lt.torrent_info(metainfo)
        self.torrent_info_hash = str(self.torrent_info.info_hash())
        self.torrent_name = self.torrent_info.name()
        self.num_pieces = self.torrent_info.num_pieces()

        self.params['save_path'] = self.save_directory_path
        self.params['ti'] = self.torrent_info
        self.params['paused'] = True
        self.params['duplicate_is_error'] = True
        self.params['storage_mode'] = lt.storage_mode_t.storage_mode_allocate

        # torrent_handle must be set by instantiating class
        self.torrent_handle = None
        self.piece_completed_map = dict()
        self.piece_buffers = dict()
        self.is_transcoding = threading.Event()
        self._is_not_transcode_setting_up = threading.Event()
        self._is_not_transcode_setting_up.set()
        self.transcode_object = None
        self.transcode_writer = None
        self.completed_piece_buffers_loaded = False
Example #31
0
 def render_POST(self, request):
     if 'torrent' in request.args and len(request.args['torrent'][0]) > 0:
         d = models.Download()
         d.mime_type = u'application/x-bittorrent'
         d.metadata = request.args['torrent'][0]
         ti = lt.torrent_info(lt.bdecode(d.metadata))
         d.description = unicode(ti.name())
         d.size = int(ti.total_size())
         self.get_manager(request).add_download(d)
         request.redirect('/downloads')
         request.finish()
         return server.NOT_DONE_YET
     else:
         return self.render_template('errors/error.html', request, {
             'title': 'No Torrent Found',
             'message': 'Torrent was not uploaded'
         })
Example #32
0
    def get(self, request, hashinfo):
        try:
            o = File.objects.get(hashinfo=hashinfo)

            torrent_file = 'torrent/%s.torrent' % hashinfo

            torinfo = lt.torrent_info(torrent_file)

            ret = []
            file_list = torinfo.files()
            for i in file_list:
                r, name = check_format(i.path)
                if r:
                    ret.append('/mp4/%s/%s' % (torrent_file[8:16], name))
            return HttpResponse(json.dumps({'ret': ret, 'code': 200}))
        except ObjectDoesNotExist:
            return HttpResponse(json.dumps({'ret': None, 'code': 400}))
Example #33
0
def add_torrent(ses, filename, options):
    atp = lt.add_torrent_params()
    if filename.startswith('magnet:'):
        atp = lt.parse_magnet_uti(filename)
    else:
        atp.ti = lt.torrent_info(filename)
        try:
            at.resume_data = open(os.path.join(options.save_path, info.name() + '.fastresume'), 'rb').read()
        except:
            pass

    atp.save_path = options.save_path
    atp.storage_mode = lt.storage_mode_t.storage_mode_sparse
    atp.flags |= lt.torrent_flags.duplicate_is_error \
        | lt.torrent_flags.auto_managed \
        | lt.torrent_flags.duplicate_is_error
    ses.async_add_torrent(atp)
Example #34
0
    def test_bencoded_constructor(self):
        info = lt.torrent_info({
            'info': {
                'name': 'test_torrent',
                'length': 1234,
                'piece length': 16 * 1024,
                'pieces': 'aaaaaaaaaaaaaaaaaaaa'
            }
        })

        self.assertEqual(info.num_files(), 1)

        f = info.files()
        self.assertEqual(f.file_path(0), 'test_torrent')
        self.assertEqual(f.file_name(0), 'test_torrent')
        self.assertEqual(f.file_size(0), 1234)
        self.assertEqual(info.total_size(), 1234)
Example #35
0
def _make_key_from_torrent(torrent):
    """
    Will attempt to return the hash from the torrent if it is reasonably obtainable (i.e. without
    having to download the torrent file), otherwise it will return an md5 of the link to the torrent
    file.
    
    @param torrent: (string) url (http or https) to a torrent file, a raw torrent file, or a magnet link
    @return: (string) a key
    """
    if torrent.startswith('magnet:'):
        from sickbeard.providers.generic import TorrentProvider
        return TorrentProvider.getHashFromMagnet(torrent)
    elif torrent.startswith('http://') or torrent.startswith('https://'):
        return md5(torrent)
    else:
        torrent_info = lt.torrent_info(lt.bdecode(torrent))
        return str(torrent_info.info_hash())
Example #36
0
    def test_torrent_handle(self):
        ses = lt.session({
            'alert_mask': lt.alert.category_t.all_categories,
            'enable_dht': False
        })
        ti = lt.torrent_info('url_seed_multi.torrent')
        h = ses.add_torrent({'ti': ti, 'save_path': os.getcwd()})

        h.prioritize_files([0, 1])
        self.assertEqual(h.file_priorities(), [0, 1])

        h.prioritize_pieces([0])
        self.assertEqual(h.piece_priorities(), [0])

        # also test the overload that takes a list of piece->priority mappings
        h.prioritize_pieces([(0, 1)])
        self.assertEqual(h.piece_priorities(), [1])
Example #37
0
def add_torrent(ses, filename, options):
    atp = {}
    if filename.startswith('magnet:'):
        atp = lt.parse_magnet_uti(filename)
    else:
        atp['ti'] = lt.torrent_info(filename)
        try:
            atp["resume_data"] = open(os.path.join(options.save_path, info.name() + '.fastresume'), 'rb').read()
        except:
            pass

    atp["save_path"] = options.save_path
    atp["storage_mode"] = lt.storage_mode_t.storage_mode_sparse
    atp["paused"] = False
    atp["auto_managed"] = True
    atp["duplicate_is_error"] = True
    ses.async_add_torrent(atp)
Example #38
0
def add_torrent(request):

    # TODO: torrent_file is higher priority than url method, is it okay?
    if request.FILES.has_key('torrent_file') is True:
        torrent_file = request.FILES['torrent_file']
        data = torrent_file.read()

    elif request.POST.has_key('torrent_url') is True:
        url = request.POST['torrent_url']
        response = requests.get(url)
        data = response.content
    else:
        #TODO: notificate to user, error!
        print "DEBUG:: Not supported yet..."
        return redirect("/")

    e = lt.bdecode(data)
    info = lt.torrent_info(e)
    torrent_hash = str(info.info_hash())

    # get current user information
    u = User.objects.get(username=request.user.username)
    account = Account.objects.get(user=u)

    # check uniqueness
    torrent_entry = TorrentEntries.objects.filter(
        hash_value=torrent_hash).first()
    if torrent_entry and torrent_entry.progress == 100.0:
        new_entry = TorrentEntries(
            name=torrent_entry.name,
            hash_value=torrent_hash,
            progress=torrent_entry.progress,
            download_rate=torrent_entry.download_rate,
            owner=account,
            file_size=torrent_entry.file_size,
            downloaded_size=torrent_entry.downloaded_size,
            peers=torrent_entry.peers,
            status=torrent_entry.status,
            worker_pid=os.getpid())
        new_entry.save()
        return redirect("/")

    # Background torrent download
    tasks.TorrentDownload.delay(account, data)
    return redirect("/")
    def _download_real_torrent(self, inner_identifier, username):

        downloaded_torrent_path = os.path.normpath(
            '{}\\hpradiotracker\\static\\hpradiotracker\\media\\'.format(
                os.getcwd()))
        downloaded_torrent_file = os.path.normpath('{}_{}.torrent'.format(
            inner_identifier, username))
        downloaded_audio_file = os.path.normpath('{}_{}.mp3'.format(
            inner_identifier, username))

        ses = lt.session()
        ses.listen_on(6881, 68911)
        info = lt.torrent_info(
            os.path.normpath(downloaded_torrent_path + '\\' +
                             downloaded_torrent_file))
        # filename, extension = os.path.splitext(info.name())
        info.rename_file(0, str(downloaded_audio_file))

        params = {
            'ti': info,
            'save_path': downloaded_torrent_path,
            'storage_mode': lt.storage_mode_t(2),
            'paused': False,
            'auto_managed': True,
            'duplicate_is_error': True
        }
        handler = ses.add_torrent(params)
        ses.start_dht()

        print('...downloading metadata...')
        while not handler.has_metadata():
            time.sleep(1)
        print('...got metadata, starting torrent download...')
        while handler.status().state != lt.torrent_status.seeding:
            s = handler.status()
            # state_str = ['queued', 'checking', 'downloading metadata',
            #              'downloading', 'finished', 'seeding', 'allocating']
            # print s.progress * 100, '% complete (down: ', s.download_rate / 1000, 'kb/s up: ', s.upload_rate / 1000,
            #  ' kB/s peers: ', ,') ', state_str[s.state], ' ', s.total_download/1000000'
            print('File: ' + str(downloaded_audio_file) + ' ' +
                  str(s.progress * 100) + '% ' + 'Download:' +
                  str(s.download_rate / 1000) + ' Seeds:' + str(s.num_peers))
            time.sleep(5)

        print('...done')
Example #40
0
def create_torrent_session(
        resource: str, save_path: pathlib.Path, seed_mode: bool):
    """Create a torrent session given a torrent file
    :param str resource: torrent resource
    :param pathlib.Path save_path: path to save torrented files to
    :param bool seed_mode: seed mode
    :param list port_range: port range to listen on
    :return: torrent_handle
    """
    torrent_handle = _TORRENT_SESSION.add_torrent({
        'ti': libtorrent.torrent_info(
            str(_TORRENTS[resource]['torrent_file'])),
        'save_path': str(save_path),
        'seed_mode': seed_mode
    })
    logger.info('created torrent session for {} is_seed={}'.format(
        resource, torrent_handle.is_seed()))
    return torrent_handle
Example #41
0
def _mkatp(tmp_path_factory: pytest.TempPathFactory,
           *,
           proto=Proto.HYBRID) -> lt.add_torrent_params:
    atp = lt.add_torrent_params()
    # As of 2.0.6, create_torrent.set_hash2 isn't bound in python
    tmp_path = tmp_path_factory.mktemp("test-atp")
    (tmp_path / "file.txt").write_bytes(random.randbytes(1024))
    fs = lt.file_storage()
    lt.add_files(fs, str(tmp_path))
    flags = 0
    if not (proto & V2):
        flags = lt.create_torrent.v1_only
    elif not (proto & V1):
        flags = lt.create_torrent.v2_only
    ct = lt.create_torrent(fs, flags=flags)
    lt.set_piece_hashes(ct, str(tmp_path.parent))
    atp.ti = lt.torrent_info(ct.generate())
    return atp
Example #42
0
    def test_web_seeds(self):
        ti = lt.torrent_info('base.torrent')

        ws = [{
            'url': 'http://foo/test',
            'auth': '',
            'type': 0
        }, {
            'url': 'http://bar/test',
            'auth': '',
            'type': 1
        }]
        ti.set_web_seeds(ws)
        web_seeds = ti.web_seeds()
        self.assertEqual(len(ws), len(web_seeds))
        for i in range(len(web_seeds)):
            self.assertEqual(web_seeds[i]["url"], ws[i]["url"])
            self.assertEqual(web_seeds[i]["auth"], ws[i]["auth"])
Example #43
0
    def test_iterable_files(self):
        # the file_strage object is only iterable for backwards compatibility
        if not HAVE_DEPRECATED_APIS:
            return

        lt.session(settings)
        ti = lt.torrent_info('url_seed_multi.torrent')
        files = ti.files()

        idx = 0
        expected = ['bar.txt', 'var.txt']
        for f in files:
            print(f.path)

            self.assertEqual(os.path.split(f.path)[1], expected[idx])
            self.assertEqual(os.path.split(f.path)[0],
                             os.path.join('temp', 'foo'))
            idx += 1
Example #44
0
	def test_iterable_files(self):

		# this detects whether libtorrent was built with deprecated APIs
		# the file_strage object is only iterable for backwards compatibility
		if not hasattr(lt, 'version'): return

		ses = lt.session({'alert_mask': lt.alert.category_t.all_categories, 'enable_dht': False})
		ti = lt.torrent_info('url_seed_multi.torrent');
		files = ti.files()

		idx = 0
		expected = ['bar.txt', 'var.txt']
		for f in files:
			print(f.path)

			self.assertEqual(os.path.split(f.path)[1], expected[idx])
			self.assertEqual(os.path.split(os.path.split(f.path)[0]), ('temp', 'foo'))
			idx += 1
Example #45
0
def parse_torrent(torrent):
    """Returns a dictionary with basic information from torrent contents.

    :param torrent:
    :return: torrent info dict - keys: hash; name; files; torrent (torrent file contents just from input).
    :rtype: dict
    """
    torrent_info = lt.torrent_info(lt.bdecode(torrent))
    files_from_torrent = [
        a_file.path.decode('utf-8') for a_file in torrent_info.files()
    ]
    info = {
        'hash': str(torrent_info.info_hash()),
        'name': str(torrent_info.name()),
        'files': files_from_torrent,
        'torrent': torrent
    }
    return info
Example #46
0
def download_bt(magnet, movie_path, task_id):
    print "download"

    try:
        bt_path = magnet2torrent(magnet=magnet)

        ses = lt.session()
        ses.listen_on(6881, 6891)

        e = lt.bdecode(open(bt_path, 'rb').read())
        info = lt.torrent_info(e)

        params = { 'save_path': movie_path, \
                'storage_mode': lt.storage_mode_t.storage_mode_sparse, \
                'ti': info }
        h = ses.add_torrent(params)
        s = h.status()

        movie = db.Movie.query.filter_by(magnet=magnet).first()
        movie.name = info.name()
        db.db.session.commit()

        download = db.Download(taskid=task_id, name=info.name(), progress=0, speed=0, peer_nums=0)

        while (not s.is_seeding):
                s = h.status()

                state_str = ['queued', 'checking', 'downloading metadata', \
                        'downloading', 'finished', 'seeding', 'allocating']

                print '%.2f%% complete (down: %.1f kb/s up: %.1f kB/s peers: %d) %s' % \
                        (s.progress * 100, s.download_rate / 1000, s.upload_rate / 1000, \
                        s.num_peers, s.state)
                download.speed = '%.1f' % (s.download_rate/1000)
                download.progress = '%.2f' % (s.progress * 100)
                download.peer_nums = s.num_peers

                cache.set(task_id, download)
                time.sleep(1)
        movie.status = 1
        db.db.session.commit()
    except Exception as e:
        print e.message
        raise e
Example #47
0
def add():
    if not is_authenticated():
        return login_redirect()
    if flask.request.method == "POST":
        directory = flask.request.form["directory"].strip()
        if directory.startswith("/"):
            directory = directory[1:]
        if "../" in directory:
            return flask.abort(400, description="Invalid path provided")
        filename = "/tmp/btpd.%d." % os.getpid()
        if flask.request.form["torrenturl"].strip():
            filename = "%surl.%s.torrent" % (
                filename,
                hashlib.md5(flask.request.form["torrenturl"].encode(
                    "utf8")).hexdigest())
            utils.download_torrent(flask.request.form["torrenturl"], filename)
        else:
            file = flask.request.files["file"]
            filename = "%sfile.%s.torrent" % (filename, hashlib.md5(
                file.read()).hexdigest())
            file.save(filename)

        torrent = libtorrent.bdecode(open(filename, "rb").read())
        info_hash = codecs.encode(
            libtorrent.torrent_info(torrent).info_hash().to_bytes(),
            "hex").decode("utf8")

        username = database.username_from_session(
            flask.request.cookies["btpd-session"])

        idle = "idle" in flask.request.form
        if not database.has_setting(username, "base_dir"):
            database.set_setting(username, "base_dir", app.config["BASE_DIR"])
        base_dir = database.get_setting(username, "base_dir")
        directory = os.path.join(base_dir, directory)

        utils.add_torrent(directory, filename, idle)
        os.remove(filename)
        with list_condition:
            list_condition.notify()

        database.add_torrent(info_hash, username)
        return flask.redirect(flask.url_for("index"))
    return make_page("add.html")
Example #48
0
    def getCompleteList(self, opt, ui, progress, tmp_dir, hist_folder):
        m = ["Not Able To Open"]
        if opt == "Open":
            MainWindow = QtWidgets.QWidget()
            item, ok = QtWidgets.QInputDialog.getText(
                ui,
                "Input Dialog",
                "Enter Torrent Url or Magnet Link or local torrent file path",
            )
            if ok and item:
                if item.startswith("http") or (os.path.isfile(item)
                                               and item.endswith(".torrent")):
                    home = hist_folder
                    name1 = os.path.basename(item).replace(".torrent", "")
                    torrent_dest1 = os.path.join(tmp_dir, name1 + ".torrent")
                    if not os.path.exists(torrent_dest1):
                        if item.startswith("http"):
                            ccurl(item + "#" + "-o" + "#" + torrent_dest1)
                        else:
                            shutil.copy(item, torrent_dest1)
                    if os.path.exists(torrent_dest1):
                        info = lt.torrent_info(torrent_dest1)
                        name = info.name()
                        torrent_dest = os.path.join(home, name + ".torrent")
                        shutil.copy(torrent_dest1, torrent_dest)
                    m = [name]
                elif item.startswith("magnet:"):

                    torrent_handle, stream_session, info = get_torrent_info_magnet(
                        item, tmp_dir, ui, progress, tmp_dir)
                    torrent_file = lt.create_torrent(info)

                    home = hist_folder
                    name = info.name()
                    torrent_dest = os.path.join(home, name + ".torrent")

                    with open(torrent_dest, "wb") as f:
                        f.write(lt.bencode(torrent_file.generate()))

                    torrent_handle.pause()
                    stream_session.pause()
                    m = [name]
        m.append(1)
        return m
Example #49
0
def create_torrent(message_digest):
    #Create torrent
    name = str(message_digest) + ".torrent"
    fs = lt.file_storage()
    lt.add_files(fs, path)
    t = lt.create_torrent(fs)
    trackerList = [
        'udp://tracker.coppersurfer.tk:6969',
        'udp://tracker.opentrackr.org:1337/announce',
        'udp://torrent.gresille.org:80/announce',
        'udp://9.rarbg.me:2710/announce', 'udp://p4p.arenabg.com:1337',
        'udp://tracker.internetwarriors.net:1337'
    ]

    for tracker in trackerList:
        t.add_tracker(tracker, 0)
        t.set_creator('libtorrent %s' % lt.version)
        t.set_comment("Test")
        lt.set_piece_hashes(t, ".")
        torrent = t.generate()
        f = open(name, "wb")
        f.write(lt.bencode(torrent))
        f.close()

        #Seed torrent
        ses = lt.session()
        ses.listen_on(6881, 6891)
        h = ses.add_torrent({
            'ti': lt.torrent_info(name),
            'save_path': '.',
            'seed_mode': True
        })
        print("Total size: " + str(h.status().total_wanted))
        print("Name: " + h.name())
        while h.is_seed():
            s = h.status()
            state_str = ['queued', 'checking', 'downloading metadata', \
              'downloading', 'finished', 'seeding', 'allocating', 'checking fastresume']

            print('\r%.2f%% complete (down: %.1f kb/s up: %.1f kB/s peers: %d) %s' % \
              (s.progress * 100, s.download_rate / 1000, s.upload_rate / 1000, s.num_peers, state_str[s.state]))
            sys.stdout.flush()

            time.sleep(1)
Example #50
0
    def run_seeder(cls):
        ses = lt.session()
        ses.listen_on(2000, 3000)

        # not so fast
        ses.set_upload_rate_limit(500)
        ses.start_dht()
        ses.start_lsd()
        ses.start_upnp()
        ses.start_natpmp()

        info = lt.torrent_info(TORRENT_PATH)

        h = ses.add_torrent({'ti': info, 'save_path': settings.TEST_DIR})
        h.super_seeding(True)

        cls.h = h
        cls.info = info
        cls.seeder = ses
Example #51
0
    def getCompleteList(self, opt, ui):
        m = ['Not Able To Open']
        if opt == 'Open':
            MainWindow = QtGui.QWidget()
            item, ok = QtGui.QInputDialog.getText(
                MainWindow, 'Input Dialog',
                'Enter Torrent Url or Magnet Link or local torrent file path')
            if ok and item:
                if (item.startswith('http')
                        or item.startswith('/')) and item.endswith('.torrent'):
                    home = os.path.expanduser(
                        '~') + '/.config/AnimeWatch/History/Torrent/'
                    name1 = item.split('/')[-1].replace('.torrent', '')
                    torrent_dest1 = '/tmp/AnimeWatch/' + name1 + '.torrent'
                    if not os.path.exists(torrent_dest1):
                        if item.startswith('http'):
                            ccurl(item + '#' + '-o' + '#' + torrent_dest1)
                        else:
                            shutil.copy(item, torrent_dest1)
                    if os.path.exists(torrent_dest1):
                        info = lt.torrent_info(torrent_dest1)
                        name = info.name()
                        torrent_dest = home + name + '.torrent'
                        shutil.copy(torrent_dest1, torrent_dest)
                    m = [name]
                elif item.startswith('magnet:'):

                    torrent_handle, stream_session, info = get_torrent_info_magnet(
                        item, '/tmp/AnimeWatch', ui)
                    torrent_file = lt.create_torrent(info)

                    home = os.path.expanduser(
                        '~') + '/.config/AnimeWatch/History/Torrent/'
                    name = info.name()
                    torrent_dest = home + name + '.torrent'

                    with open(torrent_dest, "wb") as f:
                        f.write(lt.bencode(torrent_file.generate()))

                    torrent_handle.pause()
                    stream_session.pause()
                    m = [name]
        return m
Example #52
0
def check_mp3(torrent_file):
    torrent_info = dict()
    try:
        with open(torrent_file, 'rb') as f:
            e = lt.bdecode(f.read())
            torinfo = lt.torrent_info(e)
            torrent_info['name'] = torinfo.name()
            torrent_info['hash'] = torinfo.info_hash()
            torrent_info['num_files'] = torinfo.num_files()
            torrent_info['mp3_size'] = 0
            torrent_info['size'] = 0
            for filename in torinfo.files():
                torrent_info['size'] += filename.size
                if os.path.splitext(filename.path)[1].upper()[1:] == "MP3":
                    torrent_info['mp3_size'] += filename.size
            torrent_info['path'] = os.path.abspath(torrent_file)
            return torrent_info
    except Exception as err:
        return None
Example #53
0
def download(uri,ruta):
    ses = lt.session()
    ses.listen_on(6881, 6891)
    info = lt.torrent_info(uri)
    h = ses.add_torrent({'ti': info, 'save_path': ruta})
    print 'starting', h.name()
    while (not h.is_seed()):
        s = h.status()
        state_str = ['queued', 'checking', 'downloading metadata', \
                     'downloading', 'finished', 'seeding', 'allocating', 'checking fastresume']
        print '\r%.2f%% complete (down: %.1f kb/s up: %.1f kB/s peers: %d) %s' % \
              (s.progress * 100, s.download_rate / 1000, s.upload_rate / 1000, \
               s.num_peers, state_str[s.state]),\
            sys.stdout.flush()
        time.sleep(1)

    print h.name(), 'complete'

    return h.name()
Example #54
0
 def create(self, tracker_ip):
     """
     Description: Creates a new torrent.
     
     tracker_ip: The tracker IP:PORT string to use when creating the torrent.  Should match whatever tracker we're using for the cloud, obviously! :)
     """
     
     log.debug("Creating new torrent file, setting tracker to: %s" % tracker_ip)
     
     # Set the name of our torrent
     self.ti.torr_name = self.ti.torr_path + "/" + self.name_torrent(self.ti.file_name)                      
     
     # Create a storage object, and add the file which will be torrentized
     file_st = lt.file_storage()
     lt.add_files(file_st, self.ti.file_name)
            
     # Create the torrent
     try:
         torr = lt.create_torrent(file_st, self.ti.piece_size)
         torr.add_tracker("http://" + tracker_ip.tracker_ip + tracker_ip.announce_url)
         torr.set_comment(self.ti.comments)
         torr.set_creator(self.ti.creator)
         lt.set_piece_hashes(torr, os.path.dirname(self.ti.file_name))
     except:
         log.exception("Failed to create torrent")
         raise
     
     # Write to file
     try:
         f = open(self.ti.torr_name, "wb")
         f.write(lt.bencode(torr.generate()))
         f.close()
     except:
         raise
     
     # get the info_hash before returning
     self.ti.info      = lt.torrent_info(self.ti.torr_name)
     self.ti.info_hash = self.ti.info.info_hash()
     
     log.debug("New torrent details: %s" % self.ti)
     
     # Return the TorrentMetaInfo Object
     return self.ti
Example #55
0
def scandir(path, level):
    torrent = 0
    if os.path.exists(path):
        for (path, dirs, files) in walklevel(path, level):
            for item in files:
                # if archive (dump) file
                if re.search(r'.xml.bz2$', item):
                    # add this file into our index
                    # Building a list of all files which we later use to
                    # build the index: itemname, filename, httplink,
                    #   torrentlink
                    regex = re.compile(r"([a-z]+)-\d+(-pages-meta-current).*")
                    entries.append([
                        regex.sub(r"\1\2", item), item,
                        os.stat(path + '/' + item).st_size, webpath + item,
                        webpath + item + '.torrent'
                    ])
                    # create torrent file for package if it doesn't already exist
                    if not os.path.isfile(path + "/" + item + ".torrent"):
                        with open(os.devnull, 'wb') as devnull:
                            debugmsg("Create torrent file for " + path + "/" +
                                     item)
                            subprocess.check_call(
                                mktorrent_bin + ' -a ' + announce + ' -o ' +
                                path + '/' + item + '.torrent ' + path + '/' +
                                item,
                                shell=True,
                                stdout=devnull,
                                stderr=subprocess.STDOUT)
                        # get torrent info hash and append it to opentracker whitelist
                        info = lt.torrent_info(path + "/" + item + ".torrent")
                        info_hash = info.info_hash()
                        hexadecimal = str(info_hash)
                        os.system("echo " + hexadecimal + " >> " +
                                  torrenthashlist_file)
                # if torrent file
                if re.search(r'.xml.bz2.torrent$', item):
                    # remove torrent file if the inherent package doesn't exist anymore
                    if not os.path.isfile(path + "/" + item):
                        os.system("rm " + path + "/" + item + ".torrent")
    else:
        print("File or directory does not exists")
    return 0
Example #56
0
    def _get_torrent_info(self, torrent, download_dir):
        import libtorrent as lt

        torrent_file = None
        magnet = None
        info = {}
        file_info = {}

        if torrent.startswith('magnet:?'):
            magnet = torrent
            magnet_info = lt.parse_magnet_uri(magnet)
            info = {
                'name': magnet_info.name,
                'url': magnet,
                'magnet': magnet,
                'trackers': magnet_info.trackers,
                'save_path': download_dir,
            }
        elif torrent.startswith('http://') or torrent.startswith('https://'):
            response = requests.get(torrent,
                                    headers=self.headers,
                                    allow_redirects=True)
            torrent_file = os.path.join(download_dir,
                                        self._generate_rand_filename())

            with open(torrent_file, 'wb') as f:
                f.write(response.content)
        else:
            torrent_file = os.path.abspath(os.path.expanduser(torrent))
            if not os.path.isfile(torrent_file):
                raise RuntimeError(
                    '{} is not a valid torrent file'.format(torrent_file))

        if torrent_file:
            file_info = lt.torrent_info(torrent_file)
            info = {
                'name': file_info.name(),
                'url': torrent,
                'trackers': [t.url for t in list(file_info.trackers())],
                'save_path': download_dir,
            }

        return info, file_info, torrent_file, magnet
Example #57
0
    def test_alert(self):

        ses = lt.session({
            'alert_mask': lt.alert.category_t.all_categories,
            'enable_dht': False
        })
        ti = lt.torrent_info('base.torrent')
        h = ses.add_torrent({'ti': ti, 'save_path': os.getcwd()})
        st = h.status()
        time.sleep(1)
        ses.remove_torrent(h)
        ses.wait_for_alert(1000)  # milliseconds
        alerts = ses.pop_alerts()
        for a in alerts:
            if a.what() == 'add_torrent_alert':
                self.assertEquals(a.torrent_name, 'temp')
            print(a.message())
            for field_name in dir(a):
                if field_name.startswith('__'):
                    continue
                field = getattr(a, field_name)
                if callable(field):
                    print('  ', field_name, ' = ', field())
                else:
                    print('  ', field_name, ' = ', field)

        print(st.next_announce)
        self.assertEqual(st.name, 'temp')
        print(st.errc.message())
        print(st.pieces)
        print(st.last_seen_complete)
        print(st.completed_time)
        print(st.progress)
        print(st.num_pieces)
        print(st.distributed_copies)
        if HAVE_DEPRECATED_APIS:
            print(st.paused)
        print(st.info_hash)
        print(st.seeding_duration)
        print(st.last_upload)
        print(st.last_download)
        self.assertEqual(st.save_path, os.getcwd())
Example #58
0
    def getCompleteList(self, opt, ui, progress, tmp_dir, hist_folder):
        m = ['Not Able To Open']
        if opt == 'Open':
            MainWindow = QtWidgets.QWidget()
            item, ok = QtWidgets.QInputDialog.getText(
                MainWindow, 'Input Dialog',
                'Enter Torrent Url or Magnet Link or local torrent file path')
            if ok and item:
                if (item.startswith('http') or
                    (os.path.isfile(item) and item.endswith('.torrent'))):
                    home = hist_folder
                    name1 = os.path.basename(item).replace('.torrent', '')
                    torrent_dest1 = os.path.join(tmp_dir, name1 + '.torrent')
                    if not os.path.exists(torrent_dest1):
                        if item.startswith('http'):
                            ccurl(item + '#' + '-o' + '#' + torrent_dest1)
                        else:
                            shutil.copy(item, torrent_dest1)
                    if os.path.exists(torrent_dest1):
                        info = lt.torrent_info(torrent_dest1)
                        name = info.name()
                        torrent_dest = os.path.join(home, name + '.torrent')
                        shutil.copy(torrent_dest1, torrent_dest)
                    m = [name]
                elif item.startswith('magnet:'):

                    torrent_handle, stream_session, info = get_torrent_info_magnet(
                        item, tmp_dir, ui, progress, tmp_dir)
                    torrent_file = lt.create_torrent(info)

                    home = hist_folder
                    name = info.name()
                    torrent_dest = os.path.join(home, name + '.torrent')

                    with open(torrent_dest, "wb") as f:
                        f.write(lt.bencode(torrent_file.generate()))

                    torrent_handle.pause()
                    stream_session.pause()
                    m = [name]
        m.append(1)
        return m
Example #59
0
 def test_load_decode_depth_limit(self):
     self.assertRaises(
         RuntimeError, lambda: lt.torrent_info(
             {
                 'test': {
                     'test': {
                         'test': {
                             'test': {
                                 'test': {}
                             }
                         }
                     }
                 },
                 'info': {
                     'name': 'test_torrent',
                     'length': 1234,
                     'piece length': 16 * 1024,
                     'pieces': 'aaaaaaaaaaaaaaaaaaaa'
                 }
             }, {'max_decode_depth': 1}))
Example #60
0
def add_torrent(ses, filename, options):
    atp = lt.add_torrent_params()
    if filename.startswith('magnet:'):
        atp = lt.parse_magnet_uri(filename)
    else:
        ti = lt.torrent_info(filename)
        resume_file = os.path.join(options.save_path,
                                   ti.name() + '.fastresume')
        try:
            atp = lt.read_resume_data(open(resume_file, 'rb').read())
        except Exception as e:
            print('failed to open resume file "%s": %s' % (resume_file, e))
        atp.ti = ti

    atp.save_path = options.save_path
    atp.storage_mode = lt.storage_mode_t.storage_mode_sparse
    atp.flags |= lt.torrent_flags.duplicate_is_error \
        | lt.torrent_flags.auto_managed \
        | lt.torrent_flags.duplicate_is_error
    ses.async_add_torrent(atp)