TestByJson.cs
1using System;
2using System.Collections.Generic;
3using System.Data;
4using System.IO;
5using System.Linq;
6using System.Text;
7using System.Threading.Tasks;
8using Newtonsoft.Json;
9using Newtonsoft.Json.Linq;
10using Udger.Parser;
11using Udger.Parser.Input;
12
13namespace ConsoleTest
14{
16 {
17
18 //[JsonPropertyName("Wind")]
19 private string filePath;
20 public TestByJson(string _filePath)
21 {
22 filePath = _filePath;
23 }
24
25 public List<string> doTest()
26 {
27 Udger.Parser.UserAgent u,uToCompare;
29 List<string> listToRet = new List<string>();
30 // Create a new UdgerParser object
31 UdgerParser parser = new UdgerParser();
32
33
34 // or Create and set LRU Cache capacity
35 //UdgerParser parser = new UdgerParser(5000);
36
37 // Set data dir (in this directory is stored data file: udgerdb_v4.dat)
38 // Test data file is available on: https://github.com/udger/test-data/tree/master/data_v4
39 // Full data file can be downloaded manually from http://data.udger.com/, but we recommend use udger-updater
40 parser.SetDataDir(@"C:\udger");
41
42
43 //var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestData.json");
44 var jsonText = File.ReadAllText(filePath);
45
46 try
47 {
48 Newtonsoft.Json.JsonSerializer json = new Newtonsoft.Json.JsonSerializer();
49
50 json.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
51 json.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace;
52 json.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;
53 json.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
54
55 StringReader sr = new StringReader(jsonText);
56
57 Newtonsoft.Json.JsonTextReader reader = new JsonTextReader(sr);
58 var result = json.Deserialize<List<JObject>>(reader);
59 var ts = result.Values("test");
60 var ret = result.Values("ret").GetEnumerator() ;
61
62 int count = 0;
63 foreach (var test in ts)
64 {
65 //var h = test.Select(t => new Header() { SecChUa = t.Value<string>("Sec-Ch-Ua") }).SingleOrDefault();
66 Header h = new Header() { SecChUa = test.Value<string>("Sec-Ch-Ua"),
67 SecChUaFullVersion = test.Value<string>("Sec-Ch-Ua-Full-Version"),
68 SecChUaFullVersionList = test.Value<string>("Sec-Ch-Ua-Full-Version-List"),
69 SecChUaMobile = test.Value<string>("Sec-Ch-Ua-Mobile"),
70 SecChUaModel = test.Value<string>("Sec-Ch-Ua-Model"),
71 SecChUaPlatform = test.Value<string>("Sec-Ch-Ua-Platform"),
72 SecChUaPlatformVersion = test.Value<string>("Sec-Ch-Ua-Platform-Version"),
73 Ua = test.Value<string>("User-Agent")
74
75
76 };
77 parser.ua = test.Value<string>("User-Agent");
78
79
80 parser.header = h;
81 parser.parse();
82
83 u = parser.userAgent;
84 ret.MoveNext();
85
86 var r = ret.Current;
87 uToCompare = new UserAgent()
88 {
89 CrawlerCategory = r.Value<string>("crawler_category"),
90 CrawlerCategoryCode = r.Value<string>("crawler_category_code"),
91 CrawlerLastSeen = null,
92 CrawlerRespectRobotstxt = r.Value<string>("crawler_respect_robotstxt"),
93 DeviceBrand = r.Value<string>("device_brand"),
94 DeviceBrandCode = r.Value<string>("device_brand_code"),
95 DeviceBrandHomepage = r.Value<string>("device_brand_homepage"),
96 DeviceBrandIcon = r.Value<string>("device_brand_icon"),
97 DeviceBrandIconBig = r.Value<string>("device_brand_icon_big"),
98 DeviceBrandInfoUrl = r.Value<string>("device_brand_info_url"),
99 DeviceClass = r.Value<string>("device_class"),
100 DeviceClassCode = r.Value<string>("device_class_code"),
101 DeviceClassIcon = r.Value<string>("device_class_icon"),
102 DeviceClassIconBig = r.Value<string>("device_class_icon_big"),
103 DeviceClassInfoUrl = r.Value<string>("device_class_info_url"),
104 DeviceMarketname = r.Value<string>("device_marketname"),
105 Os = r.Value<string>("os"),
106 OsCode = r.Value<string>("os_code"),
107 OsFamily = r.Value<string>("os_family"),
108 OsFamilyCode = r.Value<string>("os_family_code"),
109 OsFamilyVendor = r.Value<string>("os_family_vendor"),
110 OsFamilyVendorCode = r.Value<string>("os_family_vendor_code"),
111 OsFamilyVendorHomepage = r.Value<string>("os_family_vendor_homepage"),
112 OsHomepage = r.Value<string>("os_homepage"),
113 OsIcon = r.Value<string>("os_icon"),
114 OsIconBig = r.Value<string>("os_icon_big"),
115 OsInfoUrl = r.Value<string>("os_info_url"),
116 SecChUa = r.Value<string>("sec_ch_ua"),
117 SecChUaFullVersion = r.Value<string>("sec_ch_ua_full_version"),
118 SecChUaFullVersionList = r.Value<string>("sec_ch_ua_full_version_list"),
119 SecChUaMobile = r.Value<string>("sec_ch_ua_mobile"),
120 SecChUaModel = r.Value<string>("sec_ch_ua_model"),
121 SecChUaPlatform = r.Value<string>("sec_ch_ua_platform"),
122 SecChUaPlatformVersion = r.Value<string>("sec_ch_ua_platform_version"),
123 Ua = r.Value<string>("ua"),
124 UaClass = r.Value<string>("ua_class"),
125 UaClassCode = r.Value<string>("ua_class_code"),
126 UaEngine = r.Value<string>("ua_engine"),
127 UaFamily = r.Value<string>("ua_family"),
128 UaFamilyCode = r.Value<string>("ua_family_code"),
129 UaFamilyHompage = r.Value<string>("ua_family_homepage"),
130 UaFamilyIcon = r.Value<string>("ua_family_icon"),
131 UaFamilyIconBig = r.Value<string>("ua_family_icon_big"),
132 UaFamilyInfoUrl = r.Value<string>("ua_family_info_url"),
133 UaFamilyVendor = r.Value<string>("ua_family_vendor"),
134 UaFamilyVendorCode = r.Value<string>("ua_family_vendor_code"),
135 UaFamilyVendorHomepage = r.Value<string>("ua_family_vendor_homepage"),
136 UaString = r.Value<string>("ua_string"),
137 UaUptodateCurrentVersion = r.Value<string>("ua_uptodate_current_version"),
138 UaVersion = r.Value<string>("ua_version"),
139 UaVersionMajor = r.Value<string>("ua_version_major")
140 };
141
142 count++;
143
144 listToRet.Add($"{count}{System.Environment.NewLine}{u.Compare(uToCompare)}");
145
146 }
147 reader.Close();
148 //return result;
149 }
150 catch (Exception ex)
151 {
152 throw ex;
153 }
154 return listToRet;
155 }
156 }
157}
void SetDataDir(string dataDir)
Set the data directory
Definition: UdgerParser.cs:121
void parse()
Parse the useragent string and/or ip address ///
Definition: UdgerParser.cs:171