/// <summary> /// 查询数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void QueryData_Click(object sender, RoutedEventArgs e) { List <StudentScore> result = new List <StudentScore>(); var queryDataStartTime = DateTime.Parse(queryDataStartTimeDatePicker.Text); var queryDataEndTime = DateTime.Parse(queryDataEndTimeDatePicker.Text); //分库分表查询,再合并 //这里没有考虑时分秒维度的数据查询 var queryDate = new DateTime(queryDataStartTime.Year, queryDataStartTime.Month, queryDataStartTime.Day); while (queryDate <= queryDataEndTime) { var suffix = GetSuffix(queryDate); var tempTableName = $"{tableName}_{suffix}"; sqliteTool = new SqliteTool(); var dbName = $"HistoricalDataManageDb_{queryDate.ToString("yyyy")}"; //不必每次都重新连接,可以优化 sqliteTool.ConnectToDatabase(filePath, dbName); //查询数据表是否存在 var tableCount = sqliteTool.ExecuteQueryCount($"SELECT name FROM sqlite_master WHERE type='table' AND name='{tempTableName}';"); if (tableCount > 0) { //该用SqlParameter请不要吝惜,使用sql时需要时刻考虑sql注入的危险 ————我只是懒 var scores = sqliteTool.Query <StudentScore>($"select Name,Score,CreateTime from {tempTableName} order by Name asc"); result.AddRange(scores); } queryDate = queryDate.AddDays(1); } DataContext = result; }
//粉丝事件 private void FollowFansEvent(object sender, bool isFollow) { if (isFollow) { int countFans = ((UserLable)sender).AutoFollowCount; //关注count个用户 string oid = SqliteTool.GetRandomOid(((UserLable)sender).DisplayName); if (!oid.Equals("")) { int count = WeiboOperateTool.FollowUsersFans(oid, countFans, ((UserLable)sender).Cookies); if (count < countFans) { count += WeiboOperateTool.FollowUsersFans(SqliteTool.GetRandomOid(((UserLable)sender).DisplayName), countFans - count, ((UserLable)sender).Cookies); } UserLog.WriteNormalLog(((UserLable)sender).DisplayName, String.Format("关注{0}人", count), String.Format("被抓取oid:{0}", oid)); } } else { int count = ((UserLable)sender).AutoUnFollowCount; WeiboOperateTool.UnFollowMyFans(count, ((UserLable)sender).Cookies, ((UserLable)sender).UserId); UserLog.WriteNormalLog(((UserLable)sender).DisplayName, String.Format("取消关注{0}人", count)); } }
private void buttonStartGetWeibo_Click(object sender, EventArgs e) { //判断有效条件 if ((!this.checkBoxGetImageWeibo.Checked && !this.checkBoxGetVideoWeibo.Checked) || this.textBoxGetWeibo.Text.Equals("") || this.checkedListBox1.CheckedItems.Count == 0) { return; } //关闭操作功能 this.checkBoxGetImageWeibo.Enabled = false; this.checkBoxGetVideoWeibo.Enabled = false; this.textBoxGetWeibo.Enabled = false; this.buttonStartGetWeibo.Enabled = false; //获取图文微博 if (this.checkBoxGetImageWeibo.Checked) { //获取微博 CookieContainer cookies = (this.panel1.Controls[0] as UserLable).Cookies; List <ImageWeibo> imageWeibos = WeiboOperateTool.GetImageWeibos(this.textBoxGetWeibo.Text, out string oid, cookies); for (int i = 0; i < this.checkedListBox1.Items.Count; i++) { if (this.checkedListBox1.GetItemChecked(i)) { string nickName = this.checkedListBox1.Items[i].ToString(); int StartCount = SqliteTool.GetLaveWeiboCount(SqliteTool.WeiboType.ImageWeibo, nickName); SqliteTool.InsertImageWebos(imageWeibos, nickName); int endCount = SqliteTool.GetLaveWeiboCount(SqliteTool.WeiboType.ImageWeibo, nickName); UserLog.WriteNormalLog(nickName, String.Format("爬取图文微博{0}条", endCount - StartCount), String.Format("被爬取用户ID:{0}", this.textBoxGetWeibo.Text)); string requestStr = DateTime.Now.ToString("yyyy-MM-dd HH:MM:ss") + "\n[" + nickName + "]获取结束:此次共取得图文微博" + (endCount - StartCount).ToString() + "条"; this.richTextBox1.Text = this.richTextBox1.Text + requestStr + "\n"; //uid可以正常获取数据时说明有效,存入数据库 if (endCount - StartCount > 0) { SqliteTool.InsertUidAndOid(this.textBoxGetWeibo.Text, oid, nickName); } } } } //获取视频微博 if (this.checkBoxGetImageWeibo.Checked) { } //重启界面操作 this.checkBoxGetImageWeibo.Enabled = true; this.checkBoxGetVideoWeibo.Enabled = true; this.textBoxGetWeibo.Enabled = true; this.buttonStartGetWeibo.Enabled = true; }
//发布微博事件 private void PublishWeiboEvent(object sender, bool isImageWeibo) { if (isImageWeibo) { this.PublishAnImageWeibo((UserLable)sender); } else { this.PublishAVideoWeibo((UserLable)sender); } //更新剩余微博数 ((UserLable)sender).ImageWeiboCount = SqliteTool.GetLaveWeiboCount(SqliteTool.WeiboType.ImageWeibo, ((UserLable)sender).DisplayName).ToString(); ((UserLable)sender).VideoWeiboCount = SqliteTool.GetLaveWeiboCount(SqliteTool.WeiboType.VideoWeibo, ((UserLable)sender).DisplayName).ToString(); }
/// <summary> /// 录入数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void InsertData_Click(object sender, RoutedEventArgs e) { sqliteTool = new SqliteTool(); var insertDataTime = DateTime.Parse(insertDataTimeDatePicker.Text); var insertDataCount = int.Parse(insertDataCountTextBox.Text); //按年份分库 var dbName = $"HistoricalDataManageDb_{insertDataTime.ToString("yyyy")}"; sqliteTool.CreateNewDatabase(filePath, dbName); sqliteTool.ConnectToDatabase(filePath, dbName); //按天分表 var suffix = GetSuffix(insertDataTime); CreateTable(suffix); InsertData(insertDataCount, suffix, insertDataTime); }
//更新数据库事件 private void UpdateSQLiteEvent(object sender) { List <string> uids = SqliteTool.Get10Uid(((UserLable)sender).DisplayName); foreach (string uid in uids) { int StartCount = SqliteTool.GetLaveWeiboCount(SqliteTool.WeiboType.ImageWeibo, ((UserLable)sender).DisplayName); List <ImageWeibo> imageWeibos = WeiboOperateTool.GetImageWeibos(uid, out string oid, ((UserLable)sender).Cookies); SqliteTool.InsertImageWebos(imageWeibos, ((UserLable)sender).DisplayName); int endCount = SqliteTool.GetLaveWeiboCount(SqliteTool.WeiboType.ImageWeibo, ((UserLable)sender).DisplayName); UserLog.WriteNormalLog(((UserLable)sender).DisplayName, String.Format("后台爬取图文微博{0}条", endCount - StartCount), String.Format("被爬取用户ID:{0}", uid)); } //更新微博显示 ((UserLable)sender).ImageWeiboCount = SqliteTool.GetLaveWeiboCount(SqliteTool.WeiboType.ImageWeibo, ((UserLable)sender).DisplayName).ToString(); ((UserLable)sender).VideoWeiboCount = SqliteTool.GetLaveWeiboCount(SqliteTool.WeiboType.VideoWeibo, ((UserLable)sender).DisplayName).ToString(); }
//登录账号 private void 登录账号ToolStripMenuItem_Click(object sender, EventArgs e) { LoginView login = new LoginView(); login.ShowDialog(); if (login.IsLocked) { MessageBox.Show("账号异常,请登陆网页微博解除锁定", "提示"); return; } if (login.DialogResult == DialogResult.OK) { UserLable userLable = new UserLable(login.WBLogin.MyCookies, login.WBLogin.Username, login.WBLogin.Password, login.WBLogin.DisplayName, login.WBLogin.UserId); if (this.panel1.Controls.Count == 0) { userLable.Location = new Point(4, 4); } else { userLable.Location = new Point(4, this.panel1.Controls[this.panel1.Controls.Count - 1].Location.Y + 190 + 6); } userLable.PublishWeiboEvent += PublishWeiboEvent; userLable.UpdateCookiesEvent += UpdateCookiesEvent; userLable.SendEmailEvent += SendEmailEvent; userLable.WriteLogEvent += WriteLogEvent; userLable.UpdateSQLiteEvent += UpdateSQLiteEvent; userLable.FollowFansEvent += FollowFansEvent; //设置头像 userLable.AvatarImage = login.WBLogin.GetAvatarImage(); this.panel1.Controls.Add(userLable); SqliteTool.CreateDataBase(userLable.DisplayName); //更新微博剩余显示 userLable.ImageWeiboCount = SqliteTool.GetLaveWeiboCount(SqliteTool.WeiboType.ImageWeibo, login.WBLogin.DisplayName).ToString(); userLable.VideoWeiboCount = SqliteTool.GetLaveWeiboCount(SqliteTool.WeiboType.VideoWeibo, login.WBLogin.DisplayName).ToString(); this.checkedListBox1.Items.Add(login.WBLogin.DisplayName); } }
//发布一条图文微博 private void PublishAnImageWeibo(UserLable userLable) { if (SqliteTool.GetLaveWeiboCount(SqliteTool.WeiboType.ImageWeibo, userLable.DisplayName) != 0) { ImageWeibo weibo = SqliteTool.GetARandomImageWeiboIsNotPublished(SqliteTool.WeiboType.ImageWeibo, userLable.DisplayName); if (weibo.Pictures == null || weibo.Pictures.Length == 0) { UserLog.WriteNormalLog(userLable.DisplayName, "获取微博失败,类型不明确"); return; } //设置tags string weiboText = userLable.IsFrontTagsSet ? userLable.Tags + weibo.WeiboMessage : weibo.WeiboMessage + userLable.Tags; WeiboOperateTool.SendAnImageWeibo(userLable.Cookies, weiboText, weibo.Pictures); } else { this.richTextBox1.Text = this.richTextBox1.Text + userLable.DisplayName + " 微博库已空\n"; EMailTool.SendMail("微博库已空", String.Format("用户昵称:{0}<br/>登录账号:{1}", userLable.DisplayName, userLable.UserName)); } }