using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using System.Drawing.Imaging; using System.Net; using System.Collections; using System.Threading; using System.IO; using MySql.Data.MySqlClient; namespace FnbfoodNaverSmartstoreCapture { public partial class MainForm : Form { private string ConnectDB = "Server=211.233.11.93; Port=3306;Database=fnbfood;Uid=fnbfood;Pwd=@fnbfood!;Charset=utf8;convert zero datetime=True;"; private MySqlCommand command; private MySqlDataReader DBResult; public MainForm() { InitializeComponent(); CheckForIllegalCrossThreadCalls = false; this.DoubleBuffered = true; } private void MainForm_Load(object sender, EventArgs e) { DateTimeCheck(); } /// <summary> /// 웹을 실제 실행해야 되는지 체크하도록 함. /// </summary> private void DateTimeCheck() { string Week = System.DateTime.Now.ToString("ddd"); int TimeHM = Convert.ToInt32(System.DateTime.Now.ToString("Hmm")); switch (Week) { case "월": case "화": case "수": case "목": case "금": if (TimeHM > 750 && TimeHM < 1810) { this.Activate(); this.Show(); this.WindowState = FormWindowState.Normal; WebLoad(); } else { this.WaitTime(); } break; case "토": if (TimeHM > 750 && TimeHM < 1310) { this.Activate(); this.Show(); this.WindowState = FormWindowState.Normal; WebLoad(); } else { this.WaitTime(); } break; case "일": if (TimeHM > 2000 && TimeHM < 2100) { this.Activate(); this.Show(); this.WindowState = FormWindowState.Normal; WebLoad(); } else { this.TopMost = false; this.WaitTime(); } break; } } /// <summary> /// /// </summary> private async void WebLoad() { DateTime today = DateTime.Now; DateTime beforeday = DateTime.Now.AddDays(-7); string Today = string.Format("{0:yyyy.MM.dd}", today); string Beforeday = string.Format("{0:yyyy.MM.dd}", beforeday); webBrowser1.Navigate("https://nid.naver.com/nidlogin.login?url=https%3A%2F%2Fsell.smartstore.naver.com%2F%23%2FnaverLoginCallback%3Furl%3Dhttps%253A%252F%252Fsell.smartstore.naver.com%252F%2523"); textBox1.Text = System.DateTime.Now.ToString("MM월 dd일 (ddd) tt h시 mm분"); await Task.Delay(15000); MemberIDPwInsert(); await Task.Delay(15000); webBrowser1.Navigate("https://sell.smartstore.naver.com/#/naverpay/manage/order"); await Task.Delay(20000); webBrowser1.Navigate("https://sell.smartstore.naver.com/o/manage/order/json?range.type=PAY_COMPLETED&range.fromDate=" + Beforeday + "&range.toDate=" + Today + "& detailSearch.type=&paging.current=1&paging.rowsPerPage=100"); await Task.Delay(20000); HtmlElement elem; HtmlElementCollection elems = webBrowser1.Document.GetElementsByTagName("BODY"); if (elems.Count == 1) { elem = elems[0]; string pageSource = elem.InnerHtml; richTextBox1.AppendText(pageSource); } await Task.Delay(5000); this.SendData(); await Task.Delay(5000); this.WaitTime(); } /// <summary> /// /// </summary> private async void MemberIDPwInsert() { try { HtmlDocument hd = webBrowser1.Document; HtmlElement heUser = hd.GetElementById("id"); HtmlElement hePassword = hd.GetElementById("pw"); HtmlElement heForm = hd.GetElementById("frmNIDLogin"); heUser.SetAttribute("value", "giofood1004"); await Task.Delay(1500); hePassword.SetAttribute("value", "wldh00$"); await Task.Delay(1500); heForm.InvokeMember("submit"); } catch (Exception ex) { } } private async void SendData() { DateTime today = DateTime.Now; string Today = string.Format("{0:yyyy-MM-dd HH:mm:ss}", today); using (MySqlConnection connection = new MySqlConnection(ConnectDB)) { try { string Query = String.Format("INSERT INTO `fnbfood`.`smartstore_crawler` (data, processFlag, datetime) VALUES ('{0}', '{1}', '{2}')", richTextBox1.Text, "N", Today); connection.Open(); command = new MySqlCommand(Query, connection); command.ExecuteNonQuery(); connection.Close(); this.WindowState = FormWindowState.Minimized; } catch (Exception ex) { Console.WriteLine(ex); } } } /// <summary> /// 다음 실행까지 대기 시간 주기 /// </summary> private async void WaitTime() { //label4.Text = "다음 실행 대기중"; GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); int TimeMin = 0; int DelayMin = 0; TimeMin = Convert.ToInt32(System.DateTime.Now.ToString("mm")); if (TimeMin >= 35) { DelayMin = 65 - TimeMin; } else if (TimeMin >= 5) { DelayMin = 35 - TimeMin; } else { DelayMin = 5; } // 음수 체크하기 if (DelayMin < 1) { DelayMin = 5; } //DelayMin = 1; // 다시 시작 하도록 설정 this.Hide(); this.WindowState = FormWindowState.Minimized; // 텍스트 표시 및 딜레이 DelayMin = (DelayMin * 60) * 1000; await Task.Delay(DelayMin); // DateTimeCheck(); } } } if (HideBtn == false) { HtmlElementCollection links = webBrowser1.Document.GetElementsByTagName("a"); foreach (HtmlElement link in links) { //Console.WriteLine(link.OuterHtml.ToString()); if (link.OuterHtml.ToString().Contains("좌측 내비게이션 펼쳐보기")) { link.InvokeMember("click"); HideBtn = true; } } }