-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
75 lines (64 loc) · 2.71 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <fstream>
#include <vector>
#include <thread>
#include <chrono>
#include <memory>
#include <libtorrent/session.hpp>
#include <libtorrent/add_torrent_params.hpp>
#include <libtorrent/torrent_handle.hpp>
#include <libtorrent/torrent_info.hpp>
#include <libtorrent/create_torrent.hpp>
#include <libtorrent/bencode.hpp>
#include <libtorrent/magnet_uri.hpp>
namespace lt = libtorrent;
void createTorrentFile(const std::string& magnetLink, const std::string& outputFilename) {
try {
// Set up session
lt::settings_pack spack;
spack.set_bool(lt::settings_pack::enable_dht, false);
spack.set_bool(lt::settings_pack::enable_upnp, false);
spack.set_bool(lt::settings_pack::enable_natpmp, false);
// Create session with the settings
lt::session ses(spack);
// Parse magnet link
lt::add_torrent_params atp = lt::parse_magnet_uri(magnetLink);
atp.save_path = "."; // Save in the current directory
// Add torrent to session
lt::torrent_handle th = ses.add_torrent(atp);
// Wait for torrent metadata to be downloaded
for (int i = 0; i < 60; ++i) {
std::this_thread::sleep_for(std::chrono::seconds(1));
lt::torrent_status st = th.status();
if (st.has_metadata) {
// Metadata is available, proceed with creating the .torrent file
std::shared_ptr<const lt::torrent_info> ti = th.torrent_file();
if (ti->is_valid()) {
lt::create_torrent ct(*ti);
std::ofstream outFile(outputFilename, std::ios::binary);
if (!outFile.is_open()) {
throw std::runtime_error("Unable to open file for writing: " + outputFilename);
}
lt::bencode(std::ostream_iterator<char>(outFile), ct.generate());
std::cout << "Created .torrent file: " << outputFilename << std::endl;
} else {
std::cerr << "Invalid torrent info" << std::endl;
}
return;
}
}
std::cerr << "Failed to retrieve metadata for magnet link: " << magnetLink << std::endl;
} catch (const std::exception& e) {
std::cerr << "Exception: " << e.what() << std::endl;
}
}
int main(int argc, char *argv[]) {
if (argc != 3) {
std::cerr << "Usage: " << argv[0] << " <magnet-url> <output-filename>" << std::endl;
return 1;
}
std::string magnetLink = argv[1];
std::string outputFilename = argv[2];
createTorrentFile(magnetLink, outputFilename);
return 0;
}