private async Task<string> DownloadFromWebUrl(ReleaseInfo release, IIndexer indexer, string torrentUrl) { byte[] torrentFile = null; torrentFile = await indexer.Download(new Uri(torrentUrl)); // handle magnet URLs if (torrentFile.Length >= 7 && torrentFile[0] == 0x6d && torrentFile[1] == 0x61 && torrentFile[2] == 0x67 && torrentFile[3] == 0x6e && torrentFile[4] == 0x65 && torrentFile[5] == 0x74 && torrentFile[6] == 0x3a) { var magnetUrl = Encoding.UTF8.GetString(torrentFile); return DownloadFromMagnetUrl(release, magnetUrl); } var filename = string.Format("{0}.torrent", StringUtil.CleanFileName(release.Title)); var hash = _torrentFileInfoReader.GetHashFromTorrentFile(torrentFile); var actualHash = AddFromTorrentFile(release, hash, filename, torrentFile); if (actualHash.IsNotNullOrWhiteSpace() && hash != actualHash) { _logger.Debug( "{0} did not return the expected InfoHash for '{1}', Prowlarr could potentially lose track of the download in progress.", Definition.Implementation, release.DownloadUrl); } return actualHash; }
private string DownloadFromWebUrl(RemoteEpisode remoteEpisode, String torrentUrl) { Byte[] torrentFile = null; try { var request = new HttpRequest(torrentUrl); request.Headers.Accept = "application/x-bittorrent"; request.AllowAutoRedirect = false; var response = _httpClient.Get(request); if (response.StatusCode == HttpStatusCode.SeeOther || response.StatusCode == HttpStatusCode.Found) { var locationHeader = (string)response.Headers.GetValueOrDefault("Location", null); _logger.Trace("Torrent request is being redirected to: {0}", locationHeader); if (locationHeader != null) { if (locationHeader.StartsWith("magnet:")) { return(DownloadFromMagnetUrl(remoteEpisode, locationHeader)); } return(DownloadFromWebUrl(remoteEpisode, locationHeader)); } throw new WebException("Remote website tried to redirect without providing a location."); } torrentFile = response.ResponseData; } catch (WebException ex) { _logger.ErrorException(String.Format("Downloading torrentfile for episode '{0}' failed ({1})", remoteEpisode.Release.Title, torrentUrl), ex); throw new ReleaseDownloadException(remoteEpisode.Release, "Downloading torrent failed", ex); } var filename = String.Format("{0}.torrent", FileNameBuilder.CleanFileName(remoteEpisode.Release.Title)); var hash = _torrentFileInfoReader.GetHashFromTorrentFile(torrentFile); var actualHash = AddFromTorrentFile(remoteEpisode, hash, filename, torrentFile); if (hash != actualHash) { _logger.Warn( "{0} did not return the expected InfoHash for '{1}', NzbDrone could potential lose track of the download in progress.", Definition.Implementation, remoteEpisode.Release.DownloadUrl); } return(actualHash); }
private string DownloadFromWebUrl(RemoteBook remoteBook, string torrentUrl) { byte[] torrentFile = null; try { var request = new HttpRequest(torrentUrl); request.RateLimitKey = remoteBook?.Release?.IndexerId.ToString(); request.Headers.Accept = "application/x-bittorrent"; request.AllowAutoRedirect = false; var response = _httpClient.Get(request); if (response.StatusCode == HttpStatusCode.MovedPermanently || response.StatusCode == HttpStatusCode.Found || response.StatusCode == HttpStatusCode.SeeOther) { var locationHeader = response.Headers.GetSingleValue("Location"); _logger.Trace("Torrent request is being redirected to: {0}", locationHeader); if (locationHeader != null) { if (locationHeader.StartsWith("magnet:")) { return(DownloadFromMagnetUrl(remoteBook, locationHeader)); } return(DownloadFromWebUrl(remoteBook, locationHeader)); } throw new WebException("Remote website tried to redirect without providing a location."); } torrentFile = response.ResponseData; _logger.Debug("Downloading torrent for release '{0}' finished ({1} bytes from {2})", remoteBook.Release.Title, torrentFile.Length, torrentUrl); } catch (HttpException ex) { if (ex.Response.StatusCode == HttpStatusCode.NotFound) { _logger.Error(ex, "Downloading torrent file for book '{0}' failed since it no longer exists ({1})", remoteBook.Release.Title, torrentUrl); throw new ReleaseUnavailableException(remoteBook.Release, "Downloading torrent failed", ex); } if ((int)ex.Response.StatusCode == 429) { _logger.Error("API Grab Limit reached for {0}", torrentUrl); } else { _logger.Error(ex, "Downloading torrent file for release '{0}' failed ({1})", remoteBook.Release.Title, torrentUrl); } throw new ReleaseDownloadException(remoteBook.Release, "Downloading torrent failed", ex); } catch (WebException ex) { _logger.Error(ex, "Downloading torrent file for release '{0}' failed ({1})", remoteBook.Release.Title, torrentUrl); throw new ReleaseDownloadException(remoteBook.Release, "Downloading torrent failed", ex); } var filename = string.Format("{0}.torrent", FileNameBuilder.CleanFileName(remoteBook.Release.Title)); var hash = _torrentFileInfoReader.GetHashFromTorrentFile(torrentFile); var actualHash = AddFromTorrentFile(remoteBook, hash, filename, torrentFile); 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); }