Import new trackers from magnet link in case of duplicate torrent (closes #111)

This commit is contained in:
Christophe Dumez 2012-09-23 11:09:01 +03:00
parent 819dcacae0
commit 806ab07865
4 changed files with 52 additions and 5 deletions

View File

@ -309,7 +309,7 @@ QString misc::bcLinkToMagnet(QString bc_link) {
return magnet; return magnet;
} }
QString misc::magnetUriToName(QString magnet_uri) { QString misc::magnetUriToName(const QString& magnet_uri) {
QString name = ""; QString name = "";
QRegExp regHex("dn=([^&]+)"); QRegExp regHex("dn=([^&]+)");
const int pos = regHex.indexIn(magnet_uri); const int pos = regHex.indexIn(magnet_uri);
@ -321,7 +321,23 @@ QString misc::magnetUriToName(QString magnet_uri) {
return name; return name;
} }
QString misc::magnetUriToHash(QString magnet_uri) { QList<QUrl> misc::magnetUriToTrackers(const QString& magnet_uri)
{
QList<QUrl> trackers;
QRegExp rx("tr=([^&]+)");
int pos = 0;
while ((pos = rx.indexIn(magnet_uri, pos)) != -1) {
const QUrl tracker = QUrl::fromEncoded(rx.cap(1).toUtf8());
qDebug() << Q_FUNC_INFO << "Found tracker: " << tracker.toString();
trackers << tracker;
pos += rx.matchedLength();
}
return trackers;
}
QString misc::magnetUriToHash(const QString& magnet_uri) {
QString hash = ""; QString hash = "";
QRegExp regHex("urn:btih:([0-9A-Za-z]+)"); QRegExp regHex("urn:btih:([0-9A-Za-z]+)");
// Hex // Hex

View File

@ -42,6 +42,7 @@
#include <QPoint> #include <QPoint>
#include <QFile> #include <QFile>
#include <QDir> #include <QDir>
#include <QUrl>
#include <QCoreApplication> #include <QCoreApplication>
#ifndef DISABLE_GUI #ifndef DISABLE_GUI
#include <QIcon> #include <QIcon>
@ -99,8 +100,9 @@ public:
// value must be given in bytes // value must be given in bytes
static QString friendlyUnit(qreal val, bool is_speed = false); static QString friendlyUnit(qreal val, bool is_speed = false);
static bool isPreviewable(QString extension); static bool isPreviewable(QString extension);
static QString magnetUriToName(QString magnet_uri); static QString magnetUriToName(const QString& magnet_uri);
static QString magnetUriToHash(QString magnet_uri); static QString magnetUriToHash(const QString& magnet_uri);
static QList<QUrl> magnetUriToTrackers(const QString& magnet_uri);
static QString bcLinkToMagnet(QString bc_link); static QString bcLinkToMagnet(QString bc_link);
// Take a number of seconds and return an user-friendly // Take a number of seconds and return an user-friendly
// time duration like "1d 2h 10m". // time duration like "1d 2h 10m".

View File

@ -921,6 +921,11 @@ QTorrentHandle QBtSession::addMagnetUri(QString magnet_uri, bool resumed, bool f
if (s->find_torrent(QStringToSha1(hash)).is_valid()) { if (s->find_torrent(QStringToSha1(hash)).is_valid()) {
qDebug("/!\\ Torrent is already in download list"); qDebug("/!\\ Torrent is already in download list");
addConsoleMessage(tr("'%1' is already in download list.", "e.g: 'xxx.avi' is already in download list.").arg(magnet_uri)); addConsoleMessage(tr("'%1' is already in download list.", "e.g: 'xxx.avi' is already in download list.").arg(magnet_uri));
// Check if the torrent contains trackers or url seeds we don't know about
// and add them
QTorrentHandle h_ex = getTorrentHandle(hash);
mergeTorrents(h_ex, magnet_uri);
return h; return h;
} }
@ -1301,6 +1306,29 @@ void QBtSession::loadTorrentTempData(QTorrentHandle &h, QString savePath, bool m
TorrentPersistentData::saveTorrentPersistentData(h, savePath, magnet); TorrentPersistentData::saveTorrentPersistentData(h, savePath, magnet);
} }
void QBtSession::mergeTorrents(QTorrentHandle& h_ex, const QString& magnet_uri)
{
QList<QUrl> new_trackers = misc::magnetUriToTrackers(magnet_uri);
bool trackers_added = false;
foreach (const QUrl& new_tracker, new_trackers) {
bool found = false;
std::vector<announce_entry> existing_trackers = h_ex.trackers();
foreach (const announce_entry& existing_tracker, existing_trackers) {
if (new_tracker == QUrl(existing_tracker.url.c_str())) {
found = true;
break;
}
}
if (!found) {
h_ex.add_tracker(announce_entry(new_tracker.toString().toStdString()));
trackers_added = true;
}
}
if (trackers_added)
addConsoleMessage(tr("Note: new trackers were added to the existing torrent."));
}
void QBtSession::mergeTorrents(QTorrentHandle &h_ex, boost::intrusive_ptr<torrent_info> t) { void QBtSession::mergeTorrents(QTorrentHandle &h_ex, boost::intrusive_ptr<torrent_info> t) {
// Check if the torrent contains trackers or url seeds we don't know about // Check if the torrent contains trackers or url seeds we don't know about
// and add them // and add them

View File

@ -191,7 +191,8 @@ private slots:
void sendNotificationEmail(const QTorrentHandle &h); void sendNotificationEmail(const QTorrentHandle &h);
void autoRunExternalProgram(const QTorrentHandle &h, bool async=true); void autoRunExternalProgram(const QTorrentHandle &h, bool async=true);
void cleanUpAutoRunProcess(int); void cleanUpAutoRunProcess(int);
void mergeTorrents(QTorrentHandle &h_ex, boost::intrusive_ptr<libtorrent::torrent_info> t); void mergeTorrents(QTorrentHandle& h_ex, boost::intrusive_ptr<libtorrent::torrent_info> t);
void mergeTorrents(QTorrentHandle& h_ex, const QString& magnet_uri);
void exportTorrentFile(const QTorrentHandle &h, TorrentExportFolder folder = RegularTorrentExportFolder); void exportTorrentFile(const QTorrentHandle &h, TorrentExportFolder folder = RegularTorrentExportFolder);
void initWebUi(); void initWebUi();
void handleIPFilterParsed(int ruleCount); void handleIPFilterParsed(int ruleCount);