예제 #1
0
        /// <summary>
        /// Creates a Magnet link in the BTIH (BitTorrent Info Hash) format: xt=urn:btih:{info hash}
        /// </summary>
        /// <param name="torrent">Torrent to create Magnet link for.</param>
        /// <param name="options">Controls how the Magnet link is constructed.</param>
        /// <returns></returns>
        public static string CreateMagnetLink(Torrent torrent, MagnetLinkOptions options = MagnetLinkOptions.IncludeTrackers)
        {
            var infoHash    = torrent.GetInfoHash().ToLower();
            var displayName = torrent.DisplayName;
            var trackers    = torrent.Trackers.Flatten();

            return(CreateMagnetLink(infoHash, displayName, trackers, options));
        }
예제 #2
0
 /// <summary>
 /// Creates a Magnet link in the BTIH (BitTorrent Info Hash) format: xt=urn:btih:{info hash}
 /// </summary>
 public virtual string GetMagnetLink(MagnetLinkOptions options = MagnetLinkOptions.IncludeTrackers)
 {
     return TorrentUtil.CreateMagnetLink(this, options);
 }
예제 #3
0
 /// <summary>
 /// Creates a Magnet link in the BTIH (BitTorrent Info Hash) format: xt=urn:btih:{info hash}
 /// </summary>
 public virtual string GetMagnetLink(MagnetLinkOptions options = MagnetLinkOptions.IncludeTrackers)
 {
     return(TorrentUtil.CreateMagnetLink(this, options));
 }
예제 #4
0
        /// <summary>
        /// Creates a Magnet link in the BTIH (BitTorrent Info Hash) format: xt=urn:btih:{info hash}
        /// </summary>
        /// <param name="infoHash">The info has of the torrent.</param>
        /// <param name="displayName">The display name of the torrent. Usually the file name or directory name for multi-file torrents</param>
        /// <param name="trackers">A list of trackers if any.</param>
        /// <param name="options">Controls how the Magnet link is constructed.</param>
        /// <returns></returns>
        public static string CreateMagnetLink(string infoHash, string displayName, IEnumerable <string> trackers, MagnetLinkOptions options)
        {
            if (string.IsNullOrEmpty(infoHash))
            {
                throw new ArgumentException("Info hash cannot be null or empty.", nameof(infoHash));
            }

            var magnet = $"magnet:?xt=urn:btih:{infoHash}";

            if (!string.IsNullOrWhiteSpace(displayName))
            {
                magnet += $"&dn={displayName}";
            }

            var validTrackers = trackers?.Where(x => !string.IsNullOrWhiteSpace(x)).ToList() ?? new List <string>();

            if (options.HasFlag(MagnetLinkOptions.IncludeTrackers) && validTrackers.Any())
            {
                var trackersString = string.Join("&", validTrackers.Select(x => $"tr={x}"));
                magnet += $"&{trackersString}";
            }

            return(magnet);
        }