/// <summary> /// 构造数据源 /// </summary> public AcfunDataSource() { for (int count = 1; count < 11; count++) { //页码 dataCore.CreateGroup(" 第-" + count + "-页 ", "Group Title: " + count, "Group Subtitle: " + count, typeof(MainForm).Namespace + ".Assets.DarkGray.png", "哈哈哈哈哈哈哈哈哈哈"); //数据库分页查10个 string sql = ""; dt = conn.ExecuteDataTable(sql); //循环写入 for (int dtcount = 0; dtcount < dt.Rows.Count; dtcount++) { Article.Model.Article article = new Model.Article(); article.setComments(dt.Rows[dtcount]["comments"].ToString()); article.setTitle(dt.Rows[dtcount]["title"].ToString()); article.setContentId(dt.Rows[dtcount]["contentId"].ToString()); article.setDescription(dt.Rows[dtcount]["description"].ToString()); article.setTxt(dt.Rows[dtcount]["txt"].ToString()); dataCore.AddItem(new ArticleDataItem(article.getTitle(), " 评论数: " + article.getComments(), typeof(MainForm).Namespace + ".Assets.LightGray.png", "Description: " + article.getDescription(), article.getTxt(), " 第-" + count + "-页 ", article.getComments(), article.getContentId())); } } }
/// <summary> /// 根据json保存文章全文并下载图片 /// </summary> /// <param name="json"></param> /// <returns></returns> public Article.Model.Article getModelFromJson(string json) { if ((!json.Contains("该稿件未审核")) || json == "") { JObject jsonObj = JObject.Parse(json); JObject data = ((JObject)jsonObj["data"]); JObject tempo = ((JObject)data["fullArticle"]); Article.Model.Article article = new Article.Model.Article(); article.setChannelId(tempo["channelId"].ToString()); article.setComments(tempo["comments"].ToString()); article.setContentId(tempo["contentId"].ToString()); article.setCover(tempo["cover"].ToString()); article.setDescription(ReplaceHtmlTag(tempo["description"].ToString())); article.setIsArticle(tempo["isArticle"].ToString()); article.setIsRecommend(tempo["isRecommend"].ToString()); article.setReleaseDate(tempo["releaseDate"].ToString()); article.setStows(tempo["stows"].ToString()); article.setTitle(tempo["title"].ToString()); article.setToplevel(tempo["toplevel"].ToString()); article.setTxt(ReplaceHtmlTag(tempo["txt"].ToString())); JObject user = ((JObject)tempo["user"]); article.setUser(user["userId"].ToString()); article.setViewOnly(tempo["viewOnly"].ToString()); article.setViews(tempo["views"].ToString()); SavePostImg(GetHtmlImageUrlList(tempo["txt"].ToString()), article.getContentId()); getUserByArticle(json); return(article); } else { return(null); } }
/// <summary> /// 保存文件和数据 /// </summary> /// <param name="count"></param> public void GetData(int count) { List <Article.Model.Article> articleList = oper.getArticleList(count); foreach (Article.Model.Article article in articleList) { Article.Model.Article fullarticle = new Model.Article(); fullarticle = oper.GetArticleFromWeb(article.getContentId()); oper.SaveArticle(fullarticle); } }
/// <summary> /// 保存文章到数据库 /// </summary> /// <param name="article"></param> /// <returns></returns> public bool SaveArticle(Article.Model.Article article) { string sql = "select * from Article where contentId =" + article.getContentId(); dt = conn.ExecuteDataTable(sql); if (dt.Rows.Count == 0) { string sqlInsert = "INSERT INTO 'Article' ('user', 'txt', 'description', 'contentId', 'isArticle', 'channelId', 'releaseDate', 'title', 'isRecommend', 'views', 'comments', 'stows', 'toplevel', 'cover', 'viewOnly') VALUES ('" + article.getUser() + "', '" + article.getTxt() + "', '" + article.getDescription() + "', '" + article.getContentId() + "', '" + article.getIsArticle() + "', '" + article.getChannelId() + "', '" + article.getReleaseDate() + "', '" + article.getTitle() + "', '" + article.getIsRecommend() + "', '" + article.getViews() + "', '" + article.getComments() + "', '" + article .getStows() + "', '" + article.getToplevel() + "', '" + article.getCover() + "', '" + article.getViewOnly() + "');"; conn.ExecuteScalar(sqlInsert); } return(true); }
/// <summary> /// 根据页码获取文章列表 /// </summary> /// <param name="pageNo"></param> /// <returns></returns> public List <Article.Model.Article> getArticleList(int pageNo) { List <Article.Model.Article> articleList = new List <Model.Article>(); WebUtil util = new WebUtil(); //获取该页中所有的文章编号 string articleNoList = util.GetArticleList(20, pageNo); if (articleNoList != "") { JObject jsonObj = JObject.Parse(articleNoList); string list = ((JObject)((JObject)jsonObj["data"])["page"])["list"].ToString(); JArray alist = JArray.Parse(list); for (int i = 0; i < alist.Count; ++i) //遍历JArray { Article.Model.Article article = new Article.Model.Article(); JObject tempo = JObject.Parse(alist[i].ToString()); article.setChannelId(tempo["channelId"].ToString()); article.setComments(tempo["comments"].ToString()); article.setContentId(tempo["contentId"].ToString()); article.setCover(tempo["cover"].ToString()); article.setDescription(tempo["description"].ToString()); article.setIsArticle(tempo["isArticle"].ToString()); article.setIsRecommend(tempo["isRecommend"].ToString()); article.setReleaseDate(tempo["releaseDate"].ToString()); article.setStows(tempo["stows"].ToString()); article.setTitle(tempo["title"].ToString()); article.setToplevel(tempo["toplevel"].ToString()); article.setTxt(tempo["description"].ToString()); article.setUser(tempo["user"].ToString()); article.setViewOnly(tempo["viewOnly"].ToString()); article.setViews(tempo["views"].ToString()); getCommentByArticle(article); articleList.Add(article); } } return(articleList); }
/// <summary> /// 获取并保存所有评论内容 /// </summary> /// <param name="article"></param> /// <returns></returns> public List <Comment> getCommentByArticle(Article.Model.Article article) { List <Comment> commentList = new List <Comment>(); int commentCount = 0; int.TryParse(article.getComments(), out commentCount); if (commentCount != 0 && commentCount < 50) { string commentJson = webutil.GetCommentList(article.getContentId(), 1, 100); if (commentJson.Length < 250) { return(null); } commentList = getCommentFromJson(commentJson, article.getContentId()); } else if (commentCount == 0) { return(null); } else { for (int count = 1; count <= commentCount % 50; count++) { List <Comment> tempList = null; string commentJson = webutil.GetCommentList(article.getContentId(), count, 100); if (commentJson.Length < 250) { return(null); } tempList = getCommentFromJson(commentJson, article.getContentId()); tempList.ForEach(i => commentList.Add(i)); } } return(commentList); }