81 lines
2.6 KiB
C#
81 lines
2.6 KiB
C#
using New_College.Common.Helper;
|
||
using Microsoft.Extensions.Hosting;
|
||
using System;
|
||
using System.Threading;
|
||
using System.Threading.Tasks;
|
||
using New_College.IServices;
|
||
using System.Linq;
|
||
using New_College.Model.Models;
|
||
using System.Collections.Generic;
|
||
|
||
namespace New_College.Tasks
|
||
{
|
||
public class JobTimedSpiderService : IHostedService, IDisposable
|
||
{
|
||
private Timer _timer;
|
||
/// <summary>
|
||
///
|
||
/// </summary>
|
||
private ID_NewsInfoServices newsInfoServices;
|
||
// 这里可以注入
|
||
public JobTimedSpiderService(ID_NewsInfoServices d_NewsInfoServices)
|
||
{
|
||
newsInfoServices = d_NewsInfoServices;
|
||
}
|
||
|
||
public Task StartAsync(CancellationToken cancellationToken)
|
||
{
|
||
Console.WriteLine("Job spider is starting.");
|
||
|
||
_timer = new Timer(DoWork, null, TimeSpan.Zero,
|
||
TimeSpan.FromSeconds(60 * 60 * 8));//两个小时
|
||
return Task.CompletedTask;
|
||
}
|
||
|
||
private void DoWork(object state)
|
||
{
|
||
HtmlAgHelper agHelper = new HtmlAgHelper();
|
||
var list = agHelper.HtmlCreatePageData();
|
||
list.ForEach(async c =>
|
||
{
|
||
var newsinfo = await newsInfoServices.Query(e => e.Title.Trim() == c.title);
|
||
if (!newsinfo.Any() && c.pubtime.Year > DateTime.Now.Year - 1)
|
||
{
|
||
|
||
await newsInfoServices.Add(new Model.Models.D_NewsInfo()
|
||
{
|
||
Author = c.author,
|
||
CategoryId = 1,
|
||
CreateBy = "spdier",
|
||
ProvinceCode = "370000",
|
||
CreateId = 1,
|
||
CreateTime = c.pubtime,
|
||
Detail = c.detail,
|
||
CoverImg = "https://static-data.ycymedu.com/static/newstop.png",
|
||
OrderSort = 0,
|
||
IsDelete = false,
|
||
Title = c.title,
|
||
Summary = HtmlHelper.ReplaceHtmlTag(c.detail).Length > 200 ? HtmlHelper.ReplaceHtmlTag(c.detail).Substring(0, 200) : HtmlHelper.ReplaceHtmlTag(c.detail)
|
||
});
|
||
|
||
}
|
||
});
|
||
ConsoleHelper.WriteWarningLine($"Job spider success: {DateTime.Now}-{list.Count}");
|
||
}
|
||
|
||
public Task StopAsync(CancellationToken cancellationToken)
|
||
{
|
||
Console.WriteLine("Job spider is stopping.");
|
||
|
||
_timer?.Change(Timeout.Infinite, 0);
|
||
|
||
return Task.CompletedTask;
|
||
}
|
||
|
||
public void Dispose()
|
||
{
|
||
_timer?.Dispose();
|
||
}
|
||
}
|
||
}
|