Esempio n. 1
0
        /// <summary>
        /// Looks up data on a given web page by searching the DOM for elements matching the configured selector
        /// </summary>
        /// <param name="address">Address of the web page to retrieve</param>
        /// <param name="selector">Element selector to server as the basis for the query</param>
        /// <returns>Returns a PageQueryResult object that contains an array of the text content for matching elements</returns>
        private static async Task <PageQueryResult> getMatchingElements(string address, string selector)
        {
            // Setup AngleSharp to be able to load external pages
            var config = AngleSharp.Configuration.Default.WithDefaultLoader();

            // Load the DOM for the page at the requested address
            var document = await BrowsingContext.New(config).OpenAsync(address);

            // Perform the query to get all matching fragments on the site
            var matchingFragments = document.QuerySelectorAll(selector);

            PageQueryResult result = new PageQueryResult()
            {
                Results = matchingFragments.Select(m => m.TextContent).ToArray()
            };

            return(result);
        }
Esempio n. 2
0
        public async Task <IHttpActionResult> PollWebPageContent(
            [Metadata("Web Address", @"Site to poll for changes (e.g., ""https://msn.com/weather/news"")")]
            string address,

            [Metadata("Element Selector", @"Selector for desired content (e.g., ""li.headlines>a>h3"")")]
            string selector,

            [Metadata("Last result", "State data stored by the Logic App Runtime", Visibility = VisibilityType.Internal)]
            string lastResult = null
            )
        {
            PageQueryResult result = await getMatchingElements(address, selector);

            var paramsForNextPoll = new
            {
                address    = address,
                selector   = selector,
                lastResult = result.GetHash()
            };

            if (null != lastResult && result.GetHash() != lastResult)
            {
                // 200 OK Response with result content
                // Location header points to the next address to poll

                // In this case, since we are triggering the event:
                //  TimeSpan.Zero = Poll immediately (not at pre-configured intereval).
                return(ResponseMessage(Request.EventTriggered(result, TimeSpan.Zero, nameof(PollWebPageContent), paramsForNextPoll)));
            }
            else
            {
                // This code will execute under the following conditions:
                // If this has never been triggered before, pass a hash of the result back to the runtime as a baseline
                // If this has been run before, but the result is the same as it was the last time around

                // 202 Accepted Response with no content
                // Location header points to the next address to poll

                // In this case, since we are not triggering the event:
                //  TimeSpan.Zero = Poll at pre-configured interval in Logic App
                return(ResponseMessage(Request.EventWaitPoll(TimeSpan.Zero, nameof(PollWebPageContent), paramsForNextPoll)));
            }
        }
Esempio n. 3
0
        public PageQueryResult ExecutePageQuery(string sql, int pageIndex, int pageSize, string orderBy, IList <object> parameters)
        {
            if (string.IsNullOrEmpty(orderBy))
            {
                throw new ArgumentNullException("orderBy");
            }

            PageQueryResult result = new PageQueryResult(pageIndex, pageSize);

            #region 查询满足条件的条目数量
            string queryItemCountSql = string.Format("SELECT COUNT(*) FROM ( {0} ) T1", sql);

            result.ItemCount = this.ExecuteScalar <int>(queryItemCountSql, parameters);

            if (result.ItemCount == 0)  // 如果满足条件的数据条目数量为零,则重置页索引为 1
            {
                result.PageIndex = 1;
            }
            else if (result.PageIndex > result.PageCount)   // 如果指定的页索引大于查询直接总页数,则重置页索引为总页数
            {
                result.PageIndex = result.PageCount;
            }
            #endregion

            #region 查询最终的结果集
            if (parameters == null)
            {
                parameters = new List <object>();
            }

            parameters.Add((result.PageIndex - 1) * result.PageSize);
            parameters.Add(result.PageSize);

            string queryItemSql = string.Format("SELECT * FROM ( {0} ) T ORDER BY {1} LIMIT ?, ?", sql, orderBy);

            result.Data = this.ExecuteDataTable(queryItemSql, parameters);
            #endregion

            return(result);
        }
Esempio n. 4
0
        public async Task <IActionResult> SearchPages(string searchPattern, int start, int pageSize, string excludeId)
        {
            if (searchPattern == null)
            {
                searchPattern = "";
            }

            GoNorthProject project = await _projectDbAccess.GetDefaultProject();

            Task <List <KirjaPage> > queryTask;
            Task <int> countTask;

            queryTask = _pageDbAccess.SearchPages(project.Id, searchPattern, start, pageSize, excludeId);
            countTask = _pageDbAccess.SearchPagesCount(project.Id, searchPattern, excludeId);
            Task.WaitAll(queryTask, countTask);

            PageQueryResult queryResult = new PageQueryResult();

            queryResult.Pages   = queryTask.Result;
            queryResult.HasMore = start + queryResult.Pages.Count < countTask.Result;
            return(Ok(queryResult));
        }