Files
CyworldDownloader/Form1.cs
2014-11-10 04:40:06 +00:00

269 lines
7.8 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.IO;
using mshtml;
using System.Web;
namespace CyworldDownloader
{
public partial class Form1 : Form
{
string strLoginURL = "http://www.cyworld.com";
string strContentURL = "http://minihp.cyworld.com/pims/board/image/imgbrd_list.asp?tid=21572675&cpage=";
int m_iPage = 1;
int m_iLastPage = 616; // 616
bool m_bFirst = true;
string ORGCONTENTS_PATH = Application.StartupPath + "\\Originals";
string CONTENTS_PATH = Application.StartupPath + "\\Contents";
string IMAGE_PATH = Application.StartupPath + "\\Contents\\Images";
List<string> photoURLlist = new List<string>();
StreamWriter m_ImgLog = null;
StreamWriter m_ImgLog2 = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DirectoryInfo di = new DirectoryInfo(ORGCONTENTS_PATH);
if(di.Exists == false)
di.Create();
di = new DirectoryInfo(CONTENTS_PATH);
if(di.Exists == false)
di.Create();
di = new DirectoryInfo(IMAGE_PATH);
if(di.Exists == false)
di.Create();
m_ImgLog = new StreamWriter(CONTENTS_PATH + "\\ImageLinks.html", true);
m_ImgLog2 = new StreamWriter(CONTENTS_PATH + "\\ImageLinks2.html", true, Encoding.UTF8);
wbLogin.Navigate(strLoginURL);
}
private void btDebug_Click(object sender, EventArgs e)
{
string org = "IMG%5F2421+%BA%B9%BB%E7%2Ejpg";
string str2 = HttpUtility.UrlDecode(org, Encoding.Default);
string str3 = HttpUtility.UrlEncode(str2, Encoding.Default);
Console.WriteLine("break");
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
m_ImgLog.Close();
m_ImgLog2.Close();
}
private void wbLogin_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser browser = (WebBrowser)sender;
if(e.Url.Host != browser.Url.Host)
return;
if(m_bFirst == true)
{
wbLogin.Document.GetElementById("ID").InnerText = "_last_@hanmail.net";
wbLogin.Document.GetElementById("PASSDM").Focus();
wbLogin.Document.GetElementsByTagName("HTML")[0].ScrollTop = 201;
wbLogin.Document.GetElementsByTagName("HTML")[0].ScrollLeft = 652;
m_bFirst = false;
}
else
{
photoURLlist.Clear();
wbContent.Navigate(strContentURL+m_iPage);
}
}
private void wbContent_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser browser = (WebBrowser)sender;
if(e.Url.Host != browser.Url.Host)
return;
HtmlElement element = browser.Document.GetElementsByTagName("HTML")[0];
string strHTML = element.OuterHtml;
strHTML = strHTML.Replace("<BODY oncontextmenu=\"return false\"", "<BODY");
string strFileName = string.Format("{0}\\Content{1:000}.html", ORGCONTENTS_PATH, m_iPage);
StreamWriter writer = new StreamWriter(strFileName, false, Encoding.UTF8);
writer.Write(strHTML);
writer.Close();
string strLine = string.Format("<br><strong>Page {0}</strong><br>", m_iPage);
m_ImgLog.WriteLine(strLine);
m_ImgLog2.WriteLine(strLine);
string strTarget = strHTML;
while(true)
{
int iPos = strTarget.IndexOf("file_down");
if(iPos < 0)
break;
int iPos2 = strTarget.Substring(Math.Max(iPos-200, 0), 200).LastIndexOf("http") + Math.Max(iPos-200, 0);
int iPos3_1 = strTarget.Substring(iPos2, 500).IndexOf("\"");
int iPos3_2 = strTarget.Substring(iPos2, 500).IndexOf("'");
int iPos3 = 0;
if(iPos3_1 < 0 || iPos3_2 < 0)
iPos3 = Math.Max(iPos3_1, iPos3_2);
else
iPos3 = Math.Min(iPos3_1, iPos3_2);
if(iPos3 < 0)
{
strTarget.Remove(iPos, "file_down".Length);
Console.WriteLine("Page {0} cannot find Pos3");
continue;
}
iPos3 += iPos2;
string photourl = strTarget.Substring(iPos2, iPos3-iPos2);
string newPhotoURL = photourl;
newPhotoURL = HttpUtility.UrlDecode(newPhotoURL, Encoding.Default);
strLine = string.Format("<a href='{0}'>{0}</a><br>", photourl);
m_ImgLog.WriteLine(strLine);
strLine = string.Format("<a href='{0}'>{0}</a><br>", newPhotoURL);
m_ImgLog2.WriteLine(strLine);
string[] tokens = newPhotoURL.Split('/');
newPhotoURL = tokens.Last();
strTarget = strTarget.Replace(photourl, "Images/"+newPhotoURL);
photoURLlist.Add(photourl);
Console.WriteLine(photourl);
}
m_ImgLog.Flush();
m_ImgLog2.Flush();
strFileName = string.Format("{0}\\Content{1:000}.html", CONTENTS_PATH, m_iPage);
writer = new StreamWriter(strFileName, false, Encoding.UTF8);
writer.Write(strTarget);
writer.Close();
if(m_iPage < m_iLastPage)
{
m_iPage++;
wbContent.Navigate(strContentURL+m_iPage);
}
}
private void wbPhoto_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser browser = (WebBrowser)sender;
if(e.Url.Host != browser.Url.Host)
return;
// 참고
// http://www.developerfusion.com/code/4712/generate-an-image-of-a-web-page/
//using (Graphics srcGraphics = browser.CreateGraphics())
//{
// using (Graphics destGraphics = this.pictureBox1.CreateGraphics())
// {
// IntPtr hdcDest = destGraphics.GetHdc();
// IntPtr hdcSrc = srcGraphics.GetHdc();
// GDI32.BitBlt(
// hdcDest,
// 0, 0,
// browser.ClientRectangle.Width, browser.ClientRectangle.Height,
// hdcSrc,
// 0, 0,
// (int) GDI32.SRCCOPY
// );
// srcGraphics.ReleaseHdc(hdcSrc);
// destGraphics.ReleaseHdc(hdcDest);
// }
//}
//IHTMLDocument2 document = (mshtml.IHTMLDocument2)browser.Document.DomDocument;
//if(document != null)
//{
// IHTMLElement element = (IHTMLElement)document.body;
// if(element != null)
// {
// IHTMLElementRender render = (IHTMLElementRender)element;
// if(render != null)
// {
// using(Graphics graphics = this.pictureBox1.CreateGraphics())
// {
// IntPtr hdc = graphics.GetHdc();
// render.DrawToDC(hdc);
// }
// }
// }
//}
IHTMLDocument2 document = (IHTMLDocument2)browser.Document.DomDocument;
if(document != null)
{
IHTMLElement element = (IHTMLElement)document.body;
if(element != null)
{
IHTMLElementRender render = (IHTMLElementRender)element;
if(render != null)
{
using(Graphics graphics = this.pictureBox1.CreateGraphics())
{
IntPtr hdcDestination = graphics.GetHdc();
render.DrawToDC(hdcDestination);
IntPtr hdcMemory = GDI32.CreateCompatibleDC(hdcDestination);
IntPtr bitmap = GDI32.CreateCompatibleBitmap(
hdcDestination,
browser.ClientRectangle.Width, browser.ClientRectangle.Height
);
if(bitmap != IntPtr.Zero)
{
IntPtr hOld = (IntPtr)GDI32.SelectObject(hdcMemory, bitmap);
GDI32.BitBlt(
hdcMemory,
0, 0,
browser.ClientRectangle.Width, browser.ClientRectangle.Height,
hdcDestination,
0, 0,
(int)GDI32.TernaryRasterOperations.SRCCOPY
);
GDI32.SelectObject(hdcMemory, hOld);
GDI32.DeleteDC(hdcMemory);
graphics.ReleaseHdc(hdcDestination);
pictureBox1.Image = Image.FromHbitmap(bitmap);
}
}
}
}
}
pictureBox1.Image.Save("image.bmp");
}
}
}