From eaedc2283197bb526c317feb0b455375ff13bc7a Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sun, 10 Apr 2011 08:41:07 +0000 Subject: [PATCH] added make_torrent.py example and polished the python binding for creating torrents --- bindings/python/make_torrent.py | 27 ++++++++++++++++++++++++++ bindings/python/src/create_torrent.cpp | 12 ++++++++++-- include/libtorrent/create_torrent.hpp | 3 ++- 3 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 bindings/python/make_torrent.py diff --git a/bindings/python/make_torrent.py b/bindings/python/make_torrent.py new file mode 100644 index 000000000..90c19b3d2 --- /dev/null +++ b/bindings/python/make_torrent.py @@ -0,0 +1,27 @@ +#!/bin/python + +import sys +import os +import libtorrent + +if len(sys.argv) < 3: + print 'usage make_torrent.py file tracker-url' + sys.exit(1) + +input = os.path.abspath(sys.argv[1]) + +fs = libtorrent.file_storage() +libtorrent.add_files(fs, input) +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, os.path.split(input)[0], lambda x: sys.stderr.write('.')) +sys.stderr.write('\n') + +print libtorrent.bencode(t.generate()) diff --git a/bindings/python/src/create_torrent.cpp b/bindings/python/src/create_torrent.cpp index e522c999a..85f736dfc 100644 --- a/bindings/python/src/create_torrent.cpp +++ b/bindings/python/src/create_torrent.cpp @@ -103,7 +103,8 @@ void bind_create_torrent() class_("create_torrent", no_init) .def(init()) - .def(init()) + .def(init((arg("storage"), arg("piece_size") = 0 + , arg("pad_file_limit") = -1, arg("flags") = int(libtorrent::create_torrent::optimize)))) .def("generate", &create_torrent::generate) @@ -113,7 +114,7 @@ void bind_create_torrent() .def("set_hash", &set_hash) .def("add_url_seed", &create_torrent::add_url_seed) .def("add_node", &add_node) - .def("add_tracker", &create_torrent::add_tracker) + .def("add_tracker", &create_torrent::add_tracker, (arg("announce_url"), arg("tier") = 0)) .def("set_priv", &create_torrent::set_priv) .def("num_pieces", &create_torrent::num_pieces) .def("piece_length", &create_torrent::piece_length) @@ -121,6 +122,13 @@ void bind_create_torrent() .def("priv", &create_torrent::priv) ; + enum_("create_torrent_flags_t") + .value("optimize", create_torrent::optimize) + .value("merkle", create_torrent::merkle) + .value("modification_time", create_torrent::modification_time) + .value("symlinks", create_torrent::symlinks) + ; + def("add_files", add_files0, (arg("fs"), arg("path"), arg("flags") = 0)); def("set_piece_hashes", set_piece_hashes0); def("set_piece_hashes", set_piece_hashes_callback); diff --git a/include/libtorrent/create_torrent.hpp b/include/libtorrent/create_torrent.hpp index 575db15c2..35442c468 100644 --- a/include/libtorrent/create_torrent.hpp +++ b/include/libtorrent/create_torrent.hpp @@ -65,7 +65,8 @@ namespace libtorrent struct TORRENT_EXPORT create_torrent { - enum { + enum flags_t + { optimize = 1 , merkle = 2 , modification_time = 4