6
6
from msgpack import packb , unpackb
7
7
8
8
from frontera .core .codec import BaseDecoder , BaseEncoder
9
+ import six
9
10
from w3lib .util import to_native_str
10
11
11
12
12
13
def _prepare_request_message (request ):
13
- return [request .url , request .method , request .headers , request .cookies , request .meta ]
14
+ def serialize (obj ):
15
+ """Recursively walk object's hierarchy."""
16
+ if isinstance (obj , (bool , six .integer_types , float , six .binary_type , six .text_type )):
17
+ return obj
18
+ elif isinstance (obj , dict ):
19
+ obj = obj .copy ()
20
+ for key in obj :
21
+ obj [key ] = serialize (obj [key ])
22
+ return obj
23
+ elif isinstance (obj , list ):
24
+ return [serialize (item ) for item in obj ]
25
+ elif isinstance (obj , tuple ):
26
+ return tuple (serialize ([item for item in obj ]))
27
+ elif hasattr (obj , '__dict__' ):
28
+ return serialize (obj .__dict__ )
29
+ else :
30
+ return None
31
+ return [request .url , request .method , request .headers , request .cookies , serialize (request .meta )]
14
32
15
33
16
34
def _prepare_response_message (response , send_body ):
@@ -22,29 +40,29 @@ def __init__(self, request_model, *a, **kw):
22
40
self .send_body = True if 'send_body' in kw and kw ['send_body' ] else False
23
41
24
42
def encode_add_seeds (self , seeds ):
25
- return packb ([b'as' , [_prepare_request_message (seed ) for seed in seeds ]], use_bin_type = True , encoding = "utf-8" )
43
+ return packb ([b'as' , [_prepare_request_message (seed ) for seed in seeds ]], use_bin_type = True )
26
44
27
45
def encode_page_crawled (self , response ):
28
- return packb ([b'pc' , _prepare_response_message (response , self .send_body )], use_bin_type = True , encoding = "utf-8" )
46
+ return packb ([b'pc' , _prepare_response_message (response , self .send_body )], use_bin_type = True )
29
47
30
48
def encode_links_extracted (self , request , links ):
31
49
return packb ([b'le' , _prepare_request_message (request ), [_prepare_request_message (link ) for link in links ]],
32
- use_bin_type = True , encoding = "utf-8" )
50
+ use_bin_type = True )
33
51
34
52
def encode_request_error (self , request , error ):
35
- return packb ([b're' , _prepare_request_message (request ), str (error )], use_bin_type = True , encoding = "utf-8" )
53
+ return packb ([b're' , _prepare_request_message (request ), str (error )], use_bin_type = True )
36
54
37
55
def encode_request (self , request ):
38
- return packb (_prepare_request_message (request ), use_bin_type = True , encoding = "utf-8" )
56
+ return packb (_prepare_request_message (request ), use_bin_type = True )
39
57
40
58
def encode_update_score (self , request , score , schedule ):
41
- return packb ([b'us' , _prepare_request_message (request ), score , schedule ], use_bin_type = True , encoding = "utf-8" )
59
+ return packb ([b'us' , _prepare_request_message (request ), score , schedule ], use_bin_type = True )
42
60
43
61
def encode_new_job_id (self , job_id ):
44
- return packb ([b'njid' , int (job_id )], use_bin_type = True , encoding = "utf-8" )
62
+ return packb ([b'njid' , int (job_id )], use_bin_type = True )
45
63
46
64
def encode_offset (self , partition_id , offset ):
47
- return packb ([b'of' , int (partition_id ), int (offset )], use_bin_type = True , encoding = "utf-8" )
65
+ return packb ([b'of' , int (partition_id ), int (offset )], use_bin_type = True )
48
66
49
67
50
68
class Decoder (BaseDecoder ):
@@ -68,7 +86,7 @@ def _request_from_object(self, obj):
68
86
meta = obj [4 ])
69
87
70
88
def decode (self , buffer ):
71
- obj = unpackb (buffer , encoding = " utf-8" )
89
+ obj = unpackb (buffer , encoding = ' utf-8' )
72
90
if obj [0 ] == b'pc' :
73
91
return ('page_crawled' ,
74
92
self ._response_from_object (obj [1 ]))
@@ -89,4 +107,4 @@ def decode(self, buffer):
89
107
return TypeError ('Unknown message type' )
90
108
91
109
def decode_request (self , buffer ):
92
- return self ._request_from_object (unpackb (buffer , encoding = " utf-8" ))
110
+ return self ._request_from_object (unpackb (buffer , encoding = ' utf-8' ))
0 commit comments