コード例 #1
0
 private void populateEnginecmb()
 {
     engineInfo = Config.Instance.Apis[cmbAPIs.SelectedIndex];
     pupulatecmbEngine();
     cmbEngine.SelectedIndex = 0;
     google_api_key          = engineInfo.Api_key;
 }
コード例 #2
0
 private void cmbAPIs_SelectedIndexChanged(object sender, EventArgs e)
 {
     engineInfo = Config.Instance.Apis[cmbAPIs.SelectedIndex];
     pupulatecmbEngine();
     cmbEngine.SelectedIndex = 0;
     google_api_key          = engineInfo.Api_key;
 }
コード例 #3
0
 private void cmbAPIs_SelectedIndexChanged(object sender, EventArgs e)
 {
     engineInfo = Config.Instance.Apis[cmbAPIs.SelectedIndex];
     labelLimitReached.Visible = engineInfo.Expired;
     pupulatecmbEngine();
     cmbEngine.SelectedIndex = 0;
     google_api_key          = engineInfo.Api_key;
 }
コード例 #4
0
        public void Load_engine_xml()
        {
            try
            {
                XmlDocument doc          = new XmlDocument();
                String      assembly_loc = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                doc.Load(assembly_loc + "/" + engine_file);
                List <EngineInfo> engineInfos = new List <EngineInfo>();
                XmlNode           root        = doc.DocumentElement;
                XmlNodeList       google_apis = root.SelectNodes("google_api");
                foreach (XmlNode api in google_apis)
                {
                    XmlNodeList engineList = api.SelectNodes("engine");
                    EngineInfo  engineInfo = new EngineInfo(api.Attributes.GetNamedItem("api").InnerText, engineList.Count);
                    int         i          = 0;
                    foreach (XmlNode node in engineList)
                    {
                        XmlNode       filter_node = node.SelectSingleNode("filters");
                        List <string> filters     = new List <string>();

                        if (filter_node != null)
                        {
                            XmlNodeList filterList = filter_node.SelectNodes("entry");
                            foreach (XmlNode filter in filterList)
                            {
                                filters.Add(filter.InnerText);
                            }
                        }
                        engineInfo.Add_engine(i, new EngineInfo.Engine(node.SelectSingleNode("Title").InnerText, node.SelectSingleNode("cx").InnerText, filters));
                        i++;
                    }
                    engineInfos.Add(engineInfo);
                }
                Apis       = engineInfos;
                first_time = false;
            }
            catch (Exception e)
            {
                first_time = true;
                //MessageBox.Show(e.Message);
            }
        }
コード例 #5
0
        public SearchResult Search(string query, List <object> extras)
        {
            if (Config.Instance.Selected_API != null)
            {
                engineInfo = Config.Instance.Selected_API;
            }
            else
            {
                engineInfo = Config.Instance.Apis[0];
            }
            google_api_key = engineInfo.Api_key;
            if (Config.Instance.Selected_Engine != null)
            {
                google_cx_engine = Config.Instance.Selected_Engine;
            }
            else
            {
                google_cx_engine = engineInfo.Engines[0];
            }
            MAX_RESULTS         = Config.Instance.Max_results;
            MillisecondsTimeout = Config.Instance.MillisecondsTimeout;
            //System.Threading.Thread.Sleep(5000);
            string       json = string.Empty;
            dynamic      jsonData;
            int          total_results = -1;
            int          start_index   = -1;
            int          count         = -1;
            List <Intel> intels        = new List <Intel>();
            SearchResult searchResult;
            string       message = "Searching...";
            Dictionary <string, List <Intel> > filtered_intel = new Dictionary <string, List <Intel> >();
            List <TreeNode> intel_nodes = new List <TreeNode>();

            foreach (string filter in google_cx_engine.Filters)
            {
                filtered_intel.Add(filter, new List <Intel>());
            }
            do
            {
                string url = @"https://www.googleapis.com/customsearch/v1?" +
                             "key=" + google_api_key + "&" +
                             "cx=" + google_cx_engine.Cx + "&" +
                             "q=" + HttpUtility.UrlEncode(query) + Config.Instance.ExtraParams;
                System.Threading.Thread.Sleep(MillisecondsTimeout);
                try
                {
                    if (start_index == -1)
                    {
                        json = request_api(url);
                    }
                    else
                    {
                        string newurl = url + "&start=" + start_index;

                        json = request_api(newurl);
                    }
                }
                catch (WebException e)
                {
                    message += Environment.NewLine;
                    message += e.Message;
                    message += Environment.NewLine;
                    message += Lang.Eng.attemping_different_api;
                    message += Environment.NewLine;
                    if (e.Message.Contains("(429) Too Many Requests.") && Config.Instance.Apis.Count > 1)
                    {
                        Config.Instance.Apis.Remove(engineInfo);
                        engineInfo       = Config.Instance.Apis[0];
                        google_api_key   = engineInfo.Api_key;
                        google_cx_engine = engineInfo.Engines[0];
                        url = @"https://www.googleapis.com/customsearch/v1?" +
                              "key=" + google_api_key + "&" +
                              "cx=" + google_cx_engine.Cx + "&" +
                              "q=" + HttpUtility.UrlEncode(query) + Config.Instance.ExtraParams;
                        try
                        {
                            json = request_api(url);
                        }
                        catch (WebException e2)
                        {
                            searchResult = new SearchResult(intels, DateTime.Now, intels.Count, Lang.Eng.title, Status_Code.ERROR, e2.Message);
                            return(searchResult);
                        }
                    }
                }
                if (!json.Equals(String.Empty))
                {
                    jsonData      = JsonConvert.DeserializeObject(json);
                    start_index   = jsonData.queries.request[0].startIndex;
                    count         = jsonData.queries.request[0].count;
                    total_results = jsonData.searchInformation.totalResults;

                    if (total_results > MAX_RESULTS)
                    {
                        total_results = MAX_RESULTS;
                    }
                    if (jsonData.items != null)
                    {
                        foreach (var item in jsonData.items)
                        {
                            Result result = new Result
                            {
                                Title   = item.title,
                                Link    = item.link,
                                Snippet = item.snippet,
                            };
                            Intel intel = new Intel()
                            {
                                Title       = item.title,
                                Description = item.snippet,
                                Uri         = item.link,
                                Extras      = new ArrayList(),
                                From_module = Lang.Eng.title,
                                Timestamp   = DateTime.Now,
                            };
                            intel.Fix_Characters();
                            if (google_cx_engine.Filters.Count > 0)
                            {
                                foreach (string filter in google_cx_engine.Filters)
                                {
                                    if (intel.Uri.ToString().Contains(filter.ToLower()))
                                    {
                                        filtered_intel[filter].Add(intel);
                                        break;
                                    }
                                }
                            }
                            if (Config.Instance.Metadata)
                            {
                                try
                                {
                                    dynamic         metadata      = item.pagemap.metatags[0];
                                    List <TreeNode> metadata_node = new List <TreeNode>();
                                    foreach (var meta_item in metadata)
                                    {
                                        String value = Convert.ToString(meta_item);
                                        metadata_node.Add(new TreeNode(value));
                                    }
                                    result.Metadata = new TreeNode("Metadata", metadata_node.ToArray());
                                    intel.Extras.Add(result.Metadata);
                                }
                                catch (Exception e)
                                {
                                    logMessage("No Metadata for " + intel.Title);
                                }
                            }
                            intels.Add(intel);
                            result.Fix_characters();
                            results.Add(result);
                        }
                    }
                    start_index += count;
                }
                else
                {
                    //logMessage(Lang.Eng.empty_response);
                    message     += Environment.NewLine;
                    message     += Lang.Eng.empty_response;
                    message     += Environment.NewLine;
                    message     += Lang.Eng.completed;
                    searchResult = new SearchResult(intels, DateTime.Now, intels.Count, Lang.Eng.title, Status_Code.ERROR, message);
                    return(searchResult);
                }
            } while (start_index < total_results);
            message += Environment.NewLine;
            message += Lang.Eng.completed;
            List <TreeNode> filtered_Tree_nodes = new List <TreeNode>();

            foreach (KeyValuePair <string, List <Intel> > kvp in filtered_intel)
            {
                List <TreeNode> single_filter_tree_nodes = new List <TreeNode>();
                foreach (Intel intel in kvp.Value)
                {
                    TreeNode meta = new TreeNode("Metadata");
                    if (typeof(ArrayList).IsInstanceOfType(intel.Extras))
                    {
                        if (((ArrayList)intel.Extras).Count > 0)
                        {
                            meta = (TreeNode)intel.Extras[0];
                        }
                    }
                    TreeNode[] intel_node_array = new TreeNode[] {
                        new TreeNode(intel.Description),
                        new TreeNode(intel.Uri.ToString()),
                        new TreeNode(intel.Timestamp.ToString()),
                        meta,
                    };
                    TreeNode intel_node = new TreeNode(intel.Title, intel_node_array);
                    single_filter_tree_nodes.Add(intel_node);
                }
                filtered_Tree_nodes.Add(new TreeNode(kvp.Key, single_filter_tree_nodes.ToArray()));

                /*
                 * Console.WriteLine("Key = {0}, Value = {1}",
                 *  kvp.Key, kvp.Value);*/
            }
            if (filtered_Tree_nodes.Count > 0)
            {
                TreeNode module_node = new TreeNode(Lang.Eng.title, filtered_Tree_nodes.ToArray());
                searchResult = new SearchResult(module_node, DateTime.Now, null, module_node.GetNodeCount(true), Lang.Eng.title, Status_Code.DONE, message);
            }
            else
            {
                searchResult = new SearchResult(intels, DateTime.Now, intels.Count, Lang.Eng.title, Status_Code.DONE, message);
            }

            return(searchResult);
        }