public string DownloadProgramme(string progExtId, string episodeExtId, ProgrammeInfo progInfo, EpisodeInfo epInfo, string finalName) { XmlDocument rss = this.LoadFeedXml(new Uri(progExtId)); XmlNamespaceManager namespaceMgr = this.CreateNamespaceMgr(rss); XmlNode itemNode = this.ItemNodeFromEpisodeID(rss, episodeExtId); XmlNode enclosureNode = itemNode.SelectSingleNode("./enclosure"); XmlAttribute urlAttrib = enclosureNode.Attributes["url"]; Uri downloadUrl = new Uri(urlAttrib.Value); int fileNamePos = finalName.LastIndexOf("\\", StringComparison.Ordinal); int extensionPos = downloadUrl.AbsolutePath.LastIndexOf(".", StringComparison.Ordinal); string extension = "mp3"; if (extensionPos > -1) { extension = downloadUrl.AbsolutePath.Substring(extensionPos + 1); } using (TempFile downloadFile = new TempFile(extension)) { finalName += "." + extension; this.doDownload = new DownloadWrapper(downloadUrl, downloadFile.FilePath); this.doDownload.DownloadProgress += this.DoDownload_DownloadProgress; this.doDownload.Download(); while ((!this.doDownload.Complete) && this.doDownload.Error == null) { Thread.Sleep(500); } if (this.doDownload.Error != null) { if (this.doDownload.Error is WebException) { WebException webExp = (WebException)this.doDownload.Error; if (webExp.Status == WebExceptionStatus.NameResolutionFailure) { throw new DownloadException(ErrorType.NetworkProblem, "Unable to resolve " + downloadUrl.Host + " to download this episode from. Check your internet connection or try again later."); } else if (webExp.Response is HttpWebResponse) { HttpWebResponse webErrorResponse = (HttpWebResponse)webExp.Response; switch (webErrorResponse.StatusCode) { case HttpStatusCode.Forbidden: throw new DownloadException(ErrorType.RemoteProblem, downloadUrl.Host + " returned a status 403 (Forbidden) in response to the request for this episode. You may need to contact the podcast publisher if this problem persists."); case HttpStatusCode.NotFound: throw new DownloadException(ErrorType.NotAvailable, "This episode appears to be no longer available. You can either try again later, or cancel the download to remove it from the list and clear the error."); } } } throw this.doDownload.Error; } if (this.Progress != null) { this.Progress(100, ProgressType.Processing); } File.Move(downloadFile.FilePath, finalName); } return(extension); }
public string DownloadProgramme(string progExtId, string episodeExtId, ProgrammeInfo progInfo, EpisodeInfo epInfo, string finalName) { XmlDocument rss = this.LoadFeedXml(new Uri(progExtId)); XmlNode itemNode = this.ItemNodeFromEpisodeID(rss, episodeExtId); XmlNode enclosureNode = itemNode.SelectSingleNode("./enclosure"); XmlAttribute urlAttrib = enclosureNode.Attributes["url"]; Uri downloadUrl = new Uri(urlAttrib.Value); int extensionPos = downloadUrl.AbsolutePath.LastIndexOf(".", StringComparison.Ordinal); string extension = "mp3"; if (extensionPos > -1) { extension = downloadUrl.AbsolutePath.Substring(extensionPos + 1); } using (TempFile downloadFile = new TempFile(extension)) { finalName += "." + extension; this.doDownload = new DownloadWrapper(downloadUrl, downloadFile.FilePath); this.doDownload.DownloadProgress += this.DoDownload_DownloadProgress; this.doDownload.Download(); while ((!this.doDownload.Complete) && this.doDownload.Error == null) { Thread.Sleep(500); if (this.doDownload.Cancelled) { return null; } } if (this.doDownload.Error != null) { if (this.doDownload.Error is WebException) { WebException webExp = (WebException)this.doDownload.Error; if (webExp.Status == WebExceptionStatus.NameResolutionFailure) { throw new DownloadException(ErrorType.NetworkProblem, "Unable to resolve " + downloadUrl.Host + " to download this episode from. Check your internet connection or try again later."); } else if (webExp.Response is HttpWebResponse) { HttpWebResponse webErrorResponse = (HttpWebResponse)webExp.Response; switch (webErrorResponse.StatusCode) { case HttpStatusCode.Forbidden: throw new DownloadException(ErrorType.RemoteProblem, downloadUrl.Host + " returned a status 403 (Forbidden) in response to the request for this episode. You may need to contact the podcast publisher if this problem persists."); case HttpStatusCode.NotFound: throw new DownloadException(ErrorType.NotAvailable, "This episode appears to be no longer available. You can either try again later, or cancel the download to remove it from the list and clear the error."); } } } throw this.doDownload.Error; } if (this.Progress != null) { this.Progress(100, ProgressType.Processing); } File.Move(downloadFile.FilePath, finalName); } return extension; }
public override DownloadInfo DownloadProgramme(string progExtId, string episodeExtId, ProgrammeInfo progInfo, EpisodeInfo epInfo) { XmlDocument rss = this.LoadFeedXml(new Uri(progExtId)); XmlNode itemNode = this.ItemNodeFromEpisodeID(rss, episodeExtId); if (itemNode == null) { throw new DownloadException(ErrorType.RemoveFromList); } XmlNode enclosureNode = itemNode.SelectSingleNode("./enclosure"); XmlAttribute urlAttrib = enclosureNode.Attributes["url"]; Uri downloadUrl = new Uri(urlAttrib.Value); int extensionPos = downloadUrl.AbsolutePath.LastIndexOf(".", StringComparison.Ordinal); DownloadInfo info = new DownloadInfo(); this.EpisodeChapters(info.Chapters, itemNode, this.CreateNamespaceMgr(rss)); info.Extension = "mp3"; if (extensionPos > -1) { info.Extension = downloadUrl.AbsolutePath.Substring(extensionPos + 1); } info.Downloaded = this.GetTempFileInstance(info.Extension); this.doDownload = this.GetDownloadWrapperInstance(downloadUrl, info.Downloaded.FilePath); this.doDownload.DownloadProgress += this.DoDownload_DownloadProgress; this.doDownload.Download(); while ((!this.doDownload.Complete) && this.doDownload.Error == null) { Thread.Sleep(500); if (this.doDownload.Canceled) { return(null); } } if (this.doDownload.Error != null) { if (this.doDownload.Error is WebException webExp) { throw new DownloadException( webExp.Status == WebExceptionStatus.ProtocolError ? ErrorType.RemoteProblem : ErrorType.NetworkProblem, webExp.GetBaseException().Message); } throw this.doDownload.Error; } this.Progress?.Invoke(100, ProgressType.Downloading); return(info); }