From 3f676108d96061742d04bae0f04d4b3544d364cf Mon Sep 17 00:00:00 2001 From: pnhekgfuf <1913997697@qq.com> Date: Sat, 29 Apr 2023 12:16:55 +0800 Subject: [PATCH] ADD file via upload --- Scrapy-Redis-Zhihu/scrapy_redis/connection.py | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 Scrapy-Redis-Zhihu/scrapy_redis/connection.py diff --git a/Scrapy-Redis-Zhihu/scrapy_redis/connection.py b/Scrapy-Redis-Zhihu/scrapy_redis/connection.py new file mode 100644 index 0000000..4426559 --- /dev/null +++ b/Scrapy-Redis-Zhihu/scrapy_redis/connection.py @@ -0,0 +1,90 @@ +import six + +from scrapy.utils.misc import load_object + +from . import defaults + + +# Shortcut maps 'setting name' -> 'parmater name'. +SETTINGS_PARAMS_MAP = { + 'REDIS_URL': 'url', + 'REDIS_HOST': 'host', + 'REDIS_PORT': 'port', + 'REDIS_ENCODING': 'encoding', +} + + +def get_redis_from_settings(settings): + """Returns a redis client instance from given Scrapy settings object. + + This function uses ``get_client`` to instantiate the client and uses + ``defaults.REDIS_PARAMS`` global as defaults values for the parameters. You + can override them using the ``REDIS_PARAMS`` setting. + + Parameters + ---------- + settings : Settings + A scrapy settings object. See the supported settings below. + + Returns + ------- + server + Redis client instance. + + Other Parameters + ---------------- + REDIS_URL : str, optional + Server connection URL. + REDIS_HOST : str, optional + Server host. + REDIS_PORT : str, optional + Server port. + REDIS_ENCODING : str, optional + Data encoding. + REDIS_PARAMS : dict, optional + Additional client parameters. + + """ + params = defaults.REDIS_PARAMS.copy() + params.update(settings.getdict('REDIS_PARAMS')) + # XXX: Deprecate REDIS_* settings. + for source, dest in SETTINGS_PARAMS_MAP.items(): + val = settings.get(source) + if val: + params[dest] = val + + # Allow ``redis_cls`` to be a path to a class. + if isinstance(params.get('redis_cls'), six.string_types): + params['redis_cls'] = load_object(params['redis_cls']) + + return get_redis(**params) + + +# Backwards compatible alias. +from_settings = get_redis_from_settings + + +def get_redis(**kwargs): + """Returns a redis client instance. + + Parameters + ---------- + redis_cls : class, optional + Defaults to ``redis.StrictRedis``. + url : str, optional + If given, ``redis_cls.from_url`` is used to instantiate the class. + **kwargs + Extra parameters to be passed to the ``redis_cls`` class. + + Returns + ------- + server + Redis client instance. + + """ + redis_cls = kwargs.pop('redis_cls', defaults.REDIS_CLS) + url = kwargs.pop('url', None) + if url: + return redis_cls.from_url(url, **kwargs) + else: + return redis_cls(**kwargs)