# -*- coding: utf-8 -*-


try:
    from urllib.parse import urlparse
except ImportError:
    from urlparse import urlparse
from collections import namedtuple

import redis

from scrapy.utils.project import get_project_settings

RedisConf = namedtuple('RedisUrl', ['host', 'port', 'database'])
settings = get_project_settings()


def parse_redis_url(url):
    parser = urlparse(url)
    host = parser.hostname
    port = parser.port
    db = int(parser.path[1:])
    return RedisConf(host, port, db)


def flush_failed_spider_db():
    conf = parse_redis_url(settings['RETRY_SPIDERS_URL'])
    r = redis.Redis(host=conf.host, port=conf.port, db=conf.database)
    r.flushdb()


def save_failed_spider(sp):
    conf = parse_redis_url(settings['RETRY_SPIDERS_URL'])
    r = redis.Redis(host=conf.host, port=conf.port, db=conf.database)
    r.rpush('spider', sp)


def get_failed_spiders():
    conf = parse_redis_url(settings['RETRY_SPIDERS_URL'])
    r = redis.Redis(host=conf.host, port=conf.port, db=conf.database)
    return r.get('spider')

@@ -1004,24 +1004,96 @@
 f.database)%0A
+    l = r.llen('spider')%0A    if l == 0:%0A        return %5B%5D%0A    else:%0A    
     return r
@@ -1093,19 +1093,22 @@
 eturn r.
+lran
 ge
-t
 ('spider
@@ -1100,18 +1100,24 @@
 .lrange('spider'
+, 0, l
 )%0A

fix bug: redis get list with lrange