using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Data; using System.Net; using System.Threading.Tasks; using TelegramService.Bl; using TelegramService.Entity; using TelegramService.Help; namespace TelegramServiceV6.Bl { public class CnblogsBl { /// /// 发送 消息 /// private async void SendTextMessage(string title, string author, string authorUrl, string time, string preview, string url) { try { string sHtml = "\r\n主 题:" + title + "\n"; sHtml += "作 者:" + author + "\n"; sHtml += "时 间:" + time + "\n"; sHtml += "预 览:" + preview + "\n"; sHtml += "链 接:" + url + "\n"; JObject oObj = new JObject(); oObj.Add("chat_id", "-1001341062469"); oObj.Add("text", sHtml); oObj.Add("parse_mode", "HTML"); //是否显示url 预览 oObj.Add("disable_web_page_preview", "TRUE"); SendMessageBl.SendWx("https://api.telegram.org/bot1879430057:AAHtwdwHLH0a7cNjTPBQsI92ivoKZI6P-5A/sendMessage", oObj.ToString()); } catch (Exception ex) { EntrustHelper.STCB("show", "[CnblogsBl.SendTextMessage] " + ex.Message); } } /// /// 发送微信 /// /// /// /// /// /// /// private void SendWeixin(string title, string author, string authorUrl, string time, string preview, string url) { string sHtml = "主 题:" + title + "\n"; sHtml += "作 者:" + author + "\n"; sHtml += "时 间:" + time + "\n"; sHtml += "预 览:" + preview + "\n"; sHtml += "链 接:" + url + "\n"; JObject oObj = new JObject(); oObj.Add("text", sHtml); SendMessageBl.SendWx("https://111.230.99.204/weixin/api/send_text", oObj.ToString()); } /// /// 获取博客内容 /// public async Task GetCnblogsHtml() { try { if (DateTime.Now.Hour < 6) { EntrustHelper.STCB("show", "[CnblogsBl.GetCnblogsHtml] 晚上0-6点不执行!"); return; } EntrustHelper.STCB("show", "[CnblogsBl.GetCnblogsHtml] 开始执行!"); string shtml =await GetHtml(); if (shtml.IsNullOrWhiteSpace()) { EntrustHelper.STCB("show", "[CnblogsBl.GetCnblogsHtml] 获取不到数据!"); return; } List listHtmlTag = HtmlTag.FindTagByAttr(shtml, "div", "id", "post_list"); List listObj = null; if (listHtmlTag.Count > 0) { listHtmlTag = listHtmlTag[0].FindTag("article"); } string stitle = ""; string sauthor = ""; string sauthorUrl = ""; string stime = ""; string spreview = ""; string surl = ""; int iCount = listHtmlTag.Count; for (int i = 1; i <= listHtmlTag.Count; i++) { try { listObj = listHtmlTag[iCount - i].FindTagByAttr("a", "class", "post-item-title"); stitle = listObj[0].InnerText; surl = listObj[0].GetAttribute("href"); listObj = listHtmlTag[iCount - i].FindTagByAttr("p", "class", "post-item-summary"); spreview = listObj[0].InnerText.Trim(); listObj = listHtmlTag[iCount - i].FindTagByAttr("a", "class", "post-item-author"); sauthor = listObj[0].InnerText.Trim(); sauthorUrl = listObj[0].GetAttribute("href"); listObj = listHtmlTag[iCount - i].FindTagByAttr("span", "class", "post-meta-item"); stime = listObj[0].FindTag("span")[0].InnerHTML; if (!IsExistUrl(surl)) { //写入数据库 SetCnblogsDB(stitle, sauthor, sauthorUrl, stime, spreview, surl); //发送TG消息 SendTextMessage(stitle, sauthor, sauthorUrl, stime, spreview, surl); //发送微信 SendWeixin(stitle, sauthor, sauthorUrl, stime, spreview, surl); } else { continue; } } catch (Exception ex) { EntrustHelper.STCB("show", "[CnblogsBl.GetCnblogsHtml] " + ex.Message); } } listHtmlTag.Clear(); listObj.Clear(); EntrustHelper.STCB("show", "[CnblogsBl.GetCnblogsHtml] 执行结束!"); } catch (Exception ex) { EntrustHelper.STCB("show", "[CnblogsBl.GetCnblogsHtml] " + ex.Message); } finally { EntrustHelper.STCB("InitTimer", Appsettings.Conf.Interval); } } /// /// 判断是否存在 true=存在 false=不存在 /// /// /// private bool IsExistUrl(string url) { try { int iCount = 0; iCount = Db.dbConnection.Queryable().Where(it => it.url == url).Count(); if (iCount == 1) { return true; } } catch (Exception ex) { EntrustHelper.STCB("show", "[CnblogsBl.IsExistUrl] " + ex.Message); } return false; } /// /// 写入 数据库 /// /// /// /// /// /// /// /// private int SetCnblogsDB(string title, string author, string authorUrl, string time, string preview, string url) { string sSql = ""; int iCount = 0; cnblogsList encnblogsList = new cnblogsList(); encnblogsList.url = url; encnblogsList.title = title; encnblogsList.preview = preview; encnblogsList.author = author; encnblogsList.authorUrl = authorUrl; encnblogsList.time = time; encnblogsList.addTime = DateTime.Now; iCount = Db.dbConnection.Insertable(encnblogsList).ExecuteCommand(); return iCount; } /// /// 获取html /// /// private async Task GetHtml() { try { HttpResult httpResult = null; string html = ""; for (int i = 0; i < 5; i++) { httpResult =await HttpHelper.GetAsync("https://www.cnblogs.com/"); if (httpResult.StatusCode == 200) { if (httpResult.Html.IndexOfStr("id=\"post_list\"")) { return httpResult.Html; } } } } catch (Exception ex) { EntrustHelper.STCB("show", "[CnblogsBl.GetHtml] " + ex.Message); } return null; } } }