public List<TransmissionTorrent> GetTorrents(TransmissionSettings settings) { var result = GetTorrentStatus(settings); var torrents = ((JArray)result.Arguments["torrents"]).ToObject<List<TransmissionTorrent>>(); return torrents; }
public void AddTorrentFromUrl(String torrentUrl, String downloadDirectory, TransmissionSettings settings) { var arguments = new Dictionary<String, Object>(); arguments.Add("filename", torrentUrl); if (!downloadDirectory.IsNullOrWhiteSpace()) { arguments.Add("download-dir", downloadDirectory); } ProcessRequest("torrent-add", arguments, settings); }
public void AddTorrentFromData(Byte[] torrentData, String downloadDirectory, TransmissionSettings settings) { var arguments = new Dictionary<String, Object>(); arguments.Add("metainfo", Convert.ToBase64String(torrentData)); if (!downloadDirectory.IsNullOrWhiteSpace()) { arguments.Add("download-dir", downloadDirectory); } ProcessRequest("torrent-add", arguments, settings); }
public void SetTorrentSeedingConfiguration(string hash, TorrentSeedConfiguration seedConfiguration, TransmissionSettings settings) { var arguments = new Dictionary<string, object>(); arguments.Add("ids", new string[] { hash }); if (seedConfiguration.Ratio != null) { arguments.Add("seedRatioLimit", seedConfiguration.Ratio.Value); arguments.Add("seedRatioMode", 1); } if (seedConfiguration.SeedTime != null) { arguments.Add("seedIdleLimit", Convert.ToInt32(seedConfiguration.SeedTime.Value.TotalMinutes)); arguments.Add("seedIdleMode", 1); } ProcessRequest("torrent-set", arguments, settings); }
private TransmissionResponse GetSessionStatistics(TransmissionSettings settings) { return ProcessRequest("session-stats", null, settings); }
private IRestClient BuildClient(TransmissionSettings settings) { var protocol = settings.UseSsl ? "https" : "http"; var url = String.Format(@"{0}://{1}:{2}/{3}/rpc", protocol, settings.Host, settings.Port, settings.UrlBase.Trim('/')); var restClient = RestClientFactory.BuildClient(url); restClient.FollowRedirects = false; if (!settings.Username.IsNullOrWhiteSpace()) { restClient.Authenticator = new HttpBasicAuthenticator(settings.Username, settings.Password); } return restClient; }
protected String GetSessionId(IRestClient client, TransmissionSettings settings) { var request = new RestRequest(); request.RequestFormat = DataFormat.Json; _logger.Debug("Url: {0} GetSessionId", client.BuildUri(request)); var restResponse = client.Execute(request); if (restResponse.StatusCode == HttpStatusCode.MovedPermanently) { var uri = new Uri(restResponse.ResponseUri, (String)restResponse.GetHeaderValue("Location")); throw new DownloadClientException("Remote site redirected to " + uri); } // We expect the StatusCode = Conflict, coz that will provide us with a new session id. switch (restResponse.StatusCode) { case HttpStatusCode.Conflict: { var sessionId = restResponse.Headers.SingleOrDefault(o => o.Name == "X-Transmission-Session-Id"); if (sessionId == null) { throw new DownloadClientException("Remote host did not return a Session Id."); } return (String)sessionId.Value; } case HttpStatusCode.Unauthorized: throw new DownloadClientAuthenticationException("User authentication failed."); } restResponse.ValidateResponse(client); throw new DownloadClientException("Remote host did not return a Session Id."); }
public void RemoveTorrent(String hashString, Boolean removeData, TransmissionSettings settings) { var arguments = new Dictionary<String, Object>(); arguments.Add("ids", new String[] { hashString }); arguments.Add("delete-local-data", removeData); ProcessRequest("torrent-remove", arguments, settings); }
public string GetProtocolVersion(TransmissionSettings settings) { var config = GetConfig(settings); var version = config["rpc-version"]; return version.ToString(); }
private TransmissionResponse GetTorrentStatus(IEnumerable<String> hashStrings, TransmissionSettings settings) { var fields = new String[]{ "id", "hashString", // Unique torrent ID. Use this instead of the client id? "name", "downloadDir", "status", "totalSize", "leftUntilDone", "isFinished", "eta", "errorString" }; var arguments = new Dictionary<String, Object>(); arguments.Add("fields", fields); if (hashStrings != null) { arguments.Add("ids", hashStrings); } var result = ProcessRequest("torrent-get", arguments, settings); return result; }
public string GetClientVersion(TransmissionSettings settings) { var config = GetConfig(settings); var version = config["version"]; return version.ToString(); }
private HttpRequestBuilder BuildRequest(TransmissionSettings settings) { var requestBuilder = new HttpRequestBuilder(settings.UseSsl, settings.Host, settings.Port, settings.UrlBase) .Resource("rpc") .Accept(HttpAccept.Json); requestBuilder.LogResponseContent = true; requestBuilder.NetworkCredential = new NetworkCredential(settings.Username, settings.Password); requestBuilder.AllowAutoRedirect = false; return requestBuilder; }
private void AuthenticateClient(HttpRequestBuilder requestBuilder, TransmissionSettings settings, bool reauthenticate = false) { var authKey = string.Format("{0}:{1}", requestBuilder.BaseUrl, settings.Password); var sessionId = _authSessionIDCache.Find(authKey); if (sessionId == null || reauthenticate) { _authSessionIDCache.Remove(authKey); var authLoginRequest = BuildRequest(settings).Build(); authLoginRequest.SuppressHttpError = true; var response = _httpClient.Execute(authLoginRequest); if (response.StatusCode == HttpStatusCode.MovedPermanently) { var url = response.Headers.GetSingleValue("Location"); throw new DownloadClientException("Remote site redirected to " + url); } else if (response.StatusCode == HttpStatusCode.Conflict) { sessionId = response.Headers.GetSingleValue("X-Transmission-Session-Id"); if (sessionId == null) { throw new DownloadClientException("Remote host did not return a Session Id."); } } else { throw new DownloadClientAuthenticationException("Failed to authenticate with Transmission."); } _logger.Debug("Transmission authentication succeeded."); _authSessionIDCache.Set(authKey, sessionId); } requestBuilder.SetHeader("X-Transmission-Session-Id", sessionId); }
public TransmissionResponse ProcessRequest(string action, object arguments, TransmissionSettings settings) { var requestBuilder = BuildRequest(settings); requestBuilder.Headers.ContentType = "application/json"; requestBuilder.SuppressHttpError = true; AuthenticateClient(requestBuilder, settings); var request = requestBuilder.Post().Build(); var data = new Dictionary<string, object>(); data.Add("method", action); if (arguments != null) { data.Add("arguments", arguments); } request.SetContent(data.ToJson()); request.ContentSummary = string.Format("{0}(...)", action); var response = _httpClient.Execute(request); if (response.StatusCode == HttpStatusCode.Conflict) { AuthenticateClient(requestBuilder, settings, true); request = requestBuilder.Post().Build(); request.SetContent(data.ToJson()); request.ContentSummary = string.Format("{0}(...)", action); response = _httpClient.Execute(request); } else if (response.StatusCode == HttpStatusCode.Unauthorized) { throw new DownloadClientAuthenticationException("User authentication failed."); } var transmissionResponse = Json.Deserialize<TransmissionResponse>(response.Content); if (transmissionResponse == null) { throw new TransmissionException("Unexpected response"); } else if (transmissionResponse.Result != "success") { throw new TransmissionException(transmissionResponse.Result); } return transmissionResponse; }
private TransmissionResponse GetSessionVariables(TransmissionSettings settings) { // Retrieve transmission information such as the default download directory, bandwith throttling and seed ratio. return ProcessRequest("session-get", null, settings); }
private TransmissionResponse GetTorrentStatus(TransmissionSettings settings) { return GetTorrentStatus(null, settings); }
public String GetVersion(TransmissionSettings settings) { // Gets the transmission version. var config = GetConfig(settings); var version = config["version"]; return version.ToString(); }
public void MoveTorrentToTopInQueue(String hashString, TransmissionSettings settings) { var arguments = new Dictionary<String, Object>(); arguments.Add("ids", new String[] { hashString }); ProcessRequest("queue-move-top", arguments, settings); }
public TransmissionResponse ProcessRequest(String action, Object arguments, TransmissionSettings settings) { var client = BuildClient(settings); if (String.IsNullOrWhiteSpace(_sessionId)) { _sessionId = GetSessionId(client, settings); } var request = new RestRequest(Method.POST); request.RequestFormat = DataFormat.Json; request.AddHeader("X-Transmission-Session-Id", _sessionId); var data = new Dictionary<String, Object>(); data.Add("method", action); if (arguments != null) { data.Add("arguments", arguments); } request.AddBody(data); _logger.Debug("Url: {0} Action: {1}", client.BuildUri(request), action); var restResponse = client.Execute(request); if (restResponse.StatusCode == HttpStatusCode.Conflict) { _sessionId = GetSessionId(client, settings); request.Parameters.First(o => o.Name == "X-Transmission-Session-Id").Value = _sessionId; restResponse = client.Execute(request); } else if (restResponse.StatusCode == HttpStatusCode.Unauthorized) { throw new DownloadClientAuthenticationException("User authentication failed."); } var transmissionResponse = restResponse.Read<TransmissionResponse>(client); if (transmissionResponse == null) { throw new TransmissionException("Unexpected response"); } else if (transmissionResponse.Result != "success") { throw new TransmissionException(transmissionResponse.Result); } return transmissionResponse; }
public Dictionary<String, Object> GetConfig(TransmissionSettings settings) { // Gets the transmission version. var result = GetSessionVariables(settings); return result.Arguments; }
public void Setup() { _settings = new TransmissionSettings { Host = "127.0.0.1", Port = 2222, Username = "******", Password = "******" }; Subject.Definition = new DownloadClientDefinition(); Subject.Definition.Settings = _settings; _queued = new TransmissionTorrent { HashString = "HASH", IsFinished = false, Status = TransmissionTorrentStatus.Queued, Name = _title, TotalSize = 1000, LeftUntilDone = 1000, DownloadDir = "somepath" }; _downloading = new TransmissionTorrent { HashString = "HASH", IsFinished = false, Status = TransmissionTorrentStatus.Downloading, Name = _title, TotalSize = 1000, LeftUntilDone = 100, DownloadDir = "somepath" }; _failed = new TransmissionTorrent { HashString = "HASH", IsFinished = false, Status = TransmissionTorrentStatus.Stopped, Name = _title, TotalSize = 1000, LeftUntilDone = 100, ErrorString = "Error", DownloadDir = "somepath" }; _completed = new TransmissionTorrent { HashString = "HASH", IsFinished = true, Status = TransmissionTorrentStatus.Stopped, Name = _title, TotalSize = 1000, LeftUntilDone = 0, DownloadDir = "somepath" }; _magnet = new TransmissionTorrent { HashString = "HASH", IsFinished = false, Status = TransmissionTorrentStatus.Downloading, Name = _title, TotalSize = 0, LeftUntilDone = 100, DownloadDir = "somepath" }; Mocker.GetMock <ITorrentFileInfoReader>() .Setup(s => s.GetHashFromTorrentFile(It.IsAny <byte[]>())) .Returns("CBC2F069FE8BB2F544EAE707D75BCD3DE9DCF951"); Mocker.GetMock <IHttpClient>() .Setup(s => s.Get(It.IsAny <HttpRequest>())) .Returns <HttpRequest>(r => new HttpResponse(r, new HttpHeader(), new byte[0])); _transmissionConfigItems = new Dictionary <string, object>(); _transmissionConfigItems.Add("download-dir", @"C:/Downloads/Finished/transmission"); _transmissionConfigItems.Add("incomplete-dir", null); _transmissionConfigItems.Add("incomplete-dir-enabled", false); Mocker.GetMock <ITransmissionProxy>() .Setup(v => v.GetConfig(It.IsAny <TransmissionSettings>())) .Returns(_transmissionConfigItems); }