Beispiel #1
0
        private async void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            _mVm.ShowWaiting = true;
            using (new WaitCursor())
            {
                _mVm.RssFeed = await MarkPodcast.GetPodcast();
                await DownloadNewPodcasts(_mVm.RssFeed);
            }
            _mVm.ShowWaiting = false;

            MessageBox.Show("Downloaded Mark to Music folder");
        }
Beispiel #2
0
        private async Task DownloadNewPodcasts(MarkFeed feed)
        {
            if (feed.Items.Count == 0)
            {
                return;
            }

            var dtLast   = Settings.LastDownloadTimestamp;
            var feedList = new List <FeedItem>();

            foreach (var item in feed.Items)
            {
                if (item.PublishDate <= dtLast)
                {
                    break;
                }

                feedList.Add(item);
            }

            if (feedList.Count > 0)
            {
                feedList.Sort((f1, f2) =>
                {
                    var f1d = DateTime.Parse(f1.PublishDate.ToShortDateString());
                    var f2d = DateTime.Parse(f2.PublishDate.ToShortDateString());
                    if (f1d == f2d)
                    {
                        return(0);
                    }

                    if (f1d < f2d)
                    {
                        return(-1);
                    }

                    return(1);
                });
                feedList.Reverse();
                var feedStack = new Stack <FeedItem>();
                foreach (var item in feedList)
                {
                    feedStack.Push(item);
                }

                //var sb = new StringBuilder();
                var downloadFailed = false;
                while (feedStack.Count > 0 && !downloadFailed)
                {
                    var item = feedStack.Pop();
                    //sb.AppendLine($"Got {item.Title}");
                    var retries = 3;
                    do
                    {
                        --retries;
                        //AppendToUpdateStatusTextBox(
                        //    $"Downloading {System.IO.Path.GetFileName(System.Net.WebUtility.UrlDecode(item.Enclosure.Url))}");
                        //var targetPath = $"Rush Limbaugh - {item.Title}{Path.GetExtension(item.Enclosure.Filename)}".Replace(",", "");
                        //AppendToUpdateStatusTextBox($"Downloading {targetPath}");
                        var success = await MarkPodcast.DownloadItem(item);

                        if (success)
                        {
                            MarkPodcast.UpdateLastDownloadTimestamp(item);
                            break;
                        }

                        if (retries > 0)
                        {
                            //AppendToUpdateStatusTextBox($"Download failed.  retrying in 60 seconds...");
                            await Task.Delay(60 * 1000);
                        }
                        else
                        {
                            downloadFailed = true;
                        }
                    } while (retries > 0);
                }
            }
        }