1+ __all__ = ('OrderPreviewsParserOptions' , 'OrderPreviewsParser' , )
2+
3+ from dataclasses import dataclass
4+
5+ from funpayparsers .parsers .base import FunPayObjectParserOptions , FunPayObjectParser
6+ from funpayparsers .parsers .utils import extract_css_url
7+ from funpayparsers .types .orders import OrderPreview , OrderCounterpartyInfo
8+ from funpayparsers .types .enums import OrderStatus
9+ from funpayparsers .types .common import MoneyValue
10+
11+ from lxml import html
12+
13+
14+ @dataclass (frozen = True )
15+ class OrderPreviewsParserOptions (FunPayObjectParserOptions ):
16+ ...
17+
18+
19+ class OrderPreviewsParser (FunPayObjectParser [
20+ list [OrderPreview ],
21+ OrderPreviewsParserOptions
22+ ]):
23+ options_cls : OrderPreviewsParserOptions = OrderPreviewsParserOptions
24+
25+ def _parse (self ):
26+ result = []
27+
28+ for o in self .tree .xpath ('//a[contains(@class, "tc-item")]' ):
29+ order_id : str = o .get ('href' ).split ('/' )[- 2 ]
30+ date : str = o .xpath ('string(.//div[@class="tc-date-time"][1])' )
31+
32+ desc = o .xpath ('string(.//div[@class="order-desc"][1]/div[1])' )
33+ category_desc = o .xpath ('string(.//div[@class="text-muted"][1])' )
34+
35+ status_class = o .xpath ('string(.//div[contains(@class, "tc-status")][1]/@class)' )
36+ status = OrderStatus .get_by_css_class (status_class )
37+
38+ val_str = o .xpath ('string(.//div[contains(@class, "tc-price")])' ).strip ()
39+ val , char = val_str .split ()
40+ value = MoneyValue (raw_source = val_str , value = float (val ), character = char )
41+
42+ user_tag = o .xpath ('.//div[contains(@class, "media-user")][1]' )[0 ]
43+ photo_style = o .xpath (
44+ 'string(.//div[contains(@class, "avatar-photo")]/@style)' )
45+ nickname_tag = user_tag .xpath ('.//div[@class="media-user-name"]/span[1]' )[0 ]
46+ user_nickname = nickname_tag .text
47+ user_id = int (nickname_tag .get ('data-href' ).split ('/' )[- 2 ])
48+ online = 'online' in user_tag .get ('class' )
49+ banned = 'banned' in user_tag .get ('class' )
50+ user_status_text : str = user_tag .xpath (
51+ 'string(.//div[contains(@class, "media-user-status")][1])'
52+ )
53+ photo_url = extract_css_url (photo_style )
54+
55+ counterparty = OrderCounterpartyInfo (
56+ raw_source = html .tostring (user_tag , encoding = 'unicode' ),
57+ id = user_id ,
58+ username = user_nickname ,
59+ online = online ,
60+ avatar_url = photo_url ,
61+ banned = banned ,
62+ status_text = user_status_text
63+ )
64+
65+ order_obj = OrderPreview (
66+ raw_source = html .tostring (o , encoding = 'unicode' ),
67+ id = order_id ,
68+ date_text = date ,
69+ desc = desc ,
70+ category_text = category_desc ,
71+ status = status ,
72+ amount = value ,
73+ counterparty = counterparty
74+ )
75+ result .append (order_obj )
76+
77+ return result
0 commit comments