2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2012-2022 Mnemosyne LLC.
|
|
|
|
// It may be used under GPLv2(SPDX : GPL - 2.0), GPLv3(SPDX : GPL - 3.0),
|
|
|
|
// 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>
|
2021-12-25 22:41:18 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <ctime>
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <string>
|
2021-12-25 22:41:18 +00:00
|
|
|
#include <string_view>
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2010-12-12 16:43:19 +00:00
|
|
|
#include <curl/curl.h>
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2010-12-20 02:07:51 +00:00
|
|
|
#include <event2/buffer.h>
|
2010-06-16 14:27:24 +00:00
|
|
|
|
|
|
|
#include <libtransmission/transmission.h>
|
2021-12-25 22:41:18 +00:00
|
|
|
|
|
|
|
#include <libtransmission/error.h>
|
|
|
|
#include <libtransmission/torrent-metainfo.h>
|
2010-06-16 14:27:24 +00:00
|
|
|
#include <libtransmission/tr-getopt.h>
|
2021-12-25 22:41:18 +00:00
|
|
|
#include <libtransmission/tr-macros.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>
|
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
|
|
|
|
{
|
|
|
|
|
|
|
|
auto constexpr TimeoutSecs = long{ 30 };
|
2010-06-16 14:27:24 +00:00
|
|
|
|
2021-12-25 22:41:18 +00:00
|
|
|
char constexpr MyName[] = "transmission-show";
|
|
|
|
char constexpr Usage[] = "Usage: transmission-show [options] <.torrent file>";
|
|
|
|
char constexpr UserAgent[] = "transmission-show/" LONG_VERSION_STRING;
|
2021-12-17 20:48:02 +00:00
|
|
|
|
2021-12-25 22:41:18 +00:00
|
|
|
auto options = std::array<tr_option, 5>{
|
2021-12-17 20:48:02 +00:00
|
|
|
{ { 'm', "magnet", "Give a magnet link for the specified torrent", "m", false, nullptr },
|
|
|
|
{ '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;
|
|
|
|
};
|
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
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
2021-12-25 22:41:18 +00:00
|
|
|
auto toString(time_t timestamp)
|
2018-10-06 21:26:16 +00:00
|
|
|
{
|
|
|
|
if (timestamp == 0)
|
|
|
|
{
|
2021-12-25 22:41:18 +00:00
|
|
|
return std::string{ "Unknown" };
|
2018-10-06 21:26:16 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 21:07:45 +00:00
|
|
|
struct tm tm;
|
|
|
|
tr_localtime_r(×tamp, &tm);
|
2021-12-25 22:41:18 +00:00
|
|
|
auto buf = std::array<char, 64>{};
|
|
|
|
strftime(std::data(buf), std::size(buf), "%a %b %d %T %Y%n", &tm); /* ctime equiv */
|
|
|
|
return std::string{ std::data(buf) };
|
2018-10-06 21:26:16 +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
|
|
|
|
**/
|
|
|
|
|
|
|
|
printf("GENERAL\n\n");
|
2021-12-25 22:41:18 +00:00
|
|
|
printf(" Name: %s\n", metainfo.name().c_str());
|
|
|
|
printf(" Hash: %" TR_PRIsv "\n", TR_PRIsv_ARG(metainfo.infoHashString()));
|
|
|
|
printf(" Created by: %s\n", std::empty(metainfo.creator()) ? "Unknown" : metainfo.creator().c_str());
|
|
|
|
printf(" Created on: %s\n", toString(metainfo.dateCreated()).c_str());
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-12-25 22:41:18 +00:00
|
|
|
if (!std::empty(metainfo.comment()))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-25 22:41:18 +00:00
|
|
|
printf(" Comment: %s\n", metainfo.comment().c_str());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-12-25 22:41:18 +00:00
|
|
|
if (!std::empty(metainfo.source()))
|
|
|
|
{
|
|
|
|
printf(" Source: %s\n", metainfo.source().c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
printf(" Piece Count: %" PRIu64 "\n", metainfo.pieceCount());
|
2021-12-31 00:30:21 +00:00
|
|
|
printf(" Piece Size: %s\n", tr_formatter_mem_B(metainfo.pieceSize()).c_str());
|
|
|
|
printf(" Total Size: %s\n", tr_formatter_size_B(metainfo.totalSize()).c_str());
|
2021-12-25 22:41:18 +00:00
|
|
|
printf(" Privacy: %s\n", metainfo.isPrivate() ? "Private torrent" : "Public torrent");
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*** Trackers
|
|
|
|
**/
|
|
|
|
|
|
|
|
printf("\nTRACKERS\n");
|
2021-12-14 20:59:40 +00:00
|
|
|
auto current_tier = std::optional<tr_tracker_tier_t>{};
|
|
|
|
auto print_tier = size_t{ 1 };
|
2021-12-25 22:41:18 +00:00
|
|
|
for (auto const& tracker : metainfo.announceList())
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2021-12-14 20:59:40 +00:00
|
|
|
if (!current_tier || current_tier != tracker.tier)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2021-12-14 20:59:40 +00:00
|
|
|
current_tier = tracker.tier;
|
|
|
|
printf("\n Tier #%zu\n", print_tier);
|
|
|
|
++print_tier;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 20:59:40 +00:00
|
|
|
printf(" %" TR_PRIsv "\n", TR_PRIsv_ARG(tracker.announce.full));
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
2011-01-05 16:15:41 +00:00
|
|
|
|
2022-01-24 00:53:35 +00:00
|
|
|
if (auto const n_webseeds = metainfo.webseedCount(); n_webseeds > 0)
|
2011-01-05 16:15:41 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
printf("\nWEBSEEDS\n\n");
|
2011-01-05 16:15:41 +00:00
|
|
|
|
2022-01-09 16:55:09 +00:00
|
|
|
for (size_t i = 0; i < n_webseeds; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-09 16:55:09 +00:00
|
|
|
printf(" %s\n", metainfo.webseed(i).c_str());
|
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
|
|
|
|
**/
|
|
|
|
|
|
|
|
printf("\nFILES\n\n");
|
|
|
|
|
2021-12-25 22:41:18 +00:00
|
|
|
auto filenames = std::vector<std::string>{};
|
2022-01-08 18:53:35 +00:00
|
|
|
for (tr_file_index_t i = 0, n = metainfo.fileCount(); i < n; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-01-08 18:53:35 +00:00
|
|
|
std::string filename = metainfo.fileSubpath(i);
|
2021-12-25 22:41:18 +00:00
|
|
|
filename += " (";
|
2022-01-08 18:53:35 +00:00
|
|
|
filename += tr_formatter_size_B(metainfo.fileSize(i));
|
2021-12-25 22:41:18 +00:00
|
|
|
filename += ')';
|
|
|
|
filenames.emplace_back(filename);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2019-02-10 11:05:16 +00:00
|
|
|
|
2021-12-27 00:32:36 +00:00
|
|
|
if (!opts.unsorted)
|
2018-11-19 10:35:32 +00:00
|
|
|
{
|
2021-12-25 22:41:18 +00:00
|
|
|
std::sort(std::begin(filenames), std::end(filenames));
|
2018-11-19 10:35:32 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-12-25 22:41:18 +00:00
|
|
|
for (auto const& filename : filenames)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-12-25 22:41:18 +00:00
|
|
|
printf(" %s\n", filename.c_str());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2021-12-25 22:41:18 +00:00
|
|
|
size_t writeFunc(void* ptr, size_t size, size_t nmemb, void* vbuf)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2021-09-21 23:03:39 +00:00
|
|
|
auto* buf = static_cast<evbuffer*>(vbuf);
|
2017-04-20 16:02:19 +00:00
|
|
|
size_t const byteCount = size * nmemb;
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_add(buf, ptr, byteCount);
|
|
|
|
return byteCount;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2021-12-25 22:41:18 +00:00
|
|
|
CURL* tr_curl_easy_init(struct evbuffer* writebuf)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
CURL* curl = curl_easy_init();
|
2021-12-25 22:41:18 +00:00
|
|
|
curl_easy_setopt(curl, CURLOPT_USERAGENT, UserAgent);
|
2017-04-19 12:04:45 +00:00
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeFunc);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, writebuf);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_VERBOSE, tr_env_key_exists("TR_CURL_VERBOSE"));
|
|
|
|
curl_easy_setopt(curl, CURLOPT_ENCODING, "");
|
|
|
|
return curl;
|
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
|
|
|
{
|
2021-12-27 00:32:36 +00:00
|
|
|
auto* const buf = evbuffer_new();
|
|
|
|
auto* const curl = tr_curl_easy_init(buf);
|
|
|
|
|
2021-12-25 22:41:18 +00:00
|
|
|
for (auto const& tracker : metainfo.announceList())
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2021-12-23 17:16:05 +00:00
|
|
|
if (std::empty(tracker.scrape_str))
|
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
|
|
|
|
auto escaped = std::array<char, TR_SHA1_DIGEST_LEN * 3 + 1>{};
|
|
|
|
tr_http_escape_sha1(std::data(escaped), metainfo.infoHash());
|
2021-12-14 20:59:40 +00:00
|
|
|
auto const scrape = tracker.scrape.full;
|
2021-12-25 22:41:18 +00:00
|
|
|
auto const url = tr_strvJoin(
|
|
|
|
scrape,
|
|
|
|
(tr_strvContains(scrape, '?') ? "&"sv : "?"sv),
|
|
|
|
"info_hash="sv,
|
|
|
|
std::data(escaped));
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2021-12-14 20:59:40 +00:00
|
|
|
printf("%" TR_PRIsv " ... ", TR_PRIsv_ARG(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
|
2021-12-14 20:59:40 +00:00
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
2021-12-17 20:48:02 +00:00
|
|
|
curl_easy_setopt(curl, CURLOPT_TIMEOUT, TimeoutSecs);
|
2022-01-24 00:53:35 +00:00
|
|
|
if (auto const res = curl_easy_perform(curl); res != CURLE_OK)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
printf("error: %s\n", curl_easy_strerror(res));
|
2021-12-27 00:32:36 +00:00
|
|
|
continue;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
2021-12-27 00:32:36 +00:00
|
|
|
|
|
|
|
// check the response code
|
|
|
|
long response;
|
|
|
|
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
|
|
|
|
if (response != 200 /*HTTP OK*/)
|
2010-06-16 14:27:24 +00:00
|
|
|
{
|
2021-12-27 00:32:36 +00:00
|
|
|
printf("error: unexpected response %ld \"%s\"\n", response, tr_webGetResponseStr(response));
|
|
|
|
continue;
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-12-27 00:32:36 +00:00
|
|
|
// print it out
|
|
|
|
tr_variant top;
|
2022-01-17 18:39:50 +00:00
|
|
|
auto* const begin = (char const*)evbuffer_pullup(buf, -1);
|
2021-12-27 00:32:36 +00:00
|
|
|
auto sv = std::string_view{ begin, evbuffer_get_length(buf) };
|
|
|
|
if (!tr_variantFromBuf(&top, TR_VARIANT_PARSE_BENC | TR_VARIANT_PARSE_INPLACE, sv))
|
|
|
|
{
|
|
|
|
printf("error parsing scrape response\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-12-27 00:32:36 +00:00
|
|
|
auto hashsv = std::string_view{ reinterpret_cast<char const*>(std::data(metainfo.infoHash())),
|
|
|
|
std::size(metainfo.infoHash()) };
|
|
|
|
|
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;
|
2021-12-28 07:14:52 +00:00
|
|
|
printf("%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
|
|
|
tr_variantFree(&top);
|
|
|
|
|
|
|
|
if (!matched)
|
|
|
|
{
|
|
|
|
printf("no match\n");
|
|
|
|
}
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
2021-12-27 00:32:36 +00:00
|
|
|
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
evbuffer_free(buf);
|
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
|
|
|
{
|
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
|
|
|
{
|
2021-12-17 20:48:02 +00:00
|
|
|
fprintf(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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
fprintf(stderr, "ERROR: No .torrent file specified.\n");
|
2021-12-25 22:41:18 +00:00
|
|
|
tr_getopt_usage(MyName, Usage, std::data(options));
|
2017-04-19 12:04:45 +00:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
return EXIT_FAILURE;
|
2010-06-16 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +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;
|
2021-12-27 00:32:36 +00:00
|
|
|
auto const parsed = metainfo.parseTorrentFile(opts.filename, nullptr, &error);
|
2021-12-25 22:41:18 +00:00
|
|
|
if (error != nullptr)
|
|
|
|
{
|
|
|
|
fprintf(
|
|
|
|
stderr,
|
|
|
|
"Error parsing .torrent file \"%" TR_PRIsv "\": %s (%d)\n",
|
2021-12-27 00:32:36 +00:00
|
|
|
TR_PRIsv_ARG(opts.filename),
|
2021-12-25 22:41:18 +00:00
|
|
|
error->message,
|
|
|
|
error->code);
|
|
|
|
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
|
|
|
{
|
2021-12-25 22:41:18 +00:00
|
|
|
printf("%s", metainfo.magnet().c_str());
|
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
|
|
|
{
|
2021-12-25 22:41:18 +00:00
|
|
|
printf("Name: %s\n", metainfo.name().c_str());
|
2021-12-27 00:32:36 +00:00
|
|
|
printf("File: %" TR_PRIsv "\n", TR_PRIsv_ARG(opts.filename));
|
2017-04-19 12:04:45 +00:00
|
|
|
printf("\n");
|
|
|
|
fflush(stdout);
|
|
|
|
|
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
|
|
|
}
|