/// <summary>
        /// get the state identified by the filename
        /// </summary>
        public IState GetState(string podcastRoot)
        {
            string filename = Path.Combine(podcastRoot, XmlState.StateFileName);

            if (_fileUtilities.FileExists(filename))
            {
                return(new XmlState(filename));
            }
            return(new XmlState());
        }
        private void CreateFolderIfNeeded()
        {
            string         folder = Path.GetDirectoryName(_syncItem.DestinationPath);
            IDirectoryInfo dir    = _directoryInfoProvider.GetDirectoryInfo(folder);

            if (!dir.Exists)
            {
                dir.Create();
            }
            if (_fileUtilities.FileExists(_syncItem.DestinationPath))
            {
                _fileUtilities.FileDelete(_syncItem.DestinationPath);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// check if a file exists
        /// </summary>
        /// <param name="path">pathname to check</param>
        /// <returns>true if the file exists</returns>
        public bool FileExists(string path)
        {
            var pathInfo = MtpPath.GetPathInfo(path);

            if (!pathInfo.IsMtpPath)
            {
                return(_fileUtilities.FileExists(path));
            }

            var device = _deviceManager.GetDevice(pathInfo.DeviceName);

            if (device == null)
            {
                return(false);
            }

            return(device.GetObjectFromPath(pathInfo.RelativePathOnDevice) != null);
        }
Exemplo n.º 4
0
        public IList <ISyncItem> FindEpisodesToDownload(string rootFolder, int retryWaitTimeInSeconds, IPodcastInfo podcastInfo, bool retainFeedStream)
        {
            List <ISyncItem> episodesToDownload = new List <ISyncItem>(10);

            if (podcastInfo.Feed == null)
            {
                // it is optional to have a feed
                return(episodesToDownload);
            }

            var    stateKey     = Path.Combine(rootFolder, podcastInfo.Folder);
            string feedSaveFile = null;

            if (retainFeedStream)
            {
                CreateFolderIfNeeded(stateKey);
                feedSaveFile = Path.Combine(Path.Combine(rootFolder, podcastInfo.Folder), "last_download_feed.xml");
            }

            using (var webClient = _webClientFactory.CreateWebClient())
            {
                var downloader = new Downloader(webClient, _feedFactory);

                try
                {
                    var feed = downloader.DownloadFeed(podcastInfo.Feed.Format.Value, podcastInfo.Feed.Address, feedSaveFile);
                    feed.StatusUpdate += StatusUpdate;
                    var episodes = feed.Episodes;

                    var oldestEpisodeToAccept = DateTime.MinValue;
                    if (podcastInfo.Feed.MaximumDaysOld.Value < int.MaxValue)
                    {
                        oldestEpisodeToAccept = _timeProvider.UtcNow.AddDays(-podcastInfo.Feed.MaximumDaysOld.Value);
                    }

                    foreach (IPodcastFeedItem podcastFeedItem in episodes)
                    {
                        if (podcastFeedItem.Published > oldestEpisodeToAccept)
                        {
                            var destinationPath = GetDownloadPathname(rootFolder, podcastInfo, podcastFeedItem);
                            if (!_fileUtilities.FileExists(destinationPath))
                            {
                                var downloadItem = new SyncItem()
                                {
                                    Id       = Guid.NewGuid(),
                                    StateKey = stateKey,
                                    RetryWaitTimeInSeconds = retryWaitTimeInSeconds,
                                    Published           = podcastFeedItem.Published,
                                    EpisodeUrl          = podcastFeedItem.Address,
                                    DestinationPath     = destinationPath,
                                    EpisodeTitle        = string.Format(CultureInfo.InvariantCulture, "{0} {1}", podcastInfo.Folder, podcastFeedItem.EpisodeTitle),
                                    PostDownloadCommand = _commandGenerator.ReplaceTokensInCommand(podcastInfo.PostDownloadCommand, rootFolder, destinationPath, podcastInfo),
                                };
                                episodesToDownload.Add(downloadItem);
                            }
                            else
                            {
                                OnStatusVerbose(string.Format(CultureInfo.InvariantCulture, "Episode already downloaded: {0}", podcastFeedItem.EpisodeTitle), podcastInfo);
                            }
                        }
                        else
                        {
                            OnStatusVerbose(string.Format(CultureInfo.InvariantCulture, "Episode too old: {0}", podcastFeedItem.EpisodeTitle), podcastInfo);
                        }
                    }
                }
                catch (Exception e)
                {
                    OnStatusError(string.Format(CultureInfo.InvariantCulture, "Error processing feed {0}: {1}", podcastInfo.Feed.Address, e.Message), podcastInfo);
                }
            }

            var filteredEpisodes = ApplyDownloadStrategy(stateKey, podcastInfo, episodesToDownload);

            foreach (var filteredEpisode in filteredEpisodes)
            {
                OnStatusMessageUpdate(string.Format(CultureInfo.InvariantCulture, "Queued: {0}", filteredEpisode.EpisodeTitle), podcastInfo);
            }
            return(filteredEpisodes);
        }