Advertisement
Guest User

Untitled

a guest
May 25th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.17 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Newtonsoft.Json;
  7. using System.IO;
  8. using System.Data.SqlClient;
  9.  
  10.  
  11.  
  12. namespace JsonToDb
  13. {
  14. #region RootObjects
  15. public class ClicksUserAgent
  16. {
  17. public string deviceType { get; set; }
  18. public string operatingSystem { get; set; }
  19. public string browser { get; set; }
  20. public string rawUserAgent { get; set; }
  21. }
  22.  
  23. public class ClicksUserInfo
  24. {
  25. public string userId { get; set; }
  26. public string ip { get; set; }
  27. public ClicksUserAgent userAgent { get; set; }
  28. }
  29.  
  30. public class ClicksSiteDomain
  31. {
  32. public string countryCode { get; set; }
  33. public string domainName { get; set; }
  34. }
  35.  
  36. public class ClicksRootObject
  37. {
  38. public ClicksUserInfo userInfo { get; set; }
  39. public ClicksSiteDomain siteDomain { get; set; }
  40. public string dataCenter { get; set; }
  41. public string utcDate { get; set; }
  42. public string offerTitle { get; set; }
  43. public List<string> category { get; set; }
  44. public double price { get; set; }
  45. public string merchant { get; set; }
  46. public string source { get; set; }
  47. public List<object> keywords { get; set; }
  48. public string offerViewId { get; set; }
  49. public string clickId { get; set; }
  50. public double earning { get; set; }
  51. public string searchId { get; set; }
  52. }
  53. #endregion
  54.  
  55.  
  56.  
  57. class Clicks
  58. {
  59. SqlConnection con = new SqlConnection(GlobalVar.databaseConnection);
  60.  
  61. #region FieldsForClicks
  62. public string userInfo_userID { get; set; }
  63. public string userInfo_ip { get; set; }
  64. public string userInfo_userAgent_deviceType { get; set; }
  65. public string userInfo_userAgent_operatingSystem { get; set; }
  66. public string userInfo_userAgent_rawUserAgent { get; set; }
  67. public string siteDomain_countryCode { get; set; }
  68. public string siteDomain_domainName { get; set; }
  69. public string dataCenter { get; set; }
  70. public string utcDate { get; set; }
  71. public string offerTitle { get; set; }
  72. public string category { get; set; }
  73. public string price { get; set; }
  74. public string merchant { get; set; }
  75. public string source { get; set; }
  76. public string keywords { get; set; }
  77. public string offerViewId { get; set; }
  78. public string clickId { get; set; }
  79. public string earning { get; set; }
  80. public string searchId { get; set; }
  81. #endregion
  82.  
  83. List<ClicksRootObject> rootObjectList = new List<ClicksRootObject>();
  84. List<string> fileList = new List<string>();
  85.  
  86. public void start()
  87. {
  88. con.Open();
  89.  
  90. readAllFiles();
  91. foreach (string s in fileList)
  92. {
  93. fileToJson(s);
  94. }
  95. jsonToRow();
  96.  
  97. con.Close();
  98. }
  99.  
  100. private void readAllFiles()
  101. {
  102. //string folderPath = @"C:\Users\venskusm\Desktop\DMP\Clicks";
  103. string folderPath = @"C:\Users\romasda\Desktop\JsonToDb\clicks";
  104. string[] filePaths = Directory.GetFiles(folderPath, "*.json", SearchOption.TopDirectoryOnly);
  105.  
  106. foreach (string s in filePaths)
  107. {
  108. fileList.Add(s);
  109. }
  110.  
  111. }
  112.  
  113.  
  114. private void fileToJson(string path)
  115. {
  116. string line;
  117. int index = 0;
  118. // Read the file and display it line by line.
  119. System.IO.StreamReader file = new System.IO.StreamReader(path);
  120. while ((line = file.ReadLine()) != null)
  121. {
  122. var jsonObject = JsonConvert.DeserializeObject<ClicksRootObject>(line);
  123. rootObjectList.Add(jsonObject);
  124. index++;
  125. }
  126.  
  127. file.Close();
  128. }
  129.  
  130. private void jsonToRow()
  131. {
  132.  
  133. foreach (ClicksRootObject r in rootObjectList)//0
  134. {
  135. #region defining the values of RootObjects
  136. ClicksUserInfo clicksUserInfo = r.userInfo;//1
  137.  
  138. try
  139. {
  140. if (r.userInfo.userId == null)
  141. {
  142. string userId = "";
  143. }
  144. else
  145. {
  146. string userId = r.userInfo.userId;//2
  147. }
  148. }
  149. catch (NullReferenceException ex)
  150. {
  151. Console.WriteLine("Processor Usage" + ex.Message);
  152. }
  153.  
  154. try
  155. {
  156. if (r.userInfo.ip == null)
  157. {
  158. string ip = "";
  159. }
  160. else
  161. {
  162. string ip = r.userInfo.ip;//2
  163. }
  164. }
  165. catch (NullReferenceException ex)
  166. {
  167. Console.WriteLine("Processor Usage" + ex.Message);
  168. }
  169.  
  170. ClicksUserAgent userAgent = r.userInfo.userAgent;//2
  171.  
  172. try
  173. {
  174. if (r.userInfo.userAgent.deviceType == null)
  175. {
  176. string deviceType = "";
  177. }
  178. else
  179. {
  180. string deviceType = r.userInfo.userAgent.deviceType;//3
  181. }
  182. }
  183. catch (NullReferenceException ex)
  184. {
  185. Console.WriteLine("Processor Usage" + ex.Message);
  186. }
  187.  
  188.  
  189. try
  190. {
  191. if (r.userInfo.userAgent.operatingSystem == null)
  192. {
  193. string operatingSystem = "";
  194. }
  195. else
  196. {
  197. string operatingSystem = r.userInfo.userAgent.operatingSystem;//3
  198. }
  199. }
  200. catch (NullReferenceException ex)
  201. {
  202. Console.WriteLine("Processor Usage" + ex.Message);
  203. }
  204.  
  205. try
  206. {
  207. if (r.userInfo.userAgent.browser == null)
  208. {
  209. string browser = "";
  210. }
  211. else
  212. {
  213. string browser = r.userInfo.userAgent.browser;//3
  214. }
  215. }
  216. catch (NullReferenceException ex)
  217. {
  218. Console.WriteLine("Processor Usage" + ex.Message);
  219. }
  220.  
  221. try
  222. {
  223. if (r.userInfo.userAgent.rawUserAgent == null)
  224. {
  225. string rawUserAgent = "";
  226. }
  227. else
  228. {
  229. string rawUserAgent = r.userInfo.userAgent.rawUserAgent;//3
  230. }
  231. }
  232. catch (NullReferenceException ex)
  233. {
  234. Console.WriteLine("Processor Usage" + ex.Message);
  235. }
  236.  
  237. ClicksSiteDomain siteDomain = r.siteDomain;//1
  238.  
  239. try
  240. {
  241. if (r.siteDomain.countryCode == null)
  242. {
  243. string countryCode = "";
  244. }
  245. else
  246. {
  247. string countryCode = r.siteDomain.countryCode;//2
  248. }
  249. }
  250. catch (NullReferenceException ex)
  251. {
  252. Console.WriteLine("Processor Usage" + ex.Message);
  253. }
  254.  
  255.  
  256. try
  257. {
  258. if (r.siteDomain.domainName == null)
  259. {
  260. string domainName = "";
  261. }
  262. else
  263. {
  264. string domainName = r.siteDomain.domainName;//2
  265. }
  266. }
  267. catch (NullReferenceException ex)
  268. {
  269. Console.WriteLine("Processor Usage" + ex.Message);
  270. }
  271.  
  272. try
  273. {
  274. if (r.dataCenter == null)
  275. {
  276. string dataCenter = "";
  277. }
  278. else
  279. {
  280. string dataCenter = r.dataCenter;//1
  281. }
  282. }
  283. catch (NullReferenceException ex)
  284. {
  285. Console.WriteLine("Processor Usage" + ex.Message);
  286. }
  287.  
  288. try
  289. {
  290. if (r.utcDate == null)
  291. {
  292. string utcDate = "";
  293. }
  294. else
  295. {
  296. string utcDate = r.utcDate;//1
  297. }
  298. }
  299. catch (NullReferenceException ex)
  300. {
  301. Console.WriteLine("Processor Usage" + ex.Message);
  302. }
  303.  
  304.  
  305. try
  306. {
  307. if (r.offerTitle == null)
  308. {
  309. string offerTitle = "";
  310. }
  311. else
  312. {
  313. string offerTitle = r.offerTitle;//1
  314. }
  315. }
  316. catch (NullReferenceException ex)
  317. {
  318. Console.WriteLine("Processor Usage" + ex.Message);
  319. }
  320.  
  321. //category listas
  322.  
  323.  
  324.  
  325. double price = r.price;//1
  326.  
  327.  
  328. try
  329. {
  330. if (r.merchant == null)
  331. {
  332. string merchant = "";
  333. }
  334. else
  335. {
  336. string merchant = r.merchant;//1
  337. }
  338. }
  339. catch (NullReferenceException ex)
  340. {
  341. Console.WriteLine("Processor Usage" + ex.Message);
  342. }
  343.  
  344. try
  345. {
  346. if (r.source == null)
  347. {
  348. string source = "";
  349. }
  350. else
  351. {
  352. string source = r.source;//1
  353. }
  354. }
  355. catch (NullReferenceException ex)
  356. {
  357. Console.WriteLine("Processor Usage" + ex.Message);
  358. }
  359.  
  360. //keywords listas
  361.  
  362. try
  363. {
  364. if (r.offerViewId == null)
  365. {
  366. string offerViewId = "";
  367. }
  368. else
  369. {
  370. string offerViewId = r.offerViewId;//1
  371. }
  372. }
  373. catch (NullReferenceException ex)
  374. {
  375. Console.WriteLine("Processor Usage" + ex.Message);
  376. }
  377.  
  378. try
  379. {
  380. if (r.clickId == null)
  381. {
  382. string clickId = "";
  383. }
  384. else
  385. {
  386. string clickId = r.clickId;//1
  387. }
  388. }
  389. catch (NullReferenceException ex)
  390. {
  391. Console.WriteLine("Processor Usage" + ex.Message);
  392. }
  393.  
  394.  
  395.  
  396. double earning = r.earning;//1
  397.  
  398. try
  399. {
  400. if (r.searchId == null)
  401. {
  402. string searchId = "";
  403. }
  404. else
  405. {
  406. string searchId = r.searchId;//1
  407. }
  408. }
  409. catch (NullReferenceException ex)
  410. {
  411. Console.WriteLine("Processor Usage" + ex.Message);
  412. }
  413.  
  414. #endregion
  415.  
  416. if (r.category.Count == 0 && r.keywords.Count == 0)
  417. {/*
  418. Console.WriteLine(r.userInfo);//1
  419.  
  420. Console.WriteLine(r.userInfo.userId);//2
  421.  
  422. Console.WriteLine(r.userInfo.ip);//2
  423.  
  424. Console.WriteLine(r.userInfo.userAgent);//2
  425.  
  426. Console.WriteLine(r.userInfo.userAgent.deviceType);//3
  427.  
  428. Console.WriteLine(r.userInfo.userAgent.operatingSystem);//3
  429.  
  430. Console.WriteLine(r.userInfo.userAgent.browser);//3
  431.  
  432. Console.WriteLine(r.userInfo.userAgent.rawUserAgent);//3
  433.  
  434. Console.WriteLine(r.siteDomain);//1
  435.  
  436. Console.WriteLine(r.siteDomain.countryCode);//2
  437.  
  438. Console.WriteLine(r.siteDomain.domainName);//2
  439.  
  440. Console.WriteLine(r.dataCenter);//1
  441.  
  442. Console.WriteLine(r.utcDate);//1
  443.  
  444. Console.WriteLine(r.offerTitle);//1
  445.  
  446. //category listas
  447.  
  448. Console.WriteLine(r.price);//1
  449.  
  450. Console.WriteLine(r.merchant);//1
  451.  
  452. Console.WriteLine(r.source);//1
  453.  
  454. //keywords listas
  455.  
  456. Console.WriteLine(r.offerViewId);//1
  457.  
  458. Console.WriteLine(r.clickId);//1
  459.  
  460. Console.WriteLine(r.earning);//1
  461.  
  462. Console.WriteLine(r.searchId);//1*/
  463.  
  464.  
  465. }
  466. else if (r.category.Count != 0 && r.keywords.Count == 0)
  467. {
  468.  
  469. foreach (string s in r.category)
  470. {
  471. Console.WriteLine(r.userInfo.userId);//2
  472.  
  473. Console.WriteLine(r.userInfo.ip);//2
  474.  
  475. Console.WriteLine(r.userInfo.userAgent.deviceType);//3
  476.  
  477. Console.WriteLine(r.userInfo.userAgent.operatingSystem);//3
  478.  
  479. Console.WriteLine(r.userInfo.userAgent.browser);//3
  480.  
  481. Console.WriteLine(r.userInfo.userAgent.rawUserAgent);//3
  482.  
  483. Console.WriteLine(r.siteDomain.countryCode);//2
  484.  
  485. Console.WriteLine(r.siteDomain.domainName);//2
  486.  
  487. Console.WriteLine(r.dataCenter);//1
  488.  
  489. Console.WriteLine(r.utcDate);//1
  490.  
  491. Console.WriteLine(r.offerTitle);//1
  492.  
  493. Console.WriteLine(s);//category listas
  494.  
  495. Console.WriteLine(r.price);//1
  496.  
  497. Console.WriteLine(r.merchant);//1
  498.  
  499. Console.WriteLine(r.source);//1
  500.  
  501. //tuscias keywords listas
  502.  
  503. Console.WriteLine(r.offerViewId);//1
  504.  
  505. Console.WriteLine(r.clickId);//1
  506.  
  507. Console.WriteLine(r.earning);//1
  508.  
  509. Console.WriteLine(r.searchId);//1
  510.  
  511. Console.WriteLine(r.utcDate);
  512. Console.WriteLine("stop");
  513.  
  514. callSP(r.userInfo.userId, r.userInfo.ip, r.utcDate, r.earning);
  515.  
  516. }
  517.  
  518. }
  519. else if (r.category.Count == 0 && r.keywords.Count != 0)
  520. {
  521.  
  522. foreach (string s in r.keywords)
  523. {
  524.  
  525. Console.WriteLine(r.userInfo.userId);//2
  526.  
  527. Console.WriteLine(r.userInfo.ip);//2
  528.  
  529. Console.WriteLine(r.userInfo.userAgent.deviceType);//3
  530.  
  531. Console.WriteLine(r.userInfo.userAgent.operatingSystem);//3
  532.  
  533. Console.WriteLine(r.userInfo.userAgent.browser);//3
  534.  
  535. Console.WriteLine(r.userInfo.userAgent.rawUserAgent);//3
  536.  
  537. Console.WriteLine(r.siteDomain.countryCode);//2
  538.  
  539. Console.WriteLine(r.siteDomain.domainName);//2
  540.  
  541. Console.WriteLine(r.dataCenter);//1
  542.  
  543. Console.WriteLine(r.utcDate);//1
  544.  
  545. Console.WriteLine(r.offerTitle);//1
  546.  
  547. //tuscias category listas
  548.  
  549. Console.WriteLine(r.price);//1
  550.  
  551. Console.WriteLine(r.merchant);//1
  552.  
  553. Console.WriteLine(r.source);//1
  554.  
  555. Console.WriteLine(s);//tuscias keywords listas
  556.  
  557. Console.WriteLine(r.offerViewId);//1
  558.  
  559. Console.WriteLine(r.clickId);//1
  560.  
  561. Console.WriteLine(r.earning);//1
  562.  
  563. Console.WriteLine(r.searchId);//1
  564.  
  565. }
  566.  
  567. }
  568. else if (r.category.Count != 0 && r.keywords.Count != 0)//3 ir 2
  569. {
  570. int maxIndex = 0;
  571. int index = 0;
  572. if (r.category.Count > r.keywords.Count)
  573. {
  574. maxIndex = r.category.Count;
  575. }
  576. else
  577. {
  578. maxIndex = r.keywords.Count;
  579. }
  580.  
  581.  
  582. while (index < maxIndex)//0<3
  583. {
  584. if (r.category.Count > index && r.keywords.Count > index)//2>0 && 3>0, 2>1 && 3>1
  585. {
  586. Console.WriteLine(r.userInfo.userId);//2
  587.  
  588. Console.WriteLine(r.userInfo.ip);//2
  589.  
  590. Console.WriteLine(r.userInfo.userAgent.deviceType);//3
  591.  
  592. Console.WriteLine(r.userInfo.userAgent.operatingSystem);//3
  593.  
  594. Console.WriteLine(r.userInfo.userAgent.browser);//3
  595.  
  596. Console.WriteLine(r.userInfo.userAgent.rawUserAgent);//3
  597.  
  598. Console.WriteLine(r.siteDomain.countryCode);//2
  599.  
  600. Console.WriteLine(r.siteDomain.domainName);//2
  601.  
  602. Console.WriteLine(r.dataCenter);//1
  603.  
  604. Console.WriteLine(r.utcDate);//1
  605.  
  606. Console.WriteLine(r.offerTitle);//1
  607.  
  608. Console.WriteLine(r.category[index]);//:)
  609.  
  610. Console.WriteLine(r.price);//1
  611.  
  612. Console.WriteLine(r.merchant);//1
  613.  
  614. Console.WriteLine(r.source);//1
  615.  
  616. Console.WriteLine(r.keywords[index]);//:)
  617.  
  618. Console.WriteLine(r.offerViewId);//1
  619.  
  620. Console.WriteLine(r.clickId);//1
  621.  
  622. Console.WriteLine(r.earning);//1
  623.  
  624. Console.WriteLine(r.searchId);//1
  625. }
  626. else if (r.category.Count > index)
  627. {
  628. Console.WriteLine(r.userInfo.userId);//2
  629.  
  630. Console.WriteLine(r.userInfo.ip);//2
  631.  
  632. Console.WriteLine(r.userInfo.userAgent.deviceType);//3
  633.  
  634. Console.WriteLine(r.userInfo.userAgent.operatingSystem);//3
  635.  
  636. Console.WriteLine(r.userInfo.userAgent.browser);//3
  637.  
  638. Console.WriteLine(r.userInfo.userAgent.rawUserAgent);//3
  639.  
  640. Console.WriteLine(r.siteDomain.countryCode);//2
  641.  
  642. Console.WriteLine(r.siteDomain.domainName);//2
  643.  
  644. Console.WriteLine(r.dataCenter);//1
  645.  
  646. Console.WriteLine(r.utcDate);//1
  647.  
  648. Console.WriteLine(r.offerTitle);//1
  649.  
  650. Console.WriteLine(r.category[index]);//:)
  651.  
  652. Console.WriteLine(r.price);//1
  653.  
  654. Console.WriteLine(r.merchant);//1
  655.  
  656. Console.WriteLine(r.source);//1
  657.  
  658. //nebeliko keywordu
  659.  
  660. Console.WriteLine(r.offerViewId);//1
  661.  
  662. Console.WriteLine(r.clickId);//1
  663.  
  664. Console.WriteLine(r.earning);//1
  665.  
  666. Console.WriteLine(r.searchId);//1
  667. }
  668. else if (r.keywords.Count > index)
  669. {
  670.  
  671. Console.WriteLine(r.userInfo.userId);//2
  672.  
  673. Console.WriteLine(r.userInfo.ip);//2
  674.  
  675. Console.WriteLine(r.userInfo.userAgent.deviceType);//3
  676.  
  677. Console.WriteLine(r.userInfo.userAgent.operatingSystem);//3
  678.  
  679. Console.WriteLine(r.userInfo.userAgent.browser);//3
  680.  
  681. Console.WriteLine(r.userInfo.userAgent.rawUserAgent);//3
  682.  
  683. Console.WriteLine(r.siteDomain.countryCode);//2
  684.  
  685. Console.WriteLine(r.siteDomain.domainName);//2
  686.  
  687. Console.WriteLine(r.dataCenter);//1
  688.  
  689. Console.WriteLine(r.utcDate);//1
  690.  
  691. Console.WriteLine(r.offerTitle);//1
  692.  
  693. //nebeliko categoriju
  694.  
  695. Console.WriteLine(r.price);//1
  696.  
  697. Console.WriteLine(r.merchant);//1
  698.  
  699. Console.WriteLine(r.source);//1
  700.  
  701. Console.WriteLine(r.keywords[index]);//:)
  702.  
  703. Console.WriteLine(r.offerViewId);//1
  704.  
  705. Console.WriteLine(r.clickId);//1
  706.  
  707. Console.WriteLine(r.earning);//1
  708.  
  709. Console.WriteLine(r.searchId);//1
  710. }
  711. index++;
  712. }
  713. }
  714. }
  715. }
  716.  
  717. private void callSP(string userInfo_userID, string userInfo_ip , string utcDate, double earning)
  718. {
  719. SqlCommand command = new SqlCommand("sp_insertClick",con);
  720. command.CommandType = System.Data.CommandType.StoredProcedure;
  721. command.Parameters.AddWithValue("@userInfoUserId", userInfo_userID);
  722. command.Parameters.AddWithValue("@userInfoUserIP", userInfo_ip);
  723. command.Parameters.AddWithValue("@utcDate", utcDate);
  724. command.Parameters.AddWithValue("@earning", earning);
  725.  
  726. command.ExecuteNonQuery();
  727.  
  728. }
  729. }
  730. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement