AUTO_LT_SYNC
[tore.git] / libtorrent / bindings / python / src / torrent_handle.cpp
bloba4f1953e2fadfb90511a49745b8dcacb823c4f64
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/python/tuple.hpp>
8 #include <boost/lexical_cast.hpp>
9 #include "gil.hpp"
11 using namespace boost::python;
12 using namespace libtorrent;
14 namespace
17 list url_seeds(torrent_handle& handle)
19 list ret;
20 std::set<std::string> urls;
22 allow_threading_guard guard;
23 urls = handle.url_seeds();
26 for (std::set<std::string>::iterator i(urls.begin())
27 , end(urls.end()); i != end; ++i)
28 ret.append(*i);
29 return ret;
32 list piece_availability(torrent_handle& handle)
34 list ret;
35 std::vector<int> avail;
37 allow_threading_guard guard;
38 handle.piece_availability(avail);
41 for (std::vector<int>::iterator i(avail.begin())
42 , end(avail.end()); i != end; ++i)
43 ret.append(*i);
44 return ret;
47 list piece_priorities(torrent_handle& handle)
49 list ret;
50 std::vector<int> prio;
52 allow_threading_guard guard;
53 prio = handle.piece_priorities();
56 for (std::vector<int>::iterator i(prio.begin())
57 , end(prio.end()); i != end; ++i)
58 ret.append(*i);
59 return ret;
62 std::vector<announce_entry>::const_iterator begin_trackers(torrent_handle& i)
64 allow_threading_guard guard;
65 return i.trackers().begin();
68 std::vector<announce_entry>::const_iterator end_trackers(torrent_handle& i)
70 allow_threading_guard guard;
71 return i.trackers().end();
74 } // namespace unnamed
76 list file_progress(torrent_handle& handle)
78 std::vector<size_type> p;
81 allow_threading_guard guard;
82 p.reserve(handle.get_torrent_info().num_files());
83 handle.file_progress(p);
86 list result;
88 for (std::vector<size_type>::iterator i(p.begin()), e(p.end()); i != e; ++i)
89 result.append(*i);
91 return result;
94 list get_peer_info(torrent_handle const& handle)
96 std::vector<peer_info> pi;
99 allow_threading_guard guard;
100 handle.get_peer_info(pi);
103 list result;
105 for (std::vector<peer_info>::iterator i = pi.begin(); i != pi.end(); ++i)
106 result.append(*i);
108 return result;
111 void prioritize_pieces(torrent_handle& info, object o)
113 std::vector<int> result;
116 object iter_obj = object( handle<>( PyObject_GetIter( o.ptr() ) ));
117 while( 1 )
119 object obj = extract<object>( iter_obj.attr( "next" )() );
120 result.push_back(extract<int const>( obj ));
123 catch( error_already_set )
125 PyErr_Clear();
126 info.prioritize_pieces(result);
127 return;
131 void prioritize_files(torrent_handle& info, object o)
133 std::vector<int> result;
136 object iter_obj = object( handle<>( PyObject_GetIter( o.ptr() ) ));
137 while( 1 )
139 object obj = extract<object>( iter_obj.attr( "next" )() );
140 result.push_back(extract<int const>( obj ));
143 catch( error_already_set )
145 PyErr_Clear();
146 info.prioritize_files(result);
147 return;
151 list file_priorities(torrent_handle& handle)
153 list ret;
154 std::vector<int> priorities = handle.file_priorities();
156 for (std::vector<int>::iterator i = priorities.begin(); i != priorities.end(); ++i)
157 ret.append(*i);
159 return ret;
162 void replace_trackers(torrent_handle& info, object trackers)
164 object iter(trackers.attr("__iter__")());
166 std::vector<announce_entry> result;
168 for (;;)
170 handle<> entry(allow_null(PyIter_Next(iter.ptr())));
172 if (entry == handle<>())
173 break;
175 result.push_back(extract<announce_entry const&>(object(entry)));
178 allow_threading_guard guard;
179 info.replace_trackers(result);
182 list get_download_queue(torrent_handle& handle)
184 using boost::python::make_tuple;
186 list ret;
188 std::vector<partial_piece_info> downloading;
191 allow_threading_guard guard;
192 handle.get_download_queue(downloading);
195 for (std::vector<partial_piece_info>::iterator i = downloading.begin()
196 , end(downloading.end()); i != end; ++i)
198 dict partial_piece;
199 partial_piece["piece_index"] = i->piece_index;
200 partial_piece["blocks_in_piece"] = i->blocks_in_piece;
201 list block_list;
202 for (int k = 0; k < i->blocks_in_piece; ++k)
204 dict block_info;
205 block_info["state"] = i->blocks[k].state;
206 block_info["num_peers"] = i->blocks[k].num_peers;
207 block_info["bytes_progress"] = i->blocks[k].bytes_progress;
208 block_info["block_size"] = i->blocks[k].block_size;
209 block_info["peer"] = make_tuple(
210 boost::lexical_cast<std::string>(i->blocks[k].peer.address()), i->blocks[k].peer.port());
211 block_list.append(block_info);
213 partial_piece["blocks"] = block_list;
215 ret.append(partial_piece);
218 return ret;
221 namespace
223 tcp::endpoint tuple_to_endpoint(tuple const& t)
225 return tcp::endpoint(address::from_string(extract<std::string>(t[0])), extract<int>(t[1]));
229 void force_reannounce(torrent_handle& th, int s)
231 th.force_reannounce(boost::posix_time::seconds(s));
234 void connect_peer(torrent_handle& th, tuple ip, int source)
236 th.connect_peer(tuple_to_endpoint(ip), source);
239 void set_peer_upload_limit(torrent_handle& th, tuple const& ip, int limit)
241 th.set_peer_upload_limit(tuple_to_endpoint(ip), limit);
244 void set_peer_download_limit(torrent_handle& th, tuple const& ip, int limit)
246 th.set_peer_download_limit(tuple_to_endpoint(ip), limit);
249 void bind_torrent_handle()
251 void (torrent_handle::*force_reannounce0)() const = &torrent_handle::force_reannounce;
253 int (torrent_handle::*piece_priority0)(int) const = &torrent_handle::piece_priority;
254 void (torrent_handle::*piece_priority1)(int, int) const = &torrent_handle::piece_priority;
256 #ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES
257 bool (torrent_handle::*resolve_countries0)() const = &torrent_handle::resolve_countries;
258 void (torrent_handle::*resolve_countries1)(bool) = &torrent_handle::resolve_countries;
259 #endif
261 return_value_policy<copy_const_reference> copy;
263 #define _ allow_threads
265 class_<torrent_handle>("torrent_handle")
266 .def("get_peer_info", get_peer_info)
267 .def("status", _(&torrent_handle::status))
268 .def("get_download_queue", get_download_queue)
269 .def("file_progress", file_progress)
270 .def("trackers", range(begin_trackers, end_trackers))
271 .def("replace_trackers", replace_trackers)
272 .def("add_url_seed", _(&torrent_handle::add_url_seed))
273 .def("remove_url_seed", _(&torrent_handle::remove_url_seed))
274 .def("url_seeds", url_seeds)
275 .def("has_metadata", _(&torrent_handle::has_metadata))
276 .def("get_torrent_info", _(&torrent_handle::get_torrent_info), return_internal_reference<>())
277 .def("is_valid", _(&torrent_handle::is_valid))
278 .def("is_seed", _(&torrent_handle::is_seed))
279 .def("is_finished", _(&torrent_handle::is_finished))
280 .def("is_paused", _(&torrent_handle::is_paused))
281 .def("pause", _(&torrent_handle::pause))
282 .def("resume", _(&torrent_handle::resume))
283 .def("clear_error", _(&torrent_handle::clear_error))
285 .def("is_auto_managed", _(&torrent_handle::is_auto_managed))
286 .def("auto_managed", _(&torrent_handle::auto_managed))
287 .def("queue_position", _(&torrent_handle::queue_position))
288 .def("queue_position_up", _(&torrent_handle::queue_position_up))
289 .def("queue_position_down", _(&torrent_handle::queue_position_down))
290 .def("queue_position_top", _(&torrent_handle::queue_position_top))
291 .def("queue_position_bottom", _(&torrent_handle::queue_position_bottom))
293 #ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES
294 .def("resolve_countries", _(resolve_countries0))
295 .def("resolve_countries", _(resolve_countries1))
296 #endif
297 // deprecated
298 .def("filter_piece", _(&torrent_handle::filter_piece))
299 .def("is_piece_filtered", _(&torrent_handle::is_piece_filtered))
301 .def("piece_availability", piece_availability)
302 .def("piece_priority", _(piece_priority0))
303 .def("piece_priority", _(piece_priority1))
304 .def("prioritize_pieces", prioritize_pieces)
305 .def("piece_prioritize", piece_priorities)
306 .def("prioritize_files", prioritize_files)
307 .def("file_priorities", file_priorities)
308 .def("use_interface", &torrent_handle::use_interface)
309 .def("write_resume_data", _(&torrent_handle::write_resume_data))
310 .def("save_resume_data", _(&torrent_handle::save_resume_data))
311 .def("force_reannounce", _(force_reannounce0))
312 .def("force_reannounce", force_reannounce)
313 .def("scrape_tracker", _(&torrent_handle::scrape_tracker))
314 .def("name", _(&torrent_handle::name))
315 .def("set_upload_limit", _(&torrent_handle::set_upload_limit))
316 .def("upload_limit", _(&torrent_handle::upload_limit))
317 .def("set_download_limit", _(&torrent_handle::set_download_limit))
318 .def("download_limit", _(&torrent_handle::download_limit))
319 .def("set_sequential_download", _(&torrent_handle::set_sequential_download))
320 .def("set_peer_upload_limit", set_peer_upload_limit)
321 .def("set_peer_download_limit", set_peer_download_limit)
322 .def("connect_peer", connect_peer)
323 .def("set_ratio", _(&torrent_handle::set_ratio))
324 .def("save_path", _(&torrent_handle::save_path))
325 .def("set_max_uploads", _(&torrent_handle::set_max_uploads))
326 .def("set_max_connections", _(&torrent_handle::set_max_connections))
327 .def("set_tracker_login", _(&torrent_handle::set_tracker_login))
328 .def("move_storage", _(&torrent_handle::move_storage))
329 .def("info_hash", _(&torrent_handle::info_hash))
330 .def("force_recheck", _(&torrent_handle::force_recheck))
331 .def("rename_file", _(&torrent_handle::rename_file))