33
44__all__ = ('UpdatesParser' , 'UpdatesParsingOptions' )
55
6+ import json
7+ from typing import Any , cast
68from dataclasses import dataclass
79
810from funpayparsers .types .enums import UpdateType
911from funpayparsers .parsers .base import ParsingOptions , FunPayJSONObjectParser
12+ from funpayparsers .types .common import CurrentlyViewingOfferInfo
1013from funpayparsers .types .updates import (
1114 ChatNode ,
1215 NodeInfo ,
1619 ChatBookmarks ,
1720 ActionResponse ,
1821 OrdersCounters ,
19- CurrentlyViewingOfferInfo ,
2022)
2123from funpayparsers .parsers .cpu_parser import (
2224 CurrentlyViewingOfferInfoParser ,
@@ -82,44 +84,43 @@ def _parse(self) -> UpdatesPack:
8284 response = None ,
8385 )
8486
85- action_response = self .data .get ('response' )
87+ action_response = self .data .get ('response' ) # type: ignore[union-attr] # raise if not dict
8688 if action_response :
8789 updates_obj .response = self ._parse_action_response (action_response )
8890
89- objects = self .data .get ('objects' )
91+ objects = self .data .get ('objects' ) # type: ignore[union-attr] # raise if not dict
9092 if not objects :
9193 return updates_obj
9294
93- unknown_objects = []
94- nodes = {}
9595 for obj in objects :
9696 result = self ._parse_update (obj )
9797 if result is None :
98- unknown_objects .append (obj )
98+ updates_obj . unknown_objects .append (obj ) # type: ignore[union-attr]
9999 elif result .type is UpdateType .CHAT_NODE :
100- updates_obj .nodes .append (result )
100+ updates_obj .nodes .append (result ) # type: ignore[union-attr]
101101 else :
102102 setattr (updates_obj , self .__update_fields__ [result .type ], result )
103103
104- updates_obj .unknown_objects = unknown_objects or updates_obj .unknown_objects
105- updates_obj .nodes = nodes or updates_obj .nodes
104+ updates_obj .nodes = updates_obj .nodes or None
105+ updates_obj .unknown_objects = updates_obj .unknown_objects or None
106+
106107 return updates_obj
107108
108- def _parse_orders_counters (self , obj : dict ) -> OrdersCounters :
109+ def _parse_orders_counters (self , obj : dict [ str , Any ] ) -> OrdersCounters :
109110 return OrdersCounters (
110- raw_source = str (obj ),
111- purchases = int (obj .get ('buyer' )) if obj .get ('seller' ) else 0 ,
112- sales = int (obj .get ('seller' )) if obj .get ('seller' ) else 0 ,
111+ raw_source = json . dumps (obj , ensure_ascii = False ),
112+ purchases = int (cast ( str , obj .get ('buyer' ) )) if obj .get ('seller' ) else 0 ,
113+ sales = int (cast ( str , obj .get ('seller' ) )) if obj .get ('seller' ) else 0 ,
113114 )
114115
115- def _parse_chat_counter (self , obj : dict ) -> ChatCounter :
116+ def _parse_chat_counter (self , obj : dict [ str , Any ] ) -> ChatCounter :
116117 return ChatCounter (
117118 raw_source = str (obj ),
118119 counter = int (obj ['counter' ]),
119120 message = int (obj ['message' ]),
120121 )
121122
122- def _parse_chat_bookmarks (self , obj : dict ) -> ChatBookmarks :
123+ def _parse_chat_bookmarks (self , obj : dict [ str , Any ] ) -> ChatBookmarks :
123124 return ChatBookmarks (
124125 raw_source = str (obj ),
125126 counter = int (obj ['counter' ]),
@@ -131,14 +132,14 @@ def _parse_chat_bookmarks(self, obj: dict) -> ChatBookmarks:
131132 ).parse (),
132133 )
133134
134- def _parse_cpu (self , obj : dict ) -> CurrentlyViewingOfferInfo :
135+ def _parse_cpu (self , obj : dict [ str , Any ] ) -> CurrentlyViewingOfferInfo :
135136 html_ = obj ['html' ]['desktop' ]
136137 return CurrentlyViewingOfferInfoParser (
137138 html_ ,
138139 options = self .options .cpu_parsing_options ,
139140 ).parse ()
140141
141- def _parse_node (self , obj : dict ) -> ChatNode :
142+ def _parse_node (self , obj : dict [ str , Any ] ) -> ChatNode :
142143 node_obj = obj ['node' ]
143144 node_info = NodeInfo (
144145 raw_source = str (node_obj ),
@@ -160,14 +161,14 @@ def _parse_node(self, obj: dict) -> ChatNode:
160161 has_history = obj ['hasHistory' ],
161162 )
162163
163- def _parse_action_response (self , obj : dict ) -> ActionResponse :
164+ def _parse_action_response (self , obj : dict [ str , Any ] ) -> ActionResponse :
164165 return ActionResponse (
165166 raw_source = str (obj ),
166167 error = obj .get ('error' ),
167168 )
168169
169- def _parse_update (self , update_dict : dict ) -> UpdateObject | None :
170- update_type = UpdateType .get_by_type_str (update_dict .get ('type' ))
170+ def _parse_update (self , update_dict : dict [ str , Any ] ) -> UpdateObject [ Any ] | None :
171+ update_type = UpdateType .get_by_type_str (cast ( str , update_dict .get ('type' ) ))
171172 if update_type not in self .__parsing_methods__ :
172173 return None
173174
0 commit comments