[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);
}
}
}
}