From 9b38724417124088c71bae2b53707cb1b028ba57 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Wed, 27 Aug 2008 18:44:35 +0000 Subject: [PATCH] made dump torrent create magnet links --- examples/dump_torrent.cpp | 2 ++ include/libtorrent/magnet_uri.hpp | 1 + src/magnet_uri.cpp | 20 ++++++++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/examples/dump_torrent.cpp b/examples/dump_torrent.cpp index 8e040ac44..489a5af03 100644 --- a/examples/dump_torrent.cpp +++ b/examples/dump_torrent.cpp @@ -39,6 +39,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/bencode.hpp" #include "libtorrent/torrent_info.hpp" #include "libtorrent/lazy_entry.hpp" +#include "libtorrent/magnet_uri.hpp" #include @@ -105,6 +106,7 @@ int main(int argc, char* argv[]) std::cout << "info hash: " << t.info_hash() << "\n"; std::cout << "comment: " << t.comment() << "\n"; std::cout << "created by: " << t.creator() << "\n"; + std::cout << "magnet link: " << make_magnet_uri(t) << "\n"; std::cout << "files:\n"; int index = 0; for (torrent_info::file_iterator i = t.begin_files(); diff --git a/include/libtorrent/magnet_uri.hpp b/include/libtorrent/magnet_uri.hpp index a77880d16..88675dfe8 100644 --- a/include/libtorrent/magnet_uri.hpp +++ b/include/libtorrent/magnet_uri.hpp @@ -46,6 +46,7 @@ namespace libtorrent struct torrent_handle; std::string TORRENT_EXPORT make_magnet_uri(torrent_handle const& handle); + std::string TORRENT_EXPORT make_magnet_uri(torrent_info const& info); #ifndef TORRENT_NO_DEPRECATE // deprecated in 0.14 diff --git a/src/magnet_uri.cpp b/src/magnet_uri.cpp index af54de9e0..2570afc83 100644 --- a/src/magnet_uri.cpp +++ b/src/magnet_uri.cpp @@ -69,6 +69,26 @@ namespace libtorrent return ret.str(); } + std::string make_magnet_uri(torrent_info const& info) + { + std::stringstream ret; + if (!info.is_valid()) return ret.str(); + + std::string name = info.name(); + + ret << "magnet:?xt=urn:btih:" << base32encode( + std::string((char*)info.info_hash().begin(), 20)); + if (!name.empty()) + ret << "&dn=" << escape_string(name.c_str(), name.length()); + std::vector const& tr = info.trackers(); + if (!tr.empty()) + { + ret << "&tr=" << escape_string(tr[0].url.c_str() + , tr[0].url.length()); + } + return ret.str(); + } + #ifndef TORRENT_NO_DEPRECATE torrent_handle add_magnet_uri(session& ses, std::string const& uri , fs::path const& save_path