Esempio n. 1
0
		async void Download_Click(object sender, RoutedEventArgs e) {
			var selected = dataGrid.SelectedItems.Cast<EpisodeInfo>().Select(i => i.Episode.ID);
			IEnumerable<Episode> episodes;
			using(var db = new EpisodeTrackerDBContext()) {
				episodes = db.Episodes
					.Where(ep => selected.Contains(ep.ID))
					.Include(ep => ep.Series)
					.ToList();
			}

			var downloadingModel = new StatusModal();
			grid.Children.Add(downloadingModel);
			downloadingModel.Text = "Searching...";
			downloadingModel.SubText = "0/" + episodes.Count();
			downloadingModel.ShowProgress = true;
			downloadingModel.ShowSubText = true;
			
			var complete = 0;
			var results = new List<Tuple<Episode, EpisodeTorrentSearcherResult>>();

			await Task.Factory.StartNew(() => {
				episodes.AsParallel()
					.ForAll(ep => {
						var downloader = new EpisodeDownloader(ep);
						var result = downloader.Download();
						
						if(result != null) {
							RunTorrentHelper.Run(result);
							lock(results) results.Add(Tuple.Create<Episode, EpisodeTorrentSearcherResult>(ep, result));
						}

						Interlocked.Increment(ref complete);
						this.Dispatcher.BeginInvoke(new Action(() => {
							downloadingModel.UpdateProgress(complete, episodes.Count());
							downloadingModel.SubText = String.Format("{0}/{1}", complete, episodes.Count());

							if(result != null) {
								var win = App.Current.MainWindow as MainWindow;
								win.Taskbar.ShowCustomBalloon(new NotificationBalloon {
									HeaderText = "Episode Tracker",
									BodyText = "Found new download: " + result.Title
								}, PopupAnimation.Slide, 5000);
							}
						}));
					});
			});

			grid.Children.Remove(downloadingModel);

			MessageBox.Show(String.Format("Found {0} new download(s) out of {1}", results.Count(r => r.Item2 != null), results.Count()));
		}
        void Download(IEnumerable<Episode> episodes)
        {
            var found = new List<Tuple<Episode, EpisodeTorrentSearcherResult>>();

            Parallel.ForEach(episodes, episode => {
                Logger.Build()
                    .Message("Searching for new download: {0}", episode)
                    .Episode(episode)
                    .Info();

                var downloader = new EpisodeDownloader(episode);
                var result = downloader.Download();

                if(result == null) {
                    Logger.Build()
                        .Message("Found no download results: {0}", episode)
                        .Episode(episode)
                        .Info();
                    return;
                }

                Logger.Build()
                    .Message("Found new download: {0}", result)
                    .Episode(episode)
                    .Info();

                RunTorrentHelper.Run(result);

                if(DownloadFound != null) {
                    var f = Tuple.Create<Episode, EpisodeTorrentSearcherResult>(episode, result);
                    DownloadFound(this, new EpisodeDownloadServiceEventArgs {
                        Result = f
                    });
                }
            });
        }