public static bool TryToInsertOrUpdateABson(BsonDocument bsomDocument, string collectionName, BsonDocument condition) { var mongodbBson = MongodbGetter.GetACollection(collectionName); if (mongodbBson == null) { MongodbSaver.InsertOne(bsomDocument, collectionName); return(true); } else if (!CheckBsonIsEqual(bsomDocument, mongodbBson)) { MongodbUpdater.UpdateACollection(bsomDocument, collectionName, condition); return(true); } else { return(false); } }
private static void GetMainPageInformation(BufferBlock <string> imageTargetBlock) { var doc = WebpageHelper.GetHttpRequestDocument(PROJECT_MAIN_PAGE); var nodes = doc.DocumentNode.SelectNodes("//div[@class='x-wrap']/div[@class='title']/div"); if (nodes != null) { var bson = new BsonDocument(); foreach (var node in nodes) { switch (node.Attributes["class"].Value) { case "h30": bson.Add("title", node.InnerText); break; case "p": StringBuilder sb = new StringBuilder(); foreach (var contentNode in node.ChildNodes) { sb.Append(contentNode.InnerText); } bson.Add("content", sb.ToString()); break; } } //非遗页面的数字 nodes = doc.DocumentNode.SelectNodes("//div/div[@class='num-item']"); if (nodes != null) { var bsonArray = new BsonArray(); foreach (var node in nodes) { var numBson = new BsonDocument(); foreach (var childNode in node.ChildNodes) { switch (childNode.Attributes["class"]?.Value) { case "b": numBson.Add("num", childNode.Attributes["data-rn"].Value); break; case "h18": numBson.Add("desc", childNode.InnerText); break; } } bsonArray.Add(numBson); } bson.Add("numItem", bsonArray); } //获取首页的非物质文化遗产地图 GetHeritageMapTableInformation(bson); var mongodbBson = MongodbGetter.GetHeritageProjectMainPageDesc(); if (mongodbBson == null) { Console.WriteLine("Insert Heritage Project Content"); MongodbSaver.SaveHeritageProjectMainContent(bson); } else if (!CheckBsonIsEqual(bson, mongodbBson)) { Console.WriteLine("Update Heritage Project Content"); MongodbUpdater.UpdateHeritageProjectMainContent(bson); } else { Console.WriteLine("Not Insert Heritage Project Content"); } Console.WriteLine(bson); } }