2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2012-2023 Mnemosyne LLC.
|
2022-08-08 18:05:39 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <algorithm>
|
2021-12-17 20:48:02 +00:00
|
|
|
#include <array>
|
2022-11-30 03:53:28 +00:00
|
|
|
#include <condition_variable>
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <cstdint> // int64_t
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
2021-12-25 22:41:18 +00:00
|
|
|
#include <ctime>
|
2022-11-30 03:53:28 +00:00
|
|
|
#include <chrono>
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <iterator> // std::back_inserter
|
2022-11-30 03:53:28 +00:00
|
|
|
#include <mutex>
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <optional>
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <string>
|
2021-12-25 22:41:18 +00:00
|
|
|
#include <string_view>
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <vector>
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2022-04-22 19:56:54 +00:00
|
|
|
#include <fmt/chrono.h>
|
2022-05-24 04:05:16 +00:00
|
|
|
#include <fmt/core.h>
|
2022-04-22 19:56:54 +00:00
|
|
|
|
2010-06-16 14:27:24 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
2021-12-25 22:41:18 +00:00
|
|
|
|
|
|
|
#include <libtransmission/error.h>
|
2022-03-17 22:39:06 +00:00
|
|
|
#include <libtransmission/log.h>
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <libtransmission/quark.h>
|
2021-12-25 22:41:18 +00:00
|
|
|
#include <libtransmission/torrent-metainfo.h>
|
2010-06-16 14:27:24 +00:00
|
|
|
#include <libtransmission/tr-getopt.h>
|
2022-04-07 22:26:59 +00:00
|
|
|
#include <libtransmission/tr-strbuf.h>
|
2010-06-16 14:27:24 +00:00
|
|
|
#include <libtransmission/utils.h>
|
2012-12-14 04:34:42 +00:00
|
|
|
#include <libtransmission/variant.h>
|
2010-06-16 14:27:24 +00:00
|
|
|
#include <libtransmission/version.h>
|
2022-11-30 03:53:28 +00:00
|
|
|
#include <libtransmission/web.h>
|
2021-12-25 22:41:18 +00:00
|
|
|
#include <libtransmission/web-utils.h>
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2014-06-10 00:43:21 +00:00
|
|
|
#include "units.h"
|
|
|
|
|
2021-12-14 20:59:40 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2021-12-25 22:41:18 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2022-11-30 03:53:28 +00:00
|
|
|
auto constexpr TimeoutSecs = std::chrono::seconds{ 30 };
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2021-12-25 22:41:18 +00:00
|
|
|
char constexpr MyName[] = "transmission-show";
|
2022-03-21 14:15:48 +00:00
|
|
|
char constexpr Usage[] = "Usage: transmission-show [options] <torrent-file>";
|
2021-12-17 20:48:02 +00:00
|
|
|
|
2022-03-28 04:43:28 +00:00
|
|
|
auto options = std::array<tr_option, 14>{
|
|
|
|
{ { 'd', "header", "Show only header section", "d", false, nullptr },
|
|
|
|
{ 'i', "info", "Show only info section", "i", false, nullptr },
|
|
|
|
{ 't', "trackers", "Show only trackers section", "t", false, nullptr },
|
|
|
|
{ 'f', "files", "Show only file list", "f", false, nullptr },
|
|
|
|
{ 'D', "no-header", "Do not show header section", "D", false, nullptr },
|
|
|
|
{ 'I', "no-info", "Do not show info section", "I", false, nullptr },
|
|
|
|
{ 'T', "no-trackers", "Do not show trackers section", "T", false, nullptr },
|
|
|
|
{ 'F', "no-files", "Do not show files section", "F", false, nullptr },
|
|
|
|
{ 'b', "bytes", "Show file sizes in bytes", "b", false, nullptr },
|
|
|
|
{ 'm', "magnet", "Give a magnet link for the specified torrent", "m", false, nullptr },
|
2021-12-17 20:48:02 +00:00
|
|
|
{ 's', "scrape", "Ask the torrent's trackers how many peers are in the torrent's swarm", "s", false, nullptr },
|
|
|
|
{ 'u', "unsorted", "Do not sort files by name", "u", false, nullptr },
|
|
|
|
{ 'V', "version", "Show version number and exit", "V", false, nullptr },
|
|
|
|
{ 0, nullptr, nullptr, nullptr, false, nullptr } }
|
|
|
|
};
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2021-12-27 00:32:36 +00:00
|
|
|
struct app_opts
|
|
|
|
{
|
|
|
|
std::string_view filename;
|
|
|
|
bool scrape = false;
|
|
|
|
bool show_magnet = false;
|
|
|
|
bool show_version = false;
|
|
|
|
bool unsorted = false;
|
2022-03-28 04:43:28 +00:00
|
|
|
bool print_header = true;
|
|
|
|
bool print_info = true;
|
|
|
|
bool print_trackers = true;
|
|
|
|
bool print_files = true;
|
|
|
|
bool show_bytesize = false;
|
2021-12-27 00:32:36 +00:00
|
|
|
};
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2021-12-27 00:32:36 +00:00
|
|
|
int parseCommandLine(app_opts& opts, int argc, char const* const* argv)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int c;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* optarg;
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2021-12-25 22:41:18 +00:00
|
|
|
while ((c = tr_getopt(Usage, argc, argv, std::data(options), &optarg)) != TR_OPT_DONE)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (c)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2022-03-28 04:43:28 +00:00
|
|
|
case 'b':
|
|
|
|
opts.show_bytesize = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f':
|
|
|
|
opts.print_header = false;
|
|
|
|
opts.print_info = false;
|
|
|
|
opts.print_trackers = false;
|
|
|
|
opts.print_files = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'd':
|
|
|
|
opts.print_header = true;
|
|
|
|
opts.print_info = false;
|
|
|
|
opts.print_trackers = false;
|
|
|
|
opts.print_files = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'i':
|
|
|
|
opts.print_header = false;
|
|
|
|
opts.print_info = true;
|
|
|
|
opts.print_trackers = false;
|
|
|
|
opts.print_files = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 't':
|
|
|
|
opts.print_header = false;
|
|
|
|
opts.print_info = false;
|
|
|
|
opts.print_trackers = true;
|
|
|
|
opts.print_files = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'D':
|
|
|
|
opts.print_header = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'I':
|
|
|
|
opts.print_info = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'T':
|
|
|
|
opts.print_trackers = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'F':
|
|
|
|
opts.print_files = false;
|
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'm':
|
2021-12-27 00:32:36 +00:00
|
|
|
opts.show_magnet = true;
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 's':
|
2021-12-27 00:32:36 +00:00
|
|
|
opts.scrape = true;
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2018-11-19 10:35:32 +00:00
|
|
|
case 'u':
|
2021-12-27 00:32:36 +00:00
|
|
|
opts.unsorted = true;
|
2018-11-19 10:35:32 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'V':
|
2021-12-27 00:32:36 +00:00
|
|
|
opts.show_version = true;
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case TR_OPT_UNK:
|
2021-12-27 00:32:36 +00:00
|
|
|
opts.filename = optarg;
|
2012-12-05 17:29:46 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
default:
|
2012-12-05 17:29:46 +00:00
|
|
|
return 1;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-22 19:56:54 +00:00
|
|
|
[[nodiscard]] auto toString(time_t now)
|
2018-10-06 21:26:16 +00:00
|
|
|
{
|
2022-04-22 19:56:54 +00:00
|
|
|
return now == 0 ? "Unknown" : fmt::format("{:%a %b %d %T %Y}", fmt::localtime(now));
|
2018-10-06 21:26:16 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 04:05:16 +00:00
|
|
|
bool compareSecondField(std::string_view l, std::string_view r)
|
2022-03-28 04:43:28 +00:00
|
|
|
{
|
2022-05-24 04:05:16 +00:00
|
|
|
auto const lpos = l.find(' ');
|
|
|
|
if (lpos == std::string_view::npos)
|
2022-03-28 04:43:28 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2022-05-24 04:05:16 +00:00
|
|
|
|
|
|
|
auto const rpos = r.find(' ');
|
|
|
|
if (rpos == std::string_view::npos)
|
2022-03-28 04:43:28 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2022-05-24 04:05:16 +00:00
|
|
|
|
|
|
|
return l.substr(lpos) <= r.substr(rpos);
|
2022-03-28 04:43:28 +00:00
|
|
|
}
|
|
|
|
|
2021-12-27 00:32:36 +00:00
|
|
|
void showInfo(app_opts const& opts, tr_torrent_metainfo const& metainfo)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
/**
|
|
|
|
*** General Info
|
|
|
|
**/
|
2022-03-28 04:43:28 +00:00
|
|
|
if (opts.print_info)
|
|
|
|
{
|
2022-09-23 19:36:37 +00:00
|
|
|
fmt::print("GENERAL\n\n");
|
|
|
|
fmt::print(" Name: {:s}\n", metainfo.name());
|
2023-04-23 01:25:55 +00:00
|
|
|
if (metainfo.has_v1_metadata())
|
2022-07-01 14:49:33 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
fmt::print(" Hash v1: {:s}\n", metainfo.info_hash_string());
|
2022-07-01 14:49:33 +00:00
|
|
|
}
|
2023-04-23 01:25:55 +00:00
|
|
|
if (metainfo.has_v2_metadata())
|
2022-07-01 14:49:33 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
fmt::print(" Hash v2: {:s}\n", metainfo.info_hash2_string());
|
2022-07-01 14:49:33 +00:00
|
|
|
}
|
2022-09-23 19:36:37 +00:00
|
|
|
fmt::print(" Created by: {:s}\n", std::empty(metainfo.creator()) ? "Unknown" : metainfo.creator());
|
2023-04-23 01:25:55 +00:00
|
|
|
fmt::print(" Created on: {:s}\n\n", toString(metainfo.date_created()));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-03-28 04:43:28 +00:00
|
|
|
if (!std::empty(metainfo.comment()))
|
|
|
|
{
|
2022-09-23 19:36:37 +00:00
|
|
|
fmt::print(" Comment: {:s}\n", metainfo.comment());
|
2022-03-28 04:43:28 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-03-28 04:43:28 +00:00
|
|
|
if (!std::empty(metainfo.source()))
|
|
|
|
{
|
2022-09-23 19:36:37 +00:00
|
|
|
fmt::print(" Source: {:s}\n", metainfo.source());
|
2022-03-28 04:43:28 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
fmt::print(" Piece Count: {:d}\n", metainfo.piece_count());
|
|
|
|
fmt::print(" Piece Size: {:s}\n", tr_formatter_mem_B(metainfo.piece_size()));
|
|
|
|
fmt::print(" Total Size: {:s}\n", tr_formatter_size_B(metainfo.total_size()));
|
|
|
|
fmt::print(" Privacy: {:s}\n", metainfo.is_private() ? "Private torrent" : "Public torrent");
|
2021-12-25 22:41:18 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/**
|
|
|
|
*** Trackers
|
|
|
|
**/
|
|
|
|
|
2022-03-28 04:43:28 +00:00
|
|
|
if (opts.print_trackers)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2022-09-23 19:36:37 +00:00
|
|
|
fmt::print("\nTRACKERS\n");
|
2022-03-28 04:43:28 +00:00
|
|
|
auto current_tier = std::optional<tr_tracker_tier_t>{};
|
|
|
|
auto print_tier = size_t{ 1 };
|
2023-04-23 01:25:55 +00:00
|
|
|
for (auto const& tracker : metainfo.announce_list())
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2022-03-28 04:43:28 +00:00
|
|
|
if (!current_tier || current_tier != tracker.tier)
|
|
|
|
{
|
|
|
|
current_tier = tracker.tier;
|
2022-09-23 19:36:37 +00:00
|
|
|
fmt::print("\n Tier #{:d}\n", print_tier);
|
2022-03-28 04:43:28 +00:00
|
|
|
++print_tier;
|
|
|
|
}
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2022-09-23 19:36:37 +00:00
|
|
|
fmt::print(" {:s}\n", tracker.announce.sv());
|
2022-03-28 04:43:28 +00:00
|
|
|
}
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2022-03-28 04:43:28 +00:00
|
|
|
/**
|
2022-09-23 19:36:37 +00:00
|
|
|
***
|
|
|
|
**/
|
2011-01-05 16:15:41 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
if (auto const n_webseeds = metainfo.webseed_count(); n_webseeds > 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-23 19:36:37 +00:00
|
|
|
fmt::print("\nWEBSEEDS\n\n");
|
2022-03-28 04:43:28 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < n_webseeds; ++i)
|
|
|
|
{
|
2022-09-23 19:36:37 +00:00
|
|
|
fmt::print(" {:s}\n", metainfo.webseed(i));
|
2022-03-28 04:43:28 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-01-05 16:15:41 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/**
|
|
|
|
*** Files
|
|
|
|
**/
|
|
|
|
|
2022-03-28 04:43:28 +00:00
|
|
|
if (opts.print_files)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-03-28 04:43:28 +00:00
|
|
|
if (!opts.show_bytesize)
|
|
|
|
{
|
2022-09-23 19:36:37 +00:00
|
|
|
fmt::print("\nFILES\n\n");
|
2022-03-28 04:43:28 +00:00
|
|
|
}
|
2019-02-10 11:05:16 +00:00
|
|
|
|
2022-03-28 04:43:28 +00:00
|
|
|
auto filenames = std::vector<std::string>{};
|
2023-04-23 01:25:55 +00:00
|
|
|
for (tr_file_index_t i = 0, n = metainfo.file_count(); i < n; ++i)
|
2022-03-28 04:43:28 +00:00
|
|
|
{
|
|
|
|
std::string filename;
|
|
|
|
if (opts.show_bytesize)
|
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
filename = std::to_string(metainfo.file_size(i));
|
2022-03-28 04:43:28 +00:00
|
|
|
filename += " ";
|
2023-04-23 01:25:55 +00:00
|
|
|
filename += metainfo.file_subpath(i);
|
2022-03-28 04:43:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
filename = " ";
|
2023-04-23 01:25:55 +00:00
|
|
|
filename += metainfo.file_subpath(i);
|
2022-03-28 04:43:28 +00:00
|
|
|
filename += " (";
|
2023-04-23 01:25:55 +00:00
|
|
|
filename += tr_formatter_size_B(metainfo.file_size(i));
|
2022-03-28 04:43:28 +00:00
|
|
|
filename += ')';
|
|
|
|
}
|
|
|
|
filenames.emplace_back(filename);
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-03-28 04:43:28 +00:00
|
|
|
if (!opts.unsorted)
|
|
|
|
{
|
|
|
|
if (opts.show_bytesize)
|
|
|
|
{
|
2022-05-24 04:05:16 +00:00
|
|
|
std::sort(std::begin(filenames), std::end(filenames), compareSecondField);
|
2022-03-28 04:43:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::sort(std::begin(filenames), std::end(filenames));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto const& filename : filenames)
|
|
|
|
{
|
2022-09-23 19:36:37 +00:00
|
|
|
fmt::print("{:s}\n", filename);
|
2022-03-28 04:43:28 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2021-12-25 22:41:18 +00:00
|
|
|
void doScrape(tr_torrent_metainfo const& metainfo)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2022-12-23 14:03:26 +00:00
|
|
|
class Mediator final : public tr_web::Mediator
|
|
|
|
{
|
|
|
|
[[nodiscard]] time_t now() const override
|
|
|
|
{
|
|
|
|
return time(nullptr);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
auto mediator = Mediator{};
|
2022-11-30 03:53:28 +00:00
|
|
|
auto web = tr_web::create(mediator);
|
2021-12-27 00:32:36 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
for (auto const& tracker : metainfo.announce_list())
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2022-04-16 20:41:51 +00:00
|
|
|
if (std::empty(tracker.scrape))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2021-12-25 22:41:18 +00:00
|
|
|
// build the full scrape URL
|
2022-08-19 19:33:06 +00:00
|
|
|
auto scrape_url = tr_urlbuf{ tracker.scrape.sv() };
|
2023-06-30 14:49:58 +00:00
|
|
|
auto delimiter = tr_strv_contains(scrape_url, '?') ? '&' : '?';
|
2022-08-19 19:33:06 +00:00
|
|
|
scrape_url.append(delimiter, "info_hash=");
|
2023-04-23 01:25:55 +00:00
|
|
|
tr_urlPercentEncode(std::back_inserter(scrape_url), metainfo.info_hash());
|
2022-09-23 19:36:37 +00:00
|
|
|
fmt::print("{:s} ... ", scrape_url);
|
2017-04-19 12:04:45 +00:00
|
|
|
fflush(stdout);
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2021-12-27 00:32:36 +00:00
|
|
|
// execute the http scrape
|
2022-11-30 03:53:28 +00:00
|
|
|
auto response = tr_web::FetchResponse{};
|
|
|
|
auto response_mutex = std::mutex{};
|
|
|
|
auto response_cv = std::condition_variable{};
|
|
|
|
auto lock = std::unique_lock(response_mutex);
|
|
|
|
web->fetch({ scrape_url,
|
|
|
|
[&response, &response_cv](tr_web::FetchResponse const& resp)
|
|
|
|
{
|
|
|
|
response = resp;
|
|
|
|
response_cv.notify_one();
|
|
|
|
},
|
|
|
|
nullptr,
|
|
|
|
TimeoutSecs });
|
|
|
|
response_cv.wait(lock);
|
2021-12-27 00:32:36 +00:00
|
|
|
|
|
|
|
// check the response code
|
2022-11-30 03:53:28 +00:00
|
|
|
if (auto const code = response.status; code != 200 /*HTTP OK*/)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2022-11-30 03:53:28 +00:00
|
|
|
fmt::print("error: unexpected response {:d} '{:s}'\n", code, tr_webGetResponseStr(code));
|
2021-12-27 00:32:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-12-27 00:32:36 +00:00
|
|
|
// print it out
|
2023-08-17 16:02:45 +00:00
|
|
|
auto otop = tr_variant_serde::benc().inplace().parse(response.body);
|
|
|
|
if (!!otop)
|
2021-12-27 00:32:36 +00:00
|
|
|
{
|
2022-09-23 19:36:37 +00:00
|
|
|
fmt::print("error parsing scrape response\n");
|
2021-12-27 00:32:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-08-17 16:02:45 +00:00
|
|
|
auto& top = *otop;
|
2021-12-27 00:32:36 +00:00
|
|
|
|
|
|
|
bool matched = false;
|
2021-12-28 07:14:52 +00:00
|
|
|
if (tr_variant* files = nullptr; tr_variantDictFindDict(&top, TR_KEY_files, &files))
|
2021-12-27 00:32:36 +00:00
|
|
|
{
|
|
|
|
size_t child_pos = 0;
|
|
|
|
tr_quark key;
|
|
|
|
tr_variant* val;
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
auto hashsv = std::string_view{ reinterpret_cast<char const*>(std::data(metainfo.info_hash())),
|
|
|
|
std::size(metainfo.info_hash()) };
|
2021-12-27 00:32:36 +00:00
|
|
|
|
2021-12-28 07:14:52 +00:00
|
|
|
while (tr_variantDictChild(files, child_pos, &key, &val))
|
2021-12-27 00:32:36 +00:00
|
|
|
{
|
|
|
|
if (hashsv == tr_quark_get_string_view(key))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-27 00:32:36 +00:00
|
|
|
auto i = int64_t{};
|
|
|
|
auto const seeders = tr_variantDictFindInt(val, TR_KEY_complete, &i) ? int(i) : -1;
|
|
|
|
auto const leechers = tr_variantDictFindInt(val, TR_KEY_incomplete, &i) ? int(i) : -1;
|
2022-09-23 19:36:37 +00:00
|
|
|
fmt::print("{:d} seeders, {:d} leechers\n", seeders, leechers);
|
2021-12-27 00:32:36 +00:00
|
|
|
matched = true;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-12-28 07:14:52 +00:00
|
|
|
|
|
|
|
++child_pos;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-27 00:32:36 +00:00
|
|
|
if (!matched)
|
|
|
|
{
|
2022-09-23 19:36:37 +00:00
|
|
|
fmt::print("no match\n");
|
2021-12-27 00:32:36 +00:00
|
|
|
}
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 22:41:18 +00:00
|
|
|
} // namespace
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
int tr_main(int argc, char* argv[])
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2023-07-06 03:05:03 +00:00
|
|
|
auto const init_mgr = tr_lib_init();
|
|
|
|
|
2023-04-27 14:10:33 +00:00
|
|
|
tr_locale_set_global("");
|
|
|
|
|
2022-11-30 03:53:28 +00:00
|
|
|
tr_logSetQueueEnabled(false);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logSetLevel(TR_LOG_ERROR);
|
2021-12-28 17:23:47 +00:00
|
|
|
tr_formatter_mem_init(MemK, MemKStr, MemMStr, MemGStr, MemTStr);
|
|
|
|
tr_formatter_size_init(DiskK, DiskKStr, DiskMStr, DiskGStr, DiskTStr);
|
|
|
|
tr_formatter_speed_init(SpeedK, SpeedKStr, SpeedMStr, SpeedGStr, SpeedTStr);
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2021-12-27 00:32:36 +00:00
|
|
|
auto opts = app_opts{};
|
|
|
|
if (parseCommandLine(opts, argc, (char const* const*)argv) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2021-12-27 00:32:36 +00:00
|
|
|
if (opts.show_version)
|
2010-08-08 22:53:24 +00:00
|
|
|
{
|
2022-09-23 19:36:37 +00:00
|
|
|
fmt::print(stderr, "{:s} {:s}\n", MyName, LONG_VERSION_STRING);
|
2017-04-19 12:04:45 +00:00
|
|
|
return EXIT_SUCCESS;
|
2010-08-08 22:53:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* make sure the user specified a filename */
|
2021-12-27 00:32:36 +00:00
|
|
|
if (std::empty(opts.filename))
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2022-09-23 19:36:37 +00:00
|
|
|
fmt::print(stderr, "ERROR: No torrent file specified.\n");
|
2021-12-25 22:41:18 +00:00
|
|
|
tr_getopt_usage(MyName, Usage, std::data(options));
|
2022-09-23 19:36:37 +00:00
|
|
|
fmt::print(stderr, "\n");
|
2017-04-19 12:04:45 +00:00
|
|
|
return EXIT_FAILURE;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 14:15:48 +00:00
|
|
|
/* try to parse the torrent file */
|
2021-12-25 22:41:18 +00:00
|
|
|
auto metainfo = tr_torrent_metainfo{};
|
|
|
|
tr_error* error = nullptr;
|
2023-04-23 01:25:55 +00:00
|
|
|
auto const parsed = metainfo.parse_torrent_file(opts.filename, nullptr, &error);
|
2021-12-25 22:41:18 +00:00
|
|
|
if (error != nullptr)
|
|
|
|
{
|
2022-09-23 19:36:37 +00:00
|
|
|
fmt::print(stderr, "Error parsing torrent file '{:s}': {:s} ({:d})\n", opts.filename, error->message, error->code);
|
2021-12-25 22:41:18 +00:00
|
|
|
tr_error_clear(&error);
|
|
|
|
}
|
|
|
|
if (!parsed)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return EXIT_FAILURE;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2021-12-27 00:32:36 +00:00
|
|
|
if (opts.show_magnet)
|
2012-07-23 15:28:27 +00:00
|
|
|
{
|
2022-09-23 19:36:37 +00:00
|
|
|
fmt::print("{:s}", metainfo.magnet());
|
2012-07-23 15:28:27 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2012-07-23 15:28:27 +00:00
|
|
|
{
|
2022-03-28 04:43:28 +00:00
|
|
|
if (opts.print_header)
|
|
|
|
{
|
2022-09-23 19:36:37 +00:00
|
|
|
fmt::print("Name: {:s}\n", metainfo.name());
|
|
|
|
fmt::print("File: {:s}\n", opts.filename);
|
|
|
|
fmt::print("\n");
|
2022-03-28 04:43:28 +00:00
|
|
|
fflush(stdout);
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-12-27 00:32:36 +00:00
|
|
|
if (opts.scrape)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-25 22:41:18 +00:00
|
|
|
doScrape(metainfo);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-27 00:32:36 +00:00
|
|
|
showInfo(opts, metainfo);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2012-07-23 15:28:27 +00:00
|
|
|
}
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* cleanup */
|
|
|
|
putc('\n', stdout);
|
|
|
|
return EXIT_SUCCESS;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|