コード例 #1
0
        public TryDownloadResult TryDownload(FormatObject lastDownloadedFormat = default(FormatObject))
        {
            FileInfo       lastDestinationFile       = default(FileInfo);
            DateTimeOffset lastAttemptedDateTime     = DateTimeOffset.MinValue;
            FormatObject   lastAttemptedFormatObject = default(FormatObject);

            foreach (var formatObject in FormatIterator.GetNextCandidates(lastDownloadedFormat))
            {
                lastAttemptedDateTime     = Source.GetUpperBoundExclusive(formatObject);
                lastAttemptedFormatObject = formatObject;

                if ((string.IsNullOrWhiteSpace(Source.UriFormat)) || (string.IsNullOrWhiteSpace(Source.FilenameFormat)))
                {
                    continue;
                }

                Uri uri = new Uri(Source.UriFormat.FormatWith(formatObject));

                FileInfo destinationFile = new FileInfo(Path.Combine(
                                                            DownloadLocation.FullName,
                                                            string.Format("{0}_{1}", Source.SourceName, Source.FilenameFormat.FormatWith(formatObject))));
                try
                {
                    var response = WebClient.DownloadFile(uri, destinationFile.FullName);

                    if (Source.IsValidFile(response))
                    {
                        lastDestinationFile = destinationFile;

                        return(new TryDownloadResult()
                        {
                            IsSuccess = true,
                            FormatObject = formatObject,
                            DestinationFile = lastDestinationFile,
                            UpperBoundExclusive = lastAttemptedDateTime,
                        });
                    }
                }
                catch (System.Net.WebException)
                {
                    // Do nothing - try the next candidate until one is available
                }
            }

            return(new TryDownloadResult()
            {
                IsSuccess = false,
                FormatObject = lastAttemptedFormatObject,
                DestinationFile = default(FileInfo),
                UpperBoundExclusive = lastAttemptedDateTime,
            });
        }
コード例 #2
0
        public DownloadAggregatorResult DownloadNext(FormatObject lastDownloadedFormat)
        {
            var aggregateResult = new DownloadAggregatorResult()
            {
                IsSuccess       = false,
                CurrentPosition = lastDownloadedFormat,
            };

            foreach (var formatObject in FormatIterator.GetNextCandidates(lastDownloadedFormat))
            {
                aggregateResult.CurrentPosition = formatObject;

                var result = Downloader.TryDownload(formatObject);

                if (result.IsSuccess)
                {
                    aggregateResult.CurrentPosition = formatObject;

                    if (!(CurrentAggregatedFiles.Any(x => x.Item2 == result.DestinationFile)))
                    {
                        CurrentAggregatedFiles.Enqueue(Tuple.Create(formatObject, result.DestinationFile));

                        if (CurrentAggregatedFiles.Count > NumberOfFilesToAggregate)
                        {
                            CurrentAggregatedFiles.Dequeue();
                        }

                        if (CurrentAggregatedFiles.Count == NumberOfFilesToAggregate)
                        {
                            aggregateResult.IsSuccess        = true;
                            aggregateResult.DestinationFiles = CurrentAggregatedFiles.Select(x => x.Item2).ToArray();
                            CurrentAggregatedFiles.Dequeue();

                            break;
                        }
                    }
                }
            }

            return(aggregateResult);
        }