143 lines
4.7 KiB
C++
143 lines
4.7 KiB
C++
/*
|
|
|
|
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>
|
|
|
|
namespace lt = libtorrent;
|
|
|
|
// return the name of a torrent status enum
|
|
char const* state(lt::torrent_status::state_t s)
|
|
{
|
|
switch(s)
|
|
{
|
|
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);
|
|
|
|
lt::add_torrent_params atp;
|
|
|
|
// 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);
|
|
atp.resume_data.assign(std::istream_iterator<char>(ifs)
|
|
, std::istream_iterator<char>());
|
|
atp.url = argv[1];
|
|
atp.save_path = "."; // save in current dir
|
|
ses.async_add_torrent(atp);
|
|
|
|
// 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);
|
|
lt::bencode(std::ostream_iterator<char>(of)
|
|
, *rd->resume_data);
|
|
}
|
|
|
|
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();
|
|
|
|
// TODO: 3 call save_resume_data() once every 30 seconds or so
|
|
}
|
|
done:
|
|
std::cout << "\ndone, shutting down" << std::endl;
|
|
}
|
|
|