Esempio n. 1
0
        private void Response(IAsyncResult result)
        {
            var tuple = (Tuple <WebRequest, Action <WebSearchResponse> >)result.AsyncState;
            var ea    = new WebSearchResponse {
                Provider = this
            };

            try
            {
                var req  = tuple.Item1;
                var resp = req.EndGetResponse(result);

                using (var reader = new StreamReader(resp.GetResponseStream()))
                {
                    var data = reader.ReadToEnd();
                    ea.Results = ParseResults(data).ToList();
                }

                tuple.Item2(ea);
            }
            catch (Exception ex)
            {
                ea.Exception = ex;
                tuple.Item2(ea);
            }
        }
Esempio n. 2
0
        private void Response(IAsyncResult result)
        {
            var tuple = (Tuple <WebRequest, Action <WebSearchResponse> >)result.AsyncState;
            var ea    = new WebSearchResponse {
                Provider = this
            };

            try
            {
                var req  = tuple.Item1;
                var resp = req.EndGetResponse(result);

                var data = new StreamReader(resp.GetResponseStream(), Encoding.GetEncoding(1251)).ReadToEnd();

                ea.Results = LoadXml(data).Select(r => new WebSearchResult {
                    Name = r.Name, PosterUrl = r.Poster, ReleaseUrl = r.Link, Size = r.Size
                }).ToList();
                tuple.Item2(ea);
            }
            catch (Exception ex)
            {
                ea.Exception = ex;
                tuple.Item2(ea);
            }
        }
Esempio n. 3
0
        private void ProvidersResults(WebSearchResponse response)
        {
            if (response.Results == null)
            {
                _client.AsyncOperation.Post(o =>
                {
                    var tab  = FindByProvider(response.Provider);
                    tab.Text = string.Format("{0} (0)", response.Provider.TabTitle);
                }, null);
                return;
            }

            foreach (var result in response.Results)
            {
                result.PosterReceived += DcSharaResPosterReceived;
            }

            _client.AsyncOperation.Post(o =>
            {
                infoPanel.Hide();

                var tab = FindByProvider(response.Provider);

                tab.Text = string.Format("{0} ({1})", response.Provider.TabTitle, response.Results.Count);

                var flowPanel = (FlowLayoutPanel)tab.Controls[0];

                flowPanel.Controls.Clear();

                flowPanel.SuspendDrawing();
                foreach (var providerResult in response.Results)
                {
                    var poster = providerResult.Poster;

                    var control = new PosterControl {
                        Text = providerResult.Name,
                        Tag  = providerResult
                    };

                    if (poster != null)
                    {
                        control.Poster = poster;
                    }

                    control.Click += control_Click;

                    flowPanel.Controls.Add(control);
                }
                flowPanel.ResumeDrawing(true);
                flowPanel.Refresh();
            }, null);
        }
Esempio n. 4
0
        /// <summary>
        /// Function to parse web search responses.
        /// Displays website name, URL and a short snippet in the UI
        /// </summary>
        /// <param name="webSearchResponse"></param>
        private void ParseWebSearchResponse(WebSearchResponse webSearchResponse)
        {
            StringBuilder sb = new StringBuilder();

            Webpages webPages = webSearchResponse.webPages;

            foreach (WebValue website in webPages.value)
            {
                sb.AppendFormat("{0}\n", website.name);
                sb.AppendFormat("URL: {0}\n", website.displayUrl);
                sb.AppendFormat("About: {0}\n\n", website.snippet);
            }

            SearchResults = sb.ToString();
        }
        public async Task <WebSearchResponse> SearchWeb(string query, SafeSearch safeSearch)
        {
            string endpoint = string.Format("{0}{1}&safeSearch={2}&count=5&mkt=en-US", "https://api.cognitive.microsoft.com/bing/v5.0/search?q=", query, safeSearch.ToString());

            try
            {
                WebSearchResponse response = await _webRequest.MakeRequest <WebSearchResponse>(endpoint);

                return(response);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            return(null);
        }