Files
WebChecker/Form1.cs
2014-11-10 04:39:44 +00:00

252 lines
6.1 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Net;
namespace WebChecker
{
public partial class Form1 : Form
{
const int CHECK_PERIOD = 1000*30;
WebBrowser wb = new WebBrowser();
string url = "http://me.sayclub.com/profile/home/view/ban8";
//string url = "http://me.sayclub.com/profile/home/view/tosvmfks53";
Timer m_Timer = new Timer();
int m_iReqCnt = 0;
int m_iRespCnt = 0;
DateTime m_LastCheckT;
StreamWriter m_FileLog = new StreamWriter("WebChecker.log", true);
WebClient client = new WebClient();
public Form1()
{
InitializeComponent();
notifyIcon1.Visible = false;
tbURL.Text = url;
m_Timer.Interval = 1000;
m_Timer.Tick += new EventHandler(m_Timer_Tick);
wb.ScriptErrorsSuppressed = true;
wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
client.DownloadDataCompleted +=new DownloadDataCompletedEventHandler(client_DownloadDataCompleted);
Log("=== App Launch ===");
}
private void Form1_Load(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
private void Form1_Shown(object sender, EventArgs e)
{
this.Hide();
}
void Alert(string message)
{
this.Show();
this.TopMost = true;
FlashWindow.Start(this);
MessageBox.Show(this, message);
FlashWindow.Stop(this);
}
void Log(string message)
{
Console.WriteLine("[" + DateTime.Now + "] " + message);
m_FileLog.WriteLine("[" + DateTime.Now + "] " + message);
m_FileLog.Flush();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
notifyIcon1.Visible = false;
Log("=== App Closing ===");
m_FileLog.Close();
}
void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if(e.Url.Host != wb.Url.Host)
return;
if((sender == wb) &&
(wb.ReadyState == WebBrowserReadyState.Complete || wb.ReadyState == WebBrowserReadyState.Interactive))
{
lbStatus.Text = "상태 : 응답 받고 체크 중";
m_iRespCnt++;
lbRespCnt.Text = "응답 : " + m_iRespCnt;
HtmlDocument doc = wb.Document;
HtmlElement element = doc.GetElementById("sbPersonalInfo");
if(element == null)
return;
element = element.Children[element.Children.Count-1];
element = element.Children[0];
element = element.GetElementsByTagName("IMG")[0];
string attr = element.GetAttribute("src");
if(attr.IndexOf("login") >= 0)
{
Process.Checked = false;
Log(url + " checked");
Alert("logged in");
}
lbStatus.Text = "상태 : 체크 완료";
wb.Stop();
}
}
void m_Timer_Tick(object sender, EventArgs e)
{
if(wb.IsBusy == false)
{
TimeSpan span = DateTime.Now-m_LastCheckT;
if(span.TotalMilliseconds >= CHECK_PERIOD)
{
m_iReqCnt++;
lbReqCnt.Text = "요청 : " + m_iReqCnt;
lbStatus.Text = "상태 : 요청 중";
wb.Navigate(url);
m_LastCheckT = m_LastCheckT+TimeSpan.FromMilliseconds(CHECK_PERIOD);
}
else
{
lbStatus.Text = "상태 : 대기 (" + (int)(CHECK_PERIOD-span.TotalMilliseconds)/1000 + "초...)";
}
}
}
private void Process_CheckedChanged(object sender, EventArgs e)
{
if(Process.Checked == true)
{
m_LastCheckT = DateTime.Now - TimeSpan.FromMilliseconds(CHECK_PERIOD-1000);
m_Timer.Start();
Log(url + " check start");
url = tbURL.Text;
tbURL.Enabled = false;
}
else
{
m_iReqCnt = 0;
m_iRespCnt = 0;
lbReqCnt.Text = "요청 : 0";
lbRespCnt.Text = "응답 : 0";
lbStatus.Text = "상태 : 대기";
m_Timer.Stop();
Log(url + " check stop");
tbURL.Enabled = true;
if(wb.IsBusy == true)
wb.Stop();
}
}
private void Form1_Resize(object sender, EventArgs e)
{
if(this.WindowState == FormWindowState.Minimized)
{
notifyIcon1.Visible = true;
this.Hide();
}
else if(this.WindowState == FormWindowState.Normal)
{
notifyIcon1.Visible = false;
this.ShowInTaskbar = true;
}
}
private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
private void btOpen_Click(object sender, EventArgs e)
{
try
{
System.Diagnostics.Process.Start(tbURL.Text);
}
catch(System.ComponentModel.Win32Exception noBrowser)
{
if(noBrowser.ErrorCode == -2147467259)
MessageBox.Show(noBrowser.Message);
}
catch(System.Exception other)
{
MessageBox.Show(other.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
//url = "http://main.com";
url = tbURL.Text;
client.DownloadDataAsync(new Uri(url));
}
void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
}
void client_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
string conType = client.ResponseHeaders["Content-Type"];
int iPos = conType.IndexOf("charset");
Encoding enc = Encoding.Default;
if(iPos >= 0)
{
iPos = conType.IndexOf("=", iPos+"charset".Length);
if(iPos >= 0)
{
string charset = conType.Substring(iPos+1);
enc = Encoding.GetEncoding(charset);
}
}
string contents = enc.GetString(e.Result);
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(contents);
HtmlAgilityPack.HtmlNode node = doc.DocumentNode.SelectSingleNode("//ul[@class='info']/li/img");
HtmlAgilityPack.HtmlAttribute attr = node.Attributes["src"];
string strInnerText = "";
if(attr != null)
strInnerText = attr.Value;
}
}
}