Exemplo n.º 1
0
        /// <summary>
        /// Read response, call parse method according to response type and returns data as a List of SearchResult.
        /// </summary>
        public static List <SearchResult> ParseResponse(ParseResponseStruct prs)
        {
            if (prs.engine.Type.Type == "xml")
            {
                XmlReader xmlReader   = XmlReader.Create(prs.webResponse.GetResponseStream());
                XDocument xmlResponse = XDocument.Load(xmlReader);

                return(ParseXml(xmlResponse, prs.engine));
            }
            else if (prs.engine.Type.Type == "json")
            {
                string responseString;
                using (Stream responseStream = prs.webResponse.GetResponseStream())
                {
                    responseString = new StreamReader(responseStream).ReadToEnd();
                }

                if (string.IsNullOrEmpty(responseString))
                {
                    return(new List <SearchResult>());
                }

                return(ParseJson(responseString, prs.engine));
            }
            return(new List <SearchResult>());
        }
Exemplo n.º 2
0
        /// <summary>
        /// Send request to each search engine, wait the fastest response and parse it
        /// </summary>
        public async Task <IEnumerable <SearchResult> > GetResultsFromFastestEngine(string searchTerm, IDbEditor db)
        {
            this.db = db;
            IQueryable <SearchEngine>          searchEngines = db.GetEngines();
            List <Task <ParseResponseStruct> > responses     = new List <Task <ParseResponseStruct> >();

            foreach (SearchEngine engine in searchEngines)
            {
                if (!engine.IsDisable)
                {
                    responses.Add(Task.Run(() => SendWebRequestAsync(searchTerm, engine)));
                }
            }
            ParseResponseStruct fastestResponse = await Task.WhenAny(responses).Result;

            List <SearchResult> results = Parser.ParseResponse(fastestResponse);

            return(results);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Makes a request to the any API and returns data as a ParseResponseStruct with response and engine.
        /// </summary>
        public async Task <ParseResponseStruct> SendWebRequestAsync(string searchQuery, SearchEngine engine)
        {
            HttpWebRequest  request  = BuildRequestToEngine(searchQuery, engine);
            HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();

            foreach (string responseHeader in response.Headers)
            {
                foreach (RequestHeader requestHeader in engine.Headers)
                {
                    SaveHeader(requestHeader, responseHeader);
                }
            }

            ParseResponseStruct prs = new ParseResponseStruct
            {
                webResponse = response,
                engine      = engine
            };

            return(prs);
        }