-
Notifications
You must be signed in to change notification settings - Fork 1k
/
DatabaseSpider.cs
83 lines (77 loc) · 2.8 KB
/
DatabaseSpider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System.Threading;
using System.Threading.Tasks;
using Dapper;
using DotnetSpider.DataFlow;
using DotnetSpider.Http;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using MySqlConnector;
using Serilog;
namespace DotnetSpider.Sample.samples;
public class DatabaseSpider(
IOptions<SpiderOptions> options,
DependenceServices services,
ILogger<Spider> logger)
: CnBlogsSpider(options, services, logger)
{
public static new async Task RunAsync()
{
var builder = Builder.CreateDefaultBuilder<DatabaseSpider>();
builder.UseSerilog();
await builder.Build().RunAsync();
}
protected override async Task InitializeAsync(CancellationToken stoppingToken = default)
{
AddDataFlow<ListNewsParser>();
AddDataFlow<NewsParser>();
AddDataFlow<MyStorage>();
await AddRequestsAsync(new Request("https://news.cnblogs.com/n/page/1/"));
}
class MyStorage : DataFlowBase
{
public override async Task InitializeAsync()
{
await using var conn =
new MySqlConnection(
"Database='mysql';Data Source=localhost;password=1qazZAQ!;User ID=root;Port=3306;");
await conn.ExecuteAsync("create database if not exists cnblogs2;");
await conn.ExecuteAsync(
$"""
create table if not exists cnblogs2.news2
(
id int auto_increment
primary key,
title varchar(500) not null,
url varchar(500) not null,
summary varchar(1000) null,
views int null,
content varchar(2000) null
);
"""
);
}
public override async Task HandleAsync(DataFlowContext context, ResponseDelegate next)
{
if (IsNullOrEmpty(context))
{
Logger.LogWarning("数据流上下文不包含解析结果");
}
else
{
var typeName = typeof(News).FullName;
var data = (News)context.GetData(typeName);
if (data != null)
{
await using var conn =
new MySqlConnection(
"Database='mysql';Data Source=localhost;password=1qazZAQ!;User ID=root;Port=3306;");
await conn.ExecuteAsync(
$"INSERT IGNORE INTO cnblogs2.news2 (title, url, summary, views, content) VALUES (@Title, @Url, @Summary, @Views, @Content);",
data);
}
}
await next(context);
}
}
}