PageRenderTime 235ms CodeModel.GetById 49ms RepoModel.GetById 1ms app.codeStats 0ms

/Abot/Core/AbotConfigurationSectionHandler.cs

https://gitlab.com/kokeiro001/abot
C# | 335 lines | 273 code | 53 blank | 9 comment | 0 complexity | fe5119975dda7ea782b4917a2f97d4af MD5 | raw file
  1. using Abot.Poco;
  2. using System;
  3. using System.Configuration;
  4. namespace Abot.Core
  5. {
  6. [Serializable]
  7. public class AbotConfigurationSectionHandler : ConfigurationSection
  8. {
  9. public AbotConfigurationSectionHandler()
  10. {
  11. }
  12. [ConfigurationProperty("crawlBehavior")]
  13. public CrawlBehaviorElement CrawlBehavior
  14. {
  15. get { return (CrawlBehaviorElement)this["crawlBehavior"]; }
  16. }
  17. [ConfigurationProperty("politeness")]
  18. public PolitenessElement Politeness
  19. {
  20. get { return (PolitenessElement)this["politeness"]; }
  21. }
  22. [ConfigurationProperty("authorization")]
  23. public AuthorizationElement Authorization
  24. {
  25. get { return (AuthorizationElement)this["authorization"]; }
  26. }
  27. [ConfigurationProperty("extensionValues")]
  28. [ConfigurationCollection(typeof(ExtensionValueCollection), AddItemName = "add")]
  29. public ExtensionValueCollection ExtensionValues
  30. {
  31. get { return (ExtensionValueCollection)this["extensionValues"]; }
  32. }
  33. public CrawlConfiguration Convert()
  34. {
  35. AutoMapper.Mapper.CreateMap<CrawlBehaviorElement, CrawlConfiguration>();
  36. AutoMapper.Mapper.CreateMap<PolitenessElement, CrawlConfiguration>();
  37. AutoMapper.Mapper.CreateMap<AuthorizationElement, CrawlConfiguration>();
  38. CrawlConfiguration config = new CrawlConfiguration();
  39. AutoMapper.Mapper.Map<CrawlBehaviorElement, CrawlConfiguration>(CrawlBehavior, config);
  40. AutoMapper.Mapper.Map<PolitenessElement, CrawlConfiguration>(Politeness, config);
  41. AutoMapper.Mapper.Map<AuthorizationElement, CrawlConfiguration>(Authorization, config);
  42. foreach (ExtensionValueElement element in ExtensionValues)
  43. config.ConfigurationExtensions.Add(element.Key, element.Value);
  44. return config;
  45. }
  46. public static AbotConfigurationSectionHandler LoadFromXml()
  47. {
  48. return ((AbotConfigurationSectionHandler)System.Configuration.ConfigurationManager.GetSection("abot"));
  49. }
  50. }
  51. [Serializable]
  52. public class AuthorizationElement : ConfigurationElement
  53. {
  54. /// <summary>
  55. /// Defines whatewer each request shold be autorized via login
  56. /// </summary>
  57. [ConfigurationProperty("isAlwaysLogin", IsRequired = false)]
  58. public bool IsAlwaysLogin
  59. {
  60. get { return (bool)this["isAlwaysLogin"]; }
  61. }
  62. /// <summary>
  63. /// The user name to be used for autorization
  64. /// </summary>
  65. [ConfigurationProperty("loginUser", IsRequired = false)]
  66. public string LoginUser
  67. {
  68. get { return (string)this["loginUser"]; }
  69. }
  70. /// <summary>
  71. /// The password to be used for autorization
  72. /// </summary>
  73. [ConfigurationProperty("loginPassword", IsRequired = false)]
  74. public string LoginPassword
  75. {
  76. get { return (string)this["loginPassword"]; }
  77. }
  78. }
  79. [Serializable]
  80. public class PolitenessElement : ConfigurationElement
  81. {
  82. [ConfigurationProperty("isRespectRobotsDotTextEnabled", IsRequired = false)]
  83. public bool IsRespectRobotsDotTextEnabled
  84. {
  85. get { return (bool)this["isRespectRobotsDotTextEnabled"]; }
  86. }
  87. [ConfigurationProperty("isRespectMetaRobotsNoFollowEnabled", IsRequired = false)]
  88. public bool IsRespectMetaRobotsNoFollowEnabled
  89. {
  90. get { return (bool)this["isRespectMetaRobotsNoFollowEnabled"]; }
  91. }
  92. [ConfigurationProperty("isRespectHttpXRobotsTagHeaderNoFollowEnabled", IsRequired = false)]
  93. public bool IsRespectHttpXRobotsTagHeaderNoFollowEnabled
  94. {
  95. get { return (bool)this["isRespectHttpXRobotsTagHeaderNoFollowEnabled"]; }
  96. }
  97. [ConfigurationProperty("isRespectAnchorRelNoFollowEnabled", IsRequired = false)]
  98. public bool IsRespectAnchorRelNoFollowEnabled
  99. {
  100. get { return (bool)this["isRespectAnchorRelNoFollowEnabled"]; }
  101. }
  102. [ConfigurationProperty("isIgnoreRobotsDotTextIfRootDisallowedEnabled", IsRequired = false)]
  103. public bool IsIgnoreRobotsDotTextIfRootDisallowedEnabled
  104. {
  105. get { return (bool)this["isIgnoreRobotsDotTextIfRootDisallowedEnabled"]; }
  106. }
  107. [ConfigurationProperty("robotsDotTextUserAgentString", IsRequired = false, DefaultValue = "abot")]
  108. public string RobotsDotTextUserAgentString
  109. {
  110. get { return (string)this["robotsDotTextUserAgentString"]; }
  111. }
  112. [ConfigurationProperty("maxRobotsDotTextCrawlDelayInSeconds", IsRequired = false, DefaultValue = 5)]
  113. public int MaxRobotsDotTextCrawlDelayInSeconds
  114. {
  115. get { return (int)this["maxRobotsDotTextCrawlDelayInSeconds"]; }
  116. }
  117. [ConfigurationProperty("minCrawlDelayPerDomainMilliSeconds", IsRequired = false)]
  118. public int MinCrawlDelayPerDomainMilliSeconds
  119. {
  120. get { return (int)this["minCrawlDelayPerDomainMilliSeconds"]; }
  121. }
  122. }
  123. [Serializable]
  124. public class CrawlBehaviorElement : ConfigurationElement
  125. {
  126. [ConfigurationProperty("maxConcurrentThreads", IsRequired = false, DefaultValue = 10)]
  127. public int MaxConcurrentThreads
  128. {
  129. get { return (int)this["maxConcurrentThreads"]; }
  130. }
  131. [ConfigurationProperty("maxPagesToCrawl", IsRequired = false, DefaultValue = 1000)]
  132. public int MaxPagesToCrawl
  133. {
  134. get { return (int)this["maxPagesToCrawl"]; }
  135. }
  136. [ConfigurationProperty("maxPagesToCrawlPerDomain", IsRequired = false)]
  137. public int MaxPagesToCrawlPerDomain
  138. {
  139. get { return (int)this["maxPagesToCrawlPerDomain"]; }
  140. }
  141. [ConfigurationProperty("maxPageSizeInBytes", IsRequired = false)]
  142. public int MaxPageSizeInBytes
  143. {
  144. get { return (int)this["maxPageSizeInBytes"]; }
  145. }
  146. [ConfigurationProperty("userAgentString", IsRequired = false, DefaultValue = "Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko")]
  147. public string UserAgentString
  148. {
  149. get { return (string)this["userAgentString"]; }
  150. }
  151. [ConfigurationProperty("crawlTimeoutSeconds", IsRequired = false)]
  152. public int CrawlTimeoutSeconds
  153. {
  154. get { return (int)this["crawlTimeoutSeconds"]; }
  155. }
  156. [ConfigurationProperty("downloadableContentTypes", IsRequired = false, DefaultValue = "text/html")]
  157. public string DownloadableContentTypes
  158. {
  159. get { return (string)this["downloadableContentTypes"]; }
  160. }
  161. [ConfigurationProperty("isUriRecrawlingEnabled", IsRequired = false)]
  162. public bool IsUriRecrawlingEnabled
  163. {
  164. get { return (bool)this["isUriRecrawlingEnabled"]; }
  165. }
  166. [ConfigurationProperty("isExternalPageCrawlingEnabled", IsRequired = false)]
  167. public bool IsExternalPageCrawlingEnabled
  168. {
  169. get { return (bool)this["isExternalPageCrawlingEnabled"]; }
  170. }
  171. [ConfigurationProperty("isExternalPageLinksCrawlingEnabled", IsRequired = false)]
  172. public bool IsExternalPageLinksCrawlingEnabled
  173. {
  174. get { return (bool)this["isExternalPageLinksCrawlingEnabled"]; }
  175. }
  176. [ConfigurationProperty("isSslCertificateValidationEnabled", IsRequired = false, DefaultValue = true)]
  177. public bool IsSslCertificateValidationEnabled
  178. {
  179. get { return (bool)this["isSslCertificateValidationEnabled"]; }
  180. }
  181. [ConfigurationProperty("httpServicePointConnectionLimit", IsRequired = false, DefaultValue = 200)]
  182. public int HttpServicePointConnectionLimit
  183. {
  184. get { return (int)this["httpServicePointConnectionLimit"]; }
  185. }
  186. [ConfigurationProperty("httpRequestTimeoutInSeconds", IsRequired = false, DefaultValue = 15)]
  187. public int HttpRequestTimeoutInSeconds
  188. {
  189. get { return (int)this["httpRequestTimeoutInSeconds"]; }
  190. }
  191. [ConfigurationProperty("httpRequestMaxAutoRedirects", IsRequired = false, DefaultValue = 7)]
  192. public int HttpRequestMaxAutoRedirects
  193. {
  194. get { return (int)this["httpRequestMaxAutoRedirects"]; }
  195. }
  196. [ConfigurationProperty("isHttpRequestAutoRedirectsEnabled", IsRequired = false, DefaultValue = true)]
  197. public bool IsHttpRequestAutoRedirectsEnabled
  198. {
  199. get { return (bool)this["isHttpRequestAutoRedirectsEnabled"]; }
  200. }
  201. [ConfigurationProperty("isHttpRequestAutomaticDecompressionEnabled", IsRequired = false)]
  202. public bool IsHttpRequestAutomaticDecompressionEnabled
  203. {
  204. get { return (bool)this["isHttpRequestAutomaticDecompressionEnabled"]; }
  205. }
  206. [ConfigurationProperty("isSendingCookiesEnabled", IsRequired = false)]
  207. public bool IsSendingCookiesEnabled
  208. {
  209. get { return (bool)this["isSendingCookiesEnabled"]; }
  210. }
  211. [ConfigurationProperty("isRespectUrlNamedAnchorOrHashbangEnabled", IsRequired = false)]
  212. public bool IsRespectUrlNamedAnchorOrHashbangEnabled
  213. {
  214. get { return (bool)this["isRespectUrlNamedAnchorOrHashbangEnabled"]; }
  215. }
  216. [ConfigurationProperty("minAvailableMemoryRequiredInMb", IsRequired = false)]
  217. public int MinAvailableMemoryRequiredInMb
  218. {
  219. get { return (int)this["minAvailableMemoryRequiredInMb"]; }
  220. }
  221. [ConfigurationProperty("maxMemoryUsageInMb", IsRequired = false)]
  222. public int MaxMemoryUsageInMb
  223. {
  224. get { return (int)this["maxMemoryUsageInMb"]; }
  225. }
  226. [ConfigurationProperty("maxMemoryUsageCacheTimeInSeconds", IsRequired = false)]
  227. public int MaxMemoryUsageCacheTimeInSeconds
  228. {
  229. get { return (int)this["maxMemoryUsageCacheTimeInSeconds"]; }
  230. }
  231. [ConfigurationProperty("maxCrawlDepth", IsRequired = false, DefaultValue = 100)]
  232. public int MaxCrawlDepth
  233. {
  234. get { return (int)this["maxCrawlDepth"]; }
  235. }
  236. [ConfigurationProperty("isForcedLinkParsingEnabled", IsRequired = false)]
  237. public bool IsForcedLinkParsingEnabled
  238. {
  239. get { return (bool)this["isForcedLinkParsingEnabled"]; }
  240. }
  241. [ConfigurationProperty("maxRetryCount", IsRequired = false)]
  242. public int MaxRetryCount
  243. {
  244. get { return (int)this["maxRetryCount"]; }
  245. }
  246. [ConfigurationProperty("minRetryDelayInMilliseconds", IsRequired = false)]
  247. public int MinRetryDelayInMilliseconds
  248. {
  249. get { return (int)this["minRetryDelayInMilliseconds"]; }
  250. }
  251. }
  252. [Serializable]
  253. public class ExtensionValueElement : ConfigurationElement
  254. {
  255. [ConfigurationProperty("key", IsRequired = false, IsKey = true)]
  256. public string Key
  257. {
  258. get { return (string)this["key"]; }
  259. }
  260. [ConfigurationProperty("value", IsRequired = false, IsKey = false)]
  261. public string Value
  262. {
  263. get { return (string)this["value"]; }
  264. }
  265. }
  266. [Serializable]
  267. public class ExtensionValueCollection : ConfigurationElementCollection
  268. {
  269. public ExtensionValueElement this[int index]
  270. {
  271. get { return (ExtensionValueElement)BaseGet(index); }
  272. }
  273. protected override ConfigurationElement CreateNewElement()
  274. {
  275. return new ExtensionValueElement();
  276. }
  277. protected override object GetElementKey(ConfigurationElement element)
  278. {
  279. return ((ExtensionValueElement)element).Key;
  280. }
  281. }
  282. }