public string GetTorrentHash(SSHClient sshClient, string torrentid) { TorrentInfo info = GetTorrentInfo(sshClient, torrentid); if (info == null) { throw new Exception("Something went wrong getting the torrent info, check that the torrent exists."); } if (string.IsNullOrWhiteSpace(info.ID) || !Regex.IsMatch(info.ID, "[A-Fa-f0-9]{40}")) { throw new Exception("Incorrect torrent hash format, check that the torrent exists and isn't corrupted."); } return(info.ID); }
public TorrentInfo GetTorrentInfo(SSHClient sshClient, string torrentid) { string command = GetTorrentInfoCommand(torrentid); SshCommand response = sshClient.RunCommand(command); string result = response.Result; if (response.Result.StartsWith("Failed to connect")) { throw new Exception(response.Result); } TorrentInfo info = new TorrentInfo(); foreach (string item in result.Split('\n')) { if (item.StartsWith("Name: ")) { info.Name = item.Substring(5).TrimStart(); } else if (item.StartsWith("ID: ")) { info.ID = item.Substring(3).TrimStart(); } else if (item.StartsWith("State: ")) { info.State = item.Substring(6).TrimStart(); } else if (item.StartsWith("Size: ")) { info.Size = item.Substring(5).TrimStart(); } else if (item.StartsWith("Seed time: ")) { info.SeedTime = item.Substring(10).TrimStart(); } else if (item.StartsWith("Tracker status: ")) { info.TrackerStatus = item.Substring(15).TrimStart(); } else if (item.StartsWith("Progress: ")) { info.Progress = item.Substring(9).TrimStart(); } else if (item.StartsWith("Seeds: ")) { info.Seeds = item.Substring(6).TrimStart(); } } return(info); }
public void Download(string remotePath, string localPath, SSHClient sshClient, FTPTransferProgressEventHandler progressChanged, FTPTransferCanceledEventHandler downloadCanceled, CancellationTokenSource cts) { try { long totalBytes = 0; using (var client = new WebClient()) { totalBytes = sshClient.GetFileSize(sshClient.Root + remotePath); cts?.Token.Register(client.CancelAsync); client.Credentials = _credentials; client.DownloadProgressChanged += (sender, e) => { double percentage = (double)e.BytesReceived / (double)totalBytes; percentage *= 100; double seconds = _stopWatch.ElapsedMilliseconds / 1000; double bps = 0; if (seconds > 0) { bps = e.BytesReceived / seconds; } progressChanged.Invoke(bps, percentage); }; if (_uri.ToString().EndsWith("/") && remotePath.StartsWith("/")) { remotePath = remotePath.Length > 1 ? remotePath.Substring(1) : string.Empty; } Uri uri = new Uri($"{_uri.ToString()}{remotePath}"); _stopWatch = Stopwatch.StartNew(); client.DownloadFileTaskAsync(uri, localPath).GetAwaiter().GetResult(); _stopWatch.Stop(); } } catch (WebException exception) { downloadCanceled?.Invoke(exception); } return; }
public void AddTorrent(SSHClient sshClient, string saveLocation, string torrentPath) { string command = GetAddTorrentCommand(saveLocation, torrentPath); sshClient.RunCommand(command); }