using DynamicData.Annotations; using Jvedio.CommonNet.Entity; using Jvedio.Core.Interfaces; using Jvedio.Core.Plugins; using Jvedio.Utils; using Jvedio.Utils.Common; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.ComponentModel; using System.Reflection; using System.Runtime.CompilerServices; namespace Jvedio.Core.Crawler { /// /// 服务器源 /// public class CrawlerServer : INotifyPropertyChanged, IDictKeys { public CrawlerServer() { } public string ServerName { get; set; } public string Name { get; set; } private string _Url; public string Url { get { return _Url; } set { _Url = value; OnPropertyChanged(); } } private bool _Enabled; public bool Enabled { get { return _Enabled; } set { _Enabled = value; OnPropertyChanged(); } } /// /// -1 不可用,1-可用,2-测试中 /// private int _Available; public int Available { get { return _Available; } set { _Available = value; OnPropertyChanged(); } } public string LastRefreshDate { get; set; } private string _Cookies; public string Cookies { get { return _Cookies; } set { _Cookies = value; OnPropertyChanged(); } } public string _Headers { get; set; } public string Headers { get { return _Headers; } set { _Headers = value; OnPropertyChanged(); } } public event PropertyChangedEventHandler PropertyChanged; [NotifyPropertyChangedInvocator] protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } public bool isHeaderProper() { if (string.IsNullOrEmpty(Headers)) return true; try { Dictionary dictionary = JsonConvert.DeserializeObject>(Headers); return true; } catch (Exception ex) { System.Console.WriteLine(ex.Message); return false; } } public static RequestHeader parseHeader(CrawlerServer server) { if (server == null) return CrawlerHeader.Default; RequestHeader result = new RequestHeader(); result.WebProxy = GlobalConfig.ProxyConfig.GetWebProxy(); result.TimeOut = GlobalConfig.ProxyConfig.HttpTimeout * 1000;// 转为 ms string header = server.Headers; if (string.IsNullOrEmpty(header)) return CrawlerHeader.Default; ; Dictionary dict = JsonUtils.TryDeserializeObject>(header); if (dict != null && dict.Count > 0) { if (!dict.ContainsKey("cookie") && !string.IsNullOrEmpty(server.Cookies)) dict.Add("cookie", server.Cookies); result.Headers = dict; } return result; } public bool HasAllKeys(Dictionary dict) { if (dict == null || dict.Count == 0) return false; PropertyInfo[] propertyInfos = this.GetType().GetProperties(); foreach (var item in propertyInfos) { if (!dict.ContainsKey(item.Name)) return false; if (dict[item.Name] == null) return false; } return true; } } }