public void SaveRankModel(Dictionary <string, RankModel> dict) { System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); LiteDB.LiteDatabase liteDatabase = new LiteDB.LiteDatabase(this.DbLite); LiteDB.LiteCollection <RankModel> liteCollection = liteDatabase.GetCollection <RankModel>("博客园排行"); int count = 0; foreach (KeyValuePair <string, RankModel> itemRank in dict) { count++; LiteDB.BsonValue bsonValue = new LiteDB.BsonValue(itemRank.Value.Url); RankModel model = liteCollection.FindById(bsonValue); if (model == null) { bsonValue = liteCollection.Insert(itemRank.Value); } else { } } liteDatabase.Dispose(); stopwatch.Stop(); Console.WriteLine(stopwatch.Elapsed); }
private void LstRank_SelectedIndexChanged(object sender, EventArgs e) { RankModel rankModel = this.lstRank.SelectedItem as RankModel; if (rankModel != null) { this.txtName.Text = rankModel.Name; this.txtScore.Text = rankModel.Score; this.llblUrl.Text = rankModel.Url; this.txtDate.Text = rankModel.LastDateTime.ToLongDateString(); } }
private void BtnLook_Click(object sender, EventArgs e) { RankModel rankModel = this.lstRank.SelectedItem as RankModel; if (rankModel != null) { FrmBlog frmBlog = new FrmBlog(); frmBlog.RankModel = rankModel; frmBlog.CBRank = this.CBRank; if (frmBlog.ShowDialog(this) == DialogResult.OK) { } } }
public async Task <Dictionary <string, RankModel> > Start() { Dictionary <string, RankModel> dict = new Dictionary <string, RankModel>(); string result = await this.HttpClient.GetStringAsync(this.Url); HtmlDocument html = new HtmlDocument(); html.LoadHtml(result); HtmlNode table = html.DocumentNode.SelectSingleNode(".//table[@width='90%']"); HtmlNodeCollection tds = table.SelectNodes(".//td"); foreach (HtmlNode td in tds) { HtmlNode a = td.SelectSingleNode(".//a[not(@class)]"); if (a == null) { continue; } RankModel rankModel = new RankModel(); rankModel.Id = System.Web.HttpUtility.HtmlDecode(td.FirstChild.InnerText); rankModel.Url = a.GetAttributeValue("href", string.Empty); rankModel.Name = a.InnerText; string small = td.LastChild.InnerText; if (!string.IsNullOrEmpty(small)) { small = small.Replace("(", "").Replace(")", "").Replace(" ", ""); string[] splits = small.Split(','); if (splits.Length == 3) { rankModel.Value = splits[0]; rankModel.LastDateTime = DateTime.Parse(splits[1]); rankModel.Score = splits[2]; } } if (!dict.ContainsKey(rankModel.Name)) { dict.Add(rankModel.Name, rankModel); } else { Console.WriteLine(rankModel.Name); } } return(dict); }