mirror of
https://github.com/xboxeer/NScrapy.git
synced 2026-10-28 11:47:15 +08:00
Will use Zookeeper to manager the config for every node Also changed some methed name in old design for the purpose of align naming practice
64 lines
2.4 KiB
C#
64 lines
2.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using Microsoft.Extensions.Configuration;
|
|
using System.IO;
|
|
using System.Reflection;
|
|
using System.Linq;
|
|
using NScrapy.Infra.Attributes.SpiderAttributes;
|
|
using NScrapy.Infra;
|
|
|
|
namespace NScrapy.Spider
|
|
{
|
|
public class SpiderFactory
|
|
{
|
|
static AppDomain currentApp = System.AppDomain.CurrentDomain;
|
|
static Assembly spiderProjectAssembly = null;
|
|
static string SpiderProjectName = string.Empty;
|
|
static SpiderFactory()
|
|
{
|
|
SpiderProjectName = NScrapyContext.CurrentContext.CurrentConfig["AppSettings:SpiderProject"];
|
|
spiderProjectAssembly = currentApp.GetAssemblies().Where(assembly => assembly.GetName().Name == SpiderProjectName).FirstOrDefault();
|
|
}
|
|
|
|
public static Spider GetSpider(string name)
|
|
{
|
|
Type spiderType = null;
|
|
spiderType = spiderProjectAssembly.GetType(name);
|
|
if(spiderType==null)
|
|
{
|
|
var types = spiderProjectAssembly.ExportedTypes;
|
|
foreach(var type in types)
|
|
{
|
|
if (type.GetCustomAttribute(typeof(NameAttribute)) is NameAttribute nameAttr)
|
|
{
|
|
if (name == null)
|
|
{
|
|
continue;
|
|
}
|
|
if (name == nameAttr.Name && type.GetTypeInfo().IsSubclassOf(typeof(Spider)))
|
|
{
|
|
spiderType = type;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if(spiderType!=null)
|
|
{
|
|
if (Activator.CreateInstance(spiderType) is Spider spider)
|
|
{
|
|
if (spiderType.GetCustomAttribute(typeof(URLAttribute)) is URLAttribute urlAttr)
|
|
{
|
|
spider.URLs= urlAttr.URLs.ToList();
|
|
spider.Middlewares = new List<ISpiderMiddleware>();
|
|
}
|
|
return spider;
|
|
}
|
|
throw new Exception($"Create Spider {spiderType.FullName} failed");
|
|
}
|
|
throw new Exception($"Spider with name {name} can not be found, please check the spider name and if it is inherting NScrapy.Spider.Spider class");
|
|
}
|
|
}
|
|
}
|