public TorrentManager(string magnetUrl, string downloadLocation) { var settings = new EngineSettings { SavePath = downloadLocation, ListenPort = Port }; var torrentDefaults = new TorrentSettings(); var writer = new DiskWriter(); _dataAccessor = new MonotorrentDataAccessor(writer); _engine = new ClientEngine(settings, writer); if (!Directory.Exists(downloadLocation)) { Directory.CreateDirectory(downloadLocation); } var magnet = MagnetLink.Parse(magnetUrl); _manager = new MonoTorrent.Client.TorrentManager(magnet, downloadLocation, torrentDefaults, downloadLocation); _manager.PieceHashed += async(sender, eventArgs) => await _matroskaPlayer.OnPieceHashed(sender, eventArgs); _manager.TorrentStateChanged += delegate(object sender, TorrentStateChangedEventArgs args) { Console.WriteLine(args.NewState); }; _matroskaPlayer = new MatroskaPlayer(_manager, _dataAccessor); TorrentError += OnError; }
private string DownloadFromMagnetUrl(RemoteBook remoteBook, string magnetUrl) { string hash = null; string actualHash = null; try { hash = MagnetLink.Parse(magnetUrl).InfoHash.ToHex(); } catch (FormatException ex) { _logger.Error(ex, "Failed to parse magnetlink for release '{0}': '{1}'", remoteBook.Release.Title, magnetUrl); return(null); } if (hash != null) { actualHash = AddFromMagnetLink(remoteBook, hash, magnetUrl); } if (actualHash.IsNotNullOrWhiteSpace() && hash != actualHash) { _logger.Debug( "{0} did not return the expected InfoHash for '{1}', Readarr could potentially lose track of the download in progress.", Definition.Implementation, remoteBook.Release.DownloadUrl); } return(actualHash); }
public async Task UploadMagnet(String magnetLink, String category, Boolean autoDownload, Boolean autoUnpack, Boolean autoDelete) { var magnet = MagnetLink.Parse(magnetLink); var rdTorrent = await GetRdNetClient().AddTorrentMagnetAsync(magnetLink); await Add(rdTorrent.Id, magnet.InfoHash.ToHex(), category, autoDownload, autoUnpack, autoDelete); }
public async Task <ActionResult> CheckFilesMagnet([FromBody] TorrentControllerCheckFilesRequest request) { var magnet = MagnetLink.Parse(request.MagnetLink); var result = await _torrents.GetAvailableFiles(magnet.InfoHash.ToHex()); return(Ok(result)); }
public void DownloadMetadata_SameTwice() { var link = MagnetLink.Parse("magnet:?xt=urn:btih:1234512345123451234512345123451234512345"); using var engine = new ClientEngine(EngineSettingsBuilder.CreateForTests()); var first = engine.DownloadMetadataAsync(link, CancellationToken.None); Assert.ThrowsAsync <TorrentException> (() => engine.DownloadMetadataAsync(link, CancellationToken.None)); }
public static async Task <ClientEngine> RestoreStateAsync(ReadOnlyMemory <byte> buffer) { var state = BEncodedValue.Decode <BEncodedDictionary> (buffer.Span); var engineSettings = Serializer.DeserializeEngineSettings((BEncodedDictionary)state["Settings"]); var clientEngine = new ClientEngine(engineSettings); TorrentManager manager; foreach (BEncodedDictionary torrent in (BEncodedList)state[nameof(clientEngine.Torrents)]) { var saveDirectory = ((BEncodedString)torrent[nameof(manager.SavePath)]).Text; var streaming = bool.Parse(((BEncodedString)torrent["Streaming"]).Text); var torrentSettings = Serializer.DeserializeTorrentSettings((BEncodedDictionary)torrent[nameof(manager.Settings)]); if (torrent.ContainsKey(nameof(manager.MetadataPath))) { var metadataPath = (BEncodedString)torrent[nameof(manager.MetadataPath)]; if (streaming) { manager = await clientEngine.AddStreamingAsync(metadataPath.Text, saveDirectory, torrentSettings); } else { manager = await clientEngine.AddAsync(metadataPath.Text, saveDirectory, torrentSettings); } foreach (BEncodedDictionary file in (BEncodedList)torrent[nameof(manager.Files)]) { TorrentFileInfo torrentFile; torrentFile = (TorrentFileInfo)manager.Files.Single(t => t.Path == ((BEncodedString)file[nameof(torrentFile.Path)]).Text); torrentFile.Priority = (Priority)Enum.Parse(typeof(Priority), file[nameof(torrentFile.Priority)].ToString()); torrentFile.FullPath = ((BEncodedString)file[nameof(torrentFile.FullPath)]).Text; } } else { var magnetLink = MagnetLink.Parse(torrent[nameof(manager.MagnetLink)].ToString()); if (streaming) { await clientEngine.AddStreamingAsync(magnetLink, saveDirectory, torrentSettings); } else { await clientEngine.AddAsync(magnetLink, saveDirectory, torrentSettings); } } } return(clientEngine); }
protected virtual string GetInfoHash(XElement item) { var magnetUrl = GetMagnetUrl(item); if (magnetUrl.IsNotNullOrWhiteSpace()) { try { return(MagnetLink.Parse(magnetUrl).InfoHash.ToHex()); } catch { } } return(null); }
public async void Download(SearchResponse sr, long chatId) { System.Console.WriteLine($"downloading {sr.Title}..."); //services var transmission = _scope.CreateScope().ServiceProvider.GetRequiredService <TransmissionService>().Client; var database = _scope.CreateScope().ServiceProvider.GetRequiredService <DatabaseContext>(); System.Console.WriteLine(Directory.GetCurrentDirectory()); var torrentSettings = new TorrentSettings(); NewTorrent torrent; System.Console.WriteLine("trying with magnet"); var metadata = await _engine.DownloadMetadataAsync(MagnetLink.Parse(sr.MagnetUrl), CancellationToken.None); System.Console.WriteLine("downloaded metadata"); torrent = new NewTorrent() { Metainfo = Convert.ToBase64String(metadata) }; var torrentInfo = transmission.TorrentAdd(torrent); if (database.ActiveTorrents.Any(at => at.TorrentId == torrentInfo.ID)) { return; } var activeTorrent = new ActiveTorrent() { ChatId = chatId, TorrentId = torrentInfo.ID, IsFinished = false, }; database.ActiveTorrents.Add(activeTorrent); database.SaveChanges(); }
static async Task Main() { var magnetLink = MagnetLink.Parse(MagnetUrl); var cacheDir = Path.Join(Path.GetTempPath(), "BitTorrentStorage"); var config = new BitTorrentStorageConfig(cacheDir); using var btsStorage = BtsClient.Initialize(config); Console.WriteLine("Initializing the torrent."); using var torrentManager = await btsStorage.OpenMagnetLink(magnetLink); Console.WriteLine("Starting BitTorrentStorage FUSE."); Console.WriteLine($"Mounting to \"{MountPath}\"."); var fs = new BtsFs(torrentManager) { MountPoint = MountPath }; Directory.CreateDirectory(MountPath); fs.Start(); }
protected virtual string GetInfoHash(XElement item) { if (InfoHashElementName.IsNotNullOrWhiteSpace()) { return(item.FindDecendants(InfoHashElementName).FirstOrDefault().Value); } var magnetUrl = GetMagnetUrl(item); if (magnetUrl.IsNotNullOrWhiteSpace()) { try { return(MagnetLink.Parse(magnetUrl).InfoHash.ToHex()); } catch { } } return(null); }
protected void ValidateMagnet(string link) { MagnetLink.Parse(link); }