using System; using System.Linq; using System.Collections.Generic; using System.Text.RegularExpressions; using NzbDrone.Common.Disk; using NzbDrone.Common.Extensions; using NzbDrone.Common.Http; using NzbDrone.Core.Parser; using NzbDrone.Core.Configuration; using NLog; using FluentValidation.Results; using NzbDrone.Core.MediaFiles.TorrentInfo; using NzbDrone.Core.Validation; using System.Net; using NzbDrone.Core.Parser.Model; using NzbDrone.Core.RemotePathMappings; namespace NzbDrone.Core.Download.Clients.Transmission { public class Transmission : TorrentClientBase { private readonly ITransmissionProxy _proxy; public Transmission(ITransmissionProxy proxy, ITorrentFileInfoReader torrentFileInfoReader, IHttpClient httpClient, IConfigService configService, IDiskProvider diskProvider, IRemotePathMappingService remotePathMappingService, Logger logger) : base(torrentFileInfoReader, httpClient, configService, diskProvider, remotePathMappingService, logger) { _proxy = proxy; } protected override String AddFromMagnetLink(RemoteEpisode remoteEpisode, String hash, String magnetLink) { _proxy.AddTorrentFromUrl(magnetLink, GetDownloadDirectory(), Settings); var isRecentEpisode = remoteEpisode.IsRecentEpisode(); if (isRecentEpisode && Settings.RecentTvPriority == (int)TransmissionPriority.First || !isRecentEpisode && Settings.OlderTvPriority == (int)TransmissionPriority.First) { _proxy.MoveTorrentToTopInQueue(hash, Settings); } return hash; } protected override String AddFromTorrentFile(RemoteEpisode remoteEpisode, String hash, String filename, Byte[] fileContent) { _proxy.AddTorrentFromData(fileContent, GetDownloadDirectory(), Settings); var isRecentEpisode = remoteEpisode.IsRecentEpisode(); if (isRecentEpisode && Settings.RecentTvPriority == (int)TransmissionPriority.First || !isRecentEpisode && Settings.OlderTvPriority == (int)TransmissionPriority.First) { _proxy.MoveTorrentToTopInQueue(hash, Settings); } return hash; } private String GetDownloadDirectory() { if (Settings.TvCategory.IsNullOrWhiteSpace()) return null; var config = _proxy.GetConfig(Settings); var destDir = (String)config.GetValueOrDefault("download-dir"); return string.Format("{0}/{1}", destDir.TrimEnd('/'), Settings.TvCategory); } public override IEnumerable GetItems() { List torrents; try { torrents = _proxy.GetTorrents(Settings); } catch (DownloadClientException ex) { _logger.ErrorException(ex.Message, ex); return Enumerable.Empty(); } var items = new List(); foreach (var torrent in torrents) { var outputPath = _remotePathMappingService.RemapRemoteToLocal(Settings.Host, new OsPath(torrent.DownloadDir)); if (Settings.TvCategory.IsNotNullOrWhiteSpace()) { var directories = outputPath.FullPath.Split('\\', '/'); if (!directories.Contains(String.Format("{0}", Settings.TvCategory))) continue; } var item = new DownloadClientItem(); item.DownloadId = torrent.HashString.ToUpper(); item.Category = Settings.TvCategory; item.Title = torrent.Name; item.DownloadClient = Definition.Name; item.OutputPath = outputPath + torrent.Name; item.RemainingSize = torrent.LeftUntilDone; item.RemainingTime = TimeSpan.FromSeconds(torrent.Eta); item.TotalSize = torrent.TotalSize; if (!torrent.ErrorString.IsNullOrWhiteSpace()) { item.Status = DownloadItemStatus.Warning; item.Message = torrent.ErrorString; } else if (torrent.Status == TransmissionTorrentStatus.Seeding || torrent.Status == TransmissionTorrentStatus.SeedingWait) { item.Status = DownloadItemStatus.Completed; } else if (torrent.IsFinished && torrent.Status != TransmissionTorrentStatus.Check && torrent.Status != TransmissionTorrentStatus.CheckWait) { item.Status = DownloadItemStatus.Completed; } else if (torrent.Status == TransmissionTorrentStatus.Queued) { item.Status = DownloadItemStatus.Queued; } else { item.Status = DownloadItemStatus.Downloading; } item.IsReadOnly = torrent.Status != TransmissionTorrentStatus.Stopped; items.Add(item); } return items; } public override void RemoveItem(string downloadId, bool deleteData) { _proxy.RemoveTorrent(downloadId.ToLower(), deleteData, Settings); } public override DownloadClientStatus GetStatus() { var config = _proxy.GetConfig(Settings); var destDir = config.GetValueOrDefault("download-dir") as string; if (Settings.TvCategory.IsNotNullOrWhiteSpace()) { destDir = String.Format("{0}/.{1}", destDir, Settings.TvCategory); } return new DownloadClientStatus { IsLocalhost = Settings.Host == "127.0.0.1" || Settings.Host == "localhost", OutputRootFolders = new List { _remotePathMappingService.RemapRemoteToLocal(Settings.Host, new OsPath(destDir)) } }; } protected override void Test(List failures) { failures.AddIfNotNull(TestConnection()); if (failures.Any()) return; failures.AddIfNotNull(TestGetTorrents()); } private ValidationFailure TestConnection() { try { var versionString = _proxy.GetVersion(Settings); _logger.Debug("Transmission version information: {0}", versionString); var versionResult = Regex.Match(versionString, @"(?