//获取随机可发布微博 public static ImageWeibo GetARandomImageWeiboIsNotPublished(WeiboType type, string userName) { ImageWeibo weibo = new ImageWeibo(); SQLiteConnection connection = DataBaseConnection(userName); if (connection.State != System.Data.ConnectionState.Open) { connection.Open(); SQLiteCommand command = new SQLiteCommand(); command.Connection = connection; //获取随机一条未发布的微博 //根据类型获取剩余微博 if (type == WeiboType.ImageWeibo) { command.CommandText = "SELECT rowid,* FROM imageweibos WHERE ispublished = false ORDER BY RANDOM() limit 1"; } else if (type == WeiboType.VideoWeibo) { command.CommandText = "SELECT rowid,* FROM videoweibos WHERE ispublished = false ORDER BY RANDOM() limit 1"; } else { connection.Close(); return(weibo); } command.ExecuteNonQuery(); SQLiteDataReader reader = command.ExecuteReader(); reader.Read(); int rowid = reader.GetInt32(0); weibo.SetPicturesFromStr(reader.GetString(1)); weibo.WeiboMessage = reader.GetString(2); reader.Close(); //将微发布微博标注为发布 command.CommandText = "UPDATE imageweibos SET ispublished = true WHERE rowid = " + rowid; command.ExecuteNonQuery(); connection.Close(); } return(weibo); }
//发布一条图文微博 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)); } }
/// <summary> /// 根据用户url获取图片微博内容 /// </summary> /// <param name="userId"></param> /// <returns></returns> private static List <ImageWeibo> GetImageWeibosFromHtml(string url, CookieContainer cookies) { List <ImageWeibo> imageWeibos = new List <ImageWeibo>(); string request = HttpHelper.Get(url, cookies, false); //匹配微博 string regexString = "(WB_text W_f14)(.|\n)+?&mid="; MatchCollection matches = Regex.Matches(request, regexString); //列表是否读完 if (matches.Count == 0) { WeiboListIsEnd = true; } foreach (Match match in matches) { //防止阻塞 Application.DoEvents(); try { string message = match.Value.Substring(match.Value.IndexOf(" "), match.Value.IndexOf(@" <\/div>\n ") - match.Value.IndexOf(" ")).Trim(); //筛除广告 if (IsMessageHaveAD(message)) { continue; } if (match.Value.IndexOf("pic_ids=") == -1) { continue; } string pics = match.Value.Substring(match.Value.IndexOf("pic_ids=") + 8, match.Value.IndexOf("&mid=") - match.Value.IndexOf("pic_ids=") - 8); string[] pictures; if (!pics.Equals("")) { pictures = pics.Split(','); if (pictures.Length < WeiboPictureMin) { continue; } } else { continue; } ImageWeibo imageWeibo = new ImageWeibo(); imageWeibo.WeiboMessage = message; imageWeibo.Pictures = pictures; imageWeibos.Add(imageWeibo); } catch { continue; } } return(imageWeibos); }