Ejemplo n.º 1
0
        public Form1()
        {
            InitializeComponent();
            //this.ControlBox = false;
            this.MaximizeBox = false;

            cityCrawler          = new SimpleCrawler();
            cityCrawler.OnStart += (s, e) =>
            {
                LOG.WriteLog("爬虫开始抓取地址:" + e.Uri.ToString());
                //this.Invoke(new Action(() =>
                // {
                //     tb_msg.Text += "==============================="+"\r\n";
                //     tb_msg.Text += "爬虫开始抓取地址:" + e.Uri.ToString()+"\r\n";
                // }));
            };

            cityCrawler.OnError += (s, e) =>
            {
                LOG.WriteLog("爬虫抓取出现错误:" + e.Message);
                MessageBox.Show("爬虫抓取出现错误:" + e.Message);
                //this.Invoke(new Action(() =>
                //        {
                //            tb_msg.Text += "===============================" + "\r\n";
                //            tb_msg.Text += "爬虫抓取出现错误:" + e.Message + "\r\n";
                //        }));
            };
            cityCrawler.OnCompleted += (s, e) =>
            {
                var roomtitle = Regex.Matches(e.PageSource, @"(?<=<h2>).*(?=</h2>)"
                                              , RegexOptions.IgnoreCase);
                var ZBName = Regex.Matches(e.PageSource, @"(?<=<h1>).*(?=</h1>)"
                                           , RegexOptions.IgnoreCase);
                var lasttime = Regex.Matches(e.PageSource, @"(?<=<a data-anchor-info=""timetit"">).*(?=</a>)"
                                             , RegexOptions.IgnoreCase);

                this.Invoke(new Action(() =>
                {
                    try
                    {
                        lbl_name.Text = ZBName[0].ToString();
                        lbl_room.Text = roomtitle[0].ToString();
                        lbl_last.Text = lasttime[0].ToString();
                        if (firstbtn)
                        {
                            roomname = lbl_room.Text;
                            firstbtn = false;
                        }
                        LOG.WriteLog("主播:" + lbl_name.Text + "\r\n" + "房间号:" + lbl_room.Text + "\r\n" + "上次直播:" + lbl_last.Text);


                        //tb_msg.Text += "===============================" + "\r\n";
                        //tb_msg.Text += "主播:" + lbl_name.Text + "\r\n" + "房间号:" + lbl_room.Text + "\r\n" + "上次直播:" + lbl_last.Text + "\r\n";
                    }
                    catch (Exception ex)
                    {
                        LOG.WriteLog("房间号不对或者不能爬取,请输入正确的房间号!error:" + ex.Message);
                        MessageBox.Show("房间号不对或者爬取失败,error:" + ex.Message);
                        //      this.Invoke(new Action(() =>
                        //{
                        //    tb_msg.Text += "===============================" + "\r\n";
                        //    tb_msg.Text += "房间号不对,请输入正确的房间号!" + "\r\n";
                        //}));
                        //MessageBox.Show("房间号不对或者不能爬取,请输入正确的房间号!");
                    }
                }));
                LOG.WriteLog("爬虫抓取完毕");
                //             this.Invoke(new Action(() =>
                //{
                //    tb_msg.Text += "===============================" + "\r\n";
                //    tb_msg.Text += "爬虫抓取完毕" + "\r\n";
                //}));
            };
        }
Ejemplo n.º 2
0
        private void button2_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tb_time.Text))
            {
                MessageBox.Show("请输入轮询时间");
                return;
            }
            var time = MessageBox.Show("轮询时间:" + tb_time.Text, "确认", MessageBoxButtons.YesNo);

            if (time == DialogResult.No)
            {
                tb_time.Text = "";
            }
            //发送邮件
            if (!string.IsNullOrEmpty(tb_email.Text) && !string.IsNullOrEmpty(tb_sendEmail.Text) && !string.IsNullOrEmpty(tb_sendpwd.Text))
            {
                if (Regex.Matches(tb_email.Text, @"^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$").Count <= 0)
                {
                    MessageBox.Show("请输入正确的邮箱");
                    return;
                }
                var result = MessageBox.Show("如果房间名发送改变将发送邮件到:" + tb_email.Text, "确认", MessageBoxButtons.YesNo);
                if (result == DialogResult.No)
                {
                    tb_email.Text = "";
                }
                email.reciveemail = tb_email.Text;
                email.sendEmail   = tb_sendEmail.Text;
                email.sendpwd     = tb_sendpwd.Text;

                isSendEmail = true;
                //lbl_isok.Text = "邮件设置成功!";
                tb_msg.Text += "邮件设置成功!/n";
                LOG.WriteLog("邮件设置成功:" + tb_email.Text);
            }
            else
            {
                MessageBox.Show("邮箱设置失败,请检查接收邮箱、发送邮箱、发送密码是否输入正确");
                LOG.WriteLog("邮箱设置失败:" + tb_email.Text);
                return;
            }
            if (t.Enabled == true)
            {
                if (t.Interval != 60000 * int.Parse(tb_time.Text))
                {
                    t.Stop();
                    t.Close();
                    t.Dispose();
                    t.Enabled = false;
                }
            }
            if (!t.Enabled)
            {
                t           = new System.Timers.Timer();
                t.AutoReset = true;

                t.Interval = 60000 * int.Parse(tb_time.Text);
                t.Elapsed += t_Elapsed;
                t.Enabled  = true;
                //lbl_timeok.Text = "轮询设置成功!";
                tb_msg.Text += "轮询设置成功!/n";
                LOG.WriteLog("轮询设置成功!");
                //MessageBox.Show("设置成功");
            }
        }