Exemple #1
0
        public List<QBittorrentTorrent> GetTorrents(QBittorrentSettings settings)
        {
            var request = new RestRequest("/query/torrents", Method.GET);
            request.RequestFormat = DataFormat.Json;
            request.AddParameter("label", settings.TvCategory);

            var client = BuildClient(settings);
            var response = ProcessRequest(client, request, settings);
            response.ValidateResponse(client);
            return response.Read<List<QBittorrentTorrent>>(client);
        }
        private Tuple <IQBittorrentProxy, Version> GetProxyCache(QBittorrentSettings settings, bool force)
        {
            var proxyKey = $"{settings.Host}_{settings.Port}";

            if (force)
            {
                _proxyCache.Remove(proxyKey);
            }

            return(_proxyCache.Get(proxyKey, () => FetchProxy(settings), TimeSpan.FromMinutes(10.0)));
        }
Exemple #3
0
        public List <QBittorrentTorrent> GetTorrents(QBittorrentSettings settings)
        {
            var request = BuildRequest(settings).Resource("/api/v2/torrents/info");

            if (settings.MovieCategory.IsNotNullOrWhiteSpace())
            {
                request.AddQueryParam("category", settings.MovieCategory);
            }
            var response = ProcessRequest <List <QBittorrentTorrent> >(request, settings);

            return(response);
        }
Exemple #4
0
        public void RemoveTorrent(string hash, Boolean removeData, QBittorrentSettings settings)
        {
            var request = BuildRequest(settings).Resource(removeData ? "/command/deletePerm" : "/command/delete")
                          .Post()
                          .AddFormParameter("hashes", hash);

            if (settings.TvCategory.IsNotNullOrWhiteSpace())
            {
                request.AddFormParameter("category", settings.TvCategory);
            }

            ProcessRequest(request, settings);
        }
Exemple #5
0
        public void AddTorrentFromUrl(string torrentUrl, QBittorrentSettings settings)
        {
            var request = BuildRequest(settings).Resource("/command/download")
                          .Post()
                          .AddFormParameter("urls", torrentUrl);

            var result = ProcessRequest(request, settings);

            // Note: Older qbit versions returned nothing, so we can't do != "Ok." here.
            if (result == "Fails.")
            {
                throw new DownloadClientException("Download client failed to add torrent by url");
            }
        }
Exemple #6
0
        public void AddTorrentFromFile(string fileName, Byte[] fileContent, QBittorrentSettings settings)
        {
            var request = BuildRequest(settings).Resource("/command/upload")
                          .Post()
                          .AddFormUpload("torrents", fileName, fileContent);

            var result = ProcessRequest(request, settings);

            // Note: Current qbit versions return nothing, so we can't do != "Ok." here.
            if (result == "Fails.")
            {
                throw new DownloadClientException("Download client failed to add torrent");
            }
        }
Exemple #7
0
        public void MoveTorrentToTopInQueue(string hash, QBittorrentSettings settings)
        {
            var request = new RestRequest("/command/topPrio", Method.POST);
            request.AddParameter("hashes", hash);

            var client = BuildClient(settings);
            var response = ProcessRequest(client, request, settings);

            // qBittorrent rejects all Prio commands with 403: Forbidden if Options -> BitTorrent -> Torrent Queueing is not enabled
            if (response.StatusCode == HttpStatusCode.Forbidden)
            {
                return;
            }

            response.ValidateResponse(client);
        }
        private Tuple <IQBittorrentProxy, Version> FetchProxy(QBittorrentSettings settings)
        {
            if (_proxyV2.IsApiSupported(settings))
            {
                _logger.Trace("Using qbitTorrent API v2");
                return(Tuple.Create(_proxyV2, _proxyV2.GetApiVersion(settings)));
            }

            if (_proxyV1.IsApiSupported(settings))
            {
                _logger.Trace("Using qbitTorrent API v1");
                return(Tuple.Create(_proxyV1, _proxyV1.GetApiVersion(settings)));
            }

            throw new DownloadClientException("Unable to determine qBittorrent API version");
        }
Exemple #9
0
        public bool IsTorrentLoaded(string hash, QBittorrentSettings settings)
        {
            var request = BuildRequest(settings).Resource($"/query/propertiesGeneral/{hash}");

            request.LogHttpError = false;

            try
            {
                ProcessRequest(request, settings);

                return(true);
            }
            catch
            {
                return(false);
            }
        }
Exemple #10
0
        public bool IsTorrentLoaded(string hash, QBittorrentSettings settings)
        {
            var request = BuildRequest(settings).Resource("/api/v2/torrents/properties")
                                                .AddQueryParam("hash", hash);
            request.LogHttpError = false;

            try
            {
                ProcessRequest(request, settings);

                return true;
            }
            catch
            {
                return false;
            }
        }
Exemple #11
0
        public void MoveTorrentToTopInQueue(string hash, QBittorrentSettings settings)
        {
            var request = BuildRequest(settings).Resource("/command/topPrio")
                          .Post()
                          .AddFormParameter("hashes", hash);

            try
            {
                ProcessRequest(request, settings);
            }
            catch (DownloadClientException ex)
            {
                // qBittorrent rejects all Prio commands with 403: Forbidden if Options -> BitTorrent -> Torrent Queueing is not enabled
#warning FIXME: so wouldn't the reauthenticate logic trigger on Forbidden?
                if (ex.InnerException is HttpException && (ex.InnerException as HttpException).Response.StatusCode == HttpStatusCode.Forbidden)
                {
                    return;
                }

                throw;
            }
        }
Exemple #12
0
        private TResult ProcessRequest <TResult>(HttpRequestBuilder requestBuilder, QBittorrentSettings settings)
            where TResult : new()
        {
            AuthenticateClient(requestBuilder, settings);

            var request = requestBuilder.Build();

            request.LogResponseContent = true;

            HttpResponse response;

            try
            {
                response = _httpClient.Execute(request);
            }
            catch (HttpException ex)
            {
                if (ex.Response.StatusCode == HttpStatusCode.Forbidden)
                {
                    _logger.Debug("Authentication required, logging in.");

                    AuthenticateClient(requestBuilder, settings, true);

                    request = requestBuilder.Build();

                    response = _httpClient.Execute(request);
                }
                else
                {
                    throw new DownloadClientException("Failed to connect to qBitTorrent, check your settings.", ex);
                }
            }
            catch (WebException ex)
            {
                throw new DownloadClientException("Failed to connect to qBitTorrent, please check your settings.", ex);
            }

            return(Json.Deserialize <TResult>(response.Content));
        }
Exemple #13
0
        private bool Login(IRestClient client, QBittorrentSettings settings)
        {
            var request = new RestRequest("/login", Method.POST);
            request.AddParameter("username", settings.Username);
            request.AddParameter("password", settings.Password);

            var response = client.Execute(request);

            if (response.StatusCode != HttpStatusCode.OK)
            {
                _logger.Warn("Login failed with {0}.", response.StatusCode);
                return false;
            }

            if (response.Content != "Ok.") // returns "Fails." on bad login
            {
                _logger.Warn("Login failed, incorrect username or password.");
                return false;
            }

            response.ValidateResponse(client);
            return true;
        }
Exemple #14
0
        public void SetTorrentLabel(string hash, string label, QBittorrentSettings settings)
        {
            var setCategoryRequest = BuildRequest(settings).Resource("/command/setCategory")
                                     .Post()
                                     .AddFormParameter("hashes", hash)
                                     .AddFormParameter("category", label);

            try
            {
                ProcessRequest <object>(setCategoryRequest, settings);
            }
            catch (DownloadClientException ex)
            {
                // if setCategory fails due to method not being found, then try older setLabel command for qbittorent < v.3.3.5
                if (ex.InnerException is HttpException && (ex.InnerException as HttpException).Response.StatusCode == HttpStatusCode.NotFound)
                {
                    var setLabelRequest = BuildRequest(settings).Resource("/command/setLabel")
                                          .Post()
                                          .AddFormParameter("hashes", hash)
                                          .AddFormParameter("label", label);
                    ProcessRequest <object>(setLabelRequest, settings);
                }
            }
        }
Exemple #15
0
        public void AddTorrentFromUrl(string torrentUrl, TorrentSeedConfiguration seedConfiguration, QBittorrentSettings settings)
        {
            var request = BuildRequest(settings).Resource("/api/v2/torrents/add")
                          .Post()
                          .AddFormParameter("urls", torrentUrl);

            AddTorrentDownloadFormParameters(request, settings);

            if (seedConfiguration != null)
            {
                AddTorrentSeedingFormParameters(request, seedConfiguration);
            }

            var result = ProcessRequest(request, settings);

            // Note: Older qbit versions returned nothing, so we can't do != "Ok." here.
            if (result == "Fails.")
            {
                throw new DownloadClientException("Download client failed to add torrent by url");
            }
        }
Exemple #16
0
 public void SetTorrentSeedingConfiguration(string hash, TorrentSeedConfiguration seedConfiguration, QBittorrentSettings settings)
 {
     // Not supported on api v1
 }
Exemple #17
0
 public Dictionary <string, QBittorrentLabel> GetLabels(QBittorrentSettings settings)
 {
     throw new NotSupportedException("qBittorrent api v1 does not support getting all torrent categories");
 }
Exemple #18
0
        public void AddTorrentFromFile(string fileName, byte[] fileContent, TorrentSeedConfiguration seedConfiguration, QBittorrentSettings settings)
        {
            var request = BuildRequest(settings).Resource("/command/upload")
                          .Post()
                          .AddFormUpload("torrents", fileName, fileContent);

            if (settings.Category.IsNotNullOrWhiteSpace())
            {
                request.AddFormParameter("category", settings.Category);
            }

            // Note: ForceStart is handled by separate api call
            if ((QBittorrentState)settings.InitialState == QBittorrentState.Start)
            {
                request.AddFormParameter("paused", false);
            }
            else if ((QBittorrentState)settings.InitialState == QBittorrentState.Pause)
            {
                request.AddFormParameter("paused", true);
            }

            var result = ProcessRequest(request, settings);

            // Note: Current qbit versions return nothing, so we can't do != "Ok." here.
            if (result == "Fails.")
            {
                throw new DownloadClientException("Download client failed to add torrent");
            }
        }