Пример #1
0
        public void RemoveItem(string id, NzbgetSettings settings)
        {
            var queue   = GetQueue(settings);
            var history = GetHistory(settings);

            var queueItem   = queue.SingleOrDefault(h => h.Parameters.Any(p => p.Name == "drone" && id == (p.Value as string)));
            var historyItem = history.SingleOrDefault(h => h.Parameters.Any(p => p.Name == "drone" && id == (p.Value as string)));

            if (queueItem != null)
            {
                if (!EditQueue("GroupFinalDelete", 0, "", queueItem.NzbId, settings))
                {
                    _logger.Warn("Failed to remove item from nzbget queue, {0} [{1}]", queueItem.NzbName, queueItem.NzbId);
                }
            }

            else if (historyItem != null)
            {
                if (!EditQueue("HistoryDelete", 0, "", historyItem.Id, settings))
                {
                    _logger.Warn("Failed to remove item from nzbget history, {0} [{1}]", historyItem.Name, historyItem.Id);
                }
            }

            else
            {
                _logger.Warn("Unable to remove item from nzbget, Unknown ID: {0}", id);
                return;
            }
        }
Пример #2
0
        private bool HasVersion(int minimumVersion, NzbgetSettings settings)
        {
            var versionString = _versionCache.Find(settings.Host + ":" + settings.Port) ?? GetVersion(settings);

            var version = int.Parse(versionString.Split(new[] { '.', '-' })[0]);

            return(version >= minimumVersion);
        }
Пример #3
0
        public string GetBaseUrl(NzbgetSettings settings, string relativePath = null)
        {
            var baseUrl = HttpRequestBuilder.BuildBaseUrl(settings.UseSsl, settings.Host, settings.Port, settings.UrlBase);

            baseUrl = HttpUri.CombinePath(baseUrl, relativePath);

            return(baseUrl);
        }
Пример #4
0
        public string GetVersion(NzbgetSettings settings)
        {
            var response = ProcessRequest <string>(settings, "version");

            _versionCache.Set(settings.Host + ":" + settings.Port, response, TimeSpan.FromDays(1));

            return(response);
        }
Пример #5
0
        private bool EditQueue(string command, int offset, string editText, int id, NzbgetSettings settings)
        {
            var parameters = new object[] { command, offset, editText, id };
            var request    = BuildRequest(new JsonRequest("editqueue", parameters));
            var response   = Json.Deserialize <NzbgetResponse <Boolean> >(ProcessRequest(request, settings));

            return(response.Result);
        }
Пример #6
0
        private string ProcessRequest(IRestRequest restRequest, NzbgetSettings settings)
        {
            var client   = BuildClient(settings);
            var response = client.Execute(restRequest);

            _logger.Trace("Response: {0}", response.Content);

            CheckForError(response);

            return(response.Content);
        }
Пример #7
0
        private IRestClient BuildClient(NzbgetSettings settings)
        {
            var protocol = settings.UseSsl ? "https" : "http";

            var url = String.Format("{0}://{1}:{2}/jsonrpc",
                                    protocol,
                                    settings.Host,
                                    settings.Port);

            var client = new RestClient(url);

            client.Authenticator = new HttpBasicAuthenticator(settings.Username, settings.Password);

            return(client);
        }
Пример #8
0
        public void RemoveFromHistory(string id, NzbgetSettings settings)
        {
            var history = GetHistory(settings);
            var item    = history.SingleOrDefault(h => h.Parameters.Any(p => p.Name == "drone" && id == (p.Value as string)));

            if (item == null)
            {
                _logger.Warn("Unable to remove item from nzbget's history, Unknown ID: {0}", id);
                return;
            }

            if (!EditQueue("HistoryDelete", 0, "", item.Id, settings))
            {
                _logger.Warn("Failed to remove item from nzbget history, {0} [{1}]", item.Name, item.Id);
            }
        }
Пример #9
0
        public void RetryDownload(string id, NzbgetSettings settings)
        {
            var history = GetHistory(settings);
            var item    = history.SingleOrDefault(h => h.Parameters.SingleOrDefault(p => p.Name == "drone" && id == (p.Value as string)) != null);

            if (item == null)
            {
                _logger.Warn("Unable to return item to queue, Unknown ID: {0}", id);
                return;
            }

            if (!EditQueue("HistoryRedownload", 0, "", item.Id, settings))
            {
                _logger.Warn("Failed to return item to queue from history, {0} [{1}]", item.Name, item.Id);
            }
        }
Пример #10
0
        public void RemoveItem(string id, NzbgetSettings settings)
        {
            var queue   = GetQueue(settings);
            var history = GetHistory(settings);

            int               nzbId;
            NzbgetQueueItem   queueItem;
            NzbgetHistoryItem historyItem;

            if (id.Length < 10 && int.TryParse(id, out nzbId))
            {
                // Download wasn't grabbed by Sonarr, so the id is the NzbId reported by nzbget.
                queueItem   = queue.SingleOrDefault(h => h.NzbId == nzbId);
                historyItem = history.SingleOrDefault(h => h.Id == nzbId);
            }
            else
            {
                queueItem   = queue.SingleOrDefault(h => h.Parameters.Any(p => p.Name == "drone" && id == (p.Value as string)));
                historyItem = history.SingleOrDefault(h => h.Parameters.Any(p => p.Name == "drone" && id == (p.Value as string)));
            }

            if (queueItem != null)
            {
                if (!EditQueue("GroupFinalDelete", 0, "", queueItem.NzbId, settings))
                {
                    _logger.Warn("Failed to remove item from nzbget queue, {0} [{1}]", queueItem.NzbName, queueItem.NzbId);
                }
            }

            else if (historyItem != null)
            {
                if (!EditQueue("HistoryDelete", 0, "", historyItem.Id, settings))
                {
                    _logger.Warn("Failed to remove item from nzbget history, {0} [{1}]", historyItem.Name, historyItem.Id);
                }
            }

            else
            {
                _logger.Warn("Unable to remove item from nzbget, Unknown ID: {0}", id);
                return;
            }
        }
Пример #11
0
        private T ProcessRequest <T>(NzbgetSettings settings, string method, params object[] parameters)
        {
            var baseUrl = HttpRequestBuilder.BuildBaseUrl(settings.UseSsl, settings.Host, settings.Port, "jsonrpc");

            var requestBuilder = new JsonRpcRequestBuilder(baseUrl, method, parameters);

            requestBuilder.LogResponseContent = true;
            requestBuilder.NetworkCredential  = new NetworkCredential(settings.Username, settings.Password);

            var httpRequest = requestBuilder.Build();

            HttpResponse response;

            try
            {
                response = _httpClient.Execute(httpRequest);
            }
            catch (HttpException ex)
            {
                if (ex.Response.StatusCode == HttpStatusCode.Unauthorized)
                {
                    throw new DownloadClientAuthenticationException("Authentication failed for NzbGet, please check your settings", ex);
                }

                throw new DownloadClientException("Unable to connect to NzbGet. " + ex.Message, ex);
            }
            catch (WebException ex)
            {
                throw new DownloadClientUnavailableException("Unable to connect to NzbGet. " + ex.Message, ex);
            }

            var result = Json.Deserialize <JsonRpcResponse <T> >(response.Content);

            if (result.Error != null)
            {
                throw new DownloadClientException("Error response received from nzbget: {0}", result.Error.ToString());
            }

            return(result.Result);
        }
Пример #12
0
 private bool EditQueue(string command, int offset, string editText, int id, NzbgetSettings settings)
 {
     return(ProcessRequest <bool>(settings, "editqueue", command, offset, editText, id));
 }
Пример #13
0
 public Dictionary <string, string> GetConfig(NzbgetSettings settings)
 {
     return(ProcessRequest <List <NzbgetConfigItem> >(settings, "config").ToDictionary(v => v.Name, v => v.Value));
 }
Пример #14
0
        public string DownloadNzb(Byte[] nzbData, string title, string category, int priority, NzbgetSettings settings)
        {
            var parameters = new object[] { title, category, priority, false, Convert.ToBase64String(nzbData) };
            var request    = BuildRequest(new JsonRequest("append", parameters));

            var response = Json.Deserialize <NzbgetResponse <Boolean> >(ProcessRequest(request, settings));

            _logger.Trace("Response: [{0}]", response.Result);

            if (!response.Result)
            {
                return(null);
            }

            var queue = GetQueue(settings);
            var item  = queue.FirstOrDefault(q => q.NzbName == title.Substring(0, title.Length - 4));

            if (item == null)
            {
                return(null);
            }

            var droneId    = Guid.NewGuid().ToString().Replace("-", "");
            var editResult = EditQueue("GroupSetParameter", 0, "drone=" + droneId, item.LastId, settings);

            if (editResult)
            {
                _logger.Debug("Nzbget download drone parameter set to: {0}", droneId);
            }

            return(droneId);
        }
Пример #15
0
 public List <NzbgetHistoryItem> GetHistory(NzbgetSettings settings)
 {
     return(ProcessRequest <List <NzbgetHistoryItem> >(settings, "history"));
 }
Пример #16
0
 public List <NzbgetQueueItem> GetQueue(NzbgetSettings settings)
 {
     return(ProcessRequest <List <NzbgetQueueItem> >(settings, "listgroups"));
 }
Пример #17
0
 public NzbgetGlobalStatus GetGlobalStatus(NzbgetSettings settings)
 {
     return(ProcessRequest <NzbgetGlobalStatus>(settings, "status"));
 }
Пример #18
0
        public NzbgetGlobalStatus GetGlobalStatus(NzbgetSettings settings)
        {
            var request = BuildRequest(new JsonRequest("status"));

            return(Json.Deserialize <NzbgetResponse <NzbgetGlobalStatus> >(ProcessRequest(request, settings)).Result);
        }
Пример #19
0
        public string DownloadNzb(byte[] nzbData, string title, string category, int priority, bool addpaused, NzbgetSettings settings)
        {
            if (HasVersion(16, settings))
            {
                var droneId  = Guid.NewGuid().ToString().Replace("-", "");
                var response = ProcessRequest <int>(settings, "append", title, nzbData, category, priority, false, addpaused, string.Empty, 0, "all", new string[] { "drone", droneId });
                if (response <= 0)
                {
                    return(null);
                }

                return(droneId);
            }
            else if (HasVersion(13, settings))
            {
                return(DownloadNzbLegacy13(nzbData, title, category, priority, settings));
            }
            else
            {
                return(DownloadNzbLegacy12(nzbData, title, category, priority, settings));
            }
        }
Пример #20
0
        public Dictionary <String, String> GetConfig(NzbgetSettings settings)
        {
            var request = BuildRequest(new JsonRequest("config"));

            return(Json.Deserialize <NzbgetResponse <List <NzbgetConfigItem> > >(ProcessRequest(request, settings)).Result.ToDictionary(v => v.Name, v => v.Value));
        }
Пример #21
0
        public String GetVersion(NzbgetSettings settings)
        {
            var request = BuildRequest(new JsonRequest("version"));

            return(Json.Deserialize <NzbgetResponse <String> >(ProcessRequest(request, settings)).Version);
        }
Пример #22
0
        public List <NzbgetHistoryItem> GetHistory(NzbgetSettings settings)
        {
            var request = BuildRequest(new JsonRequest("history"));

            return(Json.Deserialize <NzbgetResponse <List <NzbgetHistoryItem> > >(ProcessRequest(request, settings)).Result);
        }
Пример #23
0
        public List <NzbgetQueueItem> GetQueue(NzbgetSettings settings)
        {
            var request = BuildRequest(new JsonRequest("listgroups"));

            return(Json.Deserialize <NzbgetResponse <List <NzbgetQueueItem> > >(ProcessRequest(request, settings)).Result);
        }
Пример #24
0
        private string DownloadNzbLegacy13(byte[] nzbData, string title, string category, int priority, NzbgetSettings settings)
        {
            var response = ProcessRequest <int>(settings, "append", title, nzbData, category, priority, false, false, string.Empty, 0, "all");

            if (response <= 0)
            {
                return(null);
            }

            var queue = GetQueue(settings);
            var item  = queue.FirstOrDefault(q => q.NzbId == response);

            if (item == null)
            {
                return(null);
            }

            var droneId    = Guid.NewGuid().ToString().Replace("-", "");
            var editResult = EditQueue("GroupSetParameter", 0, "drone=" + droneId, item.NzbId, settings);

            if (editResult)
            {
                _logger.Debug("NZBGet download drone parameter set to: {0}", droneId);
            }

            return(droneId);
        }
Пример #25
0
        public VersionResponse GetVersion(NzbgetSettings settings)
        {
            var request = BuildRequest(new JsonRequest("version"));

            return(Json.Deserialize <VersionResponse>(ProcessRequest(request, settings)));
        }
Пример #26
0
        private string DownloadNzbLegacy12(byte[] nzbData, string title, string category, int priority, NzbgetSettings settings)
        {
            var response = ProcessRequest <bool>(settings, "append", title, category, priority, false, nzbData);

            if (!response)
            {
                return(null);
            }

            var queue = GetQueue(settings);
            var item  = queue.FirstOrDefault(q => q.NzbName == title.Substring(0, title.Length - 4));

            if (item == null)
            {
                return(null);
            }

            var droneId    = Guid.NewGuid().ToString().Replace("-", "");
            var editResult = EditQueue("GroupSetParameter", 0, "drone=" + droneId, item.LastId, settings);

            if (editResult)
            {
                _logger.Debug("Nzbget download drone parameter set to: {0}", droneId);
            }

            return(droneId);
        }
Пример #27
0
        public string DownloadNzbByLink(string url, string category, int priority, bool addpaused, NzbgetSettings settings)
        {
            var droneId  = Guid.NewGuid().ToString().Replace("-", "");
            var response = ProcessRequest <int>(settings, "append", "", url, category, priority, false, addpaused, string.Empty, 0, "all", new string[] { "drone", droneId });

            if (response <= 0)
            {
                return(null);
            }

            return(droneId);
        }