Skip to content

Handle pre- and post- scrapy 1.0 modules #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions scrapy_jsonrpc/jsonrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
import traceback
from six.moves import urllib

from scrapy.utils.python import unicode_to_str
# Scrapy >= 1.0
try:
from scrapy.utils.python import to_bytes
# Scrapy 0.24 and below
except ImportError:
from scrapy.utils.python import unicode_to_str as to_bytes
from scrapy_jsonrpc.serialize import ScrapyJSONDecoder


Expand Down Expand Up @@ -37,7 +42,7 @@ def jsonrpc_client_call(url, method, *args, **kwargs):
if args and kwargs:
raise ValueError("Pass *args or **kwargs but not both to jsonrpc_client_call")
req = {'jsonrpc': '2.0', 'method': method, 'params': args or kwargs, 'id': 1}
data = unicode_to_str(json.dumps(req))
data = to_bytes(json.dumps(req))
body = urllib.request.urlopen(url, data).read()
res = json.loads(body.decode('utf-8'))
if 'result' in res:
Expand Down
7 changes: 6 additions & 1 deletion scrapy_jsonrpc/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
import six
from twisted.internet import defer

from scrapy.spiders import Spider
# Scrapy >= 1.0
try:
from scrapy.spiders import Spider
# Scrapy 0.24 and below
except ImportError:
from scrapy.spider import Spider
from scrapy.http import Request, Response
from scrapy.item import BaseItem

Expand Down