1
// Copyright Daniel Wallin 2006. Use, modification and distribution is
2
// subject to the Boost Software License, Version 1.0. (See accompanying
3
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
#include <libtorrent/torrent_handle.hpp>
6
#include <boost/python.hpp>
7
#include <boost/lexical_cast.hpp>
10
using namespace boost::python;
11
using namespace libtorrent;
16
list url_seeds(torrent_handle& handle)
19
std::set<std::string> urls;
21
allow_threading_guard guard;
22
urls = handle.url_seeds();
25
for (std::set<std::string>::iterator i(urls.begin())
26
, end(urls.end()); i != end; ++i)
31
list piece_availability(torrent_handle& handle)
34
std::vector<int> avail;
36
allow_threading_guard guard;
37
handle.piece_availability(avail);
40
for (std::vector<int>::iterator i(avail.begin())
41
, end(avail.end()); i != end; ++i)
46
list piece_priorities(torrent_handle& handle)
49
std::vector<int> prio;
51
allow_threading_guard guard;
52
prio = handle.piece_priorities();
55
for (std::vector<int>::iterator i(prio.begin())
56
, end(prio.end()); i != end; ++i)
61
std::vector<announce_entry>::const_iterator begin_trackers(torrent_handle& i)
63
allow_threading_guard guard;
64
return i.trackers().begin();
67
std::vector<announce_entry>::const_iterator end_trackers(torrent_handle& i)
69
allow_threading_guard guard;
70
return i.trackers().end();
73
} // namespace unnamed
75
list file_progress(torrent_handle& handle)
80
allow_threading_guard guard;
81
p.reserve(handle.get_torrent_info().num_files());
82
handle.file_progress(p);
87
for (std::vector<float>::iterator i(p.begin()), e(p.end()); i != e; ++i)
93
list get_peer_info(torrent_handle const& handle)
95
std::vector<peer_info> pi;
98
allow_threading_guard guard;
99
handle.get_peer_info(pi);
104
for (std::vector<peer_info>::iterator i = pi.begin(); i != pi.end(); ++i)
110
void prioritize_pieces(torrent_handle& info, object o)
112
std::vector<int> result;
115
object iter_obj = object( handle<>( PyObject_GetIter( o.ptr() ) ));
118
object obj = extract<object>( iter_obj.attr( "next" )() );
119
result.push_back(extract<int const>( obj ));
122
catch( error_already_set )
125
info.prioritize_pieces(result);
130
void prioritize_files(torrent_handle& info, object o)
132
std::vector<int> result;
135
object iter_obj = object( handle<>( PyObject_GetIter( o.ptr() ) ));
138
object obj = extract<object>( iter_obj.attr( "next" )() );
139
result.push_back(extract<int const>( obj ));
142
catch( error_already_set )
145
info.prioritize_files(result);
151
void replace_trackers(torrent_handle& info, object trackers)
153
object iter(trackers.attr("__iter__")());
155
std::vector<announce_entry> result;
159
handle<> entry(allow_null(PyIter_Next(iter.ptr())));
161
if (entry == handle<>())
164
result.push_back(extract<announce_entry const&>(object(entry)));
167
allow_threading_guard guard;
168
info.replace_trackers(result);
171
list get_download_queue(torrent_handle& handle)
175
std::vector<partial_piece_info> downloading;
178
allow_threading_guard guard;
179
handle.get_download_queue(downloading);
182
for (std::vector<partial_piece_info>::iterator i = downloading.begin()
183
, end(downloading.end()); i != end; ++i)
186
partial_piece["piece_index"] = i->piece_index;
187
partial_piece["blocks_in_piece"] = i->blocks_in_piece;
189
for (int k = 0; k < i->blocks_in_piece; ++k)
192
block_info["state"] = i->blocks[k].state;
193
block_info["num_peers"] = i->blocks[k].num_peers;
194
block_info["bytes_progress"] = i->blocks[k].bytes_progress;
195
block_info["block_size"] = i->blocks[k].block_size;
196
block_info["peer"] = std::make_pair(
197
boost::lexical_cast<std::string>(i->blocks[k].peer.address()), i->blocks[k].peer.port());
198
block_list.append(block_info);
200
partial_piece["blocks"] = block_list;
202
ret.append(partial_piece);
210
tcp::endpoint tuple_to_endpoint(tuple const& t)
212
return tcp::endpoint(address::from_string(extract<std::string>(t[0])), extract<int>(t[1]));
216
void force_reannounce(torrent_handle& th, int s)
218
th.force_reannounce(boost::posix_time::seconds(s));
221
void connect_peer(torrent_handle& th, tuple ip, int source)
223
th.connect_peer(tuple_to_endpoint(ip), source);
226
void set_peer_upload_limit(torrent_handle& th, tuple const& ip, int limit)
228
th.set_peer_upload_limit(tuple_to_endpoint(ip), limit);
231
void set_peer_download_limit(torrent_handle& th, tuple const& ip, int limit)
233
th.set_peer_download_limit(tuple_to_endpoint(ip), limit);
236
void bind_torrent_handle()
238
void (torrent_handle::*force_reannounce0)() const = &torrent_handle::force_reannounce;
240
int (torrent_handle::*piece_priority0)(int) const = &torrent_handle::piece_priority;
241
void (torrent_handle::*piece_priority1)(int, int) const = &torrent_handle::piece_priority;
243
#ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES
244
bool (torrent_handle::*resolve_countries0)() const = &torrent_handle::resolve_countries;
245
void (torrent_handle::*resolve_countries1)(bool) = &torrent_handle::resolve_countries;
248
return_value_policy<copy_const_reference> copy;
250
#define _ allow_threads
252
class_<torrent_handle>("torrent_handle")
253
.def("get_peer_info", get_peer_info)
254
.def("status", _(&torrent_handle::status))
255
.def("get_download_queue", get_download_queue)
256
.def("file_progress", file_progress)
257
.def("trackers", range(begin_trackers, end_trackers))
258
.def("replace_trackers", replace_trackers)
259
.def("add_url_seed", _(&torrent_handle::add_url_seed))
260
.def("remove_url_seed", _(&torrent_handle::remove_url_seed))
261
.def("url_seeds", url_seeds)
262
.def("has_metadata", _(&torrent_handle::has_metadata))
263
.def("get_torrent_info", _(&torrent_handle::get_torrent_info), return_internal_reference<>())
264
.def("is_valid", _(&torrent_handle::is_valid))
265
.def("is_seed", _(&torrent_handle::is_seed))
266
.def("is_finished", _(&torrent_handle::is_finished))
267
.def("is_paused", _(&torrent_handle::is_paused))
268
.def("pause", _(&torrent_handle::pause))
269
.def("resume", _(&torrent_handle::resume))
270
#ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES
271
.def("resolve_countries", _(resolve_countries0))
272
.def("resolve_countries", _(resolve_countries1))
275
.def("filter_piece", _(&torrent_handle::filter_piece))
276
.def("is_piece_filtered", _(&torrent_handle::is_piece_filtered))
278
.def("piece_availability", piece_availability)
279
.def("piece_priority", _(piece_priority0))
280
.def("piece_priority", _(piece_priority1))
281
.def("prioritize_pieces", prioritize_pieces)
282
.def("piece_prioritize", piece_priorities)
283
.def("prioritize_files", prioritize_files)
284
.def("use_interface", &torrent_handle::use_interface)
285
.def("write_resume_data", _(&torrent_handle::write_resume_data))
286
.def("force_reannounce", _(force_reannounce0))
287
.def("force_reannounce", force_reannounce)
288
.def("scrape_tracker", _(&torrent_handle::scrape_tracker))
289
.def("name", _(&torrent_handle::name))
290
.def("set_upload_limit", _(&torrent_handle::set_upload_limit))
291
.def("upload_limit", _(&torrent_handle::upload_limit))
292
.def("set_download_limit", _(&torrent_handle::set_download_limit))
293
.def("download_limit", _(&torrent_handle::download_limit))
294
.def("set_sequenced_download_threshold", _(&torrent_handle::set_sequenced_download_threshold))
295
.def("set_peer_upload_limit", set_peer_upload_limit)
296
.def("set_peer_download_limit", set_peer_download_limit)
297
.def("connect_peer", connect_peer)
298
.def("set_ratio", _(&torrent_handle::set_ratio))
299
.def("save_path", _(&torrent_handle::save_path))
300
.def("set_max_uploads", _(&torrent_handle::set_max_uploads))
301
.def("set_max_connections", _(&torrent_handle::set_max_connections))
302
.def("set_tracker_login", _(&torrent_handle::set_tracker_login))
303
.def("move_storage", _(&torrent_handle::move_storage))
304
.def("info_hash", _(&torrent_handle::info_hash), copy)