2007-01-10 17:11:43 +01:00
|
|
|
// Copyright Daniel Wallin 2006. Use, modification and distribution is
|
|
|
|
// subject to the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
|
2016-04-08 04:45:23 +02:00
|
|
|
#include "boost_python.hpp"
|
2016-09-01 03:42:18 +02:00
|
|
|
#include <memory>
|
2015-11-20 05:37:45 +01:00
|
|
|
|
2015-09-18 06:23:45 +02:00
|
|
|
#include "libtorrent/torrent_info.hpp"
|
2012-04-04 16:51:55 +02:00
|
|
|
#include "libtorrent/session_settings.hpp"
|
2014-07-06 21:18:00 +02:00
|
|
|
#include "libtorrent/time.hpp"
|
2014-10-04 21:54:12 +02:00
|
|
|
#include "libtorrent/socket_io.hpp"
|
2015-09-18 06:23:45 +02:00
|
|
|
#include "libtorrent/announce_entry.hpp"
|
2014-09-13 00:38:07 +02:00
|
|
|
#include "bytes.hpp"
|
2007-01-10 17:11:43 +01:00
|
|
|
|
2016-06-19 01:24:27 +02:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push)
|
|
|
|
// warning C4996: X: was declared deprecated
|
|
|
|
#pragma warning( disable : 4996 )
|
|
|
|
#endif
|
|
|
|
|
2007-01-10 17:11:43 +01:00
|
|
|
using namespace boost::python;
|
|
|
|
using namespace libtorrent;
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2008-09-30 20:41:43 +02:00
|
|
|
std::vector<announce_entry>::const_iterator begin_trackers(torrent_info& i)
|
|
|
|
{
|
|
|
|
return i.trackers().begin();
|
|
|
|
}
|
2007-01-10 17:11:43 +01:00
|
|
|
|
2008-09-30 20:41:43 +02:00
|
|
|
std::vector<announce_entry>::const_iterator end_trackers(torrent_info& i)
|
|
|
|
{
|
|
|
|
return i.trackers().end();
|
|
|
|
}
|
2007-01-10 17:11:43 +01:00
|
|
|
|
2008-09-30 20:41:43 +02:00
|
|
|
void add_node(torrent_info& ti, char const* hostname, int port)
|
|
|
|
{
|
|
|
|
ti.add_node(std::make_pair(hostname, port));
|
|
|
|
}
|
2007-01-10 17:11:43 +01:00
|
|
|
|
2008-09-30 20:41:43 +02:00
|
|
|
list nodes(torrent_info const& ti)
|
|
|
|
{
|
|
|
|
list result;
|
2007-01-10 17:11:43 +01:00
|
|
|
|
2008-09-30 20:41:43 +02:00
|
|
|
typedef std::vector<std::pair<std::string, int> > list_type;
|
2007-01-10 17:11:43 +01:00
|
|
|
|
2008-09-30 20:41:43 +02:00
|
|
|
for (list_type::const_iterator i = ti.nodes().begin(); i != ti.nodes().end(); ++i)
|
|
|
|
{
|
2015-04-04 06:50:20 +02:00
|
|
|
result.append(boost::python::make_tuple(i->first, i->second));
|
2008-09-30 20:41:43 +02:00
|
|
|
}
|
2007-01-10 17:11:43 +01:00
|
|
|
|
2008-09-30 20:41:43 +02:00
|
|
|
return result;
|
|
|
|
}
|
2007-01-10 17:11:43 +01:00
|
|
|
|
2013-05-03 10:24:26 +02:00
|
|
|
list get_web_seeds(torrent_info const& ti)
|
|
|
|
{
|
|
|
|
std::vector<web_seed_entry> const& ws = ti.web_seeds();
|
|
|
|
list ret;
|
|
|
|
for (std::vector<web_seed_entry>::const_iterator i = ws.begin()
|
|
|
|
, end(ws.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
dict d;
|
|
|
|
d["url"] = i->url;
|
|
|
|
d["type"] = i->type;
|
|
|
|
d["auth"] = i->auth;
|
|
|
|
d["extra_headers"] = i->extra_headers;
|
|
|
|
ret.append(d);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
list get_merkle_tree(torrent_info const& ti)
|
|
|
|
{
|
|
|
|
std::vector<sha1_hash> const& mt = ti.merkle_tree();
|
|
|
|
list ret;
|
|
|
|
for (std::vector<sha1_hash>::const_iterator i = mt.begin()
|
|
|
|
, end(mt.end()); i != end; ++i)
|
|
|
|
{
|
2014-09-13 00:38:07 +02:00
|
|
|
ret.append(bytes(i->to_string()));
|
2013-05-03 10:24:26 +02:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_merkle_tree(torrent_info& ti, list hashes)
|
|
|
|
{
|
|
|
|
std::vector<sha1_hash> h;
|
2016-06-19 01:24:27 +02:00
|
|
|
for (int i = 0, e = int(len(hashes)); i < e; ++i)
|
2016-08-13 13:04:53 +02:00
|
|
|
h.push_back(sha1_hash(bytes(extract<bytes>(hashes[i])).arr.data()));
|
2013-05-03 10:24:26 +02:00
|
|
|
|
|
|
|
ti.set_merkle_tree(h);
|
|
|
|
}
|
|
|
|
|
2016-03-31 06:19:23 +02:00
|
|
|
bytes hash_for_piece(torrent_info const& ti, int i)
|
2012-03-11 07:57:35 +01:00
|
|
|
{
|
2016-03-31 06:19:23 +02:00
|
|
|
return bytes(ti.hash_for_piece(i).to_string());
|
2012-03-11 07:57:35 +01:00
|
|
|
}
|
|
|
|
|
2016-03-31 06:19:23 +02:00
|
|
|
bytes metadata(torrent_info const& ti)
|
2015-04-05 00:37:11 +02:00
|
|
|
{
|
2016-03-31 06:19:23 +02:00
|
|
|
return bytes(ti.metadata().get(), ti.metadata_size());
|
2008-09-30 20:41:43 +02:00
|
|
|
}
|
2008-10-28 07:29:30 +01:00
|
|
|
|
2016-06-18 20:01:38 +02:00
|
|
|
list map_block(torrent_info& ti, int piece, std::int64_t offset, int size)
|
2008-12-14 21:51:22 +01:00
|
|
|
{
|
|
|
|
std::vector<file_slice> p = ti.map_block(piece, offset, size);
|
|
|
|
list result;
|
|
|
|
|
|
|
|
for (std::vector<file_slice>::iterator i(p.begin()), e(p.end()); i != e; ++i)
|
|
|
|
result.append(*i);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-09-01 01:08:49 +02:00
|
|
|
int get_tier(announce_entry const& ae) { return ae.tier; }
|
|
|
|
void set_tier(announce_entry& ae, int v) { ae.tier = v; }
|
2016-06-15 02:10:18 +02:00
|
|
|
int get_fail_limit(announce_entry const& ae) { return ae.fail_limit; }
|
2011-06-18 19:30:44 +02:00
|
|
|
void set_fail_limit(announce_entry& ae, int l) { ae.fail_limit = l; }
|
2016-06-15 02:10:18 +02:00
|
|
|
int get_fails(announce_entry const& ae) { return ae.fails; }
|
|
|
|
int get_source(announce_entry const& ae) { return ae.source; }
|
2011-06-18 19:30:44 +02:00
|
|
|
bool get_verified(announce_entry const& ae) { return ae.verified; }
|
|
|
|
bool get_updating(announce_entry const& ae) { return ae.updating; }
|
|
|
|
bool get_start_sent(announce_entry const& ae) { return ae.start_sent; }
|
|
|
|
bool get_complete_sent(announce_entry const& ae) { return ae.complete_sent; }
|
|
|
|
bool get_send_stats(announce_entry const& ae) { return ae.send_stats; }
|
|
|
|
|
2015-01-21 16:46:12 +01:00
|
|
|
#if !defined TORRENT_NO_DEPRECATE
|
2016-06-18 20:01:38 +02:00
|
|
|
std::int64_t get_size(file_entry const& fe) { return fe.size; }
|
|
|
|
std::int64_t get_offset(file_entry const& fe) { return fe.offset; }
|
|
|
|
std::int64_t get_file_base(file_entry const& fe) { return fe.file_base; }
|
2013-03-29 03:46:12 +01:00
|
|
|
void set_file_base(file_entry& fe, int b) { fe.file_base = b; }
|
2011-06-18 19:30:44 +02:00
|
|
|
bool get_pad_file(file_entry const& fe) { return fe.pad_file; }
|
|
|
|
bool get_executable_attribute(file_entry const& fe) { return fe.executable_attribute; }
|
|
|
|
bool get_hidden_attribute(file_entry const& fe) { return fe.hidden_attribute; }
|
|
|
|
bool get_symlink_attribute(file_entry const& fe) { return fe.symlink_attribute; }
|
2015-01-21 16:46:12 +01:00
|
|
|
#endif
|
2010-11-25 00:49:22 +01:00
|
|
|
|
2007-01-10 17:11:43 +01:00
|
|
|
} // namespace unnamed
|
|
|
|
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<torrent_info> buffer_constructor0(char const* buf, int len, int flags)
|
2014-11-19 10:23:08 +01:00
|
|
|
{
|
|
|
|
error_code ec;
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<torrent_info> ret = std::make_shared<torrent_info>(buf
|
|
|
|
, len, ec, flags);
|
2014-11-19 10:23:08 +01:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2016-05-15 06:33:06 +02:00
|
|
|
if (ec) throw system_error(ec);
|
2014-11-19 10:23:08 +01:00
|
|
|
#endif
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<torrent_info> buffer_constructor1(char const* buf, int len)
|
2015-07-26 01:40:07 +02:00
|
|
|
{
|
|
|
|
return buffer_constructor0(buf, len, 0);
|
|
|
|
}
|
|
|
|
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<torrent_info> file_constructor0(std::string const& filename, int flags)
|
2014-11-19 10:23:08 +01:00
|
|
|
{
|
|
|
|
error_code ec;
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<torrent_info> ret = std::make_shared<torrent_info>(filename
|
|
|
|
, ec, flags);
|
2014-11-19 10:23:08 +01:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2016-05-15 06:33:06 +02:00
|
|
|
if (ec) throw system_error(ec);
|
2014-11-19 10:23:08 +01:00
|
|
|
#endif
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<torrent_info> file_constructor1(std::string const& filename)
|
2015-07-26 01:40:07 +02:00
|
|
|
{
|
|
|
|
return file_constructor0(filename, 0);
|
|
|
|
}
|
|
|
|
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<torrent_info> bencoded_constructor0(entry const& ent, int flags)
|
2015-07-26 01:40:07 +02:00
|
|
|
{
|
|
|
|
std::vector<char> buf;
|
|
|
|
bencode(std::back_inserter(buf), ent);
|
|
|
|
|
|
|
|
bdecode_node e;
|
|
|
|
error_code ec;
|
|
|
|
if (buf.size() == 0 || bdecode(&buf[0], &buf[0] + buf.size(), e, ec) != 0)
|
|
|
|
{
|
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2016-05-15 06:33:06 +02:00
|
|
|
throw system_error(ec);
|
2015-07-26 01:40:07 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<torrent_info> ret = std::make_shared<torrent_info>(e
|
|
|
|
, ec, flags);
|
2015-07-26 01:40:07 +02:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2016-05-15 06:33:06 +02:00
|
|
|
if (ec) throw system_error(ec);
|
2015-07-26 01:40:07 +02:00
|
|
|
#endif
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-09-01 03:42:18 +02:00
|
|
|
std::shared_ptr<torrent_info> bencoded_constructor1(entry const& ent)
|
2015-07-26 01:40:07 +02:00
|
|
|
{
|
|
|
|
return bencoded_constructor0(ent, 0);
|
|
|
|
}
|
|
|
|
|
2007-01-10 17:11:43 +01:00
|
|
|
void bind_torrent_info()
|
|
|
|
{
|
|
|
|
return_value_policy<copy_const_reference> copy;
|
|
|
|
|
2009-07-16 09:10:58 +02:00
|
|
|
void (torrent_info::*rename_file0)(int, std::string const&) = &torrent_info::rename_file;
|
2013-08-28 04:38:31 +02:00
|
|
|
#if TORRENT_USE_WSTRING && !defined TORRENT_NO_DEPRECATE
|
2009-07-16 09:10:58 +02:00
|
|
|
void (torrent_info::*rename_file1)(int, std::wstring const&) = &torrent_info::rename_file;
|
|
|
|
#endif
|
|
|
|
|
2008-12-14 21:51:22 +01:00
|
|
|
class_<file_slice>("file_slice")
|
|
|
|
.def_readwrite("file_index", &file_slice::file_index)
|
|
|
|
.def_readwrite("offset", &file_slice::offset)
|
|
|
|
.def_readwrite("size", &file_slice::size)
|
|
|
|
;
|
|
|
|
|
2016-09-01 03:42:18 +02:00
|
|
|
class_<torrent_info, std::shared_ptr<torrent_info> >("torrent_info", no_init)
|
2011-06-08 05:40:14 +02:00
|
|
|
.def(init<sha1_hash const&, int>((arg("info_hash"), arg("flags") = 0)))
|
2016-01-05 07:32:04 +01:00
|
|
|
.def("__init__", make_constructor(&bencoded_constructor0))
|
|
|
|
.def("__init__", make_constructor(&bencoded_constructor1))
|
2015-07-26 01:40:07 +02:00
|
|
|
.def("__init__", make_constructor(&buffer_constructor0))
|
|
|
|
.def("__init__", make_constructor(&buffer_constructor1))
|
|
|
|
.def("__init__", make_constructor(&file_constructor0))
|
|
|
|
.def("__init__", make_constructor(&file_constructor1))
|
2014-07-06 21:18:00 +02:00
|
|
|
.def(init<torrent_info const&>((arg("ti"))))
|
2014-11-19 10:23:08 +01:00
|
|
|
|
2013-08-28 04:38:31 +02:00
|
|
|
#if TORRENT_USE_WSTRING && !defined TORRENT_NO_DEPRECATE
|
2011-06-08 05:40:14 +02:00
|
|
|
.def(init<std::wstring, int>((arg("file"), arg("flags") = 0)))
|
2009-10-29 07:45:46 +01:00
|
|
|
#endif
|
2008-10-28 07:29:30 +01:00
|
|
|
|
2010-08-03 05:00:54 +02:00
|
|
|
.def("add_tracker", &torrent_info::add_tracker, arg("url"))
|
2007-01-10 17:11:43 +01:00
|
|
|
.def("add_url_seed", &torrent_info::add_url_seed)
|
2013-05-03 10:24:26 +02:00
|
|
|
.def("add_http_seed", &torrent_info::add_http_seed)
|
|
|
|
.def("web_seeds", get_web_seeds)
|
2007-01-10 17:11:43 +01:00
|
|
|
|
|
|
|
.def("name", &torrent_info::name, copy)
|
|
|
|
.def("comment", &torrent_info::comment, copy)
|
|
|
|
.def("creator", &torrent_info::creator, copy)
|
|
|
|
.def("total_size", &torrent_info::total_size)
|
|
|
|
.def("piece_length", &torrent_info::piece_length)
|
|
|
|
.def("num_pieces", &torrent_info::num_pieces)
|
|
|
|
.def("info_hash", &torrent_info::info_hash, copy)
|
2012-03-11 07:57:35 +01:00
|
|
|
.def("hash_for_piece", &hash_for_piece)
|
2013-05-03 10:24:26 +02:00
|
|
|
.def("merkle_tree", get_merkle_tree)
|
|
|
|
.def("set_merkle_tree", set_merkle_tree)
|
2007-01-10 17:11:43 +01:00
|
|
|
.def("piece_size", &torrent_info::piece_size)
|
2008-03-28 22:37:35 +01:00
|
|
|
|
2015-01-21 16:46:12 +01:00
|
|
|
.def("num_files", &torrent_info::num_files)
|
2009-07-16 09:10:58 +02:00
|
|
|
.def("rename_file", rename_file0)
|
2015-01-21 16:46:12 +01:00
|
|
|
.def("remap_files", &torrent_info::remap_files)
|
|
|
|
.def("files", &torrent_info::files, return_internal_reference<>())
|
|
|
|
.def("orig_files", &torrent_info::orig_files, return_internal_reference<>())
|
|
|
|
#if !defined TORRENT_NO_DEPRECATE
|
|
|
|
.def("file_at", &torrent_info::file_at)
|
|
|
|
.def("file_at_offset", &torrent_info::file_at_offset)
|
|
|
|
#if TORRENT_USE_WSTRING
|
2009-07-16 09:10:58 +02:00
|
|
|
.def("rename_file", rename_file1)
|
2015-01-21 16:46:12 +01:00
|
|
|
#endif // TORRENT_USE_WSTRING
|
|
|
|
#endif // TORRENT_NO_DEPRECATE
|
2007-01-10 17:11:43 +01:00
|
|
|
|
|
|
|
.def("priv", &torrent_info::priv)
|
|
|
|
.def("trackers", range(begin_trackers, end_trackers))
|
|
|
|
|
|
|
|
.def("creation_date", &torrent_info::creation_date)
|
|
|
|
|
|
|
|
.def("add_node", &add_node)
|
|
|
|
.def("nodes", &nodes)
|
2008-09-30 20:41:43 +02:00
|
|
|
.def("metadata", &metadata)
|
|
|
|
.def("metadata_size", &torrent_info::metadata_size)
|
2008-12-14 21:51:22 +01:00
|
|
|
.def("map_block", map_block)
|
|
|
|
.def("map_file", &torrent_info::map_file)
|
2007-01-10 17:11:43 +01:00
|
|
|
;
|
2008-03-28 22:37:35 +01:00
|
|
|
|
2015-01-21 16:46:12 +01:00
|
|
|
#if !defined TORRENT_NO_DEPRECATE
|
2007-01-10 17:11:43 +01:00
|
|
|
class_<file_entry>("file_entry")
|
2010-11-29 06:44:29 +01:00
|
|
|
.def_readwrite("path", &file_entry::path)
|
|
|
|
.def_readwrite("symlink_path", &file_entry::symlink_path)
|
|
|
|
.def_readwrite("filehash", &file_entry::filehash)
|
|
|
|
.def_readwrite("mtime", &file_entry::mtime)
|
2010-11-25 00:49:22 +01:00
|
|
|
.add_property("pad_file", &get_pad_file)
|
|
|
|
.add_property("executable_attribute", &get_executable_attribute)
|
|
|
|
.add_property("hidden_attribute", &get_hidden_attribute)
|
|
|
|
.add_property("symlink_attribute", &get_symlink_attribute)
|
|
|
|
.add_property("offset", &get_offset)
|
|
|
|
.add_property("size", &get_size)
|
2013-03-29 03:46:12 +01:00
|
|
|
.add_property("file_base", &get_file_base, &set_file_base)
|
2007-01-10 17:11:43 +01:00
|
|
|
;
|
2015-01-21 16:46:12 +01:00
|
|
|
#endif
|
2007-01-10 17:11:43 +01:00
|
|
|
|
|
|
|
class_<announce_entry>("announce_entry", init<std::string const&>())
|
|
|
|
.def_readwrite("url", &announce_entry::url)
|
2011-06-18 19:30:44 +02:00
|
|
|
.add_property("tier", &get_tier, &set_tier)
|
|
|
|
.add_property("fail_limit", &get_fail_limit, &set_fail_limit)
|
2010-08-22 18:45:12 +02:00
|
|
|
.add_property("fails", &get_fails)
|
|
|
|
.add_property("source", &get_source)
|
2009-12-21 23:02:34 +01:00
|
|
|
.add_property("verified", &get_verified)
|
|
|
|
.add_property("updating", &get_updating)
|
|
|
|
.add_property("start_sent", &get_start_sent)
|
|
|
|
.add_property("complete_sent", &get_complete_sent)
|
|
|
|
.add_property("send_stats", &get_send_stats)
|
|
|
|
|
|
|
|
.def("reset", &announce_entry::reset)
|
|
|
|
.def("can_announce", &announce_entry::can_announce)
|
|
|
|
.def("is_working", &announce_entry::is_working)
|
|
|
|
.def("trim", &announce_entry::trim)
|
2007-01-10 17:11:43 +01:00
|
|
|
;
|
2010-09-05 18:49:03 +02:00
|
|
|
|
|
|
|
enum_<announce_entry::tracker_source>("tracker_source")
|
|
|
|
.value("source_torrent", announce_entry::source_torrent)
|
|
|
|
.value("source_client", announce_entry::source_client)
|
|
|
|
.value("source_magnet_link", announce_entry::source_magnet_link)
|
|
|
|
.value("source_tex", announce_entry::source_tex)
|
|
|
|
;
|
2013-07-17 01:26:07 +02:00
|
|
|
|
2016-09-01 03:42:18 +02:00
|
|
|
implicitly_convertible<std::shared_ptr<torrent_info>, std::shared_ptr<const torrent_info>>();
|
|
|
|
boost::python::register_ptr_to_python<std::shared_ptr<const torrent_info>>();
|
2007-01-10 17:11:43 +01:00
|
|
|
}
|
2010-09-05 18:49:03 +02:00
|
|
|
|
2016-06-19 01:24:27 +02:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|