예제 #1
0
        /// <summary>
        /// Connects to the specified chrome session.
        /// </summary>
        /// <param name="serviceProvider"></param>
        /// <param name="sessionInfo"></param>
        /// <returns></returns>
        public static async Task <SkraprDevTools> Connect(IServiceProvider serviceProvider, ChromeSessionInfo sessionInfo)
        {
            if (serviceProvider == null)
            {
                throw new ArgumentNullException(nameof(serviceProvider));
            }

            if (sessionInfo == null)
            {
                throw new ArgumentNullException(nameof(sessionInfo));
            }

            var chromeSessionLogger = serviceProvider
                                      .GetService <ILoggerFactory>()
                                      .CreateLogger <ChromeSession>();

            var devToolsLogger = serviceProvider
                                 .GetService <ILoggerFactory>()
                                 .CreateLogger <SkraprDevTools>();

            //Create a new session using the information in the session info.
            var session  = new ChromeSession(chromeSessionLogger, sessionInfo.WebSocketDebuggerUrl);
            var devTools = new SkraprDevTools(devToolsLogger, session, sessionInfo.Id);
            await devTools.Initialize();

            return(devTools);
        }
예제 #2
0
        public SkraprWorker(ILogger logger, SkraprDefinition definition, ChromeSession session, SkraprDevTools devTools, bool isDebugEnabled = false)
        {
            m_logger = logger ?? throw new ArgumentNullException(nameof(logger));
            m_logger = logger;
            m_cts    = new CancellationTokenSource();

            m_mainFlow = new ActionBlock <ISkraprTask>(ProcessMainSkraprTask, new ExecutionDataflowBlockOptions
            {
                CancellationToken      = m_cts.Token,
                EnsureOrdered          = true,
                MaxDegreeOfParallelism = 1
            });

            m_devTools   = devTools ?? throw new ArgumentNullException(nameof(devTools));
            m_session    = session;
            m_definition = definition;

            m_isDebugEnabled = isDebugEnabled;
        }
예제 #3
0
        /// <summary>
        /// Creates a new SkraprWorker that processes the specified definition through tasks.
        /// </summary>
        /// <param name="serviceProvider"></param>
        /// <param name="pathToSkraprDefinition"></param>
        /// <param name="session"></param>
        /// <param name="devTools"></param>
        /// <param name="debugMode"></param>
        /// <returns></returns>
        public static SkraprWorker Create(IServiceProvider serviceProvider, string pathToSkraprDefinition, ChromeSession session, SkraprDevTools devTools, bool debugMode = false)
        {
            if (!File.Exists(pathToSkraprDefinition))
            {
                throw new FileNotFoundException($"The specified skrapr definition ({pathToSkraprDefinition}) could not be found. Please check that the skrapr definition exists.");
            }

            var skraprLogger = serviceProvider
                               .GetService <ILoggerFactory>()
                               .CreateLogger <SkraprWorker>();

            JToken skraprDefinitionJson;

            //The pyramid of json reading.
            using (var fs = File.OpenRead(pathToSkraprDefinition))
            {
                using (var textReader = new StreamReader(fs))
                {
                    using (var jsonReader = new JsonTextReader(textReader))
                    {
                        skraprDefinitionJson = JToken.ReadFrom(jsonReader, new JsonLoadSettings
                        {
                            CommentHandling  = CommentHandling.Ignore,
                            LineInfoHandling = LineInfoHandling.Load
                        });
                    }
                }
            }

            var skraprDefinition = skraprDefinitionJson.ToObject <SkraprDefinition>();

            return(new SkraprWorker(skraprLogger, skraprDefinition, session, devTools, debugMode));
        }