public static async Task<Episode> DownloadPodcastEpisodeAsync(Episode episode, Action<DownloadOperation> callback, Action errorCallback = null) { var appFolder = await KnownFolders.MusicLibrary.CreateFolderAsync("PodcastR", CreationCollisionOption.OpenIfExists); var podcastFolder = await appFolder.CreateFolderAsync(episode.Podcast.Name, CreationCollisionOption.OpenIfExists); try { var uri = new Uri(episode.WebPath); var extension = uri.AbsolutePath.GetExtension(); var fileName = (episode.Name + extension).RemoveIllegalPathChars().Replace(":", ""); var episodeFile = await podcastFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting); var backgroundDownloader = new BackgroundDownloader(); var downloadOperation = backgroundDownloader.CreateDownload(uri, episodeFile); var progress = new Progress<DownloadOperation>(callback); downloads.Add(episode, downloadOperation); var cts = new CancellationTokenSource(); cancellationTokenSources.Add(episode, cts); await downloadOperation.StartAsync().AsTask(cts.Token, progress); downloads.Remove(episode); cancellationTokenSources.Remove(episode); episode.IsLocal = true; episode.WebPath = episodeFile.Path; return episode; } catch { if (errorCallback != null) errorCallback(); return episode; } }
public static async Task<IList<Episode>> CheckForNewEpisodes(IEnumerable<Podcast> podcasts) { var result = new List<Episode>(); foreach (var podcast in podcasts.ToList()) { try { var httpClient = new HttpClient(); var xml = await httpClient.GetStringAsync(podcast.FeedUrl); var xmlDocument = XDocument.Parse(xml); var elements = xmlDocument.Element("rss").Element("channel").Elements("item"); foreach (var element in elements) { var episode = new Episode(element, podcast); if (!podcast.Episodes.Any(e => string.Compare(e.Name, episode.Name) == 0)) { podcast.Episodes.Insert(0, episode); result.Add(episode); } } } catch { } } return result.OrderBy(e => e.Published).ToList(); }
public EpisodeViewModel(Episode episode) { this.Episode = episode; this.IsLocal = episode.IsLocal; this.ToggleEpisodeLocationCommand = new SymbolCommand(new GalaSoft.MvvmLight.Command.RelayCommand(async () => await this.ToggleEpisodeLocationAsync()), this.IsLocal ? Symbol.Delete : Symbol.Download, this.IsLocal ? "Delete" : "Download"); this.PlayCommand = new SymbolCommand(new GalaSoft.MvvmLight.Command.RelayCommand(() => this.Play()), Symbol.Play, "Play"); }
public static void CancelDownload(Episode episode) { var cancellationTokenSource = cancellationTokenSources[episode]; cancellationTokenSource.Cancel(); cancellationTokenSource.Dispose(); cancellationTokenSources.Remove(episode); downloads.Remove(episode); }
public static void ResumeDownload(Episode episode) { var downloadOperation = downloads[episode]; if (downloadOperation.Progress.Status == BackgroundTransferStatus.PausedByApplication) { downloadOperation.Resume(); } }
public static void PauseDownload(Episode episode) { var downloadOperation = downloads[episode]; if (downloadOperation.Progress.Status == BackgroundTransferStatus.Running) { downloadOperation.Pause(); } }
public static async Task DeletePodcastEpisodeAsync(Episode episode) { var file = await StorageFile.GetFileFromPathAsync(episode.WebPath); await file.DeleteAsync(); episode.WebPath = episode.WebPath; episode.IsLocal = false; ; }
public static void UpdateSystemControls(Episode episode) { if (systemControls != null) { systemControls.DisplayUpdater.Thumbnail = RandomAccessStreamReference.CreateFromUri(episode.ImageUrl); systemControls.DisplayUpdater.MusicProperties.AlbumArtist = episode.Podcast.Name; systemControls.DisplayUpdater.MusicProperties.Artist = episode.Author; systemControls.DisplayUpdater.MusicProperties.Title = episode.Name; systemControls.DisplayUpdater.Update(); systemControls.PlaybackStatus = MediaPlaybackStatus.Playing; } }
public static async Task<Podcast> LoadPodcastAsync(Uri url) { try { var httpClient = new HttpClient(); var xml = await httpClient.GetStringAsync(url); var xmlDocument = XDocument.Parse(xml); var channelTitle = xmlDocument.Element("rss").Element("channel").Element("title").Value; XNamespace itunes = "http://www.itunes.com/dtds/podcast-1.0.dtd"; var elements = xmlDocument.Element("rss").Element("channel").Elements("item"); var description = xmlDocument.Element("rss").Element("channel").Element("description").Value; var imageUrl = xmlDocument.Element("rss").Element("channel").Element(itunes + "image").Attribute("href").Value; var author = xmlDocument.Element("rss").Element("channel").Element(itunes + "author").Value; var podcast = new Podcast { FeedUrl = url, Name = channelTitle, ImageUrl = new Uri(imageUrl), Author = author, Description = description, DateAdded = DateTime.Now }; var episodes = new List<Episode>(); foreach (var element in elements) { var episode = new Episode(element, podcast); episodes.Add(episode); } podcast.Episodes = episodes.OrderByDescending(episode => episode.Published).ToList(); return podcast; } catch { return null; } }