def test_symlink(self) -> None: fs = lt.file_storage() fs.add_file(os.path.join("path", "test.txt"), 1024) self.assertEqual(fs.symlink(0), "") fs = lt.file_storage() fs.add_file(os.path.join("path", "test.txt"), 0, linkpath="other.txt") self.assertEqual(fs.symlink(0), os.path.join("path", "other.txt"))
def test_add_file_bytes(self) -> None: fs = lt.file_storage() fs.add_file(os.path.join(b"path", b"file.txt"), 1024) # type: ignore self.assertEqual(fs.file_path(0), os.path.join("path", "file.txt")) fs = lt.file_storage() fs.add_file( # type: ignore os.path.join("path", "file.txt"), 1024, linkpath=b"other.txt", ) self.assertEqual(fs.symlink(0), os.path.join("path", "other.txt"))
def addTorrent(filepath, torrentpath, osfilename, filename): global torrentName, ses, torrentList #, torrentHandleList fileStorage = lt.file_storage() lt.add_files(fileStorage, os.path.normpath(filepath + osfilename)) lTorrent = lt.create_torrent(fileStorage) #lTorrent = localTorrent torrentList.append(lTorrent) addTrackerList(lTorrent) lTorrent.set_creator('Hubzilla using Libtorrent %s' % lt.version) lTorrent.set_comment( "Filename:" + filename.encode('utf8', 'strict').decode('unicode_escape')) lt.set_piece_hashes(lTorrent, os.path.normpath(filepath)) gTorrent = lTorrent.generate() #gTorrent = generated Torrent ##write Torrent file to filesystem tFile = open(os.path.normpath(torrentpath + filename + ".torrent"), "wb") tFile.write(lt.bencode(gTorrent)) tFile.close() handle = ses.add_torrent({ 'ti': lt.torrent_info(os.path.normpath(torrentpath + filename + ".torrent")), 'save_path': os.path.normpath(filepath), 'seed_mode': True }) #torrentHandleList.append(handle) torrentName[str(handle.get_torrent_info().info_hash())] = filename mLink = lt.make_magnet_uri(lt.torrent_info(gTorrent)) magnetLinks[str(handle.get_torrent_info().info_hash())] = mLink return mLink
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
def test_set_hash_invalid(self) -> None: fs = lt.file_storage() ct = lt.create_torrent(fs) with self.assertRaises(IndexError): ct.set_hash(0, lib.get_random_bytes(20)) with self.assertRaises(IndexError): ct.set_hash(-1, lib.get_random_bytes(20))
def test_path_surrogate_bytes(self) -> None: fs = lt.file_storage() with get_tempdir_stack(os.fsencode("\udcff")) as (tempdir, ): with open(os.path.join(tempdir, b"test.txt"), mode="wb") as fp: fp.write(lib.get_random_bytes(1024)) lt.add_files(fs, tempdir) self.assertEqual(fs.file_name(0), "test.txt")
def test_files_and_remap(self) -> None: ti = lt.torrent_info({ b"info": { b"name": b"test.txt", b"pieces": lib.get_random_bytes(20), b"piece length": 16384, b"length": 1024, }, }) self.assertEqual(ti.name(), "test.txt") self.assertEqual(ti.files().file_path(0), "test.txt") self.assertEqual(ti.orig_files().file_path(0), "test.txt") fs = lt.file_storage() fs.add_file(os.path.join("path", "remapped1.txt"), 512) fs.add_file(os.path.join("path", "remapped2.txt"), 512) ti.remap_files(fs) self.assertEqual(ti.name(), "path") self.assertEqual(ti.files().num_files(), 2) self.assertEqual(ti.files().file_path(0), os.path.join("path", "remapped1.txt")) self.assertEqual(ti.files().file_path(1), os.path.join("path", "remapped2.txt")) self.assertEqual(ti.orig_files().num_files(), 1) self.assertEqual(ti.orig_files().file_path(0), "test.txt")
def test_set_file_hash_short(self) -> None: fs = lt.file_storage() fs.add_file("test.txt", 1024) ct = lt.create_torrent(fs) with self.assertWarns(DeprecationWarning): with self.assertRaises(ValueError): ct.set_file_hash(0, lib.get_random_bytes(19))
def create_torrent(directory, announces=None, output=None, comment=None, web_seeds=None): if not output: output = directory + ".torrent" # "If a piece size of 0 is specified, a piece_size will be calculated such that the torrent file is roughly 40 kB." piece_size_multiplier = 0 piece_size = (16 * 1024) * piece_size_multiplier # Must be multiple of 16KB # http://www.libtorrent.org/make_torrent.html#create-torrent flags = libtorrent.create_torrent_flags_t.calculate_file_hashes if not os.path.isdir(directory): raise Exception("The path {} is not a directory".format(directory)) fs = libtorrent.file_storage() is_not_whitelisted = lambda node: not is_whitelisted(unicode_helpers.decode_utf8(node)) libtorrent.add_files(fs, unicode_helpers.encode_utf8(directory), is_not_whitelisted, flags=flags) t = libtorrent.create_torrent(fs, piece_size=piece_size, flags=flags) for announce in announces: t.add_tracker(unicode_helpers.encode_utf8(announce)) if comment: t.set_comment(unicode_helpers.encode_utf8(comment)) for web_seed in web_seeds: t.add_url_seed(unicode_helpers.encode_utf8(web_seed)) # t.add_http_seed("http://...") libtorrent.set_piece_hashes(t, unicode_helpers.encode_utf8(os.path.dirname(directory))) with open(output, "wb") as file_handle: file_handle.write(libtorrent.bencode(t.generate())) return output
def make_torrent(file, tracker, save_dir): print("file:%s" % file) print("tracker:%s" % tracker) print("save:%s" % save_dir) print('version:%s' % lt.version) fs = lt.file_storage() # get file storage obj lt.add_files(fs, file) # add files to file storage print("fs files:%d" % fs.num_files()) if fs.num_files() == 0: # check os._exit(0) t = lt.create_torrent(fs, PIECE_SIZE, PAD_FILE_SIZE) print("picesize:%s" % t.piece_size(0)) t.add_tracker(tracker) t.set_creator('libtorrent %s' % lt.version) # optional. # real start create torrent lt.set_piece_hashes(t, os.path.dirname(file), func) # base file in this directory, fun callback # write to torrent file basename = os.path.basename(file) torrent_name = os.path.join(save_dir, basename + ".torrent") print("torrent:%s" % torrent_name) f = open(torrent_name, "wb+") f.write(lt.bencode(t.generate())) #generere: create bencode file. f.close()
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 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
def make_torrent(path, save): fs = lt.file_storage() lt.add_files(fs, path) if fs.num_files() == 0: print 'no files added' sys.exit(1) input = os.path.abspath(path) basename = os.path.basename(path) t = lt.create_torrent(fs, 0, 4 * 1024 * 1024) t.add_tracker("http://10.0.1.5:8760/announce") t.set_creator('libtorrent %s' % lt.version) lt.set_piece_hashes(t, os.path.split(input)[0], lambda x: sys.stderr.write('.')) sys.stderr.write('\n') save = os.path.dirname(input) save = "%s/%s.torrent" % (save, basename) f = open(save, "wb") f.write(lt.bencode(t.generate())) f.close() print "the bt torrent file is store at %s" % save
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)
def test_piece_size(self) -> None: fs = lt.file_storage() fs.add_file("test.txt", 1024) fs.set_piece_length(16384) fs.set_num_pieces(1) self.assertEqual(fs.piece_size(0), 1024) self.assertEqual(fs.piece_size(1), 16384)
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())
def test_path_non_unicode_str(self) -> None: fs = lt.file_storage() with get_tempdir_stack(os.fsdecode(b"\xff")) as (tempdir, ): with open(os.path.join(tempdir, "test.txt"), mode="wb") as fp: fp.write(lib.get_random_bytes(1024)) lt.add_files(fs, tempdir) self.assertEqual(fs.file_name(0), "test.txt")
def test_piece_size(self) -> None: fs = lt.file_storage() fs.add_file("test.txt", 1024) ct = lt.create_torrent(fs) self.assertEqual(ct.piece_size(-1), 16384) self.assertEqual(ct.piece_size(0), 1024) self.assertEqual(ct.piece_size(1), 16384)
def test_len(self) -> None: fs = lt.file_storage() with self.assertWarns(DeprecationWarning): self.assertEqual(len(fs), 0) fs.add_file("test.txt", 1024) with self.assertWarns(DeprecationWarning): self.assertEqual(len(fs), 1)
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 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)
def test_add_file_entry(self) -> None: fs = lt.file_storage() # It's not clear this path has ever been useful, as the entry size can't # be modified fe = lt.file_entry() fe.path = "file.txt" with self.assertWarns(DeprecationWarning): fs.add_file(fe)
def test_at(self) -> None: fs = lt.file_storage() fs.add_file(os.path.join("path", "test.txt"), 1024) with self.assertWarns(DeprecationWarning): fe = fs.at(0) self.assertEqual(fe.path, os.path.join("path", "test.txt")) with self.assertWarns(DeprecationWarning): self.assertEqual(fe.size, 1024)
def test_at_invalid(self) -> None: fs = lt.file_storage() with self.assertWarns(DeprecationWarning): with self.assertRaises(IndexError): fs.at(0) with self.assertWarns(DeprecationWarning): with self.assertRaises(IndexError): fs.at(-1)
def test_creator(self) -> None: fs = lt.file_storage() fs.add_file("test.txt", 1024) ct = lt.create_torrent(fs) ct.set_creator("test") ct.set_hash(0, lib.get_random_bytes(20)) entry = ct.generate() self.assertEqual(entry[b"created by"], b"test")
def test_files(self) -> None: fs = lt.file_storage() fs.add_file(os.path.join("path", "test1.txt"), 1024) ct = lt.create_torrent(fs) ct.files().add_file(os.path.join("path", "test2.txt"), 1024) ct.set_hash(0, lib.get_random_bytes(20)) entry = ct.generate() self.assertEqual(len(entry[b"info"][b"files"]), 2)
def test_http_seed(self) -> None: fs = lt.file_storage() fs.add_file("test.txt", 1024) ct = lt.create_torrent(fs) ct.add_http_seed("http://example.com") ct.set_hash(0, lib.get_random_bytes(20)) entry = ct.generate() self.assertEqual(entry[b"url-list"], [b"http://example.com"])
def test_set_file_hash(self) -> None: fs = lt.file_storage() fs.add_file("test.txt", 1024) ct = lt.create_torrent(fs) ct.set_file_hash(0, lib.get_random_bytes(20)) ct.set_hash(0, lib.get_random_bytes(20)) entry = ct.generate() self.assertIn(b"sha1", entry[b"info"])
def test_rename_file(self) -> None: fs = lt.file_storage() fs.add_file(os.path.join("path", "test.txt"), 1024) fs.rename_file(0, os.path.join("path", "other.txt")) self.assertEqual(fs.file_path(0), os.path.join("path", "other.txt")) fs.rename_file(0, os.path.join(b"path", b"bytes.txt")) # type: ignore self.assertEqual(fs.file_path(0), os.path.join("path", "bytes.txt"))
def test_add_node(self) -> None: fs = lt.file_storage() fs.add_file("test.txt", 1024) ct = lt.create_torrent(fs) ct.add_node("1.2.3.4", 5678) ct.set_hash(0, lib.get_random_bytes(20)) entry = ct.generate() self.assertEqual(entry[b"nodes"], [[b"1.2.3.4", 5678]])
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()
def test_root_cert(self) -> None: fs = lt.file_storage() fs.add_file("test.txt", 1024) ct = lt.create_torrent(fs) ct.set_hash(0, lib.get_random_bytes(20)) ct.set_root_cert("test") entry = ct.generate() self.assertEqual(entry[b"info"][b"ssl-cert"], b"test")
def test_path_non_unicode_bytes(self) -> None: fs = lt.file_storage() with get_tempdir_stack(b"\xff", b"test") as (outer, inner): with open(os.path.join(inner, b"test.txt"), mode="wb") as fp: fp.write(lib.get_random_bytes(1024)) fs.add_file(os.path.join("test", "test.txt"), 1024) ct = lt.create_torrent(fs) lt.set_piece_hashes(ct, outer)
def test_add_tracker(self) -> None: fs = lt.file_storage() fs.add_file("test.txt", 1024) ct = lt.create_torrent(fs) ct.add_tracker("http://example.com") ct.set_hash(0, lib.get_random_bytes(20)) entry = ct.generate() self.assertEqual(entry[b"announce"], b"http://example.com")
def NewTorrent(pt): info = lt.torrent_info(pt) # This is necessary for some reason fs = lt.file_storage() lt.add_files(fs,PackagesDirectory) h = ses.add_torrent({"save_path": MasterDirectory, "storage_mode": lt.storage_mode_t.storage_mode_sparse, "ti": info, "storage": fs, "flags": 1}) #h = ses.add_torrent({"save_path": PackagesDirectory, "storage_mode": lt.storage_mode_t.storage_mode_sparse, "ti": info, "storage": fs }) print("New torrent added") return h
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
def test_exceptions(self) -> None: fs = lt.file_storage() fs.add_file("test.txt", 1024) ct = lt.create_torrent(fs) with tempfile.TemporaryDirectory() as path: with self.assertRaises(RuntimeError): lt.set_piece_hashes(ct, path) with self.assertRaises(RuntimeError): lt.set_piece_hashes(ct, path, lambda _: None)
def test_similar(self) -> None: fs = lt.file_storage() fs.add_file("test.txt", 1024) ct = lt.create_torrent(fs) ct.set_hash(0, lib.get_random_bytes(20)) sha1 = lt.sha1_hash(lib.get_random_bytes(20)) ct.add_similar_torrent(sha1) entry = ct.generate() self.assertEqual(entry[b"info"][b"similar"], [sha1.to_bytes()])
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
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
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 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)
def prepare_transmit( self, job ): # create the .torrent file from the given file fs = lt.file_storage() lt.add_files( fs, job.get_attr( iftfile.JOB_ATTR_SRC_NAME ) ) ct = lt.create_torrent( fs, job.get_attr( iftfile.JOB_ATTR_CHUNKSIZE ) ) ct.set_creator("iftd: " + self.name ) self.file_to_send = job.get_attr( iftfile.JOB_ATTR_SRC_NAME ) # if we were given a tracker or list of trackers, add them if job.get_attr( IFTBITTORRENT_TRACKER ) != None: if type(job.get_attr( IFTBITTORRENT_TRACKER )) == str: ct.add_tracker( job.get_attr( IFTBITTORRENT_TRACKER ), 0 ) if type(job.get_attr( IFTBITTORRENT_TRACKER )) == list: for tracker in job.get_attr( IFTBITTORRENT_TRACKER ): ct.add_tracker(tracker, 0) else: # add some default trackers ct.add_tracker("http://tracker.openbittorrent.com/announce", 0) ct.add_tracker("udp://tracker.openbittorrent.com:80/announce", 0) ct.add_tracker("http://tracker.publicbt.com/announce", 0) ct.add_tracker("udp://tracker.publicbt.com:80/announce", 0) # if we were given one or more http seeds, add them too if job.get_attr( IFTBITTORRENT_HTTP_SEEDS ) != None: if type(job.get_attr( IFTBITTORRENT_HTTP_SEEDS )) == str: ct.add_url_seed( job.get_attr( IFTBITTORRENT_HTTP_SEEDS ) ) if type(job.get_attr( IFTBITTORRENT_HTTP_SEEDS )) == list: for seed in job.get_attr( IFTBITTORRENT_HTTP_SEEDS ): ct.add_url_seed( seed ) lt.set_piece_hashes( ct, os.path.dirname( job.get_attr( iftfile.JOB_ATTR_SRC_NAME ) ) ) # encode the torrent into a .torrent buffer self.torrent_str = lt.bencode( ct.generate() ) # if given a torrent path, write out the torrent if job.get_attr( IFTBITTORRENT_TORRENT_PATH ) != None: try: fd = open( job.get_attr( IFTBITTORRENT_TORRENT_PATH ), "wb" ) fd.write( self.torrent_str ) fd.close() except Exception, inst: iftlog.exception( self.name + ": could not output torrent data to " + job.get_attr( IFTBITTORRENT_TORRENT_PATH ), inst) return E_IOERROR
def create_torrent(file_path,file_name): fs = lt.file_storage() lt.add_files(fs, file_path) t = lt.create_torrent(fs) t.add_tracker("udp://tracker.openbittorrent.com:80/announce", 0) t.set_creator('libtorrent %s' % lt.version) t.set_comment("Test") lt.set_piece_hashes(t, ".") torrent = t.generate() f = open(file_name, "wb") f.write(lt.bencode(torrent)) f.close()
def make_torrent(torrent_path, data_path): fs = libtorrent.file_storage() libtorrent.add_files(fs, data_path) t = libtorrent.create_torrent(fs) t.set_creator("ccas") libtorrent.set_piece_hashes(t, os.path.dirname(data_path)) tdata = t.generate() if not os.access(os.path.dirname(torrent_path), os.W_OK): os.makedirs(os.path.dirname(torrent_path)) with open(torrent_path, 'wb') as f: f.write(libtorrent.bencode(tdata)) return
def create_torrent_from_dir(directory, torrent_filename): fs = file_storage() add_files(fs, directory) t = create_torrent(fs) # t = create_torrent(fs, flags=17) # piece alignment t.set_priv(False) set_piece_hashes(t, os.path.dirname(directory)) torrent = t.generate() with open(torrent_filename, 'wb') as f: f.write(bencode(torrent)) infohash = torrent_info(torrent).info_hash().to_bytes() return torrent, infohash
def maketorrent(source): print "Making .torrent file for " + source + "..." fs = lt.file_storage() lt.add_files(fs, source) t = lt.create_torrent(fs) for ip in ip4_addresses(): trAddress = "http://" + ip + ":6969/announce" t.add_tracker(trAddress,0) t.set_creator('A bunny') lt.set_piece_hashes(t, ".") torrent = t.generate() f = open("t.torrent", "wb") f.write(lt.bencode(torrent)) f.close() print "Finished making .torrent file"
def make_torrent(file): import libtorrent as lt fs = lt.file_storage() piece_size = 256 * 1024 lt.add_files(fs, file.path) if file.real_name: fs.set_name(file.real_name) t = lt.create_torrent(fs, piece_size) lt.set_piece_hashes(t, file.storage_folder) t.add_url_seed(file.blob) t.set_creator('a2p') return lt.bencode(t.generate())
def magnet2t(link,tfile): sess = lt.session() params = { "save_path": './tfile/', "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) while (not handle.has_metadata()): time.sleep(5) print handle.has_metadata() torinfo = handle.get_torrent_info() fs = lt.file_storage() for f in torinfo.files(): fs.add_file(f) torfile = lt.create_torrent(fs) torfile.set_comment(torinfo.comment()) torfile.set_creator(torinfo.creator()) #for i in xrange(0, torinfo.num_pieces()): # hashes = torinfo.hash_for_piece(i) # torfile.set_hash(i, hashes) for url_seed in torinfo.url_seeds(): torfile.add_url_seed(url_seed) for http_seed in torinfo.http_seeds(): torfile.add_http_seed(http_seed) for node in torinfo.nodes(): torfile.add_node(node) for tracker in torinfo.trackers(): torfile.add_tracker(tracker) torfile.set_priv(torinfo.priv()) t = open(tfile, "wb") t.write(lt.bencode(torfile.generate())) t.close() print '%s generated!'% tfile
def create_torrent(self): # TODO check if root tarball_uid = self.get('tarball') cluster = Cluster(mongo_db = self._mongo_db) if not bool(tarball_uid): self._logger.error("No tarball in DB.") return None tarball = cluster.get('path') + "/torrents/" + tarball_uid + ".tgz" if not os.path.exists(tarball): self._logger.error("Wrong path in DB.") return None tracker_address = cluster.get('frontend_address') if tracker_address == '': self._logger.error("Tracker address needs to be configured.") return None tracker_port = cluster.get('frontend_port') if tracker_port == 0: self._logger.error("Tracker port needs to be configured.") return None user = cluster.get('user') if not user: self._logger.error("User needs to be configured.") return None #group = cluster.get('group') #if not group: # self._logger.error("Group needs to be configured.") # return None 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 = str(cluster.get('path')) + "/torrents/" + uid fs = libtorrent.file_storage() libtorrent.add_files(fs, os.path.basename(tarball)) t = libtorrent.create_torrent(fs) t.add_tracker("http://" + 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() self.set('torrent', str(uid)) os.chown(torrentfile, user_id, grp_id) shutil.move(torrentfile, torrentfile + ".torrent") os.chdir(old_cwd) return True
def create_torrent(input_path, output_path): input_path = os.path.abspath(input_path) input_dir = os.path.dirname(input_path) input_file_name = os.path.basename(input_path) output_path = os.path.abspath(output_path) fs = libtorrent.file_storage() file_size = os.path.getsize(input_path) fs.add_file(input_file_name, file_size) torrent = libtorrent.create_torrent(fs) torrent.set_creator("OpenFUN http://github.com/openfun/") for tracker in TRACKERS: torrent.add_tracker(tracker) libtorrent.set_piece_hashes(torrent, input_dir) with open(output_path, 'wb') as f: f.write(libtorrent.bencode(torrent.generate()))
def __finalize(self, path, olduid): if self.LOGLEVEL > 2: print 'finalize', path, olduid try: # try sha256 before making torrent old_tmp_fn = os.path.join(self.tmp, olduid).encode(FS_ENCODE) uid = sha256sum(old_tmp_fn, 4096) tmp_fn = os.path.join(self.tmp, uid).encode(FS_ENCODE) path = None if self.LOGLEVEL > 3: print 'olduid',olduid for k,v in self.open_files.items(): if self.LOGLEVEL > 3: print 'k',k if self.LOGLEVEL > 3: print 'v',v if olduid.strip() == v: if self.LOGLEVEL > 3: print 'found k',k path = k if self.LOGLEVEL > 3: print 'path',path if self.LOGLEVEL > 3: print 'old_tmp',old_tmp_fn if self.LOGLEVEL > 3: print 'sha_tmp_fn',tmp_fn os.rename(old_tmp_fn, tmp_fn) fs = libtorrent.file_storage() #print tmp_fn libtorrent.add_files(fs, tmp_fn) t = libtorrent.create_torrent(fs) t.set_creator("DelugeFS"); libtorrent.set_piece_hashes(t, self.tmp) tdata = t.generate() #print tdata fn = os.path.join(self.indexdir, path).encode(FS_ENCODE) with open(fn, 'wb') as f: f.write(libtorrent.bencode(tdata)) #print 'wrote', fn dat_dir = os.path.join(self.chunksdir, uid[:2]).encode(FS_ENCODE) if not os.path.isdir(dat_dir): try: os.mkdir(dat_dir) except: pass shutil.copyfile(tmp_fn, os.path.join(dat_dir, uid).encode(FS_ENCODE)) os.remove(tmp_fn) #print 'committing', fn self.__add_torrent(tdata, path) del self.open_files[path] except Exception as e: traceback.print_exc() raise e
def execute(self): if not self._torrent_data: raise ValueError('Missing file or directory path for torrent data') if not self._output_file: raise ValueError('Missing torrent output file') if os.path.exists(self._output_file) and not self._overwrite: self._println('Torrent file already exists') return input = os.path.abspath(self._torrent_data) fs = lt.file_storage() lt.add_files(fs, input) if fs.num_files() == 0: self._println('Error: no files added to torrent') return piece_length = self._calculate_piece_length() #pad_size_limit = 4 * 1024 * 1024 pad_size_limit = -1 t = lt.create_torrent(fs, piece_length, pad_size_limit) # TODO: support multiple tracker urls if not self._tracker: raise ValueError('Missing tracker URL') t.add_tracker(self._tracker) creator = 'terasaur seedbank %s' % lt.version t.set_creator(creator) if self._comment: t.set_comment(self._comment) t.set_priv(self._private) data_dir = os.path.split(input)[0] if self._show_progress: lt.set_piece_hashes(t, data_dir, lambda x: self._print('.')) self._println('') else: lt.set_piece_hashes(t, data_dir) self._write_output_file(t, self._output_file)
def make_torrent(path, save): fs = lt.file_storage() lt.add_files(fs, path) if fs.num_files() == 0: print 'no files added' sys.exit(1) input = os.path.abspath(path) basename = os.path.basename(path) t = lt.create_torrent(fs, 0, 4 * 1024 * 1024) t.add_tracker("http://10.0.1.5:8760/announce") t.set_creator('libtorrent %s' % lt.version) lt.set_piece_hashes(t, os.path.split(input)[0], lambda x: sys.stderr.write('.')) sys.stderr.write('\n') save = os.path.dirname(input) save = "%s/%s.torrent" % (save, basename) f=open(save, "wb") f.write(lt.bencode(t.generate())) f.close() print "the bt torrent file is store at %s" % save
def create_torrent(self, file_path): print "Creating .torrent for %s..." % file_path if os.path.exists(file_path): fs = libtorrent.file_storage() libtorrent.add_files(fs, str(file_path)) #Apparently file_path is unicode, and libtorrent does not like that. self.t = libtorrent.create_torrent(fs) #self.t.add_tracker(self.tracker) tpm_daemon -> tpm_server -> opentracker self.t.set_creator("tpmd 1.0") torrent_name = self.t.generate()["info"]["name"] with open("%s.torrent" % (torrent_name), "wb") as torrent_file: torrent_file.write(libtorrent.bencode(self.t.generate())) print "Torrent %s created" % torrent_name #implement integrity check here return True else: print "File does not exist"
def magnet2torrent(link, torrent_file): sess = libtorrent.session() params = { "save_path": tempfile.gettempdir(), "storage_mode":libtorrent.storage_mode_t.storage_mode_sparse, "paused": True, "auto_managed": True, "duplicate_is_error": True } handle = libtorrent.add_magnet_uri(sess, link, params) print "Loading magnet file" while True: s = handle.status() print "waiting..." if s.state != 2: t = handle.get_torrent_info() print "Saving torrent -=%s=-" % t.name() fs = libtorrent.file_storage() for i in t.files(): fs.add_file(i) print "\tFile: %s" % i.path ct = libtorrent.create_torrent(fs) for i in t.trackers(): print "\tTracker: %s, %s " % (i.url, i.tier) ct.add_tracker(i.url, i.tier) ct.set_creator(t.creator()) ct.set_comment(t.comment()) ct.set_priv(t.priv()) f = open(torrent_file, "wb") g = ct.generate() g["info"]["pieces"] = "".join([binascii.unhexlify("%s" % t.hash_for_piece(i)) for i in range(t.num_pieces())]) g["info"]["piece length"] = t.piece_length() g["info"]["length"] = t.total_size() f.write(libtorrent.bencode(g)) f.close() return time.sleep(1) # sleep for a second
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}
handle = lt.add_magnet_uri(ses, magnet, params) #ses.start_dht() print 'saving torrent file here : ' + output + " ..." while (not handle.has_metadata()): try: time.sleep(.1) except KeyboardInterrupt: print "Abrorting..." ses.pause() print "Cleanup dir " + tempdir shutil.rmtree(tempdir) sys.exit(0) 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()) 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) #Uncomment to Download the Torrent: # print 'starting torrent download...'
def _create_single_torrent(self, resource, _from, _to, use_sudo): fs = lt.file_storage() lt.add_files(fs, _from) self._create_torrent(resource, fs, _from)
def generate_torrent(self, shard_directory, piece_size=0, pad_size_limit=4 * 1024 * 1024, flags=1, comment='Storj - Be the Cloud.', creator='Storj', private=False, torrent_name='storj.torrent', save_path=".", verbose=False): """Creates a torrent with specified files. A torrent is created by determining the files that will be included, defining the torrent properties (such as DHT nodes), reading through all the torrent files, SHA-1ing all the data, setting the piece hashes and bencoding the torrent into a file. :param piece_size: The size of each piece in bytes. It must be a multiple of 16 kiB. If a piece size of 0 is specified, a piece_size will be calculated such that the torrent file is roughly 40 kB. :type piece_size: int :param shard_directory: The directory containing the shards you wish to include in the torrent. :type shard_directory: str :param pad_size_limit: If specified (other than -1), any file larger than the specified number of bytes will be preceeded by a pad file to align it with the start of a peice. The pad_file_limit is ignored unless the optimize flag is passed. Typically it doesn't make sense to set this any lower than 4kiB. :type pad_size_limit: int :param flags: Specifies options for the torrent creation. :type flags: int :param comment: Comment to be associated with torrent. :type comment: str :param creator: Creator to be associated with torrent. :type creator: str :param private: Whether torrent should be private or not. Should be false for DHT. :type private: bool :param torrent_name: The filename for your torrent. Will default to save_path location unless explicitly stated. :type torrent_name: str :param save_path: Save location for file. :type save_path: str :param verbose: Indicate if actions should be made verbosely or not. :type verbose: bool """ if piece_size % 16384 is not 0: raise StorjTorrentError( 'Torrent piece size must be 0 or a multiple of 16 kiB.') storage = lt.file_storage() directory = os.path.abspath(shard_directory) parent_directory = os.path.split(directory)[0] for root, dirs, files in os.walk(directory): if os.path.split(root)[1][0] == '.': continue for f in files: if f[0] in ['.', 'Thumbs.db']: continue filename = os.path.join(root[len(parent_directory) + 1:], f) size = os.path.getsize( os.path.join(parent_directory, filename)) if verbose: print '%10d kiB %s' % (size / 1024, filename) storage.add_file(filename, size) if storage.num_files() == 0: raise StorjTorrentError( 'No files were loaded from the specified directory.') torrent = lt.create_torrent(storage, piece_size, pad_size_limit, flags) torrent.set_comment(comment) torrent.set_creator(creator) torrent.set_priv(private) if verbose: sys.stderr.write('Setting piece hashes.') lt.set_piece_hashes( torrent, parent_directory, lambda x: sys.stderr.write('.')) sys.stderr.write('done!\n') else: lt.set_piece_hashes(torrent, parent_directory) """ Check the save path, if it is specified absolutely then parse it.""" if os.path.isabs(save_path): torrent_name = os.path.join(os.path.abspath(save_path), torrent_name) else: torrent_name = os.path.join(save_path, torrent_name) try: open(torrent_name, 'w') except IOError: raise StorjTorrentError( 'Bad torrent save path or name, unable to save.') with open(torrent_name, 'wb+') as torrent_file: torrent_file.write(lt.bencode(torrent.generate()))