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

# Scrapy settings for scrapy_spiders project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
#     https://doc.scrapy.org/en/latest/topics/settings.html
#     https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
#     https://doc.scrapy.org/en/latest/topics/spider-middleware.html

BOT_NAME = 'scrapy_spiders'

SPIDER_MODULES = ['scrapy_spiders.spiders']
NEWSPIDER_MODULE = 'scrapy_spiders.spiders'


# Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = 'scrapy_spiders (+http://www.yourdomain.com)'

# Obey robots.txt rules
ROBOTSTXT_OBEY = False

# Configure maximum concurrent requests performed by Scrapy (default: 16)
CONCURRENT_REQUESTS = 16

# Configure a delay for requests for the same website (default: 0)
# See https://doc.scrapy.org/en/latest/topics/settings.html#download-delay
# See also autothrottle settings and docs
DOWNLOAD_DELAY = 0.25
# The download delay setting will honor only one of:
CONCURRENT_REQUESTS_PER_DOMAIN = 16
#CONCURRENT_REQUESTS_PER_IP = 16

# Disable cookies (enabled by default)
#COOKIES_ENABLED = False
# COOKIES_DEBUG = True

# Disable Telnet Console (enabled by default)
#TELNETCONSOLE_ENABLED = False

# Override the default request headers:
# DEFAULT_REQUEST_HEADERS = {
#   'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
#   'Accept-Language': 'en',
#   'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36'
# }

# Enable or disable spider middlewares
# See https://doc.scrapy.org/en/latest/topics/spider-middleware.html
SPIDER_MIDDLEWARES = {
    'scrapy_splash.SplashDeduplicateArgsMiddleware': 100,
}

# Enable or disable downloader middlewares
# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
DOWNLOADER_MIDDLEWARES = {
    'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware': None,
    'scrapy_spiders.middlewares.RandomUserAgentMiddleware': 543,
    'scrapy_splash.SplashCookiesMiddleware': 723,
    'scrapy_splash.SplashMiddleware': 725,
    'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware': 810,
}

# Enable or disable extensions
# See https://doc.scrapy.org/en/latest/topics/extensions.html
#EXTENSIONS = {
#    'scrapy.extensions.telnet.TelnetConsole': None,
#}

# Configure item pipelines
# See https://doc.scrapy.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
   'scrapy_spiders.pipelines.MyImagePipeline': 300,
   'scrapy_spiders.pipelines.MongodbPipeline': 320,
}

# Enable and configure the AutoThrottle extension (disabled by default)
# See https://doc.scrapy.org/en/latest/topics/autothrottle.html
#AUTOTHROTTLE_ENABLED = True
# The initial download delay
#AUTOTHROTTLE_START_DELAY = 5
# The maximum download delay to be set in case of high latencies
#AUTOTHROTTLE_MAX_DELAY = 60
# The average number of requests Scrapy should be sending in parallel to
# each remote server
#AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
# Enable showing throttling stats for every response received:
#AUTOTHROTTLE_DEBUG = False

# Enable and configure HTTP caching (disabled by default)
# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
HTTPCACHE_ENABLED = False
# HTTPCACHE_EXPIRATION_SECS = 0
# HTTPCACHE_DIR = 'httpcache'
#HTTPCACHE_IGNORE_HTTP_CODES = []
#HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'


# 日志的配置
# LOG_ENABLED = True
LOG_ENCODING = 'utf-8'
# # LOG_FILE = "BaiduSpider.log"
# LOG_LEVEL = "DEBUG"
# LOG_STDOUT = False
# 修改日志输出的格式
LOG_FORMAT = '%(asctime)s [%(threadName)s %(thread)d] [%(name)s] %(levelname)s: %(message)s'

# mongodb数据库的配置
MONGO_URI = '127.0.0.1:27017'
MONGO_DATABASE = 'spider'
MONGO_COLLECTION = 'lol_fail'
# MONGO_COLLECTION = 'circ'

# 设置redis数据库
REDIS_URL = "redis://127.0.0.1:6379"


# 设置Splash的地址
SPLASH_URL = 'http://192.168.137.136:8050/'
# 设置Splash的去重过滤器
DUPEFILTER_CLASS = 'scrapy_splash.SplashAwareDupeFilter'
# 设置Splash的Http缓存
HTTPCACHE_STORAGE = 'scrapy_splash.SplashAwareFSCacheStorage'


# 关掉重定向, 不会重定向到新的地址
# REDIRECT_ENABLED = False
# 返回301, 302时, 按正常返回对待, 可以正常写入cookie
# HTTPERROR_ALLOWED_CODES = [301, 302]

# 重试 配置
# 用于开启中间件，默认为TRUE
RETRY_ENABLED = True
# 重试次数, 默认为2
RETRY_TIMES = 3
# 遇到哪些返回状态码需要重试, 一个列表，默认为[500, 503, 504, 400, 408]
RETRY_HTTP_CODES = [500, 503, 504, 400, 408]

# scrapy-redis的配置
# DUPEFILTER_CLASS = "scrapy_redis.dupefilter.RFPDupeFilter"
# SCHEDULER = "scrapy_redis.scheduler.Scheduler"
# SCHEDULER_PERSIST = True


# 图片存储位置
IMAGES_STORE = 'E:\scrapy_spiders\scrapy_spiders\img'
# 过期天数
IMAGES_EXPIRES = 90  # 90天内抓取的都不会被重抓
# 图片下载结果的item键
# IMAGES_RESULT_FIELD = 'result'
