Skip to content

Commit 8331e05

Browse files
committed
fixed bug in exception handling & string check
1 parent 469b605 commit 8331e05

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

scrapeops_scrapy/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.3.3"
1+
__version__ = "0.3.4"

scrapeops_scrapy/stats/logger.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,15 @@ def log_levels(self, crawler):
176176
self.set_value(self._overall_stats, log_key, log_value)
177177

178178

179+
def exception_type_check(key):
180+
if isinstance(key, str):
181+
return key.startswith('downloader/exception_type_count/')
182+
return False
183+
179184
def get_exception_stats(self, crawler):
180185
scrapy_stats = crawler.stats.get_stats()
181186
if scrapy_stats.get('downloader/exception_count') is not None:
182-
exception_values = [ {k:v} for k,v in scrapy_stats.items() if k.startswith('downloader/exception_type_count/')]
187+
exception_values = [ {k:v} for k,v in scrapy_stats.items() if self.exception_type_check(k)]
183188
for exception in exception_values:
184189
for key, value in exception.items():
185190
key_type = key.replace('downloader/exception_type_count/', '')
@@ -189,8 +194,6 @@ def get_exception_stats(self, crawler):
189194
exception_type = key_type
190195
self.set_value(self._overall_stats, f'responses|unknown|unknown|unknown|unknown|unknown|{exception_type}|unknown|unknown|unknown|unknown|count', value)
191196

192-
193-
194197

195198

196199

scrapeops_scrapy/validators/response_validator.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ def run_validation_test(response, test_array):
3636
else: return True
3737

3838
if test.get('test_type') == 'response_length_check':
39-
if ResponseValidator.response_length_check(response, test.get('threshold', 0), test.get('comparison_type')):
39+
if ResponseValidator.response_length_check(ResponseValidator.get_response_text(response), test.get('threshold', 0), test.get('comparison_type')):
4040
fail_counter += 1
4141
else: return True
4242

4343
if test.get('test_type') == 'string_check' and test.get('test_location') == 'body':
44-
if ResponseValidator.string_check(response, test.get('text_check', ''), test.get('comparison_type'), text_slice=test.get('text_slice')):
44+
if ResponseValidator.string_check(ResponseValidator.get_response_text(response), test.get('text_check', ''), test.get('comparison_type'), text_slice=test.get('text_slice')):
4545
fail_counter += 1
4646
else: return True
4747

@@ -89,9 +89,16 @@ def failed_scan(request_response_object, domains):
8989

9090

9191
@staticmethod
92-
def string_check(response, text_check, comparison, text_slice=None):
93-
if text_check == '': return False
94-
text = response.text if isinstance(response, Response) else response
92+
def get_response_text(response):
93+
try:
94+
if isinstance(response, Response): return response.text
95+
else: return ''
96+
except AttributeError:
97+
return ''
98+
99+
100+
@staticmethod
101+
def string_check(text, text_check, comparison, text_slice=None):
95102
if text_slice is not None:
96103
text = ResponseValidator.string_slice(text, text_slice)
97104
if comparison == 'contains' and text_check in text:
@@ -100,6 +107,7 @@ def string_check(response, text_check, comparison, text_slice=None):
100107
return True
101108
return False
102109

110+
103111
@staticmethod
104112
def string_slice(text, text_slice):
105113
if text_slice.get('active'):
@@ -155,9 +163,9 @@ def bytes_check(response, threshold, comparison):
155163

156164

157165
@staticmethod
158-
def response_length_check(response, threshold, comparison):
166+
def response_length_check(text, threshold, comparison):
159167
if threshold == 0: return False
160-
response_text_length = len(response.text)
168+
response_text_length = len(text)
161169
return ResponseValidator.comparison_operators(response_text_length, threshold, comparison)
162170

163171

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from setuptools import setup, find_packages
22

33

4-
VERSION = '0.3.3'
4+
VERSION = '0.3.4'
55
DESCRIPTION = 'Scrapeops Scrapy SDK, is a monitoring tool for your Scrapy spiders.'
66

77
setup(name='scrapeops_scrapy',

0 commit comments

Comments
 (0)