diff --git a/spacy/cli/download.py b/spacy/cli/download.py index 656635f79..977545dfe 100644 --- a/spacy/cli/download.py +++ b/spacy/cli/download.py @@ -2,13 +2,14 @@ from __future__ import unicode_literals import plac -import requests import os import subprocess import sys +import ujson from .link import link from ..util import prints, get_package_path +from ..compat import url_open, url_error from .. import about @@ -56,13 +57,14 @@ def download(model, direct=False): def get_json(url, desc): - r = requests.get(url) - if r.status_code != 200: + try: + r = url_open(url) + except url_error as e: msg = ("Couldn't fetch %s. Please find a model for your spaCy " "installation (v%s), and download it manually.") prints(msg % (desc, about.__version__), about.__docs_models__, - title="Server error (%d)" % r.status_code, exits=1) - return r.json() + title="Server error (%d: %s)" % (e.code, e.reason), exits=1) + return ujson.load(r) def get_compatibility(): diff --git a/spacy/cli/validate.py b/spacy/cli/validate.py index b83753509..2b7388988 100644 --- a/spacy/cli/validate.py +++ b/spacy/cli/validate.py @@ -1,12 +1,12 @@ # coding: utf8 from __future__ import unicode_literals, print_function -import requests import pkg_resources from pathlib import Path import sys +import ujson -from ..compat import path2str, locale_escape +from ..compat import path2str, locale_escape, url_open, url_error from ..util import prints, get_data_path, read_json from .. import about @@ -15,11 +15,12 @@ def validate(): """Validate that the currently installed version of spaCy is compatible with the installed models. Should be run after `pip install -U spacy`. """ - r = requests.get(about.__compatibility__) - if r.status_code != 200: + try: + r = url_open(about.__compatibility__) + except url_error as e: prints("Couldn't fetch compatibility table.", - title="Server error (%d)" % r.status_code, exits=1) - compat = r.json()['spacy'] + title="Server error (%d: %s)" % (e.code, e.reason), exits=1) + compat = ujson.load(r)['spacy'] current_compat = compat.get(about.__version__) if not current_compat: prints(about.__compatibility__, exits=1,