175 lines
6.4 KiB
C#
175 lines
6.4 KiB
C#
|
|
using Newtonsoft.Json.Linq;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading.Tasks;
|
|
using System.Web;
|
|
using TelegramService.Bl;
|
|
using TelegramService.Entity;
|
|
using TelegramService.Help;
|
|
|
|
namespace TelegramServiceV6.Bl
|
|
{
|
|
public class TieBaBl
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
/// 发送 消息
|
|
/// </summary>
|
|
private void SendTextMessage(string title, string sType, string stime, string surl)
|
|
{
|
|
try
|
|
{
|
|
|
|
string sHtml = "\r\n标 题:<b>" + title + "</b>\n";
|
|
sHtml += "类 型:<b>" + sType + "</b>\n";
|
|
sHtml += "入库时间:<b>" + stime + "</b>\n";
|
|
sHtml += "链 接:<a href=\"" + surl + "\">" + surl + "</a>\n";
|
|
JObject oObj = new JObject();
|
|
oObj.Add("chat_id", "-1001523898048");
|
|
oObj.Add("text", sHtml);
|
|
oObj.Add("parse_mode", "HTML");
|
|
//是否显示url 预览
|
|
oObj.Add("disable_web_page_preview", "TRUE");
|
|
SendMessageBl.SendTg("https://api.telegram.org/bot1918024418:AAHj_RNtA-XkWbodtzkzaTIxc0XYxBVoQKY/sendMessage", oObj.ToString());
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
EntrustHelper.STCB("show", "[TieBaBl.SendTextMessage] " + ex.Message);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取贴吧
|
|
/// </summary>
|
|
public async void GetTieBaHtml()
|
|
{
|
|
try
|
|
{
|
|
|
|
EntrustHelper.STCB("show", "[TieBaBl.GetTieBaHtml1] 开始执行!");
|
|
|
|
HttpResult httpResult = null;
|
|
|
|
foreach (var tk in Appsettings.Conf.TieBa)
|
|
{
|
|
string key = tk.ToString();
|
|
string encodedUrl = HttpUtility.UrlEncode(key);
|
|
string shtml = null;
|
|
string tiebaUrl = "https://tieba.baidu.com/mo/m?kw=" + encodedUrl.ToUpper() + "&lm=&pn=0";
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
{
|
|
httpResult =await HttpHelper.GetAsync(tiebaUrl);
|
|
if (httpResult.StatusCode == 200)
|
|
{
|
|
shtml = httpResult.Html;
|
|
break;
|
|
}
|
|
}
|
|
if (shtml.IsNullOrWhiteSpace())
|
|
{
|
|
EntrustHelper.STCB("show", "[TieBaBl.GetTieBaHtml2] 获取不到数据!");
|
|
return;
|
|
}
|
|
List<HtmlTag> listHtmlTag = HtmlTag.FindTagByAttr(shtml, "ul", "id", "frslistcontent");
|
|
List<HtmlTag> lista = null;
|
|
if (listHtmlTag.Count == 0)
|
|
{
|
|
EntrustHelper.STCB("show", "[TieBaBl.GetTieBaHtml3] 贴吧html匹配不到div!");
|
|
return;
|
|
}
|
|
listHtmlTag = listHtmlTag[0].FindTagByAttr("li", "class", "tl_shadow tl_shadow_new ");
|
|
for (int i = 0; i < listHtmlTag.Count; i++)
|
|
{
|
|
try
|
|
{
|
|
lista = listHtmlTag[i].FindTag("a");
|
|
if (lista.Count == 0)
|
|
{
|
|
continue;
|
|
}
|
|
string url = lista[0].GetAttribute("href");
|
|
string id = Regex.Match(url, @"(?is)/p/(?<value>.*?)s*\?").Groups["value"].Value;
|
|
if (id.IsNull())
|
|
{
|
|
continue;
|
|
}
|
|
lista = lista[0].FindTagByAttr("div", "class", "ti_title");
|
|
if (lista.Count <= 0)
|
|
{
|
|
continue;
|
|
}
|
|
string title = lista[0].InnerText;
|
|
if (title.Contains(" "))
|
|
{
|
|
string[] arr = title.SplitRemoveEmptyEntries(" ");
|
|
if (arr.Length > 1)
|
|
{
|
|
title = arr[1];
|
|
}
|
|
}
|
|
tieba tb = new tieba();
|
|
tb.id = id;
|
|
tb.title = title;
|
|
if (tb.title.Length > 149)
|
|
{
|
|
tb.title = tb.title.Substring(0, 149);
|
|
}
|
|
tb.key = key;
|
|
tb.url = "tieba.baidu.com/p/" + tb.id;
|
|
tb.sysaddtime = DateTime.Now;
|
|
if (SetTieBaDB(tb) > 0)
|
|
{
|
|
SendTextMessage(tb.title, tb.key, tb.sysaddtime.ToDateStr(), "https://" + tb.url);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
EntrustHelper.STCB("show", "[TieBaBl.GetTieBaHtml4] " + ex.Message);
|
|
}
|
|
}
|
|
listHtmlTag.Clear();
|
|
lista.Clear();
|
|
Thread.Sleep(5000);
|
|
}
|
|
EntrustHelper.STCB("show", "[TieBaBl.GetTieBaHtml5] 执行结束!");
|
|
}
|
|
catch (Exception)
|
|
{
|
|
}
|
|
finally
|
|
{
|
|
EntrustHelper.STCB("InitTimerTieBa", Appsettings.Conf.IntervalTieBa);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 写入 数据库
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
private int SetTieBaDB(tieba tb)
|
|
{
|
|
try
|
|
{
|
|
var x = Db.dbConnection.Storageable(tb).ToStorage();
|
|
x.AsInsertable.ExecuteCommand();//不存在插入
|
|
return x.InsertList.Count;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
EntrustHelper.STCB("show", "[TieBaBl.SetTieBaDB] " + ex.Message);
|
|
}
|
|
return 0;
|
|
}
|
|
}
|
|
}
|