Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add d.tracker_domain command from rtorrent-ps #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/command_download.cc
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,58 @@ d_list_remove(core::Download* download,
return torrent::Object();
}

// return the "main" tracker for this download item
torrent::Tracker*
get_active_tracker(torrent::Download* item) {
torrent::TrackerList* tl = item->tracker_list();
torrent::Tracker* tracker = 0;
torrent::Tracker* fallback = 0;

for (size_t trkidx = 0; trkidx < tl->size(); trkidx++) {
tracker = tl->at(trkidx);
if (tracker->is_usable() && tracker->type() == torrent::Tracker::TRACKER_HTTP) {
if (!fallback) fallback = tracker;
if (tracker->scrape_complete() || tracker->scrape_incomplete()) {
break;
}
}
tracker = 0;
}
if (!tracker && tl->size()) tracker = fallback ? fallback : tl->at(0);

return tracker;
}

// return the domain name of the "main" tracker of the given download item
std::string
get_active_tracker_domain(torrent::Download* item) {
std::string url;
torrent::Tracker* tracker = get_active_tracker(item);

if (tracker && !tracker->url().empty()) {
url = tracker->url();

// snip url to domain name
if (url.compare(0, 7, "http://") == 0) url = url.substr(7);
if (url.compare(0, 8, "https://") == 0) url = url.substr(8);
if (url.find('/') > 0) url = url.substr(0, url.find('/'));
if (url.find(':') > 0) url = url.substr(0, url.find(':'));

// remove some common cruft
const char* domain_cruft[] = {
"tracker", "1.", "2.", "001.", ".",
"www.", "cfdata.",
0
};
for (const char** cruft = domain_cruft; *cruft; cruft++) {
int cruft_len = strlen(*cruft);
if (url.compare(0, cruft_len, *cruft) == 0) url = url.substr(cruft_len);
}
}

return url;
}

#define CMD2_ON_INFO(func) \
[](const auto& download, const auto&) { return download->info()->func(); }
#define CMD2_ON_DATA(func) \
Expand Down Expand Up @@ -822,6 +874,10 @@ initialize_command_download() {
CMD2_DL("d.creation_date", CMD2_ON_INFO(creation_date));
CMD2_DL("d.load_date", CMD2_ON_INFO(load_date));

CMD2_DL("d.tracker_domain", [](const auto& download, const auto&) {
return get_active_tracker_domain(download->download());
});

//
// Network related:
//
Expand Down