Browse Source

Check for magnet links in watched folders.

Look for files ending with ".magnet" and interpret their contents as a
magnet link.
This allows scripts to collect magnet links from the web and let
qBittorrent download them non-interactively.
adaptive-webui-19844
Christian Kandeler 13 years ago committed by Christophe Dumez
parent
commit
55a6bc3855
  1. 11
      src/filesystemwatcher.h
  2. 15
      src/qtlibtorrent/qbtsession.cpp
  3. 2
      src/qtlibtorrent/qbtsession.h

11
src/filesystemwatcher.h

@ -22,6 +22,7 @@ @@ -22,6 +22,7 @@
#endif
#include "fs_utils.h"
#include "misc.h"
#ifndef CIFS_MAGIC_NUMBER
#define CIFS_MAGIC_NUMBER 0xFF534D42
@ -118,7 +119,7 @@ private: @@ -118,7 +119,7 @@ private:
public:
FileSystemWatcher(QObject *parent): QFileSystemWatcher(parent) {
m_filters << "*.torrent";
m_filters << "*.torrent" << "*.magnet";
connect(this, SIGNAL(directoryChanged(QString)), this, SLOT(scanLocalFolder(QString)));
}
@ -271,7 +272,13 @@ private: @@ -271,7 +272,13 @@ private:
const QStringList files = dir.entryList(m_filters, QDir::Files, QDir::Unsorted);
foreach (const QString &file, files) {
const QString file_abspath = dir.absoluteFilePath(file);
if (fsutils::isValidTorrentFile(file_abspath)) {
if (file_abspath.endsWith(".magnet")) {
QFile f(file_abspath);
if (f.open(QIODevice::ReadOnly)
&& !misc::magnetUriToHash(QString::fromLocal8Bit(f.readAll())).isEmpty()) {
torrents << file_abspath;
}
} else if (fsutils::isValidTorrentFile(file_abspath)) {
torrents << file_abspath;
} else {
if (!m_partialTorrents.contains(file_abspath)) {

15
src/qtlibtorrent/qbtsession.cpp

@ -924,7 +924,7 @@ void QBtSession::loadTorrentSettings(QTorrentHandle& h) { @@ -924,7 +924,7 @@ void QBtSession::loadTorrentSettings(QTorrentHandle& h) {
#endif
}
QTorrentHandle QBtSession::addMagnetUri(QString magnet_uri, bool resumed) {
QTorrentHandle QBtSession::addMagnetUri(QString magnet_uri, bool resumed, bool fromScanDir, const QString &filePath) {
Preferences pref;
QTorrentHandle h;
const QString hash(misc::magnetUriToHash(magnet_uri));
@ -952,7 +952,7 @@ QTorrentHandle QBtSession::addMagnetUri(QString magnet_uri, bool resumed) { @@ -952,7 +952,7 @@ QTorrentHandle QBtSession::addMagnetUri(QString magnet_uri, bool resumed) {
add_torrent_params p = initializeAddTorrentParams(hash);
// Get save path
const QString savePath(getSavePath(hash, false));
const QString savePath(getSavePath(hash, fromScanDir, filePath));
if (!defaultTempPath.isEmpty() && !TorrentPersistentData::isSeed(hash) && resumed) {
qDebug("addMagnetURI: Temp folder is enabled.");
QString torrent_tmp_path = defaultTempPath.replace("\\", "/");
@ -1742,6 +1742,17 @@ bool QBtSession::isFilePreviewPossible(const QString &hash) const { @@ -1742,6 +1742,17 @@ bool QBtSession::isFilePreviewPossible(const QString &hash) const {
void QBtSession::addTorrentsFromScanFolder(QStringList &pathList) {
foreach (const QString &file, pathList) {
qDebug("File %s added", qPrintable(file));
if (file.endsWith(".magnet")) {
QFile f(file);
if (!f.open(QIODevice::ReadOnly)) {
qDebug("Failed to open magnet file: %s", qPrintable(f.errorString()));
} else {
const QString link = QString::fromLocal8Bit(f.readAll());
addMagnetUri(link, false, true, file);
f.remove();
}
continue;
}
try {
torrent_info t(file.toUtf8().constData());
if (t.is_valid())

2
src/qtlibtorrent/qbtsession.h

@ -105,7 +105,7 @@ public: @@ -105,7 +105,7 @@ public:
public slots:
QTorrentHandle addTorrent(QString path, bool fromScanDir = false, QString from_url = QString(), bool resumed = false);
QTorrentHandle addMagnetUri(QString magnet_uri, bool resumed=false);
QTorrentHandle addMagnetUri(QString magnet_uri, bool resumed=false, bool fromScanDir=false, const QString &filePath=QString());
void loadSessionState();
void saveSessionState();
void downloadFromUrl(const QString &url, const QList<QNetworkCookie>& cookies = QList<QNetworkCookie>());

Loading…
Cancel
Save