/// <summary> /// 插入被爬取用户的信息 /// </summary> /// <param name="uid">个性域名或id,爬取微博用</param> /// <param name="oid">真实id,获取粉丝用</param> public static void InsertUidAndOid(string uid, string oid, string userName) { try { SQLiteConnection connection = DataBaseConnection(userName); if (connection.State != System.Data.ConnectionState.Open) { connection.Open(); SQLiteCommand command = new SQLiteCommand(); command.Connection = connection; //判断是否已存在 command.CommandText = String.Format("SELECT COUNT(*) FROM users WHERE uid = '{0}'", uid); command.ExecuteNonQuery(); SQLiteDataReader reader = command.ExecuteReader(); reader.Read(); int count = reader.GetInt32(0); reader.Close(); if (count == 0) { command.CommandText = String.Format("INSERT INTO users VALUES('{0}','{1}',false)", uid, oid); command.ExecuteNonQuery(); } connection.Close(); } } catch (Exception ex) { UserLog.WriteNormalLog(userName, "插入oid失败", oid + "\n" + ex.Message); } }
/// <summary> ///判断微博文字中是否含有广告 /// </summary> /// <param name="message"></param> /// <returns></returns> private static bool IsMessageHaveAD(string message) { bool isHave = false; try { if (!filterADFilePath.Equals("")) { using (StreamReader sr = new StreamReader(filterADFilePath, Encoding.Default)) { string line = sr.ReadToEnd(); string[] keyWords = line.Split('%'); foreach (string keyWord in keyWords) { if (message.IndexOf(keyWord) > -1) { isHave = true; break; } } sr.Close(); } } } catch (Exception ex) { UserLog.WriteNormalLog("广告过滤文件无法正常读取", ex.Message); } return(isHave); }
/// <summary> /// 发布一条图文微博 /// </summary> /// <param name="cookies"></param> /// <param name="message"></param> /// <param name="pics"></param> public static void SendAnImageWeibo(CookieContainer cookies, string message, string[] pics) { if (message.Equals("") || message.Equals("转发微博")) { message = "分享图片"; } if (IsMessageHaveAD(message)) { UserLog.WriteNormalLog("发布失败,含广告", "微博内容:" + message); } string picsString = ""; if (pics.Length != 0) { picsString += pics[0]; for (int i = 1; i < (pics.Length < 9 ? pics.Length : 9); i++) { picsString += ("%7C" + pics[i]); } } string data; if (pics.Length == 1) { data = "location=v6_content_home&text=" + message + "&appkey=&style_type=1&pic_id=" + picsString + "&tid=&pdetail=&mid=&isReEdit=false&rank=0&rankid=&module=stissue&pub_source=main_&pub_type=dialog&isPri=0&_t=0"; } else { data = "location=v6_content_home&text=" + message + "&appkey=&style_type=1&pic_id=" + picsString + "&tid=&pdetail=&mid=&isReEdit=false&gif_ids=&rank=0&rankid=&module=stissue&pub_source=main_&updata_img_num=" + pics.Length + "&pub_type=dialog&isPri=null&_t=0"; } string url = @"https://weibo.com/aj/mblog/add?ajwvr=6&__rnd=" + GetTimeStamp(); string s = HttpHelper.SendDataByPost(url, cookies, data); }
//插入图片微博 public static void InsertImageWebos(List <ImageWeibo> imageWeibos, string userName) { SQLiteConnection connection = DataBaseConnection(userName); if (connection.State != System.Data.ConnectionState.Open) { connection.Open(); SQLiteCommand command = new SQLiteCommand(); command.Connection = connection; SQLiteDataReader reader = null; foreach (ImageWeibo weibo in imageWeibos) { try { string weiboText = weibo.WeiboMessage; string[] pics = weibo.Pictures; string picsString = ""; if (pics.Length != 0) { picsString += pics[0]; for (int i = 1; i < (pics.Length < 9 ? pics.Length : 9); i++) { //字符串间以“#”间隔 picsString += ("#" + pics[i]); } } command.CommandText = String.Format("SELECT COUNT(*) FROM imageweibos WHERE imageids = '{0}'", picsString); command.ExecuteNonQuery(); reader = command.ExecuteReader(); reader.Read(); int count = reader.GetInt32(0); reader.Close(); if (count == 0) { command.CommandText = String.Format("INSERT INTO imageweibos VALUES('{0}','{1}',false)", picsString, weiboText); command.ExecuteNonQuery(); } else { command.CommandText = String.Format("SELECT rowid,* From imageweibos WHERE imageids = '{0}'", picsString); command.ExecuteNonQuery(); reader = command.ExecuteReader(); reader.Read(); int rowid = reader.GetInt32(0); string dbText = reader.GetString(2); reader.Close(); if (dbText.Length < weiboText.Length) { command.CommandText = String.Format("UPDATE imageweibos SET weibotext = '{0}' WHERE rowid = {1}", weiboText, rowid.ToString()); command.ExecuteNonQuery(); } } } catch (Exception ex) { UserLog.WriteNormalLog(userName, "插入图片微博失败", ex.Message); } } connection.Close(); } }