public IEnumerable <FormatObject> GetNextCandidates(FormatObject lastFormat = default(FormatObject))
 {
     if (lastFormat.Equals(default(FormatObject)))
     {
         return(Source.GetPossibleFormatObjects(Source.FirstDateTimeOffset));
     }
     else
     {
         return(Source.GetNextPossibleFormatObjects(lastFormat));
     }
 }
예제 #2
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,
            });
        }
예제 #3
0
        private IEnumerable <FormatObject> Validate(FormatObject lastFormat, IEnumerable <FormatObject> newFormats)
        {
            foreach (var newFormat in newFormats)
            {
                if (newFormat.Equals(lastFormat))
                {
                    throw new ApplicationException("Next possible format is is the same as previous format.");
                }

                if (Source.GetUpperBoundExclusive(lastFormat) > Source.GetUpperBoundExclusive(newFormat))
                {
                    throw new ApplicationException("Next possible format is not later then previous format.");
                }
            }

            return(newFormats);
        }
예제 #4
0
 public IEnumerable <FormatObject> GetNextCandidates(FormatObject lastFormat)
 {
     return(Validate(lastFormat, Source.GetNextPossibleFormatObjects(lastFormat)));
 }