1
+ using System . Collections . Generic ;
2
+ using System . Configuration ;
3
+ using System . Linq ;
4
+ using System . Net ;
5
+ using System . Threading . Tasks ;
6
+ using AngleSharp ;
7
+ using AngleSharp . Dom ;
8
+ using Configuration = AngleSharp . Configuration ;
9
+
10
+ namespace TenderView . Trades . TradeDetail
11
+ {
12
+ public static class TradeDetailWebReceiver
13
+ {
14
+ private const string DeliveryAddressString = "Место поставки:" ;
15
+ private const string PurchaseObjectNameString = "Наименование товара, работ, услуг:" ;
16
+ private const string PurchaseObjectUnitNameString = "Единицы измерения:" ;
17
+ private const string PurchaseObjectQuantityString = "Количество:" ;
18
+ private const string PurchaseObjectPriceForOneString = "Стоимость единицы продукции ( в т.ч. НДС при наличии):" ;
19
+
20
+ private const string InfoBlockSelector =
21
+ "div.informationAboutCustomer__informationPurchase-infoBlock.infoBlock" ;
22
+
23
+ private const string PurchaseObjectSelector = "div.outputResults__oneResult" ;
24
+
25
+ private const string PurchaseObjectNameAndCodeAndTypeSelector =
26
+ "div.outputResults__oneResult-leftPart.leftPart" ;
27
+
28
+ private const string PurchaseObjectUnitNameAndQuantitySelector =
29
+ "div.outputResults__oneResult-centerPart.centerPart" ;
30
+
31
+ private const string PurchaseObjectPriceForOneSelector =
32
+ "div.outputResults__oneResult-rightPart.rightPart" ;
33
+
34
+
35
+ public static async Task < TradeDetail > GetAsync ( long tradeId )
36
+ {
37
+ var url = ConfigureAndGetUrl ( tradeId ) ;
38
+ var context = BrowsingContext . New ( Configuration . Default . WithDefaultLoader ( ) ) ;
39
+ var document = await context . OpenAsync ( url ) ;
40
+ var tradeDetail = new TradeDetail ( ParseDeliveryAddress ( document ) , ParsePurchaseObject ( document ) ) ;
41
+
42
+ return tradeDetail ;
43
+ }
44
+
45
+ private static string ConfigureAndGetUrl ( long tradeId )
46
+ {
47
+ var url = ConfigurationManager . AppSettings [ "GetTradesDetail" ] ;
48
+ if ( string . IsNullOrEmpty ( url ) )
49
+ throw new WebException ( "Configuration string null or empty" ) ;
50
+
51
+ return url . Replace ( "%tradeId%" , tradeId . ToString ( ) ) ;
52
+ }
53
+
54
+
55
+ private static List < PurchaseObject > ParsePurchaseObject ( IDocument document )
56
+ {
57
+ var cells = document . QuerySelectorAll ( PurchaseObjectSelector ) ;
58
+ var purchaseObjects = new List < PurchaseObject > ( ) ;
59
+ foreach ( var cell in cells )
60
+ {
61
+ var nameAndCodeAndTypeElement = cell . QuerySelector ( PurchaseObjectNameAndCodeAndTypeSelector ) ;
62
+ var unitNameAndQuantityElement = cell . QuerySelector ( PurchaseObjectUnitNameAndQuantitySelector ) ;
63
+ var priceForOneElement = cell . QuerySelector ( PurchaseObjectPriceForOneSelector ) ;
64
+
65
+ var name = ParsePurchaseObjectName ( nameAndCodeAndTypeElement ) ;
66
+ var unitName = ParsePurchaseObjectUnitName ( unitNameAndQuantityElement ) ;
67
+ var quantity = decimal . Parse ( ParsePurchaseObjectQuantity ( unitNameAndQuantityElement ) ) ;
68
+ var priceForOne = decimal . Parse ( ParsePurchaseObjectPriceForOne ( priceForOneElement ) ) ;
69
+
70
+
71
+ name = TrimStartAndEnd ( name ) ;
72
+ unitName = TrimStartAndEnd ( unitName ) ;
73
+
74
+ purchaseObjects . Add ( new PurchaseObject ( name , unitName , quantity , priceForOne ) ) ;
75
+ }
76
+
77
+ return purchaseObjects ;
78
+ }
79
+
80
+ private static string TrimStartAndEnd ( string text )
81
+ {
82
+ return text . TrimStart ( ) . TrimEnd ( ) ;
83
+ }
84
+
85
+ private static string ParseDeliveryAddress ( IDocument document )
86
+ {
87
+ var cells = document . QuerySelectorAll ( InfoBlockSelector ) ;
88
+
89
+ return ( from cell in cells
90
+ where cell . Children [ 0 ] . TextContent == DeliveryAddressString
91
+ select cell . Children [ 1 ] . TextContent ) . FirstOrDefault ( ) ;
92
+ }
93
+
94
+ private static string ParsePurchaseObjectName ( IElement element )
95
+ {
96
+ return ( from child in element . Children
97
+ where child . Children . Length > 0
98
+ where child . Children [ 0 ] . TextContent . Equals ( PurchaseObjectNameString )
99
+ select child . ChildNodes [ 2 ] . TextContent ) . FirstOrDefault ( ) ;
100
+ }
101
+
102
+ private static string ParsePurchaseObjectUnitName ( IElement element )
103
+ {
104
+ return InnerContentFinder ( element , PurchaseObjectUnitNameString ) ;
105
+ }
106
+
107
+ private static string ParsePurchaseObjectQuantity ( IElement element )
108
+ {
109
+ return InnerContentFinder ( element , PurchaseObjectQuantityString ) ;
110
+ }
111
+
112
+ private static string ParsePurchaseObjectPriceForOne ( IElement element )
113
+ {
114
+ return InnerContentFinder ( element , PurchaseObjectPriceForOneString ) ;
115
+ }
116
+
117
+ private static string InnerContentFinder ( IElement element , string nameString )
118
+ {
119
+ return ( from child in element . Children
120
+ where child . Children . Length > 0
121
+ from childParag in child . Children
122
+ where childParag . Children . Length > 0 && childParag . Children [ 0 ] . TextContent . Equals ( nameString )
123
+ select childParag . ChildNodes [ 2 ] . TextContent ) . FirstOrDefault ( ) ;
124
+ }
125
+ }
126
+ }
0 commit comments