/// <exception cref="ArgumentNullException">
        /// <paramref name="htmlLoader" /> or
        /// <paramref name="parser" /> or
        /// <paramref name="backupProcessor" /> or
        /// <paramref name="httpDownloadManager" /> or
        /// <paramref name="dataService" /> or
        /// <paramref name="fileSystemProxy" /> is <see langword="null" />.</exception>
        internal PluralsightCatalog(IHtmlLoader htmlLoader,
                                    ITrainingCatalogParser <PluralsightCategory, PluralsightCourse, PluralsightAuthor> parser,
                                    ICatalogBackupProcessor <PluralsightCategory, PluralsightCourse, PluralsightAuthor> backupProcessor,
                                    IHttpDownloadManager httpDownloadManager,
                                    IPluralsightDataService dataService,
                                    IFileSystemProxy fileSystemProxy)
            : base(httpDownloadManager, fileSystemProxy)
        {
            if (htmlLoader == null)
            {
                throw new ArgumentNullException("htmlLoader");
            }

            if (parser == null)
            {
                throw new ArgumentNullException("parser");
            }

            if (backupProcessor == null)
            {
                throw new ArgumentNullException("backupProcessor");
            }

            if (dataService == null)
            {
                throw new ArgumentNullException("dataService");
            }

            _htmlLoader      = htmlLoader;
            _parser          = parser;
            _backupProcessor = backupProcessor;
            _dataService     = dataService;
        }
예제 #2
0
        /// <exception cref="ArgumentNullException">
        /// <paramref name="archiveCurrentSaveDirectory"/> or
        /// <paramref name="httpDownloadManager"/> or
        /// <paramref name="fileSystemProxy"/> is <see langword="null" />.</exception>
        public PluralsightWebDataService(string archiveCurrentSaveDirectory, IHttpDownloadManager httpDownloadManager,
                                         IFileSystemProxy fileSystemProxy, bool createArchives = true)
            : base(fileSystemProxy)
        {
            if (archiveCurrentSaveDirectory == null)
            {
                throw new ArgumentNullException("archiveCurrentSaveDirectory");
            }

            if (httpDownloadManager == null)
            {
                throw new ArgumentNullException("httpDownloadManager");
            }

            if (fileSystemProxy == null)
            {
                throw new ArgumentNullException("fileSystemProxy");
            }

            _archiveCurrentSaveDirectory = archiveCurrentSaveDirectory;
            _httpDownloadManager         = httpDownloadManager;

            _authorsInfoContainer = new Dictionary <string, string>();
            _coursesInfoContainer = new Dictionary <string, string>();
            _coursesToCContainer  = new Dictionary <string, string>();

            _courseSpecializationsContainer = new AsyncLazy <Dictionary <string, Specializations> >(async() =>
                                                                                                    await InitializeCourseSpecializationContainerAsync());

            _createArchives = createArchives;
        }
        /// <exception cref="ArgumentNullException">
        /// <paramref name="trainingProviderName"/> or
        /// <paramref name="mediaPath"/> or
        /// <paramref name="httpDownloadManager"/> or
        /// <paramref name="fileSystemProxy"/>
        /// is <see langword="null" />.</exception>
        internal TrainingCatalogMediaContentProcessor(int trainingProviderId, string trainingProviderName,
                                                      IMediaPath mediaPath,
                                                      IHttpDownloadManager httpDownloadManager, IFileSystemProxy fileSystemProxy)
        {
            if (trainingProviderName == null)
            {
                throw new ArgumentNullException("trainingProviderName");
            }

            if (mediaPath == null)
            {
                throw new ArgumentNullException("mediaPath");
            }

            if (mediaPath == null)
            {
                throw new ArgumentNullException("httpDownloadManager");
            }

            if (mediaPath == null)
            {
                throw new ArgumentNullException("fileSystemProxy");
            }

            _trainingProviderName = trainingProviderName;
            _trainingProviderId   = trainingProviderId;
            _mediaPath            = mediaPath;
            _httpDownloadManager  = httpDownloadManager;
            _fileSystemProxy      = fileSystemProxy;
        }
        public override async Task <Author> GetAuthorAsync(string authorUrlName, IHttpDownloadManager httpDownloadManager)
        {
            using (var dataService = PluralsightWebDataService.CreateForDataQuery(HttpDownloadManager))
            {
                var author = await dataService.GetAuthorAsync(authorUrlName);

                return(author);
            }
        }
예제 #5
0
        /// <exception cref="ArgumentNullException">
        /// <paramref name="httpDownloadManager"/> or
        /// <paramref name="fileSystemProxy"/>
        /// is <see langword="null" />.</exception>
        protected TrainingCatalog(IHttpDownloadManager httpDownloadManager, IFileSystemProxy fileSystemProxy)
        {
            if (httpDownloadManager == null)
            {
                throw new ArgumentNullException("httpDownloadManager");
            }

            if (fileSystemProxy == null)
            {
                throw new ArgumentNullException("fileSystemProxy");
            }

            HttpDownloadManager = httpDownloadManager;
            FileSystemProxy     = fileSystemProxy;
        }
      /// <exception cref="ArgumentNullException">
      /// <paramref name="trainingProviderName"/> or
      /// <paramref name="mediaPath"/> or
      /// <paramref name="httpDownloadManager"/> or
      /// <paramref name="fileSystemProxy"/> 
      /// is <see langword="null" />.</exception>
      internal TrainingCatalogMediaContentProcessor(int trainingProviderId, string trainingProviderName,
         IMediaPath mediaPath,
         IHttpDownloadManager httpDownloadManager, IFileSystemProxy fileSystemProxy)
      {
         if (trainingProviderName == null)
            throw new ArgumentNullException("trainingProviderName");

         if (mediaPath == null)
            throw new ArgumentNullException("mediaPath");

         if (mediaPath == null)
            throw new ArgumentNullException("httpDownloadManager");

         if (mediaPath == null)
            throw new ArgumentNullException("fileSystemProxy");

         _trainingProviderName = trainingProviderName;
         _trainingProviderId = trainingProviderId;
         _mediaPath = mediaPath;
         _httpDownloadManager = httpDownloadManager;
         _fileSystemProxy = fileSystemProxy;
      }
예제 #7
0
 public static PluralsightWebDataService CreateForDataQuery(IHttpDownloadManager httpDownloadManager)
 {
     return(new PluralsightWebDataService(string.Empty, httpDownloadManager, Shared.FileSystemProxy.Instance, createArchives: false));
 }
예제 #8
0
      /// <exception cref="UpdateServiceException">Author already in db.</exception>
      public async Task<CallResult<Author>> CreateAuthorAsync(int trainingProviderId, string authorUrlName, IHttpDownloadManager httpDownloadManager)
      {
         if (authorUrlName == null) throw new ArgumentNullException("authorUrlName");
         if (httpDownloadManager == null) throw new ArgumentNullException("httpDownloadManager");

         if (!await _semaphore.WaitAsync(WaitTimeout))
         {
            return CallResult<Author>.Failed(Resources.UpdateService_CannotUpdateBecauseDatabaseIsInUpdateState);
         }


         Author author;

         try
         {
            using (var context = _contextFactory.CreateDbContext())
            {
               var trainingProvider = await context.TrainingProviders.FindAsync(trainingProviderId);

               var pathsContainer = new ServerMediaPathsContainer(_serverImagesPath, new[] { trainingProvider });


               using (var catalog = _activatorProxy.CreateInstance<ITrainingCatalog>(trainingProvider.AssemblyType))
               {
                  // ReSharper disable once ExceptionNotDocumented
                  catalog.Initialize(trainingProvider.Name, trainingProvider.Id, trainingProvider.SiteUrl,
                     trainingProvider.SourceUrl, trainingProvider.SourceLocation, pathsContainer, _archiveDirectoryPath);

                  author = await catalog.GetAuthorAsync(authorUrlName, httpDownloadManager);
               }

               var sameAuthorsIds = await context.Authors
                  .Where(x => x.LastName == author.LastName &&
                              x.FirstName == author.FirstName &&
                              x.Social.FacebookLink == author.Social.FacebookLink &&
                              x.Social.LinkedInLink == author.Social.LinkedInLink &&
                              x.Social.RssLink == author.Social.RssLink &&
                              x.Social.TwitterLink == author.Social.TwitterLink)
                  .Select(x => x.Id).ToListAsync();

               if (sameAuthorsIds.Any())
               {
                  var message = string.Format(Resources.UpdateServiceException_AuthorAlreadyInDb_Message, author.FullName,
                     string.Join(";", sameAuthorsIds));

                  return CallResult<Author>.Failed(message);
               }

               // save avatar to media folder
               var pathToSave = Path.Combine(pathsContainer.AuthorsLogoPath[trainingProviderId], author.Avatar.Name);

               if (!FileSystemProxy.Instance.IsFileExists(pathToSave))
               {
                  // ensure http scheme
                  var avatarUriBuilder = new UriBuilder(new Uri(author.Avatar.SiteUrl))
                  {
                     Scheme = "http"
                  };

                  var avatarResponse = await httpDownloadManager.DownloadFileAsync(avatarUriBuilder.Uri);

                  if (avatarResponse.IsSuccess)
                  {
                     await FileSystemProxy.Instance.WriteToFileAsync(pathToSave, avatarResponse.Result);
                  }
               }

               context.Authors.Add(author);

               await context.SaveChangesAsync();
            }
         }
         finally
         {
            _semaphore.Release();
         }

         return CallResult<Author>.Success(author);
      }
예제 #9
0
        /// <exception cref="UpdateServiceException">Author already in db.</exception>
        public async Task <CallResult <Author> > CreateAuthorAsync(int trainingProviderId, string authorUrlName, IHttpDownloadManager httpDownloadManager)
        {
            if (authorUrlName == null)
            {
                throw new ArgumentNullException("authorUrlName");
            }
            if (httpDownloadManager == null)
            {
                throw new ArgumentNullException("httpDownloadManager");
            }

            if (!await _semaphore.WaitAsync(WaitTimeout))
            {
                return(CallResult <Author> .Failed(Resources.UpdateService_CannotUpdateBecauseDatabaseIsInUpdateState));
            }


            Author author;

            try
            {
                using (var context = _contextFactory.CreateDbContext())
                {
                    var trainingProvider = await context.TrainingProviders.FindAsync(trainingProviderId);

                    var pathsContainer = new ServerMediaPathsContainer(_serverImagesPath, new[] { trainingProvider });


                    using (var catalog = _activatorProxy.CreateInstance <ITrainingCatalog>(trainingProvider.AssemblyType))
                    {
                        // ReSharper disable once ExceptionNotDocumented
                        catalog.Initialize(trainingProvider.Name, trainingProvider.Id, trainingProvider.SiteUrl,
                                           trainingProvider.SourceUrl, trainingProvider.SourceLocation, pathsContainer, _archiveDirectoryPath);

                        author = await catalog.GetAuthorAsync(authorUrlName, httpDownloadManager);
                    }

                    var sameAuthorsIds = await context.Authors
                                         .Where(x => x.LastName == author.LastName &&
                                                x.FirstName == author.FirstName &&
                                                x.Social.FacebookLink == author.Social.FacebookLink &&
                                                x.Social.LinkedInLink == author.Social.LinkedInLink &&
                                                x.Social.RssLink == author.Social.RssLink &&
                                                x.Social.TwitterLink == author.Social.TwitterLink)
                                         .Select(x => x.Id).ToListAsync();

                    if (sameAuthorsIds.Any())
                    {
                        var message = string.Format(Resources.UpdateServiceException_AuthorAlreadyInDb_Message, author.FullName,
                                                    string.Join(";", sameAuthorsIds));

                        return(CallResult <Author> .Failed(message));
                    }

                    // save avatar to media folder
                    var pathToSave = Path.Combine(pathsContainer.AuthorsLogoPath[trainingProviderId], author.Avatar.Name);

                    if (!FileSystemProxy.Instance.IsFileExists(pathToSave))
                    {
                        // ensure http scheme
                        var avatarUriBuilder = new UriBuilder(new Uri(author.Avatar.SiteUrl))
                        {
                            Scheme = "http"
                        };

                        var avatarResponse = await httpDownloadManager.DownloadFileAsync(avatarUriBuilder.Uri);

                        if (avatarResponse.IsSuccess)
                        {
                            await FileSystemProxy.Instance.WriteToFileAsync(pathToSave, avatarResponse.Result);
                        }
                    }

                    context.Authors.Add(author);

                    await context.SaveChangesAsync();
                }
            }
            finally
            {
                _semaphore.Release();
            }

            return(CallResult <Author> .Success(author));
        }
 /// <exception cref="ArgumentNullException">
 /// <paramref name="trainingProviderName"/> or
 /// <paramref name="mediaPath" /> or
 /// <paramref name="httpDownloadManager" />
 /// is <see langword="null" />.</exception>
 public TrainingCatalogMediaContentProcessor(int trainingProviderId, string trainingProviderName, IMediaPath mediaPath,
                                             IHttpDownloadManager httpDownloadManager)
     : this(trainingProviderId, trainingProviderName, mediaPath, httpDownloadManager, FileSystemProxy.Instance)
 {
 }
예제 #11
0
 public abstract Task <Author> GetAuthorAsync(string authorUrlName, IHttpDownloadManager httpDownloadManager);
 /// <exception cref="ArgumentNullException">
 /// <paramref name="trainingProviderName"/> or
 /// <paramref name="mediaPath" /> or
 /// <paramref name="httpDownloadManager" />
 /// is <see langword="null" />.</exception>
 public TrainingCatalogMediaContentProcessor(int trainingProviderId,string trainingProviderName, IMediaPath mediaPath,
    IHttpDownloadManager httpDownloadManager)
    : this(trainingProviderId,trainingProviderName, mediaPath, httpDownloadManager, FileSystemProxy.Instance)
 {
 }