Beispiel #1
1
def magnet2torrent(magnet):
    tempdir = tempfile.mkdtemp()
    ses = lt.session()
    params = {
        'save_path': tempdir,
        'storage_mode': lt.storage_mode_t(2),
        'paused': False,
        'auto_managed': True,
        'duplicate_is_error': True,
        'flag_merge_resume_trackers': True
    }
    handle = lt.add_magnet_uri(ses, magnet, params)

    print("Downloading Metadata (this may take a while)")
    while (not handle.has_metadata()):
        try:
            sleep(1)
        except KeyboardInterrupt:
            print("Aborting...")
            ses.pause()
            print("Cleanup dir " + tempdir)
            shutil.rmtree(tempdir)
            sys.exit(0)
    ses.pause()
    shutil.rmtree(tempdir)
    print("Done")
    
    torinfo = handle.get_torrent_info()
    torfile = lt.create_torrent(torinfo)
    torcontent = lt.bencode(torfile.generate())
    return torcontent
def generateTorrentOfdownloadedFiles(downloadedFolder):
    print("generateTorrentOfdownloaded Files for sharing...")
    # http://www.libtorrent.org/reference-Create_Torrents.html
    # downloadedFolder = utils.getHomeFolder()+"/"+config.DOWNLOAD_FOLDER
    from os import listdir
    for file in listdir (downloadedFolder):
        print (file)
        fs = lt.file_storage()
        # file_storage fs;

        # // recursively adds files in directories
        # add_files(fs, "./my_torrent");
        lt.add_files(fs, downloadedFolder)
        t = lt.create_torrent() # ?¿?¿?¿?¿?¿?¿?¿?¿¿?¿¿?
        # create_torrent t(fs);
        lt.create_torrent(t, fs)

        t.add_tracker("http://my.tracker.com/announce");
        t.set_creator("Ultraviolet test");


        # // reads the files and calculates the hashes
        # set_piece_hashes(t, ".");
        lt.set_piece_hashes(t,".")
        # ofstream out("my_torrent.torrent", std::ios_base::binary);
        # bencode(std::ostream_iterator<char>(out), t.generate());
        fileContent = t.generate()
        # byteContent= bytearray(fileContent)
        # newFile = open (utils.getHomeFolder()+"torrents/newTorrnet.torrent", "wb")
        # newFile.write(byteContent)
        writeBinaryFile(utils.getHomeFolder()+"torrents/newTorrnet.torrent", fileContent)
Beispiel #3
0
 def finalize(self, path, uid):
   #print 'finalize', path, uid
   try:
     fs = lt.file_storage()
     tmp_fn = os.path.join(self.tmp, uid)
     try: st_size = os.stat(tmp_fn).st_size
     except:
       traceback.print_exc()
       return
       
     #print tmp_fn, st_size
     lt.add_files(fs, tmp_fn, st_size)
     t = lt.create_torrent(fs)
     t.set_creator("DelugeFS");
     lt.set_piece_hashes(t, self.tmp)
     tdata = t.generate()
     #print tdata
     with open(self.hgdb+path, 'wb') as f:
       f.write(lt.bencode(tdata))
     #print 'wrote', self.hgdb+path
     dat_dir = os.path.join(self.dat, uid[:2])
     if not os.path.isdir(dat_dir): 
       try: os.mkdir(dat_dir)
       except: pass
     os.rename(tmp_fn, os.path.join(dat_dir, uid))
     #if os.path.exists(self.shadow+path): os.remove(self.shadow+path)
     #os.symlink(os.path.join(dat_dir, uid), self.shadow+path)
     #print 'committing', self.hgdb+path
     self.repo.hg_commit('wrote %s' % path, files=[self.hgdb+path])
     self.should_push = True
     self.__add_torrent(tdata, path)
   except Exception as e:
     traceback.print_exc()
     raise e
Beispiel #4
0
def generate_torrent_from_magnet(url):
    try:
        session = libtorrent.session()
        tempdir = tempfile.mkdtemp()
        params = {
            'save_path': tempdir,
            'storage_mode': libtorrent.storage_mode_t(2),
            'paused': False,
            'auto_managed': True,
            'duplicate_is_error': True
        }
        handle = libtorrent.add_magnet_uri(session, url, params)
        while (not handle.has_metadata()):
            time.sleep(.1)

        session.pause()
        torinfo = handle.get_torrent_info()

        fs = libtorrent.file_storage()
        for file in torinfo.files():
            fs.add_file(file)
        torfile = libtorrent.create_torrent(fs)
        torfile.set_comment(torinfo.comment())
        torfile.set_creator(torinfo.creator())
        torrent_data = libtorrent.bencode(torfile.generate())
        session.remove_torrent(handle)
        return torrent_data
    except:
        torrent_data = None

    if handle and session:
        session.remove_torrent(handle)

    return torrent_data
Beispiel #5
0
def magnet2torrent(link):

    sess = lt.session()
    sess.add_dht_router("router.bittorrent.com", 6881)
    sess.add_dht_router("router.utorrent.com", 6881)
    sess.add_dht_router("router.bitcomet.com", 6881)
    sess.add_dht_router("dht.transmissionbt.com", 6881)
    sess.start_dht()

    params = {
        "save_path": "/tmp/tor",
        # "storage_mode":lt.storage_mode_t.storage_mode_sparse,
        # "paused": True,
        # "auto_managed": True,
        "duplicate_is_error": True,
    }
    handle = lt.add_magnet_uri(sess, link, params)

    # waiting for metadata
    while True:
        if not handle.has_metadata():
            time.sleep(6)
        else:
            break

    sess.pause()

    # create a torrent
    torinfo = handle.get_torrent_info()
    torfile = lt.create_torrent(torinfo)
    torcontent = lt.bencode(torfile.generate())
    torname = torinfo.name()
    sess.remove_torrent(handle, 1)

    return {"status": 200, "name": torname, "data": torcontent}
Beispiel #6
0
 def file_complete(self, torrent):
     info_hash=str(torrent.info_hash())
     nt=lt.create_torrent(torrent)
     tname=self._tname(info_hash)
     f = open(tname, 'wb')
     f.write(lt.bencode(nt.generate()))
     f.close()
Beispiel #7
0
def magnet2t(link, tfile):
    sess = lt.session()
    params = {
        "save_path": '/',
        "storage_mode": lt.storage_mode_t.storage_mode_sparse,
        "paused": True,
        "auto_managed": True,
        "duplicate_is_error": True
    }
    try:
        handle = lt.add_magnet_uri(sess, link, params)
        state_str = ['queued', 'checking', 'downloading metadata', 'downloading',
                 'finished', 'seeding', 'allocating']
        while (not handle.has_metadata()):
            s = handle.status()
            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(5)
            print handle.has_metadata()

        torinfo = handle.get_torrent_info()
        torfile = lt.create_torrent(torinfo)

        t = open(tfile, "wb")
        t.write(lt.bencode(torfile.generate()))
        t.close()
        print '%s  generated!' % tfile
    except Exception, ex:
        print Exception, ":", ex
        return False
Beispiel #8
0
    def _save_torrent_info(self, torr_handle):
        """
        Save torrent metatata and a .torrent file for resume.

        @param torr_handle: object - torrent handle
        @return:
        """
        if self._persistent:
            info_hash = str(torr_handle.info_hash())
            torr_filepath = os.path.join(self._resume_dir, info_hash + '.torrent')
            meta_filepath = os.path.join(self._resume_dir, info_hash + '.resume')
            torr_info = torr_handle.get_torrent_info()
            torr_file = libtorrent.create_torrent(torr_info)
            torr_content = torr_file.generate()
            torr_bencoded = libtorrent.bencode(torr_content)
            with open(torr_filepath, 'wb') as t_file:
                t_file.write(torr_bencoded)
            metadata = {'name': torr_handle.name(),
                        'info_hash': info_hash,
                        'save_path': torr_handle.save_path(),
                        'resume_data': None}
            with open(meta_filepath, mode='wb') as m_file:
                pickle.dump(metadata, m_file)
        else:
            raise TorrenterError('Trying to save torrent metadata for a non-persistent instance!')
Beispiel #9
0
def create_torrent(path,filename=""):
    fs = lt.file_storage()
    if filename=="":
		listz = list_folder(top)
		for tuplez in listz:
			path=tuplez[0]
			filename=tuplez[1]
			fs.add_file(pathz , size)
    else:
        pathz = os.path.join(path,filename)
        print "pathz", pathz
        size = os.path.getsize(pathz)
        fs.add_file(filename, size)
        #fs.add_file(pathz , size)
    tor = lt.create_torrent(fs)
    lt.set_piece_hashes(tor,'.')
    tor.set_comment("COMEONES")
    tor.set_creator("PEONDUSUD")
    announce_url = "http://www.gks.gs/tracker"
    tor.add_tracker(announce_url)
    tor.set_priv(True)
    tor.add_url_seed("http://192.168.70.136:58888")
    print dir(tor)
    f = open(save_torrent_folder + filename + ".torrent", "wb")
    f.write(lt.bencode(tor.generate()))
    f.close()
    print "torrent raw :"
    print lt.bencode(tor.generate())
Beispiel #10
0
def fetch_magnet(magnet_uri):	
	tempdir = tempfile.mkdtemp()
	logger.debug("Fetching magnet to '%s'" % tempdir)
	params = {
		"save_path" : tempdir,
		"duplicate_is_error" : True,
		"paused" : False,
		"auto_managed" : True,
		"url" : magnet_uri,
		"storage_mode" : lt.storage_mode_t(2)
	}
	handle = ses.add_torrent(params)
	def cleanup():
		ses.remove_torrent(handle)
		shutil.rmtree(tempdir)
	while not handle.has_metadata():
		try :			
			time.sleep(1)			
		except NameError as e:
			logger.debug("Exception!! %s" % str(e))
			cleanup()
			return		
	logger.debug("Magnet %s fetched!" % magnet_uri)
	torrent_data = lt.create_torrent(handle.get_torrent_info()).generate()	
	add_from_torrent_info(handle.get_torrent_info(), lt.bencode(torrent_data))
	cleanup()
def download_torrent(magnet):
    tempdir = tempfile.mkdtemp()
    ses = libtorrent.session()
    params = {
        'save_path': tempdir,
        'duplicate_is_error': True,
        'storage_mode': libtorrent.storage_mode_t(2),
        'paused': False,
        'auto_managed': True,
        'duplicate_is_error': True
    }
    handle = libtorrent.add_magnet_uri(ses, magnet, params)

    _wait_for_metadata(handle)

    ses.pause()

    # Exract torrent file content
    torinfo = handle.get_torrent_info()

    # Cleanup
    ses.remove_torrent(handle)
    shutil.rmtree(tempdir)

    return libtorrent.create_torrent(torinfo), torinfo.name()
Beispiel #12
0
def crawl(magnet_link):

  print magnet_link

  h = lt.add_magnet_uri(ses, magnet_link, params)

  timeout_counter = 0

  while (not h.has_metadata()):
    #print "Sleeping..."
    time.sleep(1)

    # Kill this torrent when the timeout reaches 3minutes
    timeout_counter += 1

    if timeout_counter == 180:
      with open("failed_magnets.txt", "a") as fail:
        fail.write(magnet_link + '\n')

      return


  if h.has_metadata():
    #print "Got the metadata"

    torinfo = h.get_torrent_info()

    torfile = lt.create_torrent( torinfo )

    name = (magnet_link.split("magnet:?xt=urn:btih:")[1][:40]).upper()

    with open( name + ".torrent", "wb" ) as f:
      f.write(lt.bencode(torfile.generate()))

  return
Beispiel #13
0
def magnet2torrent(link, torrent_file):
    
    sess = lt.session()
    sess.add_dht_router('router.bittorrent.com', 6881)
    sess.add_dht_router('router.utorrent.com', 6881)
    sess.add_dht_router('router.bitcomet.com', 6881)
    sess.add_dht_router('dht.transmissionbt.com', 6881)
    sess.start_dht();

    params = {
        "save_path": 'D:\\Desktop',
        #"storage_mode":lt.storage_mode_t.storage_mode_sparse,
        #"paused": True,
        #"auto_managed": True,
        "duplicate_is_error": True
    }
    handle = lt.add_magnet_uri(sess, link, params)
    
    # waiting for metadata
    while (not handle.has_metadata()):
        time.sleep(5)
    
    # create a torrent
    torinfo = handle.get_torrent_info()
    torfile = lt.create_torrent(torinfo)
    torcontent = lt.bencode(torfile.generate())

    # save to file
    t = open(torrent_file, "wb")
    t.write(torcontent)
    t.close()
    
    return True
	def createTorrent(torrent_file_path, content_folder_path, progress_cb = None):
		print("createTorrent")

		fs = libtorrent.file_storage()

		if not os.path.isabs(torrent_file_path):
			raise "Torrent path not absolute"

		if not os.path.isabs(content_folder_path):
			raise "Content path not absolute"

		libtorrent.add_files(fs, content_folder_path)

		if fs.num_files() == 0:
			print("No files to add.")
			return

		t = libtorrent.create_torrent(fs, piece_size=0, pad_file_limit=(4 * 1024 * 1024))

		def progress(piece_num):
			if progress_cb:
				pc = int((100.0 * (1.0+ piece_num)) / fs.num_pieces())
				progress_cb(pc)
		
		parent = os.path.dirname(content_folder_path)
		libtorrent.set_piece_hashes(t, parent, progress)

		data = libtorrent.bencode(t.generate())

		file = open(torrent_file_path,'wb')
		file.write(data)
		file.close()
	def magnetToTorrent(self, magnet):
		session = libtorrent.session()
		session.start_dht()
		session.add_dht_router("router.bittorrent.com", 6881)
		session.add_dht_router("router.utorrent.com", 6881)
		session.add_dht_router("router.bitcomet.com", 6881)
		session.listen_on(6881, 6891)
		session.set_alert_mask(libtorrent.alert.category_t.storage_notification)
		handle = libtorrent.add_magnet_uri(session, magnet, {'save_path': self.storageDirectory})
		iterator = 0
		progressBar = xbmcgui.DialogProgress()
		progressBar.create('Подождите', 'Идёт преобразование magnet-ссылки.')
		while not handle.has_metadata():
			time.sleep(0.1)
			progressBar.update(iterator)
			iterator += 1
			if iterator == 100:
				iterator = 0
			if progressBar.iscanceled():
				progressBar.update(0)
				progressBar.close()
				return
		progressBar.update(0)
		progressBar.close()
		torrent = libtorrent.create_torrent(handle.get_torrent_info())
		torentFile = open(self.torrentFile, "wb")
		torentFile.write(libtorrent.bencode(torrent.generate()))
		torentFile.close()
		session.remove_torrent(handle)
Beispiel #16
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
Beispiel #17
0
    def render_GET(self, request):
        """
        .. http:get:: /download/(string: infohash)/torrent

        A GET request to this endpoint returns the .torrent file associated with the specified download.

            **Example request**:

                .. sourcecode:: none

                    curl -X GET http://localhost:8085/downloads/4344503b7e797ebf31582327a5baae35b11bda01/torrent

            **Example response**:

            The contents of the .torrent file.
        """
        download = self.session.get_download(self.infohash)
        if not download:
            return DownloadSpecificEndpoint.return_404(request)

        if not download.handle or not download.handle.is_valid() or not download.handle.has_metadata():
            return DownloadSpecificEndpoint.return_404(request)

        torrent_info = get_info_from_handle(download.handle)
        t = create_torrent(torrent_info)
        torrent = t.generate()
        bencoded_torrent = bencode(torrent)

        request.setHeader(b'content-type', 'application/x-bittorrent')
        request.setHeader(b'Content-Disposition', 'attachment; filename=%s.torrent' % hexlify(self.infohash))
        return bencoded_torrent
Beispiel #18
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)
    def run(self):
        """run the converter.

        using the class attribute initiated at init function.

        Returns:
            Filename of created torrent.

        Raises:
            KeyboardInterrupt: This error caused by user to stop this.
            When downloading metadata from magnet link,
            it requires an additional step before the error reraised again.
        """
        print("Downloading Metadata (this may take a while)")

        # used to control "Maybe..." and "or the" msgs after sleep(1)
        wait_time = 1
        soft_limit = 120
        while not self.handle.has_metadata():
            try:
                sleep(1)
                if wait_time > soft_limit:
                    print("Downloading is taking a while, maybe there is an "
                          "issue with the magnet link or your network connection")
                    soft_limit += 30
                wait_time += 1
            except KeyboardInterrupt:
                print("\nAborting...")
                self.ses.pause()
                print("Cleanup dir " + self.tempdir)
                shutil.rmtree(self.tempdir)
                raise
        self.ses.pause()
        print("Done")

        torinfo = self.handle.get_torrent_info()
        torfile = lt.create_torrent(torinfo)

        output = pt.abspath(torinfo.name() + ".torrent")

        if self.output_name:
            if pt.isdir(self.output_name):
                output = pt.abspath(pt.join(self.output_name,
                                            torinfo.name() + ".torrent"))
            elif pt.isdir(pt.dirname(pt.abspath(self.output_name))):
                output = pt.abspath(self.output_name)
        else:
            output = pt.abspath(torinfo.name() + ".torrent")

        print("Saving torrent file here : " + output + " ...")
        with open(output, "wb") as outfile:
            torcontent = lt.bencode(torfile.generate())
            outfile.write(torcontent)

        print("Saved! Cleaning up dir: " + self.tempdir)
        self.ses.remove_torrent(self.handle)
        shutil.rmtree(self.tempdir)

        return output
Beispiel #20
0
 def file_complete(self, torrent, url=None):
     info_hash=str(torrent.info_hash())
     nt=lt.create_torrent(torrent)
     tname=self._tname(info_hash)
     with open(tname, 'wb') as f:
         f.write(lt.bencode(nt.generate()))
     if url:
         self.save(url,info_hash)
Beispiel #21
0
	def test_from_torrent_info(self):
		ti = lt.torrent_info('unordered.torrent')
		ct = lt.create_torrent(ti)
		entry = ct.generate()
		content = lt.bencode(entry).strip()
		with open('unordered.torrent', 'r') as f:
			file_content = f.read().strip()
			self.assertEqual(content, file_content)
Beispiel #22
0
def GenerateTorrent():
	fs = lt.file_storage()
	lt.add_files(fs, PackagesDirectory)
	t = lt.create_torrent(fs, flags = 1&8&16) # 16 does nothing right now
	#t = lt.create_torrent(fs)
	t.add_tracker("http://tpm.blogwithme.net:81/tracker")
	lt.set_piece_hashes(t,MasterDirectory)# ## Not working
	return t.generate()
Beispiel #23
0
def magnet2torrent(magnet, output_name = None):
  if output_name and \
      not pt.isdir(output_name) and \
      not pt.isdir(pt.dirname(pt.abspath(output_name))):
    print "Invalid output folder: " + pt.dirname(pt.abspath(output_name))
    print ""
    return

  tempdir = tempfile.mkdtemp()
  ses = lt.session()
  params = {
    'save_path': tempdir,
    'duplicate_is_error': True,
    'storage_mode': lt.storage_mode_t(2),
    'paused': False,
    'auto_managed': True,
    'duplicate_is_error': True
  }
  handle = lt.add_magnet_uri(ses, magnet, params)
  print "Downloading Metadata (this may take a while)"
  while (not handle.has_metadata()):
    try:
      time.sleep(1)
    except KeyboardInterrupt:
      print "Abrorting..."
      ses.pause()
      print "Cleanup dir " + tempdir
      shutil.rmtree(tempdir)
      return
  print "done"

  torinfo = handle.get_torrent_info()

  output = pt.abspath(torinfo.name() + ".torrent" )

  if output_name:
    if pt.isdir(output_name):
      output = pt.abspath(pt.join(output_name, torinfo.name() + ".torrent"))
    elif pt.isdir(pt.dirname(pt.abspath(output_name))) == True:
      output = pt.abspath(output_name)
  print 'saving torrent file here : ' + output + " ..."

  fs = lt.file_storage()
  for file in torinfo.files():
    fs.add_file(file)
  torfile = lt.create_torrent(fs)
  torfile.set_comment(torinfo.comment())
  torfile.set_creator(torinfo.creator())

  torcontent = lt.bencode(torfile.generate())
  f = open(output, "wb")
  f.write(lt.bencode(torfile.generate()))
  f.close()
  print 'Saved! Cleaning up dir: ' + tempdir
  shutil.rmtree(tempdir)
  return output
Beispiel #24
0
    def create_torrent(self):
        # TODO check if root
        tarball_uid = self.get('tarball')
        if not tarball_uid:
            self.log.error("No tarball in DB.")
            return False

        cluster = Cluster(mongo_db=self._mongo_db)
        tarball = cluster.get('path') + "/torrents/" + tarball_uid + ".tgz"
        if not os.path.exists(tarball):
            self.log.error("Wrong path in DB.")
            return False

        tracker_address = cluster.get('frontend_address')
        if tracker_address == '':
            self.log.error("Tracker address needs to be configured.")
            return False

        tracker_port = cluster.get('frontend_port')
        if tracker_port == 0:
            self.log.error("Tracker port needs to be configured.")
            return False

        user = cluster.get('user')
        user_id = pwd.getpwnam(user).pw_uid
        grp_id = pwd.getpwnam(user).pw_gid

        old_cwd = os.getcwd()
        os.chdir(os.path.dirname(tarball))

        uid = str(uuid.uuid4())
        torrentfile = cluster.get('path') + "/torrents/" + uid + ".torrent"

        fs = libtorrent.file_storage()
        libtorrent.add_files(fs, os.path.basename(tarball))
        t = libtorrent.create_torrent(fs)
        if cluster.get('frontend_https'):
            proto = 'https'
        else:
            proto = 'http'
        t.add_tracker((proto + "://" + str(tracker_address) +
                       ":" + str(tracker_port) + "/announce"))

        t.set_creator(torrent_key)
        t.set_comment(uid)
        libtorrent.set_piece_hashes(t, ".")

        f = open(torrentfile, 'w')
        f.write(libtorrent.bencode(t.generate()))
        f.close()
        os.chown(torrentfile, user_id, grp_id)

        self.set('torrent', str(uid))
        os.chdir(old_cwd)

        return True
Beispiel #25
0
def make_torrent(path):
    abs_path = os.path.abspath(path)
    fs = libtorrent.file_storage()
    libtorrent.add_files(fs, abs_path)
    ct = libtorrent.create_torrent(fs)
    ct.add_tracker("http://127.0.0.1/announce")
    # set True if private torrent.
    ct.set_priv(True)
    libtorrent.set_piece_hashes(ct, os.path.split(abs_path)[0])
    return ct.generate()
def magnet2torrent(magnet, output_name=None):
    if output_name and \
            not pt.isdir(output_name) and \
            not pt.isdir(pt.dirname(pt.abspath(output_name))):
        print("Invalid output folder: " + pt.dirname(pt.abspath(output_name)))
        print("")
        sys.exit(0)

    tempdir = tempfile.mkdtemp()
    ses = lt.session()
    params = {
        'save_path': tempdir,
        'duplicate_is_error': True,
        'storage_mode': lt.storage_mode_t(2),
        'paused': False,
        'auto_managed': True,
        'duplicate_is_error': True
    }
    handle = lt.add_magnet_uri(ses, magnet, params)

    print("Downloading Metadata (this may take a while)")
    while (not handle.has_metadata()):
        try:
            sleep(1)
        except KeyboardInterrupt:
            print("Aborting...")
            ses.pause()
            print("Cleanup dir " + tempdir)
            shutil.rmtree(tempdir)
            sys.exit(0)
    ses.pause()
    print("Done")

    torinfo = handle.get_torrent_info()
    torfile = lt.create_torrent(torinfo)

    output = pt.abspath(torinfo.name() + ".torrent")

    if output_name:
        if pt.isdir(output_name):
            output = pt.abspath(pt.join(
                output_name, torinfo.name() + ".torrent"))
        elif pt.isdir(pt.dirname(pt.abspath(output_name))):
            output = pt.abspath(output_name)

    print("Saving torrent file here : " + output + " ...")
    torcontent = lt.bencode(torfile.generate())
    f = open(output, "wb")
    f.write(lt.bencode(torfile.generate()))
    f.close()
    print("Saved! Cleaning up dir: " + tempdir)
    ses.remove_torrent(handle)
    shutil.rmtree(tempdir)

    return output
Beispiel #27
0
 def create(target):
   url = "http://{0}:6969/announce".format(socket.gethostname())
   creator = "Ports Management Team <*****@*****.**>"
   fs = libtorrent.file_storage()
   libtorrent.add_files(fs, target)
   torrent = libtorrent.create_torrent(fs)
   torrent.add_tracker(url, 0)
   torrent.set_creator(creator)
   libtorrent.set_piece_hashes(torrent, os.path.dirname(target))
   data = libtorrent.bencode(torrent.generate())
   return Torrent(target, data=data)
Beispiel #28
0
	def save_metadata(self, info_hash, torinfo):
		torfile = lt.create_torrent(torinfo)
		dirname = self._metadata_dir + '/' + '%s_%s' % (time.strftime('%Y%m%d'), "meta")
		if os.path.isdir(dirname) is False:
			os.mkdir(dirname)

		filename = dirname + '/' + str(info_hash) + '.torrent'
		with open(filename,'wb') as f:
			f.write(lt.bencode(torfile.generate()))

		print "save metadata " , filename
Beispiel #29
0
 def test_from_torrent_info(self):
     ti = lt.torrent_info("unordered.torrent")
     ct = lt.create_torrent(ti)
     entry = ct.generate()
     content = lt.bencode(entry).strip()
     with open("unordered.torrent", "rb") as f:
         file_content = bytearray(f.read().strip())
         print(content)
         print(file_content)
         print(entry)
         self.assertEqual(content, file_content)
Beispiel #30
0
 def file_complete(self, torrent, url=None):
     info_hash=str(torrent.info_hash())
     nt=lt.create_torrent(torrent).generate()
     #this is a fix for issue https://github.com/arvidn/libtorrent/issues/945
     #however proper fix is to migrate to latest 1.1
     if not nt['info']:
         nt['info'] = lt.bdecode(torrent.metadata())
     tname=self._tname(info_hash)
     with open(tname, 'wb') as f:
         f.write(lt.bencode(nt))
     if url:
         self.save(url,info_hash)
Beispiel #31
0
def magnet_to_torrent(magnet_uri, book_uid, dst):
    ses = lt.session()
    params = {
        'save_path': dst,
        'storage_mode': lt.storage_mode_t(2),
        'paused': False,
        'auto_managed': True,
        'duplicate_is_error': True
    }
    link = magnet_uri
    handle = lt.add_magnet_uri(ses, link, params)

    print('downloading metadata...')
    while not handle.has_metadata():
        s = handle.status()
        time.sleep(1)

    torinfo = handle.get_torrent_info()
    torrent_file = lt.create_torrent(torinfo)
    torrent_path = os.path.join(dst, "{0}.torrent".format(book_uid))
    with open(torrent_path, "wb") as f:
        f.write(lt.bencode(torrent_file.generate()))
    print("Torrent saved to %s" % torrent_path)
    return torrent_path
Beispiel #32
0
    def createTorrent(torrent_file_path,
                      content_folder_path,
                      progress_cb=None):
        print("createTorrent")

        fs = libtorrent.file_storage()

        if not os.path.isabs(torrent_file_path):
            raise "Torrent path not absolute"

        if not os.path.isabs(content_folder_path):
            raise "Content path not absolute"

        libtorrent.add_files(fs, content_folder_path)

        if fs.num_files() == 0:
            print("No files to add.")
            return

        t = libtorrent.create_torrent(fs,
                                      piece_size=0,
                                      pad_file_limit=(4 * 1024 * 1024))

        def progress(piece_num):
            if progress_cb:
                pc = int((100.0 * (1.0 + piece_num)) / fs.num_pieces())
                progress_cb(pc)

        parent = os.path.dirname(content_folder_path)
        libtorrent.set_piece_hashes(t, parent, progress)

        data = libtorrent.bencode(t.generate())

        file = open(torrent_file_path, 'wb')
        file.write(data)
        file.close()
Beispiel #33
0
 def magnet_to_torrent(self, magnet_uri, destination_folder, timeout):
     import libtorrent
     params = libtorrent.parse_magnet_uri(magnet_uri)
     session = libtorrent.session()
     lt_version = [int(v) for v in libtorrent.version.split('.')]
     if lt_version > [0,16,13,0]:
         # for some reason the info_hash needs to be bytes but it's a struct called sha1_hash
         params['info_hash'] = params['info_hash'].to_bytes()
     handle = libtorrent.add_magnet_uri(session, magnet_uri, params)
     log.debug('Acquiring torrent metadata for magnet %s', magnet_uri)
     timeout_value = timeout
     while not handle.has_metadata():
         time.sleep(0.1)
         timeout_value -= 0.1
         if timeout_value <= 0:
             raise plugin.PluginError('Timed out after {} seconds trying to magnetize'.format(timeout))
     log.debug('Metadata acquired')
     torrent_info = handle.get_torrent_info()
     torrent_file = libtorrent.create_torrent(torrent_info)
     torrent_path = pathscrub(os.path.join(destination_folder, torrent_info.name() + ".torrent"))
     with open(torrent_path, "wb") as f:
         f.write(libtorrent.bencode(torrent_file.generate()))
     log.debug('Torrent file wrote to %s', torrent_path)
     return torrent_path
Beispiel #34
0
def magnet2torrent(magnet, output_name=None):
    if output_name and \
            not pt.isdir(output_name) and \
            not pt.isdir(pt.dirname(pt.abspath(output_name))):
        print("Invalid output folder: " + pt.dirname(pt.abspath(output_name)))
        print("")
        sys.exit(0)

    tempdir = tempfile.mkdtemp()
    ses = lt.session()
    # one could want to set this
    #ses.listen_on(6881, 6882)
    
    # add 'url'. for add_torrent()
    params = {
        'url': magnet,
        'save_path': tempdir,
        'storage_mode': lt.storage_mode_t(2),
        'paused': False,
        'auto_managed': True,
        'duplicate_is_error': True
    }
    # add_magnet_uri is deprecated
    # http://www.rasterbar.com/products/libtorrent/manual.html#add-magnet-uri
    #handle = lt.add_magnet_uri(ses, magnet, params)
    handle = ses.add_torrent(params)

    print("Downloading Metadata (this may take a while)")
    
    # used to control "Maybe..." and "or the" msgs
    # after sleep(1)
    x = 1
    limit = 120
    
    while (not handle.has_metadata()):
        try:
            sleep(1)
            if x > limit:
                print("Maybe your firewall is blocking, ")
                print("     or the magnet link is not right...")
                limit += 30
            x += 1
        except KeyboardInterrupt:
            print("Aborting...")
            ses.pause()
            print("Cleanup dir " + tempdir)
            shutil.rmtree(tempdir)
            sys.exit(0)
    ses.pause()
    print("Done")

    torinfo = handle.get_torrent_info()
    torfile = lt.create_torrent(torinfo)

    output = pt.abspath(torinfo.name() + ".torrent")

    if output_name:
        if pt.isdir(output_name):
            output = pt.abspath(pt.join(
                output_name, torinfo.name() + ".torrent"))
        elif pt.isdir(pt.dirname(pt.abspath(output_name))):
            output = pt.abspath(output_name)

    print("Saving torrent file here : " + output + " ...")
    torcontent = lt.bencode(torfile.generate())
    f = open(output, "wb")
    f.write(lt.bencode(torfile.generate()))
    f.close()
    print("Saved! Cleaning up dir: " + tempdir)
    ses.remove_torrent(handle)
    shutil.rmtree(tempdir)

    return output
    #ses.listen_on(6881, 6891)
    params = {'save_path': TorrentFilePath, 'duplicate_is_error': True}
    link = pyperclip.paste()
    #    link = "magnet:?xt=urn:btih:91AF2B3F172B251E5106146D3DDDCB16C0C631D2&dn=%5B+FreeCourseWeb+%5D+The+Complete+2019+Web+Development+Bootcamp+%28Updated+2-2019%29&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.torrent.eu.org%3A451%2Fannounce&tr=udp%3A%2F%2Fthetracker.org%3A80%2Fannounce&tr=udp%3A%2F%2Fretracker.lanta-net.ru%3A2710%2Fannounce&tr=udp%3A%2F%2Fdenis.stalker.upeer.me%3A6969%2Fannounce&tr=udp%3A%2F%2Fexplodie.org%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.filemail.com%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.iamhansen.xyz%3A2000%2Fannounce&tr=udp%3A%2F%2Fretracker.netbynet.ru%3A2710%2Fannounce&tr=udp%3A%2F%2Ftracker.nyaa.uk%3A6969%2Fannounce&tr=udp%3A%2F%2Ftorrentclub.tech%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.supertracker.net%3A1337%2Fannounce&tr=udp%3A%2F%2Fopen.demonii.si%3A1337%2Fannounce&tr=udp%3A%2F%2Ftracker.moeking.me%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.zer0day.to%3A1337%2Fannounce&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969%2Fannounce&tr=udp%3A%2F%2Fcoppersurfer.tk%3A6969%2Fannounce"
    handle = lt.add_magnet_uri(ses, link, params)
    #    ses.start_dht()
    print 'saving torrent file here : ' + TorrentFilePath2 + " ..."
    while (not handle.has_metadata()):
        time.sleep(.1)

    torinfo = handle.get_torrent_info()

    fs = lt.file_storage()
    for file in torinfo.files():
        fs.add_file(file)
    torfile = lt.create_torrent(fs)
    torfile.set_comment(torinfo.comment())
    torfile.set_creator(torinfo.creator())

    f = open(TorrentFilePath2 + "torrentfile.torrent", "wb")
    f.write(lt.bencode(torfile.generate()))
    f.close()
    print 'saved and closing...'

#Uncomment to Download the Torrent:
#    print 'starting torrent download...'

#    while (handle.status().state != lt.torrent_status.seeding):
#        s = handle.status()
#        time.sleep(55)
#        print 'downloading...'
Beispiel #36
0
    print("Downloading Metadata (this may take a while)")
    while (not handle.has_metadata()):
        try:
            print '.',
            time.sleep(.5)
        except KeyboardInterrupt:
            print("Aborting...")
            ses.pause()
            sys.exit(0)
        except Exception, E:
            traceback.print_exc()

    print("Done")

    torinfo = handle.get_torrent_info()
    torfile = lt.create_torrent(torinfo)

    print(torinfo.name() + ".torrent")

    s = handle.status()
    while (not s.is_seeding):
        s = handle.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)
Beispiel #37
0
def create_torrent_file(file_path_list, params):
    fs = libtorrent.file_storage()

    # filter all non-files
    file_path_list_filtered = []
    for path in file_path_list:
        if not os.path.exists(path):
            raise IOError('Path does not exist: %s' % path)
        elif os.path.isfile(path):
            file_path_list_filtered.append(path)

    # get the directory where these files are in. If there are multiple files, take the common directory they are in
    if len(file_path_list_filtered) == 1:
        base_path = os.path.split(file_path_list_filtered[0])[0]
    else:
        base_path = os.path.abspath(commonprefix(file_path_list_filtered))

    # the base_dir directory is the parent directory of the base_path and is passed to the set_piece_hash method
    base_dir = os.path.split(base_path)[0]

    if len(file_path_list_filtered) == 1:
        filename = os.path.basename(file_path_list_filtered[0])
        fs.add_file(filename, os.path.getsize(file_path_list_filtered[0]))
    else:
        for full_file_path in file_path_list_filtered:
            filename = os.path.join(base_path[len(base_dir) + 1:], full_file_path[len(base_dir):])[1:]
            fs.add_file(filename, os.path.getsize(full_file_path))

    if params.get('piece length'):
        piece_size = params['piece length']
    else:
        piece_size = 0

    flags = libtorrent.create_torrent_flags_t.optimize

    # This flag doesn't exist anymore in libtorrent V1.1.0
    if hasattr(libtorrent.create_torrent_flags_t, 'calculate_file_hashes'):
        flags |= libtorrent.create_torrent_flags_t.calculate_file_hashes

    torrent = libtorrent.create_torrent(fs, piece_size=piece_size, flags=flags)
    if params.get('comment'):
        torrent.set_comment(params['comment'])
    if params.get('created by'):
        torrent.set_creator(params['created by'])
    # main tracker
    if params.get('announce'):
        torrent.add_tracker(params['announce'])
    # tracker list
    if params.get('announce-list'):
        tier = 1
        for tracker in params['announce-list']:
            torrent.add_tracker(tracker, tier=tier)
            tier += 1
    # DHT nodes
    # http://www.bittorrent.org/beps/bep_0005.html
    if params.get('nodes'):
        for node in params['nodes']:
            torrent.add_node(*node)
    # HTTP seeding
    # http://www.bittorrent.org/beps/bep_0017.html
    if params.get('httpseeds'):
        torrent.add_http_seed(params['httpseeds'])

    # Web seeding
    # http://www.bittorrent.org/beps/bep_0019.html
    if len(file_path_list) == 1:
        if params.get('urllist', False):
            torrent.add_url_seed(params['urllist'])

    # read the files and calculate the hashes
    if len(file_path_list) == 1:
        libtorrent.set_piece_hashes(torrent, base_path)
    else:
        libtorrent.set_piece_hashes(torrent, base_dir)

    t1 = torrent.generate()
    torrent = libtorrent.bencode(t1)

    postfix = u'.torrent'

    if params.get('name'):
        if not isinstance(params['name'], unicode):
            params['name'] = unicode(params['name'], 'utf-8')
        torrent_file_name = os.path.join(base_path, params['name'] + postfix)
    else:
        torrent_file_name = os.path.join(base_path, unicode(t1['info']['name'], 'utf-8') + postfix)
    with open(torrent_file_name, 'wb') as f:
        f.write(torrent)

    return {'success': True,
            'base_path': base_path,
            'base_dir': base_dir,
            'torrent_file_path': torrent_file_name}
Beispiel #38
0
                              ]) and alert.info_hash not in seen:
            seen.add(alert.info_hash)
            torrent_params = get_params_for_info_hash(alert.info_hash)
            handles.add(session.add_torrent(torrent_params))

    # TODO: https://www.libtorrent.org/reference-Core.html#post_torrent_updates()
    to_remove = set()
    for handle in handles:
        status = handle.status()
        if (status.has_metadata):
            metadata = status.torrent_file
            print('<ut_metadata> {} ({})'.format(metadata.name(),
                                                 handle.info_hash()),
                  flush=True)
            with open(metadata.name() + '.torrent', 'wb') as f:
                f.write(lt.bencode(lt.create_torrent(metadata).generate()))
            meta_info_count += 1
            to_remove.add(handle)
        else:
            print('{} - {} peers ({} connected), prio {}, {} {:2}%'.format(
                status.info_hash, status.list_peers, status.num_peers,
                status.queue_position, state_str[status.state],
                status.progress * 100),
                  flush=True)

    for handle in to_remove:
        session.remove_torrent(handle)

    handles -= to_remove

    time.sleep(10)
Beispiel #39
0
 def file_complete(self, torrent):
     info_hash = str(torrent.info_hash())
     nt = lt.create_torrent(torrent)
     tname = self._tname(info_hash)
     with open(tname, 'wb') as f:
         f.write(lt.bencode(nt.generate()))
Beispiel #40
0
def create_torrent(path,
                   trackers=(),
                   private=True,
                   pieces=None,
                   piece_size=None,
                   source=None,
                   predicate=None,
                   progress=None,
                   min_piece_exp=14,
                   max_piece_exp=24):
    # type: (Path, Iterable[str], Optional[int], Optional[int], int, Optional[str], Optional[Callable], Optional[Callable], int, int) -> bytes
    """
		min_piece_exp: default 16k
		max_piece_exp: default 16M
	"""

    if pieces is None and piece_size is None:
        pieces = 1000
    elif pieces is None and piece_size is not None:
        if piece_size % 16384 != 0:
            raise ValueError("piece_size must be a multiple of 16384")
    elif pieces is not None and piece_size is None:
        pass
    else:
        raise ValueError("Cannot specify `pieces` and `piece_size`")

    fs = libtorrent.file_storage()

    path = path.resolve()

    if path.is_file():
        fs.add_file(path.name, path.stat().st_size)

    elif path.is_dir():
        predicate = predicate or (lambda path: True)
        libtorrent.add_files(fs, fspath(path), predicate)
    else:
        assert False, "Path neither file nor directory"

    if pieces:
        piece_size = 2**min(
            max(round(log2(fs.total_size() / pieces)), min_piece_exp),
            max_piece_exp)
        logger.info("piece_size", piece_size)

    t = libtorrent.create_torrent(fs, piece_size)

    for tracker in trackers:
        t.add_tracker(tracker)
    if private:
        t.set_priv(private)

    libtorrent.set_piece_hashes(
        t, fspath(path.parent),
        partial(progress, ceil(fs.total_size() / piece_size)))

    d = t.generate()
    if source:
        d[b"info"][b"source"] = source.encode("utf-8")

    return libtorrent.bencode(d)
Beispiel #41
0
def play(url, is_view=None):

    # -- Necesario para algunas webs -------------------------- -
    if not url.endswith(".torrent") and not url.startswith("magnet"):
        t_file = scrapertools.get_header_from_response(
            url, header_to_get="location")
        if len(t_file) > 0:
            url = t_file
            t_file = scrapertools.get_header_from_response(
                url, header_to_get="location")
        if len(t_file) > 0:
            url = t_file

    # -- Crear dos carpetas en descargas para los archivos ------
    save_path_videos = os.path.join(config.get_setting("downloadpath"),
                                    "torrent-videos")
    save_path_torrents = os.path.join(config.get_setting("downloadpath"),
                                      "torrent-torrents")
    if not os.path.exists(save_path_torrents): os.mkdir(save_path_torrents)

    # -- Usar - archivo torrent desde web, meagnet o HD ---------
    if not os.path.isfile(url) and not url.startswith("magnet"):
        # -- http - crear archivo torrent -----------------------
        data = url_get(url)
        # -- El nombre del torrent será el que contiene en los --
        # -- datos.                                             -
        #re_name_len = int( scrapertools.get_match(data,':name(\d+):') )
        re_name = library.title_to_folder_name(
            urllib.unquote(scrapertools.get_match(data, ':name\d+:(.*?)\d+:')))
        torrent_file = os.path.join(save_path_torrents, re_name + '.torrent')

        f = open(torrent_file, 'wb')
        f.write(data)
        f.close()
    elif os.path.isfile(url):
        # -- file - para usar torrens desde el HD ---------------
        torrent_file = url
    else:
        # -- magnet ---------------------------------------------
        torrent_file = url
    # -----------------------------------------------------------

    # -- MCT - MiniClienteTorrent -------------------------------
    ses = lt.session()

    print "#########################"
    print lt.version
    print "#########################"

    ses.add_dht_router("router.bittorrent.com", 6881)
    ses.add_dht_router("router.utorrent.com", 6881)
    ses.add_dht_router("router.bitcomet.com", 554)
    ses.add_dht_router("dht.transmissionbt.com", 6881)

    trackers = [
        "http://exodus.desync.com:6969/announce",
        "udp://tracker.publicbt.com:80/announce",
        "udp://tracker.openbittorrent.com:80/announce",
        "http://tracker.torrentbay.to:6969/announce",
        "http://fr33dom.h33t.com:3310/announce",
        "http://tracker.pow7.com/announce",
        "udp://tracker.ccc.de:80/announce",
        "http://tracker.bittorrent.am:80/announce",
        "http://denis.stalker.h3q.com:6969/announce",
        "udp://tracker.prq.to:80/announce",
        "udp://tracker.istole.it:80/announce",
        "udp://open.demonii.com:1337",
        "http://9.rarbg.com:2710/announce",
        "http://announce.torrentsmd.com:6969/announce",
        "http://bt.careland.com.cn:6969/announce",
        "http://explodie.org:6969/announce",
        "http://mgtracker.org:2710/announce",
        "http://tracker.best-torrents.net:6969/announce",
        "http://tracker.tfile.me/announce",
        "http://tracker.torrenty.org:6969/announce",
        "http://tracker1.wasabii.com.tw:6969/announce",
        "udp://9.rarbg.com:2710/announce",
        "udp://9.rarbg.me:2710/announce",
        "udp://coppersurfer.tk:6969/announce",
        "udp://tracker.btzoo.eu:80/announce",
        "http://www.spanishtracker.com:2710/announce",
        "http://www.todotorrents.com:2710/announce",
    ]

    video_file = ""
    ## -- magnet2torrent ----------------------------------------
    if torrent_file.startswith("magnet"):
        tempdir = tempfile.mkdtemp()
        params = {
            'save_path': tempdir,
            'trackers': trackers,
            'storage_mode': lt.storage_mode_t.storage_mode_allocate,
            'paused': False,
            'auto_managed': True,
            'duplicate_is_error': True
        }
        h = lt.add_magnet_uri(ses, torrent_file, params)
        dp = xbmcgui.DialogProgress()
        dp.create('pelisalacarta-MCT')
        while not h.has_metadata():
            message, porcent, msg_file, s, download = getProgress(
                h, "Creando torrent desde magnet")
            dp.update(porcent, message, msg_file)
        dp.close()
        info = h.get_torrent_info()
        data = lt.bencode(lt.create_torrent(info).generate())
        torrent_file = os.path.join(save_path_torrents,
                                    info.name() + ".torrent")
        f = open(torrent_file, 'wb')
        f.write(data)
        f.close()
        ses.remove_torrent(h)
        shutil.rmtree(tempdir)
    # -----------------------------------------------------------

    # -- Archivos torrent ---------------------------------------
    e = lt.bdecode(open(torrent_file, 'rb').read())
    info = lt.torrent_info(e)

    # -- El más gordo o uno de los más gordo se entiende que es -
    # -- el vídeo o es el vídeo que se usará como referencia    -
    # -- para el tipo de archivo                                -
    _index_file, _video_file, _size_file = get_video_file(info)

    if not _video_file.endswith('.avi'):
        print "##### storage_mode_t.storage_mode_sparse (no avi) #####"
        h = ses.add_torrent({
            'ti':
            info,
            'save_path':
            save_path_videos,
            'trackers':
            trackers,
            'storage_mode':
            lt.storage_mode_t.storage_mode_sparse
        })
    else:
        print "##### storage_mode_t.storage_mode_allocate (avi) #####"
        h = ses.add_torrent({
            'ti':
            info,
            'save_path':
            save_path_videos,
            'trackers':
            trackers,
            'storage_mode':
            lt.storage_mode_t.storage_mode_allocate
        })
    ## ----------------------------------------------------------

    # -- Prioritarizar ------------------------------------------
    _index, video_file, video_size = get_video_files_sizes(info)
    if _index == -1:
        _index = _index_file
        video_file = _video_file
        video_size = _size_file

    piece_set = set_priority_pieces(h,
                                    _index,
                                    video_file,
                                    video_size,
                                    _log=True)

    # -- Descarga secuencial - trozo 1, trozo 2, ... ------------
    #h.set_sequential_download(True)

    h.force_reannounce()
    h.force_dht_announce()

    # -- Crear diálogo de progreso para el primer bucle ---------
    dp = xbmcgui.DialogProgress()
    dp.create('pelisalacarta-MCT')

    # -- Para log -----------------------------------------------
    timer = time.time()

    # -- Local con el número de piezas por cluster global -------
    _cluster_pieces = __pieces__
    '''
    # -- Estimar cuando se comenzará el visionado ---------------
    # -- Pruebas: Porcentaje fijo                               -
    porcentage_to_play = 1.50
    '''

    # -- Estimar cuando se comenzará el visionado ---------------
    # -- Pruebas: Porcentaje segun tamaño cuando sólo hay un    -
    # -- vídeo en torrent                                       -
    porcentage_to_play = set_porcentage_to_play(video_size)

    # -- Doble bucle anidado ------------------------------------
    # -- Descarga - Primer bucle                                -
    while not h.is_seed():
        s = h.status()

        xbmc.sleep(100)

        # -- Recuperar los datos del progreso -------------------
        message, porcent, msg_file, s, download = getProgress(h, video_file)

        # -- Si hace 'checking' existe descarga -----------------
        # -- 'download' Se usará para saber si hay datos        -
        # -- descargados para el diálogo de 'remove_files'      -
        if s.state == 1: download = 1

        # -- Añadido: Log para las pruebas ----------------------
        # -- Print log have_piece. Procedimiento al final del   -
        # -- archivo                                            -
        timer2 = time.time() - timer
        if timer2 > time_sleep:
            print_have_piece_set(h, piece_set)
            timer = time.time()

            # -- Print log y procedimiento incrementar cluster --
            # -- Parte 1                                        -
            _cluster = False
            if _cluster_pieces < len(piece_set):
                _cluster = \
                    cluster_stat(
                        h,
                        piece_set[_cluster_pieces - __pieces__],
                        piece_set[_cluster_pieces],
                        _log=True
                    )
            else:
                _cluster_pieces = len(piece_set) - 1
            # -- Parte 2                                        -
            if _cluster:
                _cluster_pieces += __pieces__
                cluster_set(h, _cluster_pieces, piece_set, 7, _log=True)

                _cluster_pieces2 = _cluster_pieces + __pieces__
                cluster_set(h, _cluster_pieces2, piece_set, 6, _log=True)

                _cluster_pieces3 = _cluster_pieces2 + __pieces__
                cluster_set(h, _cluster_pieces3, piece_set, 5, _log=True)

        # -- Player - play --------------------------------------
        option_view = ((s.progress * 100) >= porcentage_to_play)

        # -- Modificado: Se tendrá en cuenta el primer cluster --
        # -- completado para el inicio de la reproducción       -
        first_cluster = True
        _p = "##### first_cluster: "
        for i in range(piece_set[0], piece_set[__pieces__]):
            _p += "[%s:%s]" % (i, h.have_piece(i))
            first_cluster &= h.have_piece(i)
        print _p

        if (option_view and is_view != "Ok" and s.state == 3
                and first_cluster):
            print "##### porcentage_to_play ## %s ##" % porcentage_to_play

            is_view = "Ok"
            dp.close()

            # -- Player - Ver el vídeo --------------------------
            player = play_video()
            player.play(os.path.join(save_path_videos, video_file))

            # -- Segundo bucle - Player - Control de eventos ----
            while player.isPlaying():
                xbmc.sleep(100)

                # -- Añadido: Log para las pruebas --------------
                # -- Print log have_piece. Procedimiento al     -
                # -- final del archivo                          -
                timer2 = time.time() - timer
                if timer2 > time_sleep:
                    print_have_piece_set(h, piece_set)
                    timer = time.time()

                    # -- Print log y procedimiento incrementar --
                    # -- cluster                                -
                    # -- Parte 1                                -
                    _cluster = False
                    if _cluster_pieces < len(piece_set):
                        _cluster = \
                            cluster_stat(
                                h,
                                piece_set[_cluster_pieces - __pieces__],
                                piece_set[_cluster_pieces],
                                _log=True
                            )
                    else:
                        _cluster_pieces = len(piece_set) - 1
                    # -- Parte 2                                -
                    if _cluster:
                        _cluster_pieces += __pieces__
                        cluster_set(h,
                                    _cluster_pieces,
                                    piece_set,
                                    7,
                                    _log=True)

                        _cluster_pieces2 = _cluster_pieces + __pieces__
                        cluster_set(h,
                                    _cluster_pieces2,
                                    piece_set,
                                    6,
                                    _log=True)

                        _cluster_pieces3 = _cluster_pieces2 + __pieces__
                        cluster_set(h,
                                    _cluster_pieces3,
                                    piece_set,
                                    5,
                                    _log=True)

                # -- Cerrar el diálogo de progreso --------------
                if player.resumed:
                    dp.close()

                # -- Mostrar el diálogo de progreso -------------
                if player.paused:
                    # -- Crear diálogo si no existe -------------
                    if not player.statusDialogoProgress:
                        dp = xbmcgui.DialogProgress()
                        dp.create('pelisalacarta-MCT')
                        player.setDialogoProgress()

                    # -- Diálogos de estado en el visionado -----
                    if not h.is_seed():
                        # -- Recuperar los datos del progreso ---
                        message, porcent, msg_file, s, download = getProgress(
                            h, video_file)
                        dp.update(porcent, message, msg_file)
                    else:
                        dp.update(100, "Descarga completa: " + video_file)

                    # -- Se canceló el progreso en el visionado -
                    # -- Continuar                              -
                    if dp.iscanceled():
                        dp.close()
                        player.pause()

                    # -- El usuario cancelo el visionado --------
                    # -- Terminar                               -
                    if player.ended:
                        if info.num_files() > 1:
                            _index, video_file, video_size = get_video_files_sizes(
                                info)
                            if _index == -1:
                                # -- Diálogo eliminar archivos ----------
                                remove_files(download, torrent_file,
                                             video_file, ses, h)
                                return
                            else:
                                set_porcentage_to_play(video_size)
                                piece_set = set_priority_pieces(h,
                                                                _index,
                                                                video_file,
                                                                video_size,
                                                                _log=True)
                        else:
                            # -- Diálogo eliminar archivos ----------
                            remove_files(download, torrent_file, video_file,
                                         ses, h)
                            return

        # -- Kodi - Se cerró el visionado -----------------------
        # -- Continuar | Terminar                               -
        if is_view == "Ok" and not xbmc.Player().isPlaying():

            # -- Diálogo continuar o terminar -------------------
            d = xbmcgui.Dialog()
            ok = d.yesno('pelisalacarta-MCT', 'XBMC-Kodi Cerró el vídeo.',
                         '¿Continuar con la sesión?')

            # -- SI -------------------------------------------------
            if ok:
                # -- Continuar --------------------------------------
                is_view = None
            else:
                # -- Terminar ---------------------------------------
                if info.num_files() > 1:
                    _index, video_file, video_size = get_video_files_sizes(
                        info)
                    if _index == -1:
                        # -- Diálogo eliminar archivos                      -
                        remove_files(download, torrent_file, video_file, ses,
                                     h)
                        return
                    else:
                        set_porcentage_to_play(video_size)
                        piece_set = set_priority_pieces(h,
                                                        _index,
                                                        video_file,
                                                        video_size,
                                                        _log=True)
                        is_view = None
                        dp = xbmcgui.DialogProgress()
                        dp.create('pelisalacarta-MCT')
                else:
                    # -- Diálogo eliminar archivos ----------
                    remove_files(download, torrent_file, video_file, ses, h)
                    return

        # -- Mostar progeso antes del visionado -----------------
        if is_view != "Ok":
            dp.update(porcent, message, msg_file)

        # -- Se canceló el progreso antes del visionado ---------
        # -- Terminar                                           -
        if dp.iscanceled():
            dp.close()
            if info.num_files() > 1:
                _index, video_file, video_size = get_video_files_sizes(info)
                if _index == -1:
                    # -- Diálogo eliminar archivos                      -
                    remove_files(download, torrent_file, video_file, ses, h)
                    return
                else:
                    set_porcentage_to_play(video_size)
                    piece_set = set_priority_pieces(h,
                                                    _index,
                                                    video_file,
                                                    video_size,
                                                    _log=True)
                    is_view = None
                    dp = xbmcgui.DialogProgress()
                    dp.create('pelisalacarta-MCT')
            else:
                # -- Diálogo eliminar archivos ----------
                remove_files(download, torrent_file, video_file, ses, h)
                return

    # -- Kodi - Error? - No debería llegar aquí -----------------
    if is_view == "Ok" and not xbmc.Player().isPlaying():
        dp.close()
        # -- Diálogo eliminar archivos --------------------------
        remove_files(download, torrent_file, video_file, ses, h)

    return
Beispiel #42
0
    def parse_magnet_uri(
        self,
        magnet_uri,
        scrape=None,
        use_dht=None,
        timeout=None,
        trackers=None,
        no_cache=None,
        n_try=None,
        to_torrent=None,
        http_proxy=None,
    ):
        try:
            import libtorrent as lt
        except ImportError as _e:
            raise ImportError("libtorrent package required") from _e

        # default function arguments from db
        if scrape is None:
            scrape = ModelSetting.get_bool("scrape")
        if use_dht is None:
            use_dht = ModelSetting.get_bool("use_dht")
        if timeout is None:
            timeout = ModelSetting.get_int("timeout")
        if trackers is None:
            trackers = json.loads(ModelSetting.get("trackers"))
        if n_try is None:
            n_try = ModelSetting.get_int("n_try")
        if no_cache is None:
            no_cache = False
        if to_torrent is None:
            to_torrent = False
        if http_proxy is None:
            http_proxy = ModelSetting.get("http_proxy")

        # parameters
        params = lt.parse_magnet_uri(magnet_uri)

        # prevent downloading
        # https://stackoverflow.com/q/45680113
        if isinstance(params, dict):
            params["flags"] |= lt.add_torrent_params_flags_t.flag_upload_mode
        else:
            params.flags |= lt.add_torrent_params_flags_t.flag_upload_mode

        lt_version = [int(v) for v in lt.version.split(".")]
        if [0, 16, 13, 0] < lt_version < [1, 1, 3, 0]:
            # for some reason the info_hash needs to be bytes but it's a struct called sha1_hash
            if isinstance(params, dict):
                params["info_hash"] = params["info_hash"].to_bytes()
            else:
                params.info_hash = params.info_hash.to_bytes()

        # 캐시에 있으면...
        info_hash_from_magnet = str(params["info_hash"] if isinstance(params, dict) else params.info_hash)
        self.cache_init()
        if (not no_cache) and (info_hash_from_magnet in self.torrent_cache):
            return self.torrent_cache[info_hash_from_magnet]["info"]

        # add trackers
        if isinstance(params, dict):
            if len(params["trackers"]) == 0:
                params["trackers"] = trackers
        else:
            if len(params.trackers) == 0:
                params.trackers = trackers

        # session
        settings = {
            # basics
            # 'user_agent': 'libtorrent/' + lt.__version__,
            "listen_interfaces": "0.0.0.0:6881",
            # dht
            "enable_dht": use_dht,
            "use_dht_as_fallback": True,
            "dht_bootstrap_nodes": "router.bittorrent.com:6881,dht.transmissionbt.com:6881,router.utorrent.com:6881,127.0.0.1:6881",
            "enable_lsd": False,
            "enable_upnp": True,
            "enable_natpmp": True,
            "announce_to_all_tiers": True,
            "announce_to_all_trackers": True,
            "aio_threads": 4 * 2,
            "checking_mem_usage": 1024 * 2,
        }
        if http_proxy:
            proxy_url = urlparse(http_proxy)
            settings.update(
                {
                    "proxy_username": proxy_url.username,
                    "proxy_password": proxy_url.password,
                    "proxy_hostname": proxy_url.hostname,
                    "proxy_port": proxy_url.port,
                    "proxy_type": lt.proxy_type_t.http_pw
                    if proxy_url.username and proxy_url.password
                    else lt.proxy_type_t.http,
                    "force_proxy": True,
                    "anonymous_mode": True,
                }
            )
        session = lt.session(settings)

        session.add_extension("ut_metadata")
        session.add_extension("ut_pex")
        session.add_extension("metadata_transfer")

        # handle
        handle = session.add_torrent(params)

        if use_dht:
            handle.force_dht_announce()

        max_try = max(n_try, 1)
        for tryid in range(max_try):
            timeout_value = timeout
            logger.debug("Trying to get metadata... %d/%d", tryid + 1, max_try)
            while not handle.has_metadata():
                time.sleep(0.1)
                timeout_value -= 0.1
                if timeout_value <= 0:
                    break

            if handle.has_metadata():
                lt_info = handle.get_torrent_info()
                logger.debug(
                    "Successfully got metadata after %d*%d+%.2f seconds", tryid, timeout, timeout - timeout_value
                )
                time_metadata = tryid * timeout + (timeout - timeout_value)
                break
            if tryid + 1 == max_try:
                session.remove_torrent(handle, True)
                raise Exception(f"Timed out after {max_try}*{timeout} seconds")

        # create torrent object and generate file stream
        torrent = lt.create_torrent(lt_info)
        torrent.set_creator(f"libtorrent v{lt.version}")  # signature
        torrent_dict = torrent.generate()

        torrent_info = self.convert_torrent_info(lt_info)
        torrent_info.update(
            {
                "trackers": params.trackers if not isinstance(params, dict) else params["trackers"],
                "creation_date": datetime.fromtimestamp(torrent_dict[b"creation date"]).isoformat(),
                "time": {"total": time_metadata, "metadata": time_metadata},
            }
        )

        if scrape:
            # start scraping
            for tryid in range(max_try):
                timeout_value = timeout
                logger.debug("Trying to get peerinfo... %d/%d", tryid + 1, max_try)
                while handle.status(0).num_complete < 0:
                    time.sleep(0.1)
                    timeout_value -= 0.1
                    if timeout_value <= 0:
                        break

                if handle.status(0).num_complete >= 0:
                    torrent_status = handle.status(0)
                    logger.debug(
                        "Successfully got peerinfo after %d*%d+%.2f seconds", tryid, timeout, timeout - timeout_value
                    )
                    time_scrape = tryid * timeout + (timeout - timeout_value)

                    torrent_info.update(
                        {
                            "seeders": torrent_status.num_complete,
                            "peers": torrent_status.num_incomplete,
                        }
                    )
                    torrent_info["time"]["scrape"] = time_scrape
                    torrent_info["time"]["total"] = torrent_info["time"]["metadata"] + torrent_info["time"]["scrape"]
                    break
                if tryid + 1 == max_try:
                    logger.error("Timed out after %d*%d seconds", max_try, timeout)

        session.remove_torrent(handle, True)

        # caching for later use
        self.torrent_cache[torrent_info["info_hash"]] = {
            "info": torrent_info,
        }
        if to_torrent:
            return lt.bencode(torrent_dict), pathscrub(lt_info.name(), os="windows", filename=True)
        return torrent_info
Beispiel #43
0
        dp.create('Alfa-MCT')
        while not h.has_metadata():
            message, porcent, msg_file, s, download = getProgress(
                h, "Creando torrent desde magnet")
            dp.update(porcent, message, msg_file)
            if s.state == 1: download = 1
            if dp.iscanceled():
                dp.close()
                remove_files(download, torrent_file, video_file, ses, h)
                return
            h.force_dht_announce()
            xbmc.sleep(1000)

        dp.close()
        info = h.get_torrent_info()
        data = lt.bencode(lt.create_torrent(info).generate())

        #torrent_file = os.path.join(save_path_torrents, unicode(info.name()+"-"+btih, "'utf-8'", errors="replace") + ".torrent")
        torrent_file = os.path.join(save_path_torrents,
                                    info.name() + "-" + btih + ".torrent")
        f = open(torrent_file, 'wb')
        f.write(data)
        f.close()
        ses.remove_torrent(h)
        shutil.rmtree(tempdir)
    # -----------------------------------------------------------

    # -- Archivos torrent ---------------------------------------
    e = lt.bdecode(open(torrent_file, 'rb').read())
    info = lt.torrent_info(e)
 def test_priv(self) -> None:
     fs = lt.file_storage()
     fs.add_file("test.txt", 1024)
     ct = lt.create_torrent(fs)
     ct.set_priv(True)
     self.assertTrue(ct.priv())
Beispiel #45
0
    for f in files:
        # skip files starting with .
        if f[0] == '.':
            continue

        # skip thumbs.db on windows
        if f == 'Thumbs.db':
            continue

        fname = os.path.join(root[len(parent_input) + 1:], f)
        size = os.path.getsize(os.path.join(parent_input, fname))
        print('%10d kiB  %s' % (size / 1024, fname))
        fs.add_file(fname, size)

if fs.num_files() == 0:
    print('no files added')
    sys.exit(1)

t = libtorrent.create_torrent(fs, 0, 4 * 1024 * 1024)

t.add_tracker(sys.argv[2])
t.set_creator('libtorrent %s' % libtorrent.__version__)

libtorrent.set_piece_hashes(t, parent_input, lambda x: sys.stdout.write('.'))
sys.stdout.write('\n')

f = open('out.torrent', 'wb+')
f.write(libtorrent.bencode(t.generate()))
f.close()
Beispiel #46
0
    def run(self):
        """run the converter.

        using the class attribute initiated at init function.

        Returns:
            Filename of created torrent.

        Raises:
            KeyboardInterrupt: This error caused by user to stop this.
            When downloading metadata from magnet link,
            it requires an additional step before the error reraised again.
        """
        print("Downloading Metadata (this may take a while)")

        # used to control "Maybe..." and "or the" msgs after sleep(1)
        wait_time = 1
        soft_limit = 120
        while not self.handle.has_metadata():
            try:
                sleep(1)
                if wait_time > soft_limit:
                    print(
                        "Downloading is taking a while, maybe there is an "
                        "issue with the magnet link or your network connection"
                    )
                    soft_limit += 30
                wait_time += 1
            except KeyboardInterrupt:
                print("\nAborting...")
                self.ses.pause()
                print("Cleanup dir " + self.tempdir)
                shutil.rmtree(self.tempdir)
                raise
        self.ses.pause()
        print("Done")

        torinfo = self.handle.get_torrent_info()
        torfile = lt.create_torrent(torinfo)

        output = pt.abspath(torinfo.name() + ".torrent")

        if self.output_name:
            if pt.isdir(self.output_name):
                output = pt.abspath(
                    pt.join(self.output_name,
                            torinfo.name() + ".torrent"))
            elif pt.isdir(pt.dirname(pt.abspath(self.output_name))):
                output = pt.abspath(self.output_name)
        else:
            output = pt.abspath(torinfo.name() + ".torrent")

        print("Saving torrent file here : " + output + " ...")
        with open(output, "wb") as outfile:
            torcontent = lt.bencode(torfile.generate())
            outfile.write(torcontent)

        print("Saved! Cleaning up dir: " + self.tempdir)
        self.ses.remove_torrent(self.handle)
        shutil.rmtree(self.tempdir)

        return output
Beispiel #47
0
import binascii

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

magnet = lt.parse_magnet_uri(sys.argv[1])
e = str(magnet.info_hash)
if len(e) == 40:
    info_hash = binascii.unhexlify(e)
elif len(e) == 32:
    info_hash = base64.b32decode(e)
else:
    raise Exception("Unable to parse infohash")

trackers = magnet.trackers
h = ses.add_torrent({'info_hash': info_hash, 'trackers': trackers})
dots=0
while not h.has_metadata():
    dots += 1
    sys.stdout.write('.')
    sys.stdout.flush()
    time.sleep(1)
if (dots): sys.stdout.write('\n')
ses.pause()
tinf = h.get_torrent_info()
f = open('/tmp/b2pgen' + '.torrent', 'wb')
f.write(lt.bencode(
    lt.create_torrent(tinf).generate()))
f.close()
ses.remove_torrent(h)
 def test_num_pieces(self) -> None:
     fs = lt.file_storage()
     fs.add_file("test.txt", 1024)
     ct = lt.create_torrent(fs)
     self.assertEqual(ct.num_pieces(), 1)
Beispiel #49
0
    def run(self):
        """
        Called every few seconds to handle any running/finished torrents
        """

        if not LIBTORRENT_AVAILABLE:
            return

        if not self.loadedRunningTorrents:
            torrent_save_file = _get_running_torrents_pickle_path(False)
            if os.path.isfile(torrent_save_file):
                logger.log(
                    u'Saved torrents found in %s, loading' %
                    (torrent_save_file), logger.DEBUG)
                _load_saved_torrents()

            self.loadedRunningTorrents = True

        sess = _get_session(False)
        if sess is not None:
            while 1:
                a = sess.pop_alert()
                if not a: break

                if type(a) == str:
                    logger.log(a, logger.DEBUG)
                else:
                    logger.log(
                        u'(%s): %s' %
                        (type(a).__name__,
                         ek.fixStupidEncodings(a.message(), True)),
                        logger.DEBUG)

            logTorrentStatus = (time.time() -
                                self.lastTorrentStatusLogTS) >= 600

            for torrent_data in running_torrents:
                if torrent_data['handle'].has_metadata():
                    ti = torrent_data['handle'].get_torrent_info()
                    name = ti.name()
                    torrent_data['name'] = name
                    torrent_data['total_size'] = ti.total_size()

                    if not torrent_data['have_torrentFile']:
                        # if this was a magnet or url, and we now have downloaded the metadata
                        # for it, best to save it locally in case we need to resume
                        ti = torrent_data['handle'].get_torrent_info()
                        torrentFile = lt.create_torrent(ti)
                        torrent_data['torrent'] = lt.bencode(
                            torrentFile.generate())
                        torrent_data['have_torrentFile'] = True
                        logger.log(
                            u'Created torrent file for %s as metadata d/l is now complete'
                            % (name), logger.DEBUG)

                else:
                    name = '-'

                s = torrent_data['handle'].status()
                torrent_data['status'] = str(s.state)
                torrent_data['progress'] = s.progress
                torrent_data['rate_down'] = s.download_rate
                torrent_data['rate_up'] = s.upload_rate
                torrent_data['paused'] = s.paused
                torrent_data['error'] = s.error

                #currentRatio = 0.0 if s.total_download == 0 else float(s.total_upload)/float(s.total_download)
                currentRatio = 0.0 if s.all_time_download == 0 else float(
                    s.all_time_upload) / float(s.all_time_download)
                torrent_data['ratio'] = currentRatio

                if s.state in [
                        lt.torrent_status.seeding, lt.torrent_status.finished
                ]:
                    with torrent_data['lock']:
                        # this is the post-processing & removing code, so make sure that there's
                        # only one thread doing either here, as the two could easily interfere with
                        # one another
                        if not torrent_data['post_processed']:
                            # torrent has just completed download, so we need to do
                            # post-processing on it.
                            ti = torrent_data['handle'].get_torrent_info()
                            any_file_success = False
                            for f in ti.files():
                                fullpath = os.path.join(
                                    sickbeard.LIBTORRENT_WORKING_DIR, 'data',
                                    f.path)
                                logger.log(
                                    u'Post-processing "%s"' % (fullpath),
                                    logger.DEBUG)
                                if isMediaFile(fullpath):
                                    logger.log(u'this is a media file',
                                               logger.DEBUG)
                                    try:
                                        processor = postProcessor.PostProcessor(
                                            fullpath, name)
                                        if processor.process(
                                                forceKeepOriginalFiles=True):
                                            logger.log(
                                                u'Success post-processing "%s"'
                                                % (fullpath), logger.DEBUG)
                                            any_file_success = True
                                    except exceptions.PostProcessingFailed, e:
                                        logger.log(
                                            u'Failed post-processing file "%s" with error "%s"'
                                            % (fullpath, ex(e)), logger.ERROR)

                            if not any_file_success:
                                logger.log(
                                    u'When post-processing the completed torrent %s, no useful files were found.'
                                    % (name), logger.ERROR)

                            torrent_data['post_processed'] = True
                        else:
                            # post-processing has already been performed.  So we just
                            # need to ensure check the ratio and delete the torrent
                            # if we're good.
                            if currentRatio >= sickbeard.LIBTORRENT_SEED_TO_RATIO:
                                logger.log(
                                    u'Torrent "%s" has seeded to ratio %f.  Removing it.'
                                    % (name, currentRatio), logger.MESSAGE)
                                deleteFilesToo = True
                                if not torrent_data['post_processed']:
                                    logger.log(
                                        u'Torrent has not been post_processed.  Keeping files.',
                                        logger.MESSAGE)
                                    deleteFilesToo = False
                                _remove_torrent_by_handle(
                                    torrent_data['handle'], deleteFilesToo)
                            else:
                                if logTorrentStatus:
                                    self.lastTorrentStatusLogTS = time.time()
                                    logger.log(
                                        u'"%s" seeding %0.3f' %
                                        (name, currentRatio), logger.DEBUG)
                elif s.state == lt.torrent_status.downloading:
                    if logTorrentStatus:
                        self.lastTorrentStatusLogTS = time.time()
                        logger.log(
                            u'"%s" downloading %0.2f' %
                            (name, s.progress * 100.0), logger.DEBUG)
 def test_piece_length(self) -> None:
     fs = lt.file_storage()
     fs.add_file("test.txt", 1024)
     ct = lt.create_torrent(fs)
     self.assertEqual(ct.piece_length(), 16384)
Beispiel #51
0
print data

# collect sha1
piece_hash = re.findall(r'^sha1: (.*)$', data, re.M)
print piece_hash

# collect offset and length
offset = re.findall(r'^offset: (.*)$', data, re.M)
length = re.findall(r'^length: (.*)$', data, re.M)

print "offset: ", offset
print "length: ", length

fs = lt.file_storage()
fs.set_piece_length(16 * 1024 * 1024)  # 16MiB

for o, l in zip(offset, length):
    fs.add_file("a/" + o, int(l, 16))

torrent = lt.create_torrent(fs, 16 * 1024 * 1024, flags=0)
torrent.set_creator("Test")

print "file:", torrent.files().num_files()
print torrent.files().file_path(0)

for index, h in enumerate(piece_hash):
    torrent.set_hash(index, h.decode('hex'))

with open('a.torrent', 'wb') as f:
    f.write(lt.bencode(torrent.generate()))
 def test_http_seed_deprecated(self) -> None:
     fs = lt.file_storage()
     ct = lt.create_torrent(fs)
     with self.assertWarns(DeprecationWarning):
         ct.add_http_seed("http://example.com")
Beispiel #53
0
def play(url, xlistitem={}, is_view=None, subtitle="", item=None):
    allocate = True
    try:
        import platform
        xbmc.log(
            "XXX KODI XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
        )
        xbmc.log("OS platform: %s %s" %
                 (platform.system(), platform.release()))
        xbmc.log("xbmc/kodi version: %s" %
                 xbmc.getInfoLabel("System.BuildVersion"))
        xbmc_version = int(xbmc.getInfoLabel("System.BuildVersion")[:2])
        xbmc.log("xbmc/kodi version number: %s" % xbmc_version)
        xbmc.log(
            "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX KODI XXXX"
        )

        _platform = get_platform()
        if str(_platform['system']) in [
                "android_armv7", "linux_armv6", "linux_armv7"
        ]:
            allocate = False
        # -- log ------------------------------------------------
        xbmc.log(
            "XXX platform XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
        )
        xbmc.log("_platform['system']: %s" % _platform['system'])
        xbmc.log("allocate: %s" % allocate)
        xbmc.log(
            "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX platform XXXX"
        )
        # -- ----------------------------------------------------
    except:
        pass

    DOWNLOAD_PATH = config.get_setting("downloadpath")

    # -- adfly: ------------------------------------
    if url.startswith("http://adf.ly/"):
        try:
            data = httptools.downloadpage(url).data
            url = decode_adfly(data)
        except:
            ddd = xbmcgui.Dialog()
            ddd.ok(
                "alfa-MCT: Sin soporte adf.ly",
                "El script no tiene soporte para el acortador de urls adf.ly.",
                "", "url: " + url)
            return

    # -- Necesario para algunas webs ----------------------------
    if not url.endswith(".torrent") and not url.startswith("magnet"):
        t_file = scrapertools.get_header_from_response(
            url, header_to_get="location")
        if len(t_file) > 0:
            url = t_file
            t_file = scrapertools.get_header_from_response(
                url, header_to_get="location")
        if len(t_file) > 0:
            url = t_file

    # -- Crear dos carpetas en descargas para los archivos ------
    save_path_videos = os.path.join(DOWNLOAD_PATH, "torrent-videos")
    save_path_torrents = os.path.join(DOWNLOAD_PATH, "torrent-torrents")
    if not os.path.exists(save_path_torrents): os.mkdir(save_path_torrents)

    # -- Usar - archivo torrent desde web, magnet o HD ---------
    if not os.path.isfile(url) and not url.startswith("magnet"):
        # -- http - crear archivo torrent -----------------------
        data = url_get(url)
        # -- El nombre del torrent será el que contiene en los --
        # -- datos.                                             -
        re_name = urllib.unquote(
            scrapertools.scrapertools.find_single_match(
                data, ':name\d+:(.*?)\d+:'))
        torrent_file = filetools.join(save_path_torrents,
                                      filetools.encode(re_name + '.torrent'))

        f = open(torrent_file, 'wb')
        f.write(data)
        f.close()
    elif os.path.isfile(url):
        # -- file - para usar torrens desde el HD ---------------
        torrent_file = url
    else:
        # -- magnet ---------------------------------------------
        torrent_file = url
    # -----------------------------------------------------------

    # -- MCT - MiniClienteTorrent -------------------------------
    ses = lt.session()

    # -- log ----------------------------------------------------
    xbmc.log("### Init session ########")
    xbmc.log(lt.version)
    xbmc.log("#########################")
    # -- --------------------------------------------------------

    ses.add_dht_router("router.bittorrent.com", 6881)
    ses.add_dht_router("router.utorrent.com", 6881)
    ses.add_dht_router("dht.transmissionbt.com", 6881)

    trackers = [
        "udp://tracker.openbittorrent.com:80/announce",
        "http://tracker.torrentbay.to:6969/announce",
        "http://tracker.pow7.com/announce",
        "udp://tracker.ccc.de:80/announce",
        "udp://open.demonii.com:1337",
        "http://9.rarbg.com:2710/announce",
        "http://bt.careland.com.cn:6969/announce",
        "http://explodie.org:6969/announce",
        "http://mgtracker.org:2710/announce",
        "http://tracker.best-torrents.net:6969/announce",
        "http://tracker.tfile.me/announce",
        "http://tracker1.wasabii.com.tw:6969/announce",
        "udp://9.rarbg.com:2710/announce",
        "udp://9.rarbg.me:2710/announce",
        "udp://coppersurfer.tk:6969/announce",
        "http://www.spanishtracker.com:2710/announce",
        "http://www.todotorrents.com:2710/announce",
    ]

    video_file = ""
    # -- magnet2torrent -----------------------------------------
    if torrent_file.startswith("magnet"):
        try:
            import zlib
            btih = hex(
                zlib.crc32(
                    scrapertools.scrapertools.find_single_match(
                        torrent_file,
                        'magnet:\?xt=urn:(?:[A-z0-9:]+|)([A-z0-9]{32})'))
                & 0xffffffff)
            files = [
                f for f in os.listdir(save_path_torrents)
                if os.path.isfile(os.path.join(save_path_torrents, f))
            ]
            for file in files:
                if btih in os.path.basename(file):
                    torrent_file = os.path.join(save_path_torrents, file)
        except:
            pass

    if torrent_file.startswith("magnet"):
        try:
            tempdir = tempfile.mkdtemp()
        except IOError:
            tempdir = os.path.join(save_path_torrents, "temp")
            if not os.path.exists(tempdir):
                os.mkdir(tempdir)
        params = {
            'save_path': tempdir,
            'trackers': trackers,
            'storage_mode': lt.storage_mode_t.storage_mode_allocate,
            'paused': False,
            'auto_managed': True,
            'duplicate_is_error': True
        }
        h = lt.add_magnet_uri(ses, torrent_file, params)
        dp = xbmcgui.DialogProgress()
        dp.create('alfa-MCT')
        while not h.has_metadata():
            message, porcent, msg_file, s, download = getProgress(
                h, "Creando torrent desde magnet")
            dp.update(porcent, message, msg_file)
            if s.state == 1: download = 1
            if dp.iscanceled():
                dp.close()
                remove_files(download, torrent_file, video_file, ses, h)
                return
            h.force_dht_announce()
            xbmc.sleep(1000)

        dp.close()
        info = h.get_torrent_info()
        data = lt.bencode(lt.create_torrent(info).generate())

        torrent_file = os.path.join(
            save_path_torrents,
            unicode(info.name() + "-" + btih, "'utf-8'", errors="replace") +
            ".torrent")
        f = open(torrent_file, 'wb')
        f.write(data)
        f.close()
        ses.remove_torrent(h)
        shutil.rmtree(tempdir)
    # -----------------------------------------------------------

    # -- Archivos torrent ---------------------------------------
    e = lt.bdecode(open(torrent_file, 'rb').read())
    info = lt.torrent_info(e)

    # -- El más gordo o uno de los más gordo se entiende que es -
    # -- el vídeo o es el vídeo que se usará como referencia    -
    # -- para el tipo de archivo                                -
    xbmc.log("##### Archivos ## %s ##" % len(info.files()))
    _index_file, _video_file, _size_file = get_video_file(info)

    # -- Prioritarizar/Seleccionar archivo-----------------------
    _index, video_file, video_size, len_files = get_video_files_sizes(info)
    if len_files == 0:
        dp = xbmcgui.Dialog().ok(
            "No se puede reproducir",
            "El torrent no contiene ningún archivo de vídeo")

    if _index == -1:
        _index = _index_file
        video_file = _video_file
        video_size = _size_file

    _video_file_ext = os.path.splitext(_video_file)[1]
    xbmc.log("##### _video_file_ext ## %s ##" % _video_file_ext)
    if (_video_file_ext == ".avi" or _video_file_ext == ".mp4") and allocate:
        xbmc.log("##### storage_mode_t.storage_mode_allocate (" +
                 _video_file_ext + ") #####")
        h = ses.add_torrent({
            'ti':
            info,
            'save_path':
            save_path_videos,
            'trackers':
            trackers,
            'storage_mode':
            lt.storage_mode_t.storage_mode_allocate
        })
    else:
        xbmc.log("##### storage_mode_t.storage_mode_sparse (" +
                 _video_file_ext + ") #####")
        h = ses.add_torrent({
            'ti':
            info,
            'save_path':
            save_path_videos,
            'trackers':
            trackers,
            'storage_mode':
            lt.storage_mode_t.storage_mode_sparse
        })
        allocate = True
    # -----------------------------------------------------------

    # -- Descarga secuencial - trozo 1, trozo 2, ... ------------
    h.set_sequential_download(True)

    h.force_reannounce()
    h.force_dht_announce()

    # -- Inicio de variables para 'pause' automático cuando el  -
    # -- el vídeo se acerca a una pieza sin completar           -
    is_greater_num_pieces = False
    is_greater_num_pieces_plus = False
    is_greater_num_pieces_pause = False

    porcent4first_pieces = int(video_size * 0.000000005)
    if porcent4first_pieces < 10: porcent4first_pieces = 10
    if porcent4first_pieces > 100: porcent4first_pieces = 100
    porcent4last_pieces = int(porcent4first_pieces / 2)

    num_pieces_to_resume = int(video_size * 0.0000000025)
    if num_pieces_to_resume < 5: num_pieces_to_resume = 5
    if num_pieces_to_resume > 25: num_pieces_to_resume = 25

    xbmc.log("##### porcent4first_pieces ## %s ##" % porcent4first_pieces)
    xbmc.log("##### porcent4last_pieces ## %s ##" % porcent4last_pieces)
    xbmc.log("##### num_pieces_to_resume ## %s ##" % num_pieces_to_resume)

    # -- Prioritarizar o seleccionar las piezas del archivo que -
    # -- se desea reproducir con 'file_priorities'              -
    piece_set = set_priority_pieces(h, _index, video_file, video_size,
                                    porcent4first_pieces, porcent4last_pieces,
                                    allocate)

    # -- Crear diálogo de progreso para el primer bucle ---------
    dp = xbmcgui.DialogProgress()
    dp.create('alfa-MCT')

    _pieces_info = {}

    # -- Doble bucle anidado ------------------------------------
    # -- Descarga - Primer bucle                                -
    while not h.is_seed():
        s = h.status()

        xbmc.sleep(100)

        # -- Recuperar los datos del progreso -------------------
        message, porcent, msg_file, s, download = getProgress(h,
                                                              video_file,
                                                              _pf=_pieces_info)

        # -- Si hace 'checking' existe descarga -----------------
        # -- 'download' Se usará para saber si hay datos        -
        # -- descargados para el diálogo de 'remove_files'      -
        if s.state == 1: download = 1

        # -- Player - play --------------------------------------
        # -- Comprobar si se han completado las piezas para el  -
        # -- inicio del vídeo                                   -
        first_pieces = True

        _c = 0
        for i in range(piece_set[0], piece_set[porcent4first_pieces]):
            first_pieces &= h.have_piece(i)
            if h.have_piece(i): _c += 1
        _pieces_info = {
            'current': 0,
            'continuous': "%s/%s" % (_c, porcent4first_pieces),
            'continuous2': "",
            'have': h.status().num_pieces,
            'len': len(piece_set)
        }

        last_pieces = True
        if not allocate:
            _c = len(piece_set) - 1
            _cc = 0
            for i in range(
                    len(piece_set) - porcent4last_pieces, len(piece_set)):
                last_pieces &= h.have_piece(i)
                if h.have_piece(i):
                    _c -= 1
                    _cc += 1
            _pieces_info['continuous2'] = "[%s/%s] " % (_cc,
                                                        porcent4last_pieces)

        if is_view != "Ok" and first_pieces and last_pieces:
            _pieces_info['continuous2'] = ""
            xbmc.log("##### porcent [%.2f%%]" % (s.progress * 100))
            is_view = "Ok"
            dp.close()

            # -- Player - Ver el vídeo --------------------------
            playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
            playlist.clear()

            ren_video_file = os.path.join(save_path_videos, video_file)
            try:
                playlist.add(ren_video_file, xlistitem)
            except:
                playlist.add(ren_video_file)

            if xbmc_version < 17:
                player = play_video(xbmc.PLAYER_CORE_AUTO)
            else:
                player = play_video()
            player.play(playlist)

            # -- Contador de cancelaciones para la ventana de   -
            # -- 'pause' automático                             -
            is_greater_num_pieces_canceled = 0
            continuous_pieces = 0
            porcent_time = 0.00
            current_piece = 0
            set_next_continuous_pieces = porcent4first_pieces

            # -- Impedir que kodi haga 'resume' a un archivo ----
            # -- que se reprodujo con anterioridad y que se     -
            # -- eliminó para impedir que intente la reprucción -
            # -- en una pieza que aún no se ha completado y se  -
            # -- active 'pause' automático                      -
            not_resume = True

            # -- Bandera subTítulos
            _sub = False

            # -- Segundo bucle - Player - Control de eventos ----
            while player.isPlaying():
                xbmc.sleep(100)

                # -- Añadir subTítulos
                if subtitle != "" and not _sub:
                    _sub = True
                    player.setSubtitles(subtitle)

                # -- Impedir que kodi haga 'resume' al inicio ---
                # -- de la descarga de un archivo conocido      -
                if not_resume:
                    player.seekTime(0)
                    not_resume = False

                # -- Control 'pause' automático                 -
                continuous_pieces = count_completed_continuous_pieces(
                    h, piece_set)

                if xbmc.Player().isPlaying():

                    # -- Porcentage del progreso del vídeo ------
                    player_getTime = player.getTime()
                    player_getTotalTime = player.getTotalTime()
                    porcent_time = player_getTime / player_getTotalTime * 100

                    # -- Pieza que se está reproduciendo --------
                    current_piece = int(porcent_time / 100 * len(piece_set))

                    # -- Banderas de control --------------------
                    is_greater_num_pieces = (
                        current_piece >
                        continuous_pieces - num_pieces_to_resume)
                    is_greater_num_pieces_plus = (
                        current_piece + porcent4first_pieces >
                        continuous_pieces)
                    is_greater_num_pieces_finished = (
                        current_piece + porcent4first_pieces >= len(piece_set))

                    # -- Activa 'pause' automático --------------
                    if is_greater_num_pieces and not player.paused and not is_greater_num_pieces_finished:
                        is_greater_num_pieces_pause = True
                        player.pause()

                    if continuous_pieces >= set_next_continuous_pieces:
                        set_next_continuous_pieces = continuous_pieces + num_pieces_to_resume
                    next_continuous_pieces = str(
                        continuous_pieces -
                        current_piece) + "/" + str(set_next_continuous_pieces -
                                                   current_piece)
                    _pieces_info = {
                        'current': current_piece,
                        'continuous': next_continuous_pieces,
                        'continuous2': _pieces_info['continuous2'],
                        'have': h.status().num_pieces,
                        'len': len(piece_set)
                    }

                    # si es un archivo de la videoteca enviar a marcar como visto
                    if item.strm_path:
                        from platformcode import xbmc_videolibrary
                        xbmc_videolibrary.mark_auto_as_watched(item)

                # -- Cerrar el diálogo de progreso --------------
                if player.resumed:
                    dp.close()

                # -- Mostrar el diálogo de progreso -------------
                if player.paused:
                    # -- Crear diálogo si no existe -------------
                    if not player.statusDialogoProgress:
                        dp = xbmcgui.DialogProgress()
                        dp.create('alfa-MCT')
                        player.setDialogoProgress()

                    # -- Diálogos de estado en el visionado -----
                    if not h.is_seed():
                        # -- Recuperar los datos del progreso ---
                        message, porcent, msg_file, s, download = getProgress(
                            h, video_file, _pf=_pieces_info)
                        dp.update(porcent, message, msg_file)
                    else:
                        dp.update(100, "Descarga completa: " + video_file)

                    # -- Se canceló el progreso en el visionado -
                    # -- Continuar                              -
                    if dp.iscanceled():
                        dp.close()
                        player.pause()

                    # -- Se canceló el progreso en el visionado -
                    # -- en la ventana de 'pause' automático.   -
                    # -- Parar si el contador llega a 3         -
                    if dp.iscanceled() and is_greater_num_pieces_pause:
                        is_greater_num_pieces_canceled += 1
                        if is_greater_num_pieces_canceled == 3:
                            player.stop()

                    # -- Desactiva 'pause' automático y ---------
                    # -- reinicia el contador de cancelaciones  -
                    if not dp.iscanceled(
                    ) and not is_greater_num_pieces_plus and is_greater_num_pieces_pause:
                        dp.close()
                        player.pause()
                        is_greater_num_pieces_pause = False
                        is_greater_num_pieces_canceled = 0

                    # -- El usuario cancelo el visionado --------
                    # -- Terminar                               -
                    if player.ended:
                        # -- Diálogo eliminar archivos ----------
                        remove_files(download, torrent_file, video_file, ses,
                                     h)
                        return

        # -- Kodi - Se cerró el visionado -----------------------
        # -- Continuar | Terminar                               -
        if is_view == "Ok" and not xbmc.Player().isPlaying():

            if info.num_files() == 1:
                # -- Diálogo continuar o terminar ---------------
                d = xbmcgui.Dialog()
                ok = d.yesno('alfa-MCT', 'XBMC-Kodi Cerró el vídeo.',
                             '¿Continuar con la sesión?')
            else:
                ok = False
            # -- SI ---------------------------------------------
            if ok:
                # -- Continuar: ---------------------------------
                is_view = None
            else:
                # -- Terminar: ----------------------------------
                # -- Comprobar si el vídeo pertenece a una ------
                # -- lista de archivos                          -
                _index, video_file, video_size, len_files = get_video_files_sizes(
                    info)
                if _index == -1 or len_files == 1:
                    # -- Diálogo eliminar archivos --------------
                    remove_files(download, torrent_file, video_file, ses, h)
                    return
                else:
                    # -- Lista de archivos. Diálogo de opciones -
                    piece_set = set_priority_pieces(h, _index, video_file,
                                                    video_size,
                                                    porcent4first_pieces,
                                                    porcent4last_pieces,
                                                    allocate)
                    is_view = None
                    dp = xbmcgui.DialogProgress()
                    dp.create('alfa-MCT')

        # -- Mostar progeso antes del visionado -----------------
        if is_view != "Ok":
            dp.update(porcent, message, msg_file)

        # -- Se canceló el progreso antes del visionado ---------
        # -- Terminar                                           -
        if dp.iscanceled():
            dp.close()
            # -- Comprobar si el vídeo pertenece a una lista de -
            # -- archivos                                       -
            _index, video_file, video_size, len_files = get_video_files_sizes(
                info)
            if _index == -1 or len_files == 1:
                # -- Diálogo eliminar archivos ------------------
                remove_files(download, torrent_file, video_file, ses, h)
                return
            else:
                # -- Lista de archivos. Diálogo de opciones -----
                piece_set = set_priority_pieces(h, _index, video_file,
                                                video_size,
                                                porcent4first_pieces,
                                                porcent4last_pieces, allocate)
                is_view = None
                dp = xbmcgui.DialogProgress()
                dp.create('alfa-MCT')

    # -- Kodi - Error? - No debería llegar aquí -----------------
    if is_view == "Ok" and not xbmc.Player().isPlaying():
        dp.close()
        # -- Diálogo eliminar archivos --------------------------
        remove_files(download, torrent_file, video_file, ses, h)

    return
Beispiel #54
0
    def make_torrent(cls, path, save_dir, tracker_url, _type='file'):
        """make a torrent file."""
        chain = list()
        if _type == 'vhd':
            vhd = RBTreeVHD(path)
            chain = vhd.get_chain()
            log.debug("vhd-chain: %s" % chain)

        def file_filter(name):
            print "filter name:", name
            print "torrent type:", _type
            if os.path.samefile(name, os.path.dirname(path)):
                return True
            #if _type == 'vhd':
            #    vhd = RBTreeVHD(path)
            #    chain = vhd.get_chain()
            #    log.debug("vhd-chain: %s" % chain)
            if _type == 'vhd':
                if name in chain:
                    return True
            else:
                if os.path.samefile(name, path):
                    return True
            #return False

        if not os.path.exists(path):
            raise InvalidParamError("%s not exists." % path)
        source_path = os.path.abspath(path)
        torrent_file = "%s/%s.torrent" % (save_dir,
                                          os.path.basename(source_path))
        fs = lt.file_storage()
        #        lt.add_files(fs, source_path)
        #        add_files(fs, source_path, _type)
        lt.add_files(fs, os.path.dirname(source_path), file_filter)
        log.debug("add %s files to torrent." % fs.num_files())
        log.debug("total_size: %s" % fs.total_size())
        creator = lt.create_torrent(fs, 0, 4 * 1024 * 1024)
        print "tracker_url:", tracker_url, "type:", type(
            tracker_url), "len:", len(tracker_url)
        creator.add_tracker(str(tracker_url).strip())
        creator.set_creator("RBTree Vtrans %s" % VTRANS_VERSION)
        comment = dict()
        comment["type"] = _type
        creator.set_comment(json.dumps(comment))
        #lt.set_piece_hashes(creator, os.path.split(source_path)[0], lambda x: sys.stderr.write('.'))
        #lt.set_piece_hashes(creator, "/home/xjc/vtrans-master/test", lambda x: sys.stderr.write('.'))
        #lt.set_piece_hashes(creator, os.path.dirname(os.path.dirname(source_path)), lambda x: sys.stderr.write('.'))
        num_pieces = creator.num_pieces()

        def piece_hash_process(x):
            print "\rall %s pieces, setting piece %s, porcess: %s%%" % (
                num_pieces, x + 1, (x + 1) * 100 / num_pieces),
            sys.stdout.flush()

        #lt.set_piece_hashes(creator, os.path.dirname(os.path.dirname(source_path)), lambda x: sys.stderr.write("%s " % x/))
        lt.set_piece_hashes(creator,
                            os.path.dirname(os.path.dirname(source_path)),
                            piece_hash_process)
        sys.stderr.write('\n')
        with open(torrent_file, "wb") as f:
            f.write(lt.bencode(creator.generate()))
        return 0, "make torrent succesfully."
def magnet2torrent(magnet, output_name=None):
    try:
        import libtorrent as lt
    except ImportError:
        try:
            # noinspection PyUnresolvedReferences
            from lib.libtorrent import libtorrent as lt
        except ImportError:
            logger.error(
                "Unable to import libtorrent, disabling magnet conversion")
            lazylibrarian.CONFIG['TOR_CONVERT_MAGNET'] = False
            return False

    if output_name and \
            not pt.isdir(output_name) and \
            not pt.isdir(pt.dirname(pt.abspath(output_name))):
        logger.debug("Invalid output folder: " +
                     pt.dirname(pt.abspath(output_name)))
        return False

    tempdir = tempfile.mkdtemp()
    # noinspection PyUnresolvedReferences
    ses = lt.session()
    # noinspection PyUnresolvedReferences
    params = {
        'save_path': tempdir,
        'storage_mode': lt.storage_mode_t(2),
        'paused': False,
        'auto_managed': True,
        'duplicate_is_error': True
    }
    # noinspection PyUnresolvedReferences
    handle = lt.add_magnet_uri(ses, magnet, params)

    logger.debug("Downloading Metadata (this may take a while)")
    counter = 90
    while counter and not handle.has_metadata():
        try:
            sleep(1)
            counter -= 1
        except KeyboardInterrupt:
            counter = 0
    if not counter:
        logger.debug("magnet2Torrent Aborting...")
        ses.pause()
        logger.debug("Cleanup dir " + tempdir)
        try:
            shutil.rmtree(tempdir)
        except Exception as e:
            logger.debug("%s removing directory: %s" %
                         (type(e).__name__, str(e)))
        return False
    ses.pause()

    torinfo = handle.get_torrent_info()
    # noinspection PyUnresolvedReferences
    torfile = lt.create_torrent(torinfo)
    # noinspection PyUnresolvedReferences
    torcontent = lt.bencode(torfile.generate())
    ses.remove_torrent(handle)

    output = pt.abspath(torinfo.name() + ".torrent")
    if output_name:
        if pt.isdir(output_name):
            output = pt.abspath(
                pt.join(output_name,
                        torinfo.name() + ".torrent"))
        elif pt.isdir(pt.dirname(pt.abspath(output_name))):
            output = pt.abspath(output_name)

    logger.debug("Saving torrent file here : " + output + " ...")
    with open(output, 'wb') as f:
        f.write(torcontent)
    logger.debug("Saved! Cleaning up dir: " + tempdir)
    try:
        shutil.rmtree(tempdir)
    except Exception as e:
        logger.debug("%s removing directory: %s" % (type(e).__name__, str(e)))
    return output
Beispiel #56
0
 def record_torrent(self, item, hist_folder):
     tmp_dir = TMPDIR
     name = ''
     if not os.path.exists(hist_folder):
         os.makedirs(hist_folder)
     if item.startswith('http') or os.path.isfile(item):
         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)
         logger.info(name)
     elif item.startswith('magnet:'):
         torrent_handle, stream_session, info = get_torrent_info_magnet(
             item, tmp_dir, self, 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()
         ui.stop_torrent_forcefully()
     if name:
         torrent_dest = os.path.join(home, name+'.torrent')
         info = lt.torrent_info(torrent_dest)
         file_arr = []
         for f in info.files():
             file_path = f.path
             file_path = os.path.basename(file_path)	
             file_arr.append(file_path)
         if file_arr:
             hist_path = os.path.join(home, 'history.txt')
             if not os.path.isfile(hist_path):
                 hist_dir, last_field = os.path.split(hist_path)
                 if not os.path.exists(hist_dir):
                     os.makedirs(hist_dir)
                 f = open(hist_path, 'w').close()
             if os.path.isfile(hist_path):
                 if (os.stat(hist_path).st_size == 0):
                     write_files(hist_path, name, line_by_line=True)
                 else:
                     lines = open_files(hist_path, True)
                     line_list = []
                     for i in lines:
                         i = i.strip()
                         line_list.append(i)
                     if name not in line_list:
                         write_files(hist_path, name, line_by_line=True)
             
             hist_site = os.path.join(hist_folder, name)
             if not os.path.exists(hist_site):
                 try:
                     os.makedirs(hist_site)
                     hist_epn = os.path.join(hist_site, 'Ep.txt')
                     write_files(hist_epn, file_arr, line_by_line=True)
                 except Exception as e:
                     print(e)
     return name
Beispiel #57
0
        ["zip", "-r", zip_filename, image_directory + "/"])
    subprocess.check_call(zip_commandline,
                          stdout=sys.stdout,
                          stderr=sys.stderr,
                          stdin=subprocess.PIPE,
                          shell=True)
else:
    print("ZIP file already created: {}".format(zip_filename))

torrent_filename = filename_base + ".torrent"
if not torrent_filename in os.listdir("."):
    print("Creating torrent file: {}".format(torrent_filename))
    #Make a torrent file
    fs = lt.file_storage()
    lt.add_files(fs, zip_filename)
    t = lt.create_torrent(fs)
    t.add_tracker("udp://tracker.openbittorrent.com:80/announce", 0)
    t.set_creator(torrent_author)
    torrent_info = torrent_info_template.format(**dict(globals(), **locals()))
    t.set_comment(torrent_info)
    lt.set_piece_hashes(t, '.')
    torrent = t.generate()
    with open(torrent_filename, 'w') as f:
        f.write(lt.bencode(torrent))
else:
    print("Torrent file already created: {}".format(torrent_filename))

wiki_description_filename = filename_base + "_wiki.txt"
if not wiki_description_filename in os.listdir("."):
    print("Creating wiki description: {}".format(wiki_description_filename))
    image_sha1 = get_sha1_for_file(image_path, sha1_bufsize)
Beispiel #58
0
    def parse_magnet_uri(magnet_uri,
                         scrape=False,
                         use_dht=True,
                         force_dht=False,
                         timeout=10,
                         trackers=None,
                         no_cache=False,
                         n_try=3):
        try:
            import libtorrent as lt
        except ImportError:
            raise ImportError('libtorrent package required')

        # parameters
        params = lt.parse_magnet_uri(magnet_uri)

        # prevent downloading
        # https://stackoverflow.com/q/45680113
        if isinstance(params, dict):
            params['flags'] |= lt.add_torrent_params_flags_t.flag_upload_mode
        else:
            params.flags |= lt.add_torrent_params_flags_t.flag_upload_mode

        lt_version = [int(v) for v in lt.version.split('.')]
        if [0, 16, 13, 0] < lt_version < [1, 1, 3, 0]:
            # for some reason the info_hash needs to be bytes but it's a struct called sha1_hash
            if type({}) == type(params):
                params['info_hash'] = params['info_hash'].to_bytes()
            else:
                params.info_hash = params.info_hash.to_bytes()

        # 캐시에 있으면 ...
        info_hash_from_magnet = str(params['info_hash'] if type({}) ==
                                    type(params) else params.info_hash)
        if (not no_cache) and (info_hash_from_magnet in Logic.torrent_cache):
            return Logic.torrent_cache[info_hash_from_magnet]['info']

        # add trackers
        if type({}) == type(params):
            if len(params['trackers']) == 0:
                if trackers is None:
                    trackers = json.loads(ModelSetting.get('trackers'))
                params['trackers'] = random.sample(trackers, 5)
        else:
            if len(params.trackers) == 0:
                if trackers is None:
                    trackers = json.loads(ModelSetting.get('trackers'))
                params.trackers = random.sample(trackers, 5)

        # session
        session = lt.session()

        session.listen_on(6881, 6891)

        session.add_extension('ut_metadata')
        session.add_extension('ut_pex')
        session.add_extension('metadata_transfer')

        if use_dht:
            session.add_dht_router('router.utorrent.com', 6881)
            session.add_dht_router('router.bittorrent.com', 6881)
            session.add_dht_router("dht.transmissionbt.com", 6881)
            session.add_dht_router('127.0.0.1', 6881)
            session.start_dht()

        # handle
        handle = session.add_torrent(params)

        if force_dht:
            handle.force_dht_announce()

        for tryid in range(max(n_try, 1)):
            timeout_value = timeout
            while not handle.has_metadata():
                time.sleep(0.1)
                timeout_value -= 0.1
                if timeout_value <= 0:
                    logger.debug(
                        'Failed to get metadata on trial: {}/{}'.format(
                            tryid + 1, n_try))
                    break

            if handle.has_metadata():
                lt_info = handle.get_torrent_info()
                logger.debug(
                    'Successfully get metadata after {} seconds on trial {}'.
                    format(timeout - timeout_value, tryid + 1))
                break
            else:
                if tryid + 1 == max(n_try, 1):
                    session.remove_torrent(handle, True)
                    raise Exception(
                        'Timed out after {}x{} seconds trying to get metainfo'.
                        format(timeout, n_try))

        # create torrent object and generate file stream
        torrent = lt.create_torrent(lt_info)
        torrent.set_creator('libtorrent v{}'.format(lt.version))  # signature
        torrent_dict = torrent.generate()

        torrent_info = Logic.convert_torrent_info(lt_info)
        torrent_info.update({
            'trackers':
            params.trackers
            if type({}) != type(params) else params['trackers'],
            'creation_date':
            datetime.fromtimestamp(torrent_dict[b'creation date']).isoformat(),
            'time': {
                'total': timeout - timeout_value,
                'metadata': timeout - timeout_value
            },
        })

        if scrape:
            # start scraping
            timeout_value = timeout
            while handle.status(0).num_complete < 0:
                time.sleep(0.1)
                timeout_value -= 0.1
                if timeout_value <= 0:
                    logger.error(
                        'Timed out after {} seconds trying to get peer info'.
                        format(timeout))

            if handle.status(0).num_complete >= 0:
                torrent_status = handle.status(0)

                torrent_info.update({
                    'seeders': torrent_status.num_complete,
                    'peers': torrent_status.num_incomplete,
                })
                torrent_info['time']['scrape'] = timeout - timeout_value
                torrent_info['time']['total'] = torrent_info['time'][
                    'metadata'] + torrent_info['time']['scrape']

        session.remove_torrent(handle, True)

        # caching for later use
        if Logic.torrent_cache is None:
            Logic.cache_init()
        Logic.torrent_cache[torrent_info['info_hash']] = {
            'info': torrent_info,
        }
        return torrent_info
Beispiel #59
0
def play(url, xlistitem, is_view=None, subtitle=""):

    # -- Necesario para algunas webs ----------------------------
    if not url.endswith(".torrent") and not url.startswith("magnet"):
        t_file = scrapertools.get_header_from_response(
            url, header_to_get="location")
        if len(t_file) > 0:
            url = t_file
            t_file = scrapertools.get_header_from_response(
                url, header_to_get="location")
        if len(t_file) > 0:
            url = t_file

    # -- Crear dos carpetas en descargas para los archivos ------
    save_path_videos = os.path.join(config.get_setting("downloadpath"),
                                    "torrent-videos")
    save_path_torrents = os.path.join(config.get_setting("downloadpath"),
                                      "torrent-torrents")
    if not os.path.exists(save_path_torrents): os.mkdir(save_path_torrents)

    # -- Usar - archivo torrent desde web, meagnet o HD ---------
    if not os.path.isfile(url) and not url.startswith("magnet"):
        # -- http - crear archivo torrent -----------------------
        data = url_get(url)
        # -- El nombre del torrent será el que contiene en los --
        # -- datos.                                             -
        re_name = urllib.unquote(
            scrapertools.get_match(data, ':name\d+:(.*?)\d+:'))
        torrent_file = filetools.join(save_path_torrents,
                                      filetools.encode(re_name + '.torrent'))

        f = open(torrent_file, 'wb')
        f.write(data)
        f.close()
    elif os.path.isfile(url):
        # -- file - para usar torrens desde el HD ---------------
        torrent_file = url
    else:
        # -- magnet ---------------------------------------------
        torrent_file = url
    # -----------------------------------------------------------

    # -- MCT - MiniClienteTorrent -------------------------------
    ses = lt.session()

    print "### Init session ########"
    print lt.version
    print "#########################"

    ses.add_dht_router("router.bittorrent.com", 6881)
    ses.add_dht_router("router.utorrent.com", 6881)
    ses.add_dht_router("dht.transmissionbt.com", 6881)

    trackers = [
        "udp://tracker.openbittorrent.com:80/announce",
        "http://tracker.torrentbay.to:6969/announce",
        "http://tracker.pow7.com/announce",
        "udp://tracker.ccc.de:80/announce",
        "udp://open.demonii.com:1337",
        "http://9.rarbg.com:2710/announce",
        "http://bt.careland.com.cn:6969/announce",
        "http://explodie.org:6969/announce",
        "http://mgtracker.org:2710/announce",
        "http://tracker.best-torrents.net:6969/announce",
        "http://tracker.tfile.me/announce",
        "http://tracker1.wasabii.com.tw:6969/announce",
        "udp://9.rarbg.com:2710/announce",
        "udp://9.rarbg.me:2710/announce",
        "udp://coppersurfer.tk:6969/announce",
        "http://www.spanishtracker.com:2710/announce",
        "http://www.todotorrents.com:2710/announce",
    ]

    video_file = ""
    # -- magnet2torrent -----------------------------------------
    if torrent_file.startswith("magnet"):
        try:
            tempdir = tempfile.mkdtemp()
        except IOError:
            tempdir = os.path.join(save_path_torrents, "temp")
            if not os.path.exists(tempdir): os.mkdir(tempdir)

        params = {
            'save_path': tempdir,
            'trackers': trackers,
            'storage_mode': lt.storage_mode_t.storage_mode_allocate,
            'paused': False,
            'auto_managed': True,
            'duplicate_is_error': True
        }
        h = lt.add_magnet_uri(ses, torrent_file, params)
        dp = xbmcgui.DialogProgress()
        dp.create('pelisalacarta-MCT')
        while not h.has_metadata():
            message, porcent, msg_file, s, download = getProgress(
                h, "Creando torrent desde magnet")
            dp.update(porcent, message, msg_file)
            if s.state == 1: download = 1
            if dp.iscanceled():
                dp.close()
                remove_files(download, torrent_file, video_file, ses, h)
                return

            h.force_dht_announce()
            xbmc.sleep(1000)

        dp.close()
        info = h.get_torrent_info()
        data = lt.bencode(lt.create_torrent(info).generate())
        #torrent_file = os.path.join(save_path_torrents, info.name() + ".torrent")
        torrent_file = os.path.join(
            save_path_torrents,
            unicode(info.name(), "'utf-8'", errors="replace") + ".torrent")
        f = open(torrent_file, 'wb')
        f.write(data)
        f.close()
        ses.remove_torrent(h)
        shutil.rmtree(tempdir)
    # -----------------------------------------------------------

    # -- Archivos torrent ---------------------------------------
    e = lt.bdecode(open(torrent_file, 'rb').read())
    info = lt.torrent_info(e)

    # -- El más gordo o uno de los más gordo se entiende que es -
    # -- el vídeo o es el vídeo que se usará como referencia    -
    # -- para el tipo de archivo                                -
    print "##### Archivos ## %s ##" % len(info.files())
    _index_file, _video_file, _size_file = get_video_file(info)

    _video_file_ext = os.path.splitext(_video_file)[1]
    if _video_file_ext == ".avi" or _video_file_ext == ".mp4":
        print "##### storage_mode_t.storage_mode_allocate (" + _video_file_ext + ") #####"
        h = ses.add_torrent({
            'ti':
            info,
            'save_path':
            save_path_videos,
            'trackers':
            trackers,
            'storage_mode':
            lt.storage_mode_t.storage_mode_allocate
        })
    else:
        print "##### storage_mode: none (" + _video_file_ext + ") #####"
        h = ses.add_torrent({
            'ti':
            info,
            'save_path':
            save_path_videos,
            'trackers':
            trackers,
            'storage_mode':
            lt.storage_mode_t.storage_mode_sparse
        })
    # -----------------------------------------------------------

    # -- Descarga secuencial - trozo 1, trozo 2, ... ------------
    h.set_sequential_download(True)

    h.force_reannounce()
    h.force_dht_announce()

    # -- Prioritarizar/Seleccionar archivo-----------------------
    _index, video_file, video_size = get_video_files_sizes(info)
    if _index == -1:
        _index = _index_file
        video_file = _video_file
        video_size = _size_file

    # -- Inicio de variables para 'pause' automático cuando el  -
    # -- el vídeo se acerca a una pieza sin completar           -
    is_greater_num_pieces = False
    is_greater_num_pieces_plus = False
    is_greater_num_pieces_pause = False

    #porcent4first_pieces = int( video_size / 1073741824 )
    porcent4first_pieces = int(video_size * 0.000000005)
    if porcent4first_pieces < 10: porcent4first_pieces = 10
    if porcent4first_pieces > 100: porcent4first_pieces = 100
    #num_pieces_to_resume = int( video_size / 1610612736 )
    num_pieces_to_resume = int(video_size * 0.0000000025)
    if num_pieces_to_resume < 5: num_pieces_to_resume = 5
    if num_pieces_to_resume > 25: num_pieces_to_resume = 25

    print "##### porcent4first_pieces ## %s ##" % porcent4first_pieces
    print "##### num_pieces_to_resume ## %s ##" % num_pieces_to_resume

    # -- Prioritarizar o seleccionar las piezas del archivo que -
    # -- se desea reproducir con 'file_priorities'              -
    piece_set = set_priority_pieces(h, _index, video_file, video_size)

    # -- Crear diálogo de progreso para el primer bucle ---------
    dp = xbmcgui.DialogProgress()
    dp.create('pelisalacarta-MCT')

    _pieces_info = {}

    # -- Doble bucle anidado ------------------------------------
    # -- Descarga - Primer bucle                                -
    while not h.is_seed():
        s = h.status()

        xbmc.sleep(100)

        # -- Recuperar los datos del progreso -------------------
        message, porcent, msg_file, s, download = getProgress(h,
                                                              video_file,
                                                              _pf=_pieces_info)

        # -- Si hace 'checking' existe descarga -----------------
        # -- 'download' Se usará para saber si hay datos        -
        # -- descargados para el diálogo de 'remove_files'      -
        if s.state == 1: download = 1

        # -- Player - play --------------------------------------
        # -- Comprobar si se han completado las piezas para el  -
        # -- inicio del vídeo    ...............                -
        first_pieces = True
        _p = ""
        _c = 0
        for i in range(piece_set[0], piece_set[porcent4first_pieces]):
            _p += "[%s:%s]" % (i, h.have_piece(i))
            first_pieces &= h.have_piece(i)
            if h.have_piece(i): _c += 1
        _pieces_info = {
            'current': 0,
            'continuous': "%s/%s" % (_c, porcent4first_pieces),
            'have': h.status().num_pieces,
            'len': len(piece_set)
        }
        _p = "##### first_pieces [%s/%s][%s]: " % (_c, porcent4first_pieces,
                                                   len(piece_set)) + _p
        print _p
        # -- -------------------------------------------------- -

        if is_view != "Ok" and first_pieces:
            print "##### porcent [%.2f%%]" % (s.progress * 100)
            is_view = "Ok"
            dp.close()

            # -- Player - Ver el vídeo --------------------------
            playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
            playlist.clear()
            #ren_video_file = os.path.join( save_path_videos, video_file ).replace('\\','\\\\')
            ren_video_file = os.path.join(save_path_videos, video_file)
            playlist.add(ren_video_file, xlistitem)
            #playlist.add( os.path.join( save_path_videos, video_file ), xlistitem )
            #playlist.add( "http://192.168.0.200/mctplay/" + video_file.replace(' ','%20'), xlistitem )
            player = play_video(xbmc.PLAYER_CORE_AUTO)
            player.play(playlist)
            '''
            # -- Player - Ver el vídeo --------------------------
            player = play_video()
            #player.play( os.path.join( save_path_videos, video_file ) )
            player.play( "http://192.168.0.200/mctplay/" + video_file.replace(' ','%20') )
            '''

            #player.play( os.path.join( save_path_videos, video_file ) )

            # -- Contador de cancelaciones para la ventana de   -
            # -- 'pause' automático                             -
            is_greater_num_pieces_canceled = 0
            continuous_pieces = 0
            porcent_time = 0.00
            current_piece = 0

            # -- Impedir que kodi haga 'resume' a un archivo ----
            # -- que se reprodució con anterioridad y que se    -
            # -- eliminó para impedir que intente la reprucción -
            # -- en una pieza que aún no se ha completado y se  -
            # -- active 'pause' automático                      -
            not_resume = True

            # -- Bandera subTítulos
            _sub = False

            # -- Segundo bucle - Player - Control de eventos ----
            while player.isPlaying():
                xbmc.sleep(100)

                # -- Añadir subTítulos
                if subtitle != "" and not _sub:
                    _sub = True
                    player.setSubtitles(subtitle)

                # -- Impedir que kodi haga 'resume' al inicio ---
                # -- de la descarga de un archivo conocido      -
                if not_resume:
                    player.seekTime(0)
                    not_resume = False
                    #xbmc.sleep(1000)

                # -- Control 'pause' automático                 -
                continuous_pieces = count_completed_continuous_pieces(
                    h, piece_set)

                if xbmc.Player().isPlaying():

                    # -- Porcentage del progreso del vídeo ------
                    porcent_time = player.getTime() / player.getTotalTime(
                    ) * 100

                    # -- Pieza que se está reproduciendo --------
                    current_piece = int(porcent_time / 100 * len(piece_set))

                    # -- Banderas de control --------------------
                    is_greater_num_pieces = (
                        current_piece >
                        continuous_pieces - num_pieces_to_resume)
                    is_greater_num_pieces_plus = (
                        current_piece + porcent4first_pieces >
                        continuous_pieces)
                    is_greater_num_pieces_finished = (
                        current_piece + porcent4first_pieces >= len(piece_set))

                    # -- Activa 'pause' automático --------------
                    if is_greater_num_pieces and not player.paused and not is_greater_num_pieces_finished:
                        is_greater_num_pieces_pause = True
                        player.pause()

                    # -- Log ------------------------------------
                    _TotalTime = player.getTotalTime()
                    _Time = player.getTime()
                    _print_log = "\n##### Player ##################################"
                    _print_log += "\nTamaño del vídeo: %s" % video_size
                    _print_log += "\nTotal piezas: %s" % len(piece_set)
                    _print_log += "\nPiezas contiguas: %s" % continuous_pieces
                    _print_log += "\n-----------------------------------------------"
                    _print_log += "\nVídeo-Total segundos: %s" % _TotalTime
                    _print_log += "\nVídeo-Progreso segundos: %s" % _Time
                    _print_log += "\nVídeo-Progreso porcentaje: %.2f%%" % porcent_time
                    _print_log += "\n-----------------------------------------------"
                    _print_log += "\ncurrent_piece: %s" % current_piece
                    _print_log += "\nis_greater_num_pieces: %s" % is_greater_num_pieces
                    _print_log += "\nis_greater_num_pieces_plus: %s" % is_greater_num_pieces_plus
                    _print_log += "\nis_greater_num_pieces_pause: %s" % is_greater_num_pieces_pause
                    _print_log += "\nis_greater_num_pieces_finished: %s" % is_greater_num_pieces_finished
                    _print_log += "\nPieza que se está visionando: %.2f" % (
                        porcent_time / 100 * len(piece_set))
                    _print_log += "\nOffset que se está visionando: %.2f" % (
                        porcent_time / 100 * video_size)
                    if is_greater_num_pieces and not player.paused and not is_greater_num_pieces_finished:
                        _print_log += "\n+++++++++++++++++++++++++++++++++++++++++++++++"
                        _print_log += "\nPausa con:"
                        _print_log += "\n    current_piece = %s" % current_piece
                        _print_log += "\n    continuous_pieces = %s" % continuous_pieces
                    _print_log += "\n###############################################"
                    print _print_log
                    # -------------------------------------------
                    _pieces_info = {
                        'current': current_piece,
                        'continuous': continuous_pieces,
                        'have': h.status().num_pieces,
                        'len': len(piece_set)
                    }

                # -- Cerrar el diálogo de progreso --------------
                if player.resumed:
                    dp.close()

                # -- Mostrar el diálogo de progreso -------------
                if player.paused:
                    # -- Crear diálogo si no existe -------------
                    if not player.statusDialogoProgress:
                        dp = xbmcgui.DialogProgress()
                        dp.create('pelisalacarta-MCT')
                        player.setDialogoProgress()

                    # -- Diálogos de estado en el visionado -----
                    if not h.is_seed():
                        # -- Recuperar los datos del progreso ---
                        message, porcent, msg_file, s, download = getProgress(
                            h, video_file, _pf=_pieces_info)
                        dp.update(porcent, message, msg_file)
                    else:
                        dp.update(100, "Descarga completa: " + video_file)

                    # -- Se canceló el progreso en el visionado -
                    # -- Continuar                              -
                    if dp.iscanceled():
                        dp.close()
                        player.pause()

                    # -- Se canceló el progreso en el visionado -
                    # -- en la ventana de 'pause' automático.   -
                    # -- Parar si el contador llega a 3         -
                    if dp.iscanceled() and is_greater_num_pieces_pause:
                        is_greater_num_pieces_canceled += 1
                        if is_greater_num_pieces_canceled == 3:
                            player.stop()

                    # -- Desactiva 'pause' automático y ---------
                    # -- reinicia el contador de cancelaciones  -
                    if not dp.iscanceled(
                    ) and not is_greater_num_pieces_plus and is_greater_num_pieces_pause:
                        dp.close()
                        player.pause()
                        is_greater_num_pieces_pause = False
                        is_greater_num_pieces_canceled = 0

                    # -- El usuario cancelo el visionado --------
                    # -- Terminar                               -
                    if player.ended:
                        # -- Diálogo eliminar archivos ----------
                        remove_files(download, torrent_file, video_file, ses,
                                     h)
                        return

        # -- Kodi - Se cerró el visionado -----------------------
        # -- Continuar | Terminar                               -
        if is_view == "Ok" and not xbmc.Player().isPlaying():

            if info.num_files() == 1:
                # -- Diálogo continuar o terminar ---------------
                d = xbmcgui.Dialog()
                ok = d.yesno('pelisalacarta-MCT', 'XBMC-Kodi Cerró el vídeo.',
                             '¿Continuar con la sesión?')
            else:
                ok = False
            # -- SI ---------------------------------------------
            if ok:
                # -- Continuar: ---------------------------------
                is_view = None
            else:
                # -- Terminar: ----------------------------------
                # -- Comprobar si el vídeo pertenece a una ------
                # -- lista de archivos                          -
                _index, video_file, video_size = get_video_files_sizes(info)
                if _index == -1 or info.num_files() == 1:
                    # -- Diálogo eliminar archivos --------------
                    remove_files(download, torrent_file, video_file, ses, h)
                    return
                else:
                    # -- Lista de archivos. Diálogo de opciones -
                    piece_set = set_priority_pieces(h, _index, video_file,
                                                    video_size)
                    is_view = None
                    dp = xbmcgui.DialogProgress()
                    dp.create('pelisalacarta-MCT')

        # -- Mostar progeso antes del visionado -----------------
        if is_view != "Ok":
            dp.update(porcent, message, msg_file)

        # -- Se canceló el progreso antes del visionado ---------
        # -- Terminar                                           -
        if dp.iscanceled():
            dp.close()
            # -- Comprobar si el vídeo pertenece a una lista de -
            # -- archivos                                       -
            _index, video_file, video_size = get_video_files_sizes(info)
            if _index == -1 or info.num_files() == 1:
                # -- Diálogo eliminar archivos ------------------
                remove_files(download, torrent_file, video_file, ses, h)
                return
            else:
                # -- Lista de archivos. Diálogo de opciones -----
                piece_set = set_priority_pieces(h, _index, video_file,
                                                video_size)
                is_view = None
                dp = xbmcgui.DialogProgress()
                dp.create('pelisalacarta-MCT')

    # -- Kodi - Error? - No debería llegar aquí -----------------
    if is_view == "Ok" and not xbmc.Player().isPlaying():
        dp.close()
        # -- Diálogo eliminar archivos --------------------------
        remove_files(download, torrent_file, video_file, ses, h)

    return
Beispiel #60
0
import libtorrent as lt
import time

piece_size = 256 * 1024
creator_str = "peerbay.p2p"
thetracker = "bttracker.debian.org"
theurlseed = "your desired url seed"

fs = lt.file_storage()
lt.add_files(fs, "demoCA")
fs.num_files()

t = lt.create_torrent(fs, piece_size)
t.add_tracker(thetracker)
lt.set_piece_hashes(t, ".")
t.set_root_cert("newkey.pem")
t.set_creator(creator_str)
#~ t.add_url_seed(theurlseed)

f = open("dom.torrent", "wb")
f.write(lt.bencode(t.generate()))
f.close()
ses = lt.session()
ses.listen_on(6881, 6891)

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

params = { "save_path": './', \
        "storage_mode": lt.storage_mode_t.storage_mode_sparse, \
        "ti": info }