c# 调用迅雷下载漫画

鉴于家里网速的问题,上同人の侍这个工口网站很慢而且提心吊胆而且一页一页的翻着很麻烦~于是出现了同人の侍专用漫画下载器~ALPHA版

同人の侍网址:http://samurai3.net/

已完成功能:下载大部分的漫画

欠缺:部分漫画的命名:有的是000~00n

有的是00~n 未作相关处理,对于漫画页是否有效未作验证如漫画实际命名为000.JPG 001.JPG 002.JPG 099.JPG

会从000.jpg一直尝试下载到099.jpg 而对无效的003.jpg~098.jpg未作处理(处理方法很简单 不过懒得做了~)

程序使用方法:给出标准网址如火影:http://samurai3.net/doujinshi/naruto.html

                                                         死神:http://samurai3.net/doujinshi/bleach.html

                                                         海贼王:http://samurai3.net/doujinshi/onepeace.html

(应该是one piece对吧 所以说日本人啊~)

                                                           EVA:http://samurai3.net/doujinshi/evangelion.html

双击给出的可用下载漫画列表中的某项即开始下载

代码如下:

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.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
using System.Runtime.InteropServices;
//using System.Threading;
using Microsoft.Win32;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        #region //调用的dLL
        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        private static extern IntPtr FindWindow(string lpClassName,
    string lpWindowName);

        [DllImport("user32.dll", EntryPoint = "FindWindowEx")]
        private static extern IntPtr FindWindowEx(IntPtr hwndParent,
    IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        private static extern int SendMessage(IntPtr hWnd,
    int Msg, IntPtr wParam, string lParam);
        [DllImport("user32.dll")]
        static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);

        const int WM_GETTEXT = 0x000D;
        const int WM_SETTEXT = 0x000C;
        const int WM_CLICK = 0x00F5;
        #endregion

        private Regex urlpartten=new Regex( "href=\"http\\S+/manga.html\"");
        private Regex numpartten = new Regex("\\d+\\.jpg");
        private void button1_Click(object sender, EventArgs e)
        {
            Thread threadmain = new Thread(new ThreadStart(begintowork));
            threadmain.IsBackground = true;
            threadmain.Start();
          
        }
        private void begintowork()
        {
            string[] urls= GetPage(textBox1.Text).ToArray();
            sf = new showinfo(showf);
            this.Invoke(sf, (object)urls);
            ThreadPool.SetMaxThreads(50, 1000);
           // ThreadPool.QueueUserWorkItem(new WaitCallback(tpproc),urls);
           // tpproc(urls);
            ThreadPool.QueueUserWorkItem(new WaitCallback(ClosePopWindow));

        }

        private delegate void showinfo(object s);
        showinfo sf;
        private void showf(object urls)
        {
            string[] url = urls as string[];
            if (url != null)
            {
               foreach(string s in url)
               {
                   this.listView1.Items.Add(s);
               }
            }
        }
        private void tpproc(object url2down)
        {
          // string[] urls = mangaurls as string[];
            agent = new THUNDERAGENTLib.AgentClass();


            string a_url = url2down as string;
           
           
           
           
          // foreach (string a_url in urls)
           // {
                int num = 0;
                GetPage(a_url, out num);
                string arl = a_url.Replace("http://", "").Replace('/', '_').Replace('.','_');
                string brl_1 = a_url.Replace("manga.html", "image/00" );
                string brl_2 = a_url.Replace("manga.html", "image/0");
                if (num != 0)
                {
                    DirectoryInfo dr = new DirectoryInfo("d:\\downloadmanga\\"+arl);
                    if (!dr.Exists)
                    {
                        dr.Create();
                    }
                    //MessageBox.Show(a_url + num.ToString());
                    if (num < 100)
                    {
                       // WebClient wc = new WebClient();
                      // try
                       // {
                            for (int i = 1; i < num + 1; i++)
                            {
                                if (i < 10)
                                {
                                   // wc.DownloadFile(brl_1 + i.ToString() + ".jpg", "d:\\downloadmanga\\"+arl + "00" + i.ToString() + ".jpg");
                                    string address = brl_1 + i.ToString() + ".jpg";
                                    string filename = "d:\\downloadmanga\\" + arl+"\\" + "00" + i.ToString() + ".jpg";
                                  
                                                                       agent.AddTask(address,"00" + i.ToString() + ".jpg","d:\\downloadmanga\\" + arl+"\\","","" ,1 ,0 ,0);
                                                                      //.... you can add some other task here
                                                                        //killthunder.;
                                    ThreadPool.QueueUserWorkItem(new WaitCallback(ClosePopWindow));
                                    agent.CommitTasks();
                                    Thread.Sleep(50);
                                                                  }
                                else
                                {
                                   
                                    string address = brl_2 + i.ToString() + ".jpg";
                                    string filename = "d:\\downloadmanga\\" + arl + "\\" + "0" + i.ToString() + ".jpg";
                                    agent.AddTask(address, "0" + i.ToString() + ".jpg", "d:\\downloadmanga\\" + arl + "\\", "", "", 1, 0, 0);
                                    //.... you can add some other task here
                                             ThreadPool.QueueUserWorkItem(new WaitCallback(ClosePopWindow));
                                    agent.CommitTasks();
                                }
                            }
                        //}
                      // catch { }
                    }
                    else
                    {
                        //没看见有那么多的 先留个接口;
                    }
               
                }
                else
                {
               
                }
            //}
        }
        private List<string> GetPage(string orgurl)
        {
           
            try
            {
                StringBuilder pagecontent = new StringBuilder();
                WebRequest getpagerequest = WebRequest.Create(new Uri(orgurl));
                WebResponse respond = getpagerequest.GetResponse();
                StreamReader SR = new StreamReader(respond.GetResponseStream(), Encoding.Default);//Encoding.GetEncoding("utf-8"));
                pagecontent.Append(SR.ReadToEnd());
                SR.Close();
                MatchCollection urlmatched=urlpartten.Matches( pagecontent.ToString());
                List<string> s = new List<string>();
                foreach(Match urlm in urlmatched )
                {
                   
                s.Add(urlm.Value.Substring(6,urlm.Value.Length-7));
               
                }
                return s;
                           
            }
            catch
            {
                return null;
            }
        }
        private void GetPage(string orgurl, out int num)
        {

            try
            {
                StringBuilder pagecontent = new StringBuilder();
                WebRequest getpagerequest = WebRequest.Create(new Uri(orgurl));
                WebResponse respond = getpagerequest.GetResponse();
                StreamReader SR = new StreamReader(respond.GetResponseStream(), Encoding.Default);//Encoding.GetEncoding("utf-8"));
                pagecontent.Append(SR.ReadToEnd());
                SR.Close();

                MatchCollection nummatched = numpartten.Matches(pagecontent.ToString());
                int max = 0;
                foreach (Match nummatch in nummatched)
                {
                    int temp = int.Parse(nummatch.Value.Substring(0, nummatch.Value.Length - 4));
                    if (temp > max)
                    {
                        max = temp;
                    }
                }
                num= max;

            }
            catch {num= 0; }
       
       
        }
        THUNDERAGENTLib.AgentClass agent;
        Thread killthunder;
        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.Text = @"http://samurai3.net/doujinshi/doujinshi.html";
            DirectoryInfo downfolder = new DirectoryInfo(Application.StartupPath + "\\sandcu");
            if (!downfolder.Exists)
            {
                downfolder.Create();
            }
                      agent = new THUNDERAGENTLib.AgentClass();
            
        }
        private void ClosePopWindow(object obj)
        {
            //这些用spy++可以看到
            string lpszParentClass = "#32770"; //整个窗口的类名
            string lpszParentWindow = "建立新的下载任务"; //窗口标题
            string lpszClass_Submit = "Button"; //需要查找的Button的类名
            string lpszName_Submit = "确定(&O)"; //需要查找的Button的标题         

            IntPtr ParenthWnd = new IntPtr(0);
            IntPtr EdithWnd = new IntPtr(0);
            int i = 0;
            while (true)
            {
                //查到窗体,得到整个窗体
                ParenthWnd = FindWindow(lpszParentClass, lpszParentWindow);
                //判断这个窗体是否有效
                if (!ParenthWnd.Equals(IntPtr.Zero))
                {

                    //得到Button这个子窗体,并触发它的Click事件
                    EdithWnd = FindWindowEx(ParenthWnd,
                        EdithWnd, lpszClass_Submit, lpszName_Submit);
                    if (!EdithWnd.Equals(IntPtr.Zero))
                    {
                        SendMessage(EdithWnd, WM_CLICK, (IntPtr)0, "0");
                    }
                    return;
                }
                Thread.Sleep(1000);
                i++;
                 if (i > 5)
                {
                    break;
                }
            }
        }

        private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Thread s=new Thread (new ParameterizedThreadStart(tpproc));
            s.IsBackground=true;
            s.Start(listView1.SelectedItems[0].Text.ToString());
        }

    }
}

PS:本程序需在项目中加载迅雷组件的引用 因为webclient.dowmloadfile()不支持断点续传,自己实现起来挺麻烦,迅雷的弹出确认对话框也很麻烦 这里采取的方法是用线程池监视并自动确认 有好方法的同志望不吝赐教~

你可能感兴趣的:(C#)