2016-02-21 23:09:41 +01:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2016, Arvid Norberg
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <thread>
|
|
|
|
#include <chrono>
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
#include <libtorrent/session.hpp>
|
|
|
|
#include <libtorrent/add_torrent_params.hpp>
|
|
|
|
#include <libtorrent/torrent_handle.hpp>
|
|
|
|
#include <libtorrent/alert_types.hpp>
|
|
|
|
#include <libtorrent/bencode.hpp>
|
|
|
|
#include <libtorrent/torrent_status.hpp>
|
2016-04-13 02:10:09 +02:00
|
|
|
#include <libtorrent/read_resume_data.hpp>
|
2017-03-05 15:31:28 +01:00
|
|
|
#include <libtorrent/write_resume_data.hpp>
|
2016-04-13 02:10:09 +02:00
|
|
|
#include <libtorrent/error_code.hpp>
|
2017-03-29 08:16:07 +02:00
|
|
|
#include <libtorrent/magnet_uri.hpp>
|
2016-02-21 23:09:41 +01:00
|
|
|
|
2016-03-30 08:31:37 +02:00
|
|
|
using clk = std::chrono::steady_clock;
|
2016-02-21 23:09:41 +01:00
|
|
|
|
|
|
|
// return the name of a torrent status enum
|
|
|
|
char const* state(lt::torrent_status::state_t s)
|
|
|
|
{
|
2016-02-28 21:44:59 +01:00
|
|
|
switch(s) {
|
2016-02-21 23:09:41 +01:00
|
|
|
case lt::torrent_status::checking_files: return "checking";
|
|
|
|
case lt::torrent_status::downloading_metadata: return "dl metadata";
|
|
|
|
case lt::torrent_status::downloading: return "downloading";
|
|
|
|
case lt::torrent_status::finished: return "finished";
|
|
|
|
case lt::torrent_status::seeding: return "seeding";
|
|
|
|
case lt::torrent_status::allocating: return "allocating";
|
|
|
|
case lt::torrent_status::checking_resume_data: return "checking resume";
|
|
|
|
default: return "<>";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char const* argv[])
|
|
|
|
{
|
|
|
|
if (argc != 2) {
|
|
|
|
std::cerr << "usage: " << argv[0] << " <magnet-url>" << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
lt::settings_pack pack;
|
|
|
|
pack.set_int(lt::settings_pack::alert_mask
|
|
|
|
, lt::alert::error_notification
|
|
|
|
| lt::alert::storage_notification
|
|
|
|
| lt::alert::status_notification);
|
|
|
|
|
|
|
|
lt::session ses(pack);
|
2016-03-30 08:31:37 +02:00
|
|
|
clk::time_point last_save_resume = clk::now();
|
2016-02-21 23:09:41 +01:00
|
|
|
|
|
|
|
// load resume data from disk and pass it in as we add the magnet link
|
|
|
|
std::ifstream ifs(".resume_file", std::ios_base::binary);
|
|
|
|
ifs.unsetf(std::ios_base::skipws);
|
2016-04-13 02:10:09 +02:00
|
|
|
std::vector<char> buf{std::istream_iterator<char>(ifs)
|
|
|
|
, std::istream_iterator<char>()};
|
|
|
|
|
|
|
|
lt::error_code ec;
|
2017-03-29 22:10:32 +02:00
|
|
|
lt::add_torrent_params atp = lt::read_resume_data(buf, ec);
|
2017-03-29 08:16:07 +02:00
|
|
|
if (ec) {
|
|
|
|
std::cerr << "failed to read resume data: " << ec.message() << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
lt::parse_magnet_uri(argv[1], atp, ec);
|
|
|
|
if (ec) {
|
|
|
|
std::cerr << "invalid magnet URI: " << ec.message() << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
2016-02-21 23:09:41 +01:00
|
|
|
atp.save_path = "."; // save in current dir
|
2017-03-26 20:54:16 +02:00
|
|
|
ses.async_add_torrent(std::move(atp));
|
2016-02-21 23:09:41 +01:00
|
|
|
|
|
|
|
// this is the handle we'll set once we get the notification of it being
|
|
|
|
// added
|
|
|
|
lt::torrent_handle h;
|
|
|
|
for (;;) {
|
|
|
|
std::vector<lt::alert*> alerts;
|
|
|
|
ses.pop_alerts(&alerts);
|
|
|
|
|
|
|
|
for (lt::alert const* a : alerts) {
|
|
|
|
if (auto at = lt::alert_cast<lt::add_torrent_alert>(a)) {
|
|
|
|
h = at->handle;
|
|
|
|
}
|
|
|
|
// if we receive the finished alert or an error, we're done
|
|
|
|
if (lt::alert_cast<lt::torrent_finished_alert>(a)) {
|
|
|
|
h.save_resume_data();
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (lt::alert_cast<lt::torrent_error_alert>(a)) {
|
|
|
|
std::cout << a->message() << std::endl;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
// when resume data is ready, save it
|
|
|
|
if (auto rd = lt::alert_cast<lt::save_resume_data_alert>(a)) {
|
|
|
|
std::ofstream of(".resume_file", std::ios_base::binary);
|
|
|
|
of.unsetf(std::ios_base::skipws);
|
2017-03-05 15:31:28 +01:00
|
|
|
auto buf = write_resume_data_buf(rd->params);
|
|
|
|
of.write(buf.data(), buf.size());
|
2016-02-21 23:09:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (auto st = lt::alert_cast<lt::state_update_alert>(a)) {
|
|
|
|
if (st->status.empty()) continue;
|
|
|
|
|
|
|
|
// we only have a single torrent, so we know which one
|
|
|
|
// the status is for
|
|
|
|
lt::torrent_status const& s = st->status[0];
|
|
|
|
std::cout << "\r" << state(s.state) << " "
|
|
|
|
<< (s.download_payload_rate / 1000) << " kB/s "
|
|
|
|
<< (s.total_done / 1000) << " kB ("
|
|
|
|
<< (s.progress_ppm / 10000) << "%) downloaded\x1b[K";
|
|
|
|
std::cout.flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
|
|
|
|
|
|
|
// ask the session to post a state_update_alert, to update our
|
|
|
|
// state output for the torrent
|
|
|
|
ses.post_torrent_updates();
|
|
|
|
|
2016-02-28 21:44:59 +01:00
|
|
|
// save resume data once every 30 seconds
|
2016-03-30 08:31:37 +02:00
|
|
|
if (clk::now() - last_save_resume > std::chrono::seconds(30)) {
|
2016-02-28 21:44:59 +01:00
|
|
|
h.save_resume_data();
|
2016-08-23 06:27:11 +02:00
|
|
|
last_save_resume = clk::now();
|
2016-02-28 21:44:59 +01:00
|
|
|
}
|
2016-02-21 23:09:41 +01:00
|
|
|
}
|
2016-02-28 21:44:59 +01:00
|
|
|
|
|
|
|
// TODO: ideally we should save resume data here
|
|
|
|
|
|
|
|
done:
|
2016-02-21 23:09:41 +01:00
|
|
|
std::cout << "\ndone, shutting down" << std::endl;
|
|
|
|
}
|
|
|
|
|