[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using ZzSDK;
using ZzSDK.TaoBao;
namespace BuyerData
{
public partial class FrmMain : Form
{
private AutoResetEvent autoEvent = new AutoResetEvent(false);
private List<string> HashList = new List<string>();
private Thread thMain;
public FrmMain() {
InitializeComponent();
this.dateStart.Value = this.dateEnd.Value.AddMonths(-6);
}
/// <summary>
/// 开始抓取数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonStart_Click(object sender, EventArgs e) {
if (this.txtName.Text.Length <= 0) {
MessageBox.Show("请先添加买家旺旺号,才可以继续", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
} else {
this.lvShow.Items.Clear();
this.tm.Start();
this.thMain = new Thread(delegate(object x) {
this.Invoke(new MethodInvoker(delegate {
this.buttonStart.Enabled = false;
}));
try {
this.Run();
} catch {
} finally {
this.Invoke(new MethodInvoker(delegate {
this.buttonStart.Enabled = true;
}));
}
});
this.thMain.IsBackground = true;
this.thMain.Start();
}
}
private void InsertBuyers(List<TBbuyData> Buyers, string Nick) {
foreach (TBbuyData data in Buyers) {
this.autoEvent.WaitOne();
if (!this.HashList.Contains(data.Link)) {
data.BuyerNick = Nick;
this.addItem(data);
}
}
}
/// <summary>
/// 添加Item的委托事件
/// </summary>
/// <param name="tbUser"></param>
private delegate void d_AddItem(TBbuyData tbUser);
private void addItem(TBbuyData tbUser) {
if (this.lvShow.InvokeRequired) {
this.lvShow.Invoke(new d_AddItem(this.addItem), new object[] { tbUser });
} else {
ListViewItem item = new ListViewItem(new string[5]);
item.SubItems[0].Text = tbUser.BuyerNick;
item.SubItems[1].Text = tbUser.Title;
item.SubItems[2].Text = Convert.ToDateTime(tbUser.Date).ToString("yyyy/MM/dd HH:mm:ss");
item.SubItems[3].Text = tbUser.Price;
item.SubItems[4].Text = tbUser.Link;
this.lvShow.Items.Add(item);
this.HashList.Add(tbUser.ItemId);
}
}
/// <summary>
/// 开始抓取时调用的函数
/// </summary>
private void Run() {
string userid = "";
string str = txtName.Text;
try {
userid = Spider.Get_User_RankId(str);
} catch {
}
if (userid != "") {
int num = 0;
while (num >= 0) {
this.autoEvent.WaitOne();
num++;
try {
List<TBbuyData> buyers = Spider.Get_BuyerData(userid, num.ToString());
if ((buyers == null) || (buyers.Count == 0)) {
break;
}
this.InsertBuyers(buyers, str);
} catch {
}
Random random = new Random();
Thread.Sleep((int)(500 * random.Next(2, 5)));
}
num = 0;
while (num >= 0) {
this.autoEvent.WaitOne();
num++;
try {
List<TBbuyData> list2 = Spider.Get_BuyerData2(userid, num.ToString());
if (list2.Count == 0) {
break;
}
this.InsertBuyers(list2, str);
} catch {
}
Random random2 = new Random();
Thread.Sleep((int)(500 * random2.Next(2, 5)));
}
}
}
/// <summary>
/// 双击ListView打开宝贝地址
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void lvShow_DoubleClick(object sender, EventArgs e) {
if (this.lvShow.SelectedItems.Count > 0) {
Process.Start(this.lvShow.SelectedItems[0].SubItems[4].Text);
}
}
private void tm_Tick(object sender, EventArgs e) {
this.autoEvent.Set();
}
/// <summary>
/// 暂停
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonSleep_Click(object sender, EventArgs e) {
this.tm.Stop();
}
/// <summary>
/// 继续
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonTo_Click(object sender, EventArgs e) {
this.tm.Start();
}
/// <summary>
/// 停止
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonStop_Click(object sender, EventArgs e) {
this.tm.Stop();
this.buttonStart.Enabled = true;
}
/// <summary>
/// 导出
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonOut_Click(object sender, EventArgs e) {
StringBuilder builder = new StringBuilder("所属买家\t购买产品\t购买日期\t购买金额\t连接\n");
for (int i = 0; i < this.lvShow.Items.Count; i++) {
string str = "";
str = ((((str + this.lvShow.Items.SubItems[0].Text + "\t") + this.lvShow.Items.SubItems[1].Text + "\t") + this.lvShow.Items.SubItems[2].Text + "\t") + this.lvShow.Items.SubItems[3].Text + "\t") + this.lvShow.Items.SubItems[4].Text + "\t";
str = str.Substring(0, str.Length - 1) + "\n";
builder.Append(str);
}
if (builder != null) {
SaveFileDialog dialog = new SaveFileDialog {
Filter = "表格CSV文件(*.csv)|*.csv",
FileName = "买家信息" + DateTime.Now.ToString("yy_MM_dd_HH_mm_ss") + ".csv"
};
if (dialog.ShowDialog() == DialogResult.OK) {
File.WriteAllText(dialog.FileName, builder.ToString(), Encoding.Unicode);
MessageBox.Show("导出完毕,路径:\r\n" + dialog.FileName, "导出完毕", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
}
}
/// <summary>
/// 统计金额
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnMoney_Click(object sender, EventArgs e) {
decimal num = new decimal(0);
this.dateStart.Value = DateTime.Parse(this.dateStart.Value.ToString("yyyy-MM-dd"));
this.dateEnd.Value = DateTime.Parse(this.dateEnd.Value.ToString("yyyy-MM-dd"));
foreach (ListViewItem item in this.lvShow.Items) {
try {
DateTime time = DateTime.Parse(Convert.ToDateTime(item.SubItems[2].Text).ToString("yyyy-MM-dd"));
if ((time >= this.dateStart.Value) && (time <= this.dateEnd.Value)) {
num += Convert.ToDecimal(item.SubItems[3].Text);
}
} catch {
}
}
this.lblMoney.Text = string.Format("总金额:{0}元", num.ToString());
}
}
}