添加项目文件。
This commit is contained in:
241
TelegramService.Bl/CnblogsBl.cs
Normal file
241
TelegramService.Bl/CnblogsBl.cs
Normal file
@@ -0,0 +1,241 @@
|
||||
|
||||
|
||||
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
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 发送 消息
|
||||
/// </summary>
|
||||
private async void SendTextMessage(string title, string author, string authorUrl, string time, string preview, string url)
|
||||
{
|
||||
try
|
||||
{
|
||||
string sHtml = "\r\n主 题:<b>" + title + "</b>\n";
|
||||
sHtml += "作 者:<a href=\"" + authorUrl + "\">" + author + "</a>\n";
|
||||
sHtml += "时 间:<b>" + time + "</b>\n";
|
||||
sHtml += "预 览:<b>" + preview + "</b>\n";
|
||||
sHtml += "链 接:<a href=\"" + url + "\">" + url + "</a>\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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 发送微信
|
||||
/// </summary>
|
||||
/// <param name="title"></param>
|
||||
/// <param name="author"></param>
|
||||
/// <param name="authorUrl"></param>
|
||||
/// <param name="time"></param>
|
||||
/// <param name="preview"></param>
|
||||
/// <param name="url"></param>
|
||||
private void SendWeixin(string title, string author, string authorUrl, string time, string preview, string url)
|
||||
{
|
||||
|
||||
string sHtml = "主 题:" + title + "\n";
|
||||
sHtml += "作 者:<a href=\"" + authorUrl + "\">" + author + "</a>\n";
|
||||
sHtml += "时 间:" + time + "\n";
|
||||
sHtml += "预 览:" + preview + "\n";
|
||||
sHtml += "链 接:<a href=\"" + url + "\">" + url + "</a>\n";
|
||||
JObject oObj = new JObject();
|
||||
oObj.Add("text", sHtml);
|
||||
|
||||
SendMessageBl.SendWx("https://111.230.99.204/weixin/api/send_text", oObj.ToString());
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取博客内容
|
||||
/// </summary>
|
||||
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<HtmlTag> listHtmlTag = HtmlTag.FindTagByAttr(shtml, "div", "id", "post_list");
|
||||
|
||||
List<HtmlTag> 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断是否存在 true=存在 false=不存在
|
||||
/// </summary>
|
||||
/// <param name="url"></param>
|
||||
/// <returns></returns>
|
||||
private bool IsExistUrl(string url)
|
||||
{
|
||||
try
|
||||
{
|
||||
int iCount = 0;
|
||||
iCount = Db.dbConnection.Queryable<cnblogsList>().Where(it => it.url == url).Count();
|
||||
if (iCount == 1)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
EntrustHelper.STCB("show", "[CnblogsBl.IsExistUrl] " + ex.Message);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写入 数据库
|
||||
/// </summary>
|
||||
/// <param name="title"></param>
|
||||
/// <param name="author"></param>
|
||||
/// <param name="authorUrl"></param>
|
||||
/// <param name="time"></param>
|
||||
/// <param name="preview"></param>
|
||||
/// <param name="url"></param>
|
||||
/// <returns></returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取html
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private async Task<string> 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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user