diff --git a/NaverSearcher/Form1.cs b/NaverSearcher/Form1.cs index 0f612a5..41f91f3 100644 --- a/NaverSearcher/Form1.cs +++ b/NaverSearcher/Form1.cs @@ -16,10 +16,38 @@ namespace NaverSearcher { private static Random m_Random = new Random(); - WebSocket m_WebSocket = new WebSocket("ws://api.inrose.com"); + private static readonly string x00 = "w"; + private static readonly string x01 = "s"; + private static readonly string x02 = ":"; + private static readonly string x03 = "/"; + private static readonly string x04 = "/"; + private static readonly string x05 = "a"; + private static readonly string x06 = "p"; + private static readonly string x07 = "i"; + private static readonly string x08 = "."; + private static readonly string x09 = "i"; + private static readonly string x0A = "n"; + private static readonly string x0B = "r"; + private static readonly string x0C = "o"; + private static readonly string x0D = "s"; + private static readonly string x0E = "e"; + private static readonly string x0F = "."; + private static readonly string x10 = "c"; + private static readonly string x11 = "o"; + private static readonly string x12 = "m"; + + private static readonly string xFF = (x00 + x01 + x02 + x03 + x04 + x05 + x06 + x07 + x08 + x09 + x0A + x0B + x0C + x0D + x0E + x0F + x10 + x11 + x12); + + WebSocket m_WebSocket = new WebSocket(xFF); Thread m_Thread = new Thread(NaverSearcherExcute); + private static uint optPageMoveWaitMin = 2000; + private static uint optPageMoveWaitMax = 3000; + + private static uint optSearchWaitMin = 2000; + private static uint optSearchWaitMax = 6000; + public Form1() { InitializeComponent(); @@ -33,8 +61,41 @@ namespace NaverSearcher { JObject _JObject = JObject.Parse(e.Data); - if (_JObject.ContainsKey("keepalive")) + if (_JObject.ContainsKey("option")) { + JObject _JObject_option = JObject.Parse(_JObject["option"].ToString()); + + if (_JObject_option.ContainsKey("PageMoveWaitMin")) + { + optPageMoveWaitMin = uint.Parse(_JObject_option.GetValue("PageMoveWaitMin").ToString()); + + if (optPageMoveWaitMin > optPageMoveWaitMax) + optPageMoveWaitMax = optPageMoveWaitMin; + } + + if (_JObject_option.ContainsKey("PageMoveWaitMax")) + { + optPageMoveWaitMax = uint.Parse(_JObject_option.GetValue("PageMoveWaitMax").ToString()); + + if (optPageMoveWaitMax < optPageMoveWaitMin) + optPageMoveWaitMin = optPageMoveWaitMax; + } + + if (_JObject_option.ContainsKey("SearchWaitMin")) + { + optSearchWaitMin = uint.Parse(_JObject_option.GetValue("SearchWaitMin").ToString()); + + if (optSearchWaitMin > optSearchWaitMax) + optSearchWaitMax = optSearchWaitMin; + } + + if (_JObject_option.ContainsKey("SearchWaitMax")) + { + optSearchWaitMax = uint.Parse(_JObject_option.GetValue("SearchWaitMax").ToString()); + + if (optSearchWaitMax < optSearchWaitMin) + optSearchWaitMin = optSearchWaitMax; + } } if (_JObject.ContainsKey("key_pairs")) @@ -45,6 +106,7 @@ namespace NaverSearcher if (m_Thread.ThreadState == ThreadState.Stopped) m_Thread = new Thread(NaverSearcherExcute); + m_Thread.IsBackground = true; m_Thread.Start(_JObject); } }; @@ -200,6 +262,8 @@ namespace NaverSearcher JObject _JObject = (JObject)_Object; ChromeOptions _ChromeOptions = new ChromeOptions(); + + /* _ChromeOptions.AddArguments("disable-infobars"); _ChromeOptions.AddArguments("--js-flags=--expose-gc"); _ChromeOptions.AddArguments("--enable-precise-memory-info"); @@ -207,6 +271,8 @@ namespace NaverSearcher _ChromeOptions.AddArguments("--disable-default-apps"); _ChromeOptions.AddArguments("--headless"); _ChromeOptions.AddArguments("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"); + */ + _ChromeOptions.BinaryLocation = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\SmartSearch\\chrome.exe"; // 프록시 설정 //Proxy proxy = new Proxy(); @@ -220,12 +286,9 @@ namespace NaverSearcher ChromeDriverService _ChromeDriverService = ChromeDriverService.CreateDefaultService(); _ChromeDriverService.HideCommandPromptWindow = true; - //_ChromeDriver = new ChromeDriver(_ChromeDriverService, _ChromeOptions); - ChromeDriver _ChromeDriver = new ChromeDriver(); + ChromeDriver _ChromeDriver = new ChromeDriver(_ChromeDriverService, _ChromeOptions); - //_ChromeOptions.add_argument("user-agent=Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko") - - WebDriverWait _WebDriverWait = new WebDriverWait(_ChromeDriver, TimeSpan.FromSeconds(3)); + WebDriverWait _WebDriverWait = new WebDriverWait(_ChromeDriver, TimeSpan.FromSeconds(5)); try { @@ -275,13 +338,17 @@ namespace NaverSearcher { _query_elment.Clear(); _query_elment.SendKeys(item); + Thread.Sleep(m_Random.Next(500, 1000)); } _query_elment.SendKeys(OpenQA.Selenium.Keys.Enter); + PageMoveWait(_ChromeDriver); + ((IJavaScriptExecutor)_ChromeDriver).ExecuteScript("window.scrollBy(0, document.body.scrollHeight);"); - Thread.Sleep(m_Random.Next(2000, 6000)); + + SearchWait(_ChromeDriver); ReadOnlyCollection links = _ChromeDriver.FindElements(By.CssSelector("a[href*='https://']")); @@ -292,7 +359,6 @@ namespace NaverSearcher try { int _sel = m_Random.Next(0, links.Count); - string kknd = links[_sel].GetAttribute("href"); links[_sel].Click(); break; @@ -304,8 +370,11 @@ namespace NaverSearcher } } while (--_try > 0); + PageMoveWait(_ChromeDriver); + ((IJavaScriptExecutor)_ChromeDriver).ExecuteScript("window.scrollBy(0, document.body.scrollHeight);"); - Thread.Sleep(m_Random.Next(2000, 6000)); + + SearchWait(_ChromeDriver); if (_ChromeDriver.WindowHandles.First() != _ChromeDriver.WindowHandles.Last()) { @@ -364,108 +433,6 @@ namespace NaverSearcher { try { - ChromeDriver _ChromeDriver = new ChromeDriver(); - WebDriverWait _WebDriverWait = new WebDriverWait(_ChromeDriver, TimeSpan.FromSeconds(3)); - - foreach (var key_words in _JObject["key_pairs"]) - { - // 로그인 여부 확인 - // 로그아웃 실행 - // 네이버 메인 - // 스크롤 내린다. - // 20 ~ 60초 대기 - - try - { - _ChromeDriver.Navigate().GoToUrl(@"https://naver.com"); - _WebDriverWait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//*[@id=\"query\"]"))); - - string _logout = @"/html/body/div/div/div[1]/div[1]/a[1]"; - - if (IsElementPresent(_ChromeDriver, By.XPath(_logout))) - { - _ChromeDriver.FindElement(By.XPath(_logout)).Click(); - } - - foreach (var key_word in key_words["key_words"]) - { - // 검색어 입력 (검색어 1) - // 검색어 확인 - // 스크롤 내린다. - // 20 ~ 60초 대기 - // 아무 게시글 클릭 - // 이동된 페이지 작업 - // 스크롤 내리기 - // 20 ~ 60초 대기 - // 새창 닫기 - - try - { - _ChromeDriver.Navigate().GoToUrl(@"https://naver.com"); - - string _query_xpath = "//*[@id=\"query\"]"; - - _WebDriverWait.Until(ExpectedConditions.ElementIsVisible(By.XPath(_query_xpath))); - - IWebElement _query_elment = _ChromeDriver.FindElement(By.XPath(_query_xpath)); - - foreach (var item in StringParser.CharacterMakeHistory(key_word["key_word"].ToString())) - { - _query_elment.Clear(); - _query_elment.SendKeys(item); - Thread.Sleep(m_Random.Next(500, 1000)); - } - - _query_elment.SendKeys(OpenQA.Selenium.Keys.Enter); - - ((IJavaScriptExecutor)_ChromeDriver).ExecuteScript("window.scrollBy(0, document.body.scrollHeight);"); - Thread.Sleep(m_Random.Next(2000, 6000)); - - ReadOnlyCollection links = _ChromeDriver.FindElements(By.CssSelector("a[href*='https://']")); - - int _try = links.Count; - - do - { - try - { - int _sel = m_Random.Next(0, links.Count); - string kknd = links[_sel].GetAttribute("href"); - links[_sel].Click(); - - break; - } - catch (Exception ex) - { - Console.WriteLine("=== 무작위 링크 접속 ==="); - Console.WriteLine(ex.Message); - } - } while (--_try > 0); - - ((IJavaScriptExecutor)_ChromeDriver).ExecuteScript("window.scrollBy(0, document.body.scrollHeight);"); - Thread.Sleep(m_Random.Next(2000, 6000)); - - if (_ChromeDriver.WindowHandles.First() != _ChromeDriver.WindowHandles.Last()) - { - _ChromeDriver.SwitchTo().Window(_ChromeDriver.WindowHandles.Last()); - _ChromeDriver.Close(); - _ChromeDriver.SwitchTo().Window(_ChromeDriver.WindowHandles.First()); - } - } - catch (Exception ex) - { - Console.WriteLine("=== 검색어 ==="); - Console.WriteLine(ex.Message); - } - } - } - catch (Exception ex) - { - Console.WriteLine("=== 검색어 페어 ==="); - Console.WriteLine(ex.Message); - } - } - /* var elem = _ChromeDriver.FindElementByXPath("//*"); @@ -597,5 +564,19 @@ namespace NaverSearcher return false; } } + + private static void PageMoveWait(IWebDriver _IWebDriver) + { + // 페이지 이동 후 대기 및 끝 탭 설정 + Thread.Sleep(m_Random.Next((int)optPageMoveWaitMin, (int)optPageMoveWaitMax)); + _IWebDriver.SwitchTo().Window(_IWebDriver.WindowHandles.Last()); + } + + private static void SearchWait(IWebDriver _IWebDriver) + { + // 페이지 이동 후 대기 및 끝 탭 설정 + Thread.Sleep(m_Random.Next((int)optSearchWaitMin, (int)optSearchWaitMax)); + _IWebDriver.SwitchTo().Window(_IWebDriver.WindowHandles.Last()); + } } } diff --git a/NaverSearcher/NaverSearcher.csproj b/NaverSearcher/NaverSearcher.csproj index 3bef28c..2c7eeba 100644 --- a/NaverSearcher/NaverSearcher.csproj +++ b/NaverSearcher/NaverSearcher.csproj @@ -2,18 +2,24 @@ WinExe - net45 + net35 true + + + True + True + Resources.resx + True True @@ -21,6 +27,13 @@ + + + ResXFileCodeGenerator + Resources.Designer.cs + + + SettingsSingleFileGenerator diff --git a/NaverSearcher/Program.cs b/NaverSearcher/Program.cs index 1752519..d52bfe3 100644 --- a/NaverSearcher/Program.cs +++ b/NaverSearcher/Program.cs @@ -1,5 +1,7 @@ using System; +using System.IO; using System.Windows.Forms; +using ICSharpCode.SharpZipLib.Zip; namespace NaverSearcher { @@ -11,7 +13,21 @@ namespace NaverSearcher [STAThread] static void Main() { - //SetEnvironmentVariable + FileInfo _FileInfo = new FileInfo(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\SmartSearch\\chrome.exe"); + if (_FileInfo.Exists == false) + { + if (!Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\SmartSearch")) + { + Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\SmartSearch"); + } + + // ҽ + File.WriteAllBytes(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\SmartSearch\\chrome.zip", Properties.Resources.chrome); + + FastZip fastZip = new FastZip(); + fastZip.ExtractZip(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\SmartSearch\\chrome.zip", Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\SmartSearch", ""); + } + string _guid = Properties.Settings.Default.GUID; if (_guid == string.Empty || _guid == null) diff --git a/NaverSearcher/Properties/Resources.Designer.cs b/NaverSearcher/Properties/Resources.Designer.cs new file mode 100644 index 0000000..b4327f3 --- /dev/null +++ b/NaverSearcher/Properties/Resources.Designer.cs @@ -0,0 +1,73 @@ +//------------------------------------------------------------------------------ +// +// 이 코드는 도구를 사용하여 생성되었습니다. +// 런타임 버전:4.0.30319.42000 +// +// 파일 내용을 변경하면 잘못된 동작이 발생할 수 있으며, 코드를 다시 생성하면 +// 이러한 변경 내용이 손실됩니다. +// +//------------------------------------------------------------------------------ + +namespace NaverSearcher.Properties { + using System; + + + /// + /// 지역화된 문자열 등을 찾기 위한 강력한 형식의 리소스 클래스입니다. + /// + // 이 클래스는 ResGen 또는 Visual Studio와 같은 도구를 통해 StronglyTypedResourceBuilder + // 클래스에서 자동으로 생성되었습니다. + // 멤버를 추가하거나 제거하려면 .ResX 파일을 편집한 다음 /str 옵션을 사용하여 ResGen을 + // 다시 실행하거나 VS 프로젝트를 다시 빌드하십시오. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() { + } + + /// + /// 이 클래스에서 사용하는 캐시된 ResourceManager 인스턴스를 반환합니다. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("NaverSearcher.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// 이 강력한 형식의 리소스 클래스를 사용하여 모든 리소스 조회에 대해 현재 스레드의 CurrentUICulture 속성을 + /// 재정의합니다. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// System.Byte[] 형식의 지역화된 리소스를 찾습니다. + /// + internal static byte[] chrome { + get { + object obj = ResourceManager.GetObject("chrome", resourceCulture); + return ((byte[])(obj)); + } + } + } +} diff --git a/NaverSearcher/Properties/Resources.resx b/NaverSearcher/Properties/Resources.resx new file mode 100644 index 0000000..58d200e --- /dev/null +++ b/NaverSearcher/Properties/Resources.resx @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + + chrome.zip;System.Byte[], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/NaverSearcher/Properties/chrome.zip b/NaverSearcher/Properties/chrome.zip new file mode 100644 index 0000000..df4cfa6 Binary files /dev/null and b/NaverSearcher/Properties/chrome.zip differ