private IWebElement FindElementWithXPathDict(string xPathKey)
 {
     try
     {
         lock (xpaths)
         {
             foreach (var xpath in this.xpaths[xPathKey])
             {
                 var element = driver.FindElementByXPathOrNull(xpath);
                 if (element != null)
                 {
                     return(element);
                 }
             }
         }
     }
     catch (Exception e)
     {
         if (!this.IsAborted)
         {
             Console.WriteLine("[Exception] Hooker-FindElement FindElementWithXPathDict: Key {0} XPath {1}", xPathKey, e.Message);
         }
     }
     return(null);
 }
Exemplo n.º 2
0
        private void Update()
        {
            if (driver != null)
            {
                driver.Quit();
                driver = null;
            }

            var options = new ChromeOptions();

            options.AddArgument("headless");
            options.AddArgument("disable-gpu");
            options.AddArgument("disable-infobars");
            options.AddArgument("--disable-extensions");
            //options.AddUserProfilePreference("profile.default_content_settings", 2);
            //options.AddUserProfilePreference("profile.default_content_setting_values", 2);

            options.AddArgument("user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6)" +
                                "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"); //Not bot

            var service = ChromeDriverService.CreateDefaultService();

            service.HideCommandPromptWindow = true;

            try
            {
                driver = new ChromeDriver(service, options);
                driver.Navigate().GoToUrl("http://www.google.com/translate_t?hl=en");
                this.Ready = true;
            }
            catch (Exception e)
            {
                if (!this.IsAborted)
                {
                    Console.WriteLine("[Exception] Hooker-CrawlingScripts-LoadChromeDriver: " + e.Message);
                }
                Process[] chromeDriverProcesses = Process.GetProcessesByName("chromedriver");
                foreach (var chromeDriverProcess in chromeDriverProcesses)
                {
                    if (chromeDriverProcess.MainModule.FileName.StartsWith(
                            System.AppDomain.CurrentDomain.BaseDirectory))
                    {
                        chromeDriverProcess.Kill();
                    }
                }
                System.Environment.Exit(-1);
            }

            LoadDictionary();

            while (true)
            {
                if (this.IsAborted)
                {
                    break;
                }

                TranslateWorker currentWork = null;
                lock (works)
                {
                    if (works.Count > 0)
                    {
                        currentWork = works[0];
                        works.RemoveAt(0);

                        if (currentWork.isAborted)
                        {
                            continue;
                        }
                    }
                }

                if (currentWork != null)
                {
                    if (string.IsNullOrEmpty(currentWork.Text) || string.IsNullOrEmpty(currentWork.From) || string.IsNullOrEmpty(currentWork.To))
                    {
                        currentWork.FailedCallback("Input string empty");
                        continue;
                    }

                    try
                    {
                        var targetText = currentWork.Text;

                        //var replaceFormat = "\"{{{0}}}\"";
                        //int currentReplaceIndex = 0;
                        //Dictionary<string, string> replaceStrings = new Dictionary<string, string>();

                        lock (this.translateDictionary)
                        {
                            using (var enumerator = this.translateDictionary.Keys.GetEnumerator())
                            {
                                while (enumerator.MoveNext())
                                {
                                    var key   = enumerator.Current;
                                    var value = this.translateDictionary[key];
                                    var regex = string.Format("\\b({0})\\b", key);

                                    if (Regex.IsMatch(targetText, regex))
                                    {
                                        targetText = Regex.Replace(targetText, regex, value);
                                        //var replaced = string.Format(replaceFormat, currentReplaceIndex);
                                        //targetText = Regex.Replace(targetText, regex, replaced);
                                        //replaceStrings.Add(replaced, value);
                                        //currentReplaceIndex++;
                                    }
                                }
                            }
                        }

                        //string url = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", HttpUtility.UrlEncode(targetText), string.Format("{0}|{1}", currentWork.From, currentWork.To));
                        string url = String.Format("https://translate.google.com/?sl={1}&tl={2}&text={0}&op=translate", HttpUtility.UrlEncode(targetText), currentWork.From, currentWork.To);
                        driver.Navigate().GoToUrl(url);
                        IWebElement translatedElement = null;

                        var startedAt = DateTime.Now;
                        do
                        {
                            if ((DateTime.Now - startedAt).TotalSeconds > TimeOut)
                            {
                                throw new Exception("Timeout");
                            }

                            try
                            {
                                translatedElement = driver.FindElementByXPathOrNull(TranslateResultXPath);
                            }
                            catch (Exception e)
                            {
                                if (!this.IsAborted)
                                {
                                    Console.WriteLine("[Exception] Translator-Update(TranslateWork)-FindElement: " + e.Message);
                                }
                            } //Needs timeout
                        } while (translatedElement == null);

                        var translated = translatedElement.GetAttribute("data-text");
                        //foreach (var key in replaceStrings.Keys)
                        //    translated = translated.Replace(key, replaceStrings[key]);

                        //Add empty lines.
                        string[] lines = targetText.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
                        for (int i = 0; i < lines.Length; i++)
                        {
                            if (string.IsNullOrWhiteSpace(lines[i]))
                            {
                                translated = System.Environment.NewLine + translated;
                            }
                            else
                            {
                                break;
                            }
                        }
                        for (int i = lines.Length - 1; i >= 0; i--)
                        {
                            if (string.IsNullOrWhiteSpace(lines[i]))
                            {
                                translated = translated + System.Environment.NewLine;
                            }
                            else
                            {
                                break;
                            }
                        }

                        currentWork.TranslatedCallback(translated);
                    }
                    catch (Exception e)
                    {
                        if (!this.IsAborted)
                        {
                            Console.WriteLine("[Exception] Translator-Update(TranslateWork): " + e.Message);
                        }
                        currentWork.FailedCallback(e.Message);
                    }
                }

                System.Threading.Thread.Sleep(1);
            }

            //Dispose
            if (works != null)
            {
                lock (works)
                {
                    foreach (var work in works)
                    {
                        work.FailedCallback("Abort");
                    }

                    works.Clear();
                }
                works = null;
            }
            if (driver != null)
            {
                driver.Quit();
                driver = null;
            }
            workThread = null;
        }
        private void Work()
        {
            if (driver != null)
            {
                driver.Quit();
                driver = null;
            }

            var options = new ChromeOptions();

            options.SetLoggingPreference(LogType_Performance, LogLevel.All);
            options.AddArgument("disable-gpu");
            options.AddArgument("disable-infobars");
            options.AddArgument("--disable-extensions");
            //options.AddUserProfilePreference("profile.default_content_settings", 2);
            //options.AddUserProfilePreference("profile.default_content_setting_values", 2);

            //options.AddArgument("-homepage \"" + StartUpURL + "\"");
            options.AddArgument("user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6)" +
                                "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"); //Not bot

            var service = ChromeDriverService.CreateDefaultService();

            service.HideCommandPromptWindow = true;

            try
            {
                driver = new ChromeDriver(service, options);
                driver.Navigate().GoToUrl(StartUpURL);
                this.Ready = true;
            }
            catch (Exception e)
            {
                if (!this.IsAborted)
                {
                    Console.WriteLine("[Exception] Hooker-CrawlingScripts-LoadChromeDriver: " + e.Message);
                }
                Process[] chromeDriverProcesses = Process.GetProcessesByName("chromedriver");
                foreach (var chromeDriverProcess in chromeDriverProcesses)
                {
                    try
                    {
                        if (chromeDriverProcess.MainModule.FileName.StartsWith(
                                System.AppDomain.CurrentDomain.BaseDirectory))
                        {
                            chromeDriverProcess.Kill();
                        }
                    }
                    catch (Exception _e)
                    {
                        if (!this.IsAborted)
                        {
                            Console.WriteLine("[Exception] Hooker-CrawlingScripts-Kill: " + _e.Message);
                        }
                    }
                }
                System.Environment.Exit(-1);
            }
            //Do login
            try
            {
                string account_id;
                if (SaveAccountWindow.GetSavedAccount_ID(out account_id))
                {
                    //Loading..?
                    IWebElement loginFormElement = null;
                    IWebElement idInput          = null;
                    IWebElement pwInput          = null;
                    var         started          = System.DateTime.Now;
                    do
                    {
                        loginFormElement = driver.FindElementByXPathOrNull("/html/body/div[2]");
                        idInput          = FindElementWithXPathDict(XPathKey_Login_IDInputBox);
                        pwInput          = FindElementWithXPathDict(XPathKey_Login_PWInputBox);
                        if ((System.DateTime.Now - started).TotalSeconds > LoginFormWaitTimeout)
                        {
                            throw new Exception("Timeout");
                        }
                        if (this.IsAborted)
                        {
                            throw new Exception("Aborted");
                        }
                    } while (loginFormElement == null || idInput == null || pwInput == null);
                    Console.WriteLine("[Log] Loginform wait completed: {0}", (System.DateTime.Now - started).TotalSeconds);

                    idInput.SendKeys(account_id);
                    account_id = null;

                    string account_pw;
                    if (SaveAccountWindow.GetSavedAccount_Password(out account_pw))
                    {
                        pwInput.SendKeys(account_pw);
                        account_pw = null;

                        var loginButton = FindElementWithXPathDict(XPathKey_Login_LoginButton);
                        if (loginButton != null)
                        {
                            loginButton.Click();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                if (!this.IsAborted)
                {
                    Console.WriteLine("[Exception] Hooker-AutoLogin: "******"message"];
                        var method  = message["method"].Value <string>();
                        #endregion

                        if (string.Equals(method, "Network.webSocketFrameReceived"))
                        {
                            var response = message["params"]["response"];
                            if (response.HasValues)
                            {
                                var payloadData = JToken.Parse(response["payloadData"].Value <string>());
                                ParsePayloadData(payloadData);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    if (!this.IsAborted)
                    {
                        Console.WriteLine("[Exception] Hooker-CrawlingScripts: " + e.Message);
                    }
                }
            }

            //Dispose
            if (driver != null)
            {
                driver.Quit();
                driver = null;
            }
            crawlingThread = null;
        }