吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 20313|回复: 41
收起左侧

[其他转载] C#百度网盘搜索工具附源码

  [复制链接]
明月几时有~ 发表于 2014-2-15 10:23
本人C#新手,这个软件是半成品,仅供大家学习交流之用。
2014-02-15_100818.jpg

Release版本下载:
链接:http://pan.baidu.com/s/1qW8nxbY 密码:5jin
本以为正则匹配html文本是件非常简单的事情,所以一时心血来潮想写个百度网盘搜索工具来练练手,结果折腾了两天,软件没写好问题倒一大堆,最大的问题就是ListView控件加载资源速度太慢,希望能得到吾爱网友的指导~
C#源码下载(我用的是VS2012,打不开的可以直接看以下代码)
附件地址: BaiDuYunVS2012源码.zip (61.23 KB, 下载次数: 377)
百度网盘地址:链接:http://pan.baidu.com/s/1sjPMsJr 密码:oco8
[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
//using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading;
using System.Runtime.InteropServices;
using System.IO;
/*本人C#新手,这个软件是半成品,仅供大家学习交流之用*/
namespace BaiDuYun
{
   
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
        }
        class ListViewNF : System.Windows.Forms.ListView//此类是为了防止ListView控件加载数据时闪烁
        {
            public ListViewNF()
            {
                // 开启双缓冲
                this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);

                // Enable the OnNotifyMessage event so we get a chance to filter out 
                // Windows messages before they get to the form's WndProc
                this.SetStyle(ControlStyles.EnableNotifyMessage, true);
            }

            protected override void OnNotifyMessage(Message m)
            {
                //Filter out the WM_ERASEBKGND message
                if (m.Msg != 0x14)
                {
                    base.OnNotifyMessage(m);
                }

            }


        }
        public struct strPCFileinfo
        {
            public string strName;
            public string strSize;
            public string strOwner;
            public string strTime;
            public string strCount;
            public string strRemark;
            public string strDownloadUrl;
        };
        List<strPCFileinfo> file = new List<strPCFileinfo>();//这个应该可以替换下面的fileinfo数组,本版本未用

        public static object locker = new object();//添加一个对象作为锁

        private delegate void SearchResultCallBack(int index,strPCFileinfo file);
        private SearchResultCallBack searchResultCallBack;
        private void SearchResultMethod(int index,strPCFileinfo file)//往listview控件添加信息
        {
            ListViewItem firstrecord = new ListViewItem(index.ToString());
            firstrecord.SubItems.Add(file.strName);
            firstrecord.SubItems.Add(file.strSize);
            firstrecord.SubItems.Add(file.strOwner);
            firstrecord.SubItems.Add(file.strTime);
            firstrecord.SubItems.Add(file.strCount);
            firstrecord.SubItems.Add(file.strRemark);
            listView1.Items.Add(firstrecord);
        }
        int index;
        strPCFileinfo[] fileinfo;
        string htmlCompare=null;
        int page;
        private bool IsValideMethod(string url)//判断文件是否有效
        {
            // DateTime start = DateTime.Now;
            //url = @"http://pan.baidu.com/share/link?shareid=1407451583&uk=2318901111";
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "GET";
            req.ContentType = "application/x-www-form-urlencoded";
            req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            Stream ReceiveStream = res.GetResponseStream();
            Encoding encode = System.Text.Encoding.UTF8;
            StreamReader sr = new StreamReader(ReceiveStream, encode);

            string strResult = "";
            Char[] read = new Char[512];
            int count = sr.Read(read, 0, 512);
            while (count > 0)
            {
                String str = new String(read, 0, count);
                strResult += str;
                count = sr.Read(read, 0, 256);
            }
            MatchCollection match3 = Regex.Matches(strResult, "<title>百度云 网盘-链接不存在</title>");
            try
            {
                if (match3[0].Success)
                    return false;
                else
                    return true;
            }
            catch { }
            return true;
        }
        private void RegexMatchResult()//核心代码,正则匹配所有符合要求的资源列表
        {
            //}\"[\s]*?(href=\"http://.*\")+?[^(文件名)]*立即关注 (匹配文件夹)
            //string strPat ="}\"[\\s]*?(href=\"http://.*\")+?[\\s\\S]*?文件名:(.+?) 文件大小:(.+?) 分享者:(.+?) 分享时间:(.+?) 下载次数:(.+?)</div>";
            index=page =0;
            fileinfo = new strPCFileinfo[2000];
            string strPat = "}\"[\\s]*?(href=\"http://.*\")+?[^(立即关注)]*?文件名:(.+?) 文件大小:(.+?) 分享者:(.+?) 分享时间:(.+?) 下载次数:(.+?)</div>";
            Regex reg = new Regex(strPat);
            WebClient client = new WebClient();
            client.Proxy = null;
            client.Encoding = Encoding.GetEncoding("utf-8");
            string strUrl = string.Format(@"http://www.baidu.com/s?wd=site%3Apan.baidu.com%20{0}&pn={1}&ie=utf-8", textBox1.Text, page);
            string html = client.DownloadString(strUrl);
            bool IsSame = true;
            //string html = client.DownloadString(@"http://www.baidu.com/s?wd=site%3Apan.baidu.com%20易语言&pn=10&ie=utf-8");
            lock (locker)//锁 
            {
                strPCFileinfo[] fileinfocompare = new strPCFileinfo[10];
                while (IsSame && (page <= 2000))
                {
                    MatchCollection matches = Regex.Matches(html, strPat);
                    //Trace.WriteLine("hello");
                    //fileinfo = new strPCFileinfo[matches.Count];
                    
                    for (int i = 0; i < matches.Count; i++)
                    {
                        if (matches[i].Success)
                        {
                            //Trace.WriteLine(matches[i].Value);
                            int j = index;
                            index++;
                            string strMatch = matches[i].Value;
                            strMatch = strMatch.Replace("<em>", "");
                            strMatch = strMatch.Replace("</em>", "");
                            strMatch = strMatch.Replace("</div>", "");
                            strMatch = strMatch.Replace(" ", "");
                            //strMatch = strMatch.Replace("\n", "");
                            //Regex reg1 = new Regex("[a-zA-z]+://[^\\s]*");
                            MatchCollection match = Regex.Matches(strMatch, "[a-zA-z]+://[^\\s]*");
                            fileinfo[j].strDownloadUrl = match[0].Value.Substring(0, match[0].Value.IndexOf("\""));
                            fileinfo[j].strName = strMatch.Substring(strMatch.IndexOf("文件名") + 4);
                            fileinfo[j].strName = fileinfo[j].strName.Substring(0, fileinfo[j].strName.IndexOf("文件大小"));

                            fileinfo[j].strSize = strMatch.Substring(strMatch.IndexOf("文件大小") + 5);
                            fileinfo[j].strSize = fileinfo[j].strSize.Substring(0, fileinfo[j].strSize.IndexOf("分享者"));

                            fileinfo[j].strOwner = strMatch.Substring(strMatch.IndexOf("分享者") + 4);
                            fileinfo[j].strOwner = fileinfo[j].strOwner.Substring(0, fileinfo[j].strOwner.IndexOf("分享时间"));

                            fileinfo[j].strTime = strMatch.Substring(strMatch.IndexOf("分享时间") + 5);
                            fileinfo[j].strTime = fileinfo[j].strTime.Substring(0, fileinfo[j].strTime.IndexOf("下载次数"));

                            fileinfo[j].strCount = strMatch.Substring(strMatch.IndexOf("下载次数") + 5);
                            MatchCollection match2 = Regex.Matches(fileinfo[j].strCount, "[0-9]+");
                            try
                            {
                                if (match2[0].Success)
                                {
                                    fileinfo[j].strCount = match2[0].Value;
                                }
                                else
                                    fileinfo[j].strCount = "...";
                            }
                            catch (Exception ex)
                            {
                                // MessageBox.Show(ex.Message);
                            }
                            if (fileinfo[j].strSize == "-")
                            {
                                fileinfo[j].strRemark = "文件夹";
                            }
                            else
                            {
                                if(IsValideMethod(fileinfo[j].strDownloadUrl)==false)
                                    fileinfo[j].strRemark = "该资源已失效";
                                else
                                    fileinfo[j].strRemark = " ";
                            }
                            if (fileinfocompare[0].strOwner == fileinfo[j].strOwner && fileinfocompare[0].strTime == fileinfo[j].strTime)
                            {
                                IsSame = false;
                            }
                            else
                            {
                                fileinfocompare[i] = fileinfo[j];
                                //fileinfo[i].strSize = fileinfo[i].strSize.Substring(0, fileinfo[i].strSize.IndexOf("分享者"));
                                listView1.Invoke(searchResultCallBack, index, fileinfo[j]);
                                //Trace.WriteLine(strMatch);
                            }
                        
                        }
                    }
                    htmlCompare = html;
                    page = page + 10;
                    string strUrl1 = string.Format(@"http://www.baidu.com/s?wd=site%3Apan.baidu.com%20{0}&pn={1}&ie=utf-8", textBox1.Text, page);
                    html = client.DownloadString(strUrl1);
                }
            } //锁        
            
        }
        private void button1_Click(object sender, EventArgs e)//单击搜索按钮触发事件
        {
            listView1.Items.Clear();
            //RegexMatch();
            searchResultCallBack = new SearchResultCallBack(SearchResultMethod);
            Thread[] SearchResultThread = new Thread[5];
            for (int p = 0; p < 5; p++)
            {
                SearchResultThread[p] = new Thread(RegexMatchResult);
                SearchResultThread[p].Start();
            }
            //SearchResultThread.Start();
        }
        //右击打开资源地址事件
        [DllImport("shell32.dll")]
        public static extern int ShellExecute(IntPtr hwnd, StringBuilder lpszOp, StringBuilder lpszFile, StringBuilder lpszParams, StringBuilder lpszDir, int FsShowCmd);
        private void 打开资源地址ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedIndices.Count > 0)
            {
                // 转到下载页ToolStripMenuItem.Visible = true;
                int index = listView1.SelectedIndices[0];//选中第index+1行
                ShellExecute(IntPtr.Zero, new StringBuilder("Open"), new StringBuilder(fileinfo[index].strDownloadUrl), new StringBuilder(""), new StringBuilder(""), 1);

            }
        }
    }
}

免费评分

参与人数 2热心值 +2 收起 理由
a804853956 + 1 谢谢@Thanks!
netcat + 1 感谢发布原创作品,吾爱破解论坛因你更精彩.

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

九零-鑫鑫 发表于 2014-2-15 10:48
原来是这样实现的啊 谢谢楼主开源分享
转身 发表于 2014-2-15 10:53
gosun 发表于 2014-2-15 11:14
a804853956 发表于 2014-2-15 11:28
认真学习中。。。。。。厉害楼主
吾爱-路人甲 发表于 2014-2-15 11:33
感谢分享呀。最近也打算学习c#
Perry 发表于 2014-2-15 11:56
感觉C#写的东西太鸡肋,,还不如VC写
lovejgx 发表于 2014-2-15 12:10
看看效果怎么样
xie83544109 发表于 2014-2-15 12:50
{:1_914:}
多谢楼主分享哟
a448629708 发表于 2014-2-15 13:27
顶楼主一个
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-15 03:56

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表