/// <summary>
        /// start running the task - the task is started in the background and the method will return
        /// </summary>
        public void Start(object state)
        {
            lock (_lock)
            {
                if (SyncItem == null)
                {
                    throw new DownloaderException("SyncItem has not been set before calling the downloader");
                }

                if (_started || _client != null)
                {
                    throw new DownloaderException("Cannot start the task twice");
                }

                _started            = true;
                _progressPercentage = 0;
                _bytesDownloaded    = 0;
                _stopWatch          = new Stopwatch();
                _stopWatch.Start();

                _client = _webClientFactory.CreateWebClient();
                _client.ProgressUpdate        += new EventHandler <ProgressEventArgs>(ClientProgressUpdate);
                _client.DownloadFileCompleted += new AsyncCompletedEventHandler(ClientDownloadFileCompleted);

                CreateFolderIfNeeded();
                _client.DownloadFileAsync(SyncItem.EpisodeUrl, GetDownloadFilename(), SyncItem);
            }
        }
        private List <WallpaperInfo> GetWallpaperInfosFromPage(Uri uri)
        {
            var webClient = _webClientFactory.CreateWebClient();
            var result    = webClient.DownloadString(uri);

            var dom            = _htmlParser.Parse(result);
            var figureElements = dom.QuerySelectorAll("figure");
            var wallpaperInfos = CreateWallpaperInfoFromPage(figureElements);

            return(wallpaperInfos);
        }
Example #3
0
        public ABTestConfiguration Read(PathDescriptor cdnUrl)
        {
            using (var webClient = _webClientFactory.CreateWebClient())
            {
                try
                {
                    var xml = webClient.DownloadString(cdnUrl + new PathDescriptor("application/ABTesting_Configuration.xml"));

                    if (string.IsNullOrEmpty(xml?.Trim()))
                    {
                        return(new ABTestConfiguration());
                    }

                    return(ParseABTestingConfiguration(xml, _clientUrlBuilder, webClient));
                }
                catch (HttpException)
                {
                    return(new ABTestConfiguration());
                }
            }
        }
Example #4
0
        public ABTestConfiguration Read(PathDescriptor cdnUrl)
        {
            using (var webClient = _webClientFactory.CreateWebClient())
            {
                try
                {
                    var json = webClient.DownloadString(cdnUrl + new PathDescriptor("application/ABTestConfig.json"));

                    if (string.IsNullOrEmpty(json?.Trim()))
                    {
                        return(new ABTestConfiguration());
                    }

                    return(ParseABTestingConfiguration(json));
                }
                catch (HttpNotFoundException)
                {
                    return(new ABTestConfiguration());
                }
            }
        }
Example #5
0
        private async Task DownloadAsync(DownloadProcessInfo info)
        {
            try
            {
                _logger.LogInformation($"Downloading data from link: {info.Link} to {info.Filename}...");

                if (!File.Exists(info.Filename) || (File.Exists(info.Filename) && _configuration.OverwriteResults))
                {
                    using var client = _webClientFactory.CreateWebClient(info);
                    await client.DownloadFile(info.Key, info.Link, info.Filename, info.TokenSource.Token);

                    _logger.LogInformation($"File {info.Filename} successfully downloaded");
                }
                else
                {
                    _logger.LogWarning($"File {info.Filename} already exists and overwrite is not allowed!");
                }
            }
            catch (Exception ex)
            {
                await _notificationService.ProccessError(_logger, ex);
            }
        }
Example #6
0
 /// <summary>
 /// コンストラクタ
 /// </summary>
 /// <param name="factory">使用するIWebClientFactoryオブジェクト</param>
 public LivetubeClient(IWebClientFactory factory)
 {
     _webClient = factory.CreateWebClient();
 }
Example #7
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);
        }
 protected override void When()
 {
     _client = _factory.CreateWebClient();
 }
Example #9
0
 /// <summary>
 /// コンストラクタ
 /// </summary>
 /// <param name="factory">使用するIWebClientFactoryオブジェクト</param>
 public LivetubeClient(IWebClientFactory factory)
 {
     _webClient = factory.CreateWebClient();
 }