Skip to content

Commit

Permalink
fix push json (rmax#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
songhao8080 authored and LuckyPigeon committed Apr 5, 2022
1 parent eab4ace commit d423083
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/scrapy_redis/spiders.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import time

from . import connection, defaults
from .utils import bytes_to_str
from .utils import bytes_to_str, is_dict


class RedisMixin(object):
Expand Down Expand Up @@ -171,7 +171,7 @@ def make_request_from_data(self, data):

# change to json array
parameter = {}
if type(formatted_data) == dict:
if is_dict(formatted_data):
parameter = json.loads(formatted_data)
else:
print(TextColor.WARNING + "WARNING: String request is deprecated, please use JSON data format. \
Expand Down
12 changes: 11 additions & 1 deletion src/scrapy_redis/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import json
from json import JSONDecodeError

import six


Expand All @@ -11,9 +14,16 @@ class TextColor:
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'

def bytes_to_str(s, encoding='utf-8'):
"""Returns a str if a bytes object is given."""
if six.PY3 and isinstance(s, bytes):
return s.decode(encoding)
return s

def is_dict(string_content):
try:
json.loads(string_content)
except JSONDecodeError:
return False
return True

0 comments on commit d423083

Please sign in to comment.