@@ -139,55 +139,56 @@ def next_requests(self):
139139 self .logger .debug ("Read %s requests from '%s'" , found , self .redis_key )
140140
141141 def make_request_from_data (self , data ):
142- """Returns a Request instance from data coming from Redis.
142+ """
143+ Returns a `Request` instance for data coming from Redis.
143144
144- Overriding this function to support the ' json' requested `` data` ` that contains
145+ Overriding this function to support the ` json` requested `data` that contains
145146 `url` ,`meta` and other optional parameters. `meta` is a nested json which contains sub-data.
146147
147148 Along with:
148- After accessing the data, sending the FormRequest with `url`, `meta` and addition `formdata`
149-
149+ After accessing the data, sending the FormRequest with `url`, `meta` and addition `formdata`, `method`
150150 For example:
151151 {
152152 "url": "https://exaple.com",
153153 "meta": {
154154 'job-id':'123xsd',
155155 'start-date':'dd/mm/yy'
156156 },
157- "url_cookie_key":"fertxsas"
157+ "url_cookie_key":"fertxsas",
158+ "method":"POST"
158159 }
159160
160- this data can be accessed from 'scrapy.spider' through response.
161- 'request.url', 'request.meta', 'request.cookies'
161+ If `url` is empty, return []. So you should verify the `url` in the data.
162+ If `method` is empty, the request object will set method to 'GET', optional.
163+ If `meta` is empty, the request object will set `meta` to {}, optional.
164+
165+ This json supported data can be accessed from 'scrapy.spider' through response.
166+ 'request.url', 'request.meta', 'request.cookies', 'request.method'
162167
163168 Parameters
164169 ----------
165170 data : bytes
166171 Message from redis.
167172
168173 """
169- # url = bytes_to_str(data, self.redis_encoding)
170174 formatted_data = bytes_to_str (data , self .redis_encoding )
171175
172- # change to json array
173- parameter = {}
174176 if is_dict (formatted_data ):
175177 parameter = json .loads (formatted_data )
176178 else :
177- print (TextColor .WARNING + "WARNING: String request is deprecated, please use JSON data format. \
179+ self . logger . warning (TextColor .WARNING + "WARNING: String request is deprecated, please use JSON data format. \
178180 Detail information, please check https://github.com/rmax/scrapy-redis#features" + TextColor .ENDC )
179181 return FormRequest (formatted_data , dont_filter = True )
180182
181- url = parameter ['url' ]
182- del parameter ['url' ]
183- metadata = {}
184- try :
185- metadata = parameter ['meta' ]
186- del parameter ['meta' ]
187- except KeyError as e :
188- print ('Failed to delete metadata: ' , e )
183+ if parameter .get ('url' , None ) is None :
184+ self .logger .warning (TextColor .WARNING + "The data from Redis has no url key in push data" + TextColor .ENDC )
185+ return []
186+
187+ url = parameter .pop ("url" )
188+ method = parameter .pop ("method" ).upper () if "method" in parameter else "GET"
189+ metadata = parameter .pop ("meta" ) if "meta" in parameter else {}
189190
190- return FormRequest (url , dont_filter = True , formdata = parameter , meta = metadata )
191+ return FormRequest (url , dont_filter = True , method = method , formdata = parameter , meta = metadata )
191192
192193 def schedule_next_requests (self ):
193194 """Schedules a request if available"""
0 commit comments