mirror of https://github.com/scrapy/scrapy.git
added StatsMailer extension
This commit is contained in:
parent
7c2476bb25
commit
fd0e490157
|
|
@ -192,6 +192,18 @@ one.
|
|||
|
||||
.. _BeautifulSoup: http://www.crummy.com/software/BeautifulSoup/documentation.html
|
||||
|
||||
StatsMailer extension
|
||||
---------------------
|
||||
|
||||
.. module:: scrapy.contrib.statsmailer
|
||||
:synopsis: StatsMailer extension
|
||||
|
||||
.. class:: scrapy.contrib.statsmailer.StatsMailer
|
||||
|
||||
This simple extension can be used to send a notification email every time a
|
||||
domain has finished scraping, including the Scrapy stats collected. The email
|
||||
will be sent to all recipients specified in the :setting:`STATSMAILER_RCPTS`
|
||||
setting.
|
||||
|
||||
Web console extensions
|
||||
======================
|
||||
|
|
|
|||
|
|
@ -861,6 +861,16 @@ Default: ``True``
|
|||
|
||||
Enable stats collection.
|
||||
|
||||
.. setting:: STATSMAILER_RCPTS
|
||||
|
||||
STATSMAILER_RCPTS
|
||||
-----------------
|
||||
|
||||
Default: ``[]`` (empty list)
|
||||
|
||||
Send Scrapy stats after domains finish scrapy. See
|
||||
:class:`~scrapy.contrib.statsmailer.StatsMailer` for more info.
|
||||
|
||||
.. setting:: TELNETCONSOLE_ENABLED
|
||||
|
||||
TELNETCONSOLE_ENABLED
|
||||
|
|
|
|||
|
|
@ -189,6 +189,8 @@ STATS_DEBUG = False
|
|||
STATS_WSPORT = 8089
|
||||
STATS_WSTIMEOUT = 15
|
||||
|
||||
STATSMAILER_RCPTS = []
|
||||
|
||||
TEMPLATES_DIR = abspath(join(dirname(__file__), '..', 'templates'))
|
||||
|
||||
URLLENGTH_LIMIT = 2083
|
||||
|
|
|
|||
|
|
@ -0,0 +1,28 @@
|
|||
"""
|
||||
StatsMailer extension sends an email when a domain finishes scraping.
|
||||
|
||||
Use STATSMAILER_RCPTS setting to enable and give the recipient mail address
|
||||
"""
|
||||
|
||||
import pprint
|
||||
|
||||
from pydispatch import dispatcher
|
||||
|
||||
from scrapy.stats import stats
|
||||
from scrapy.mail import MailSender
|
||||
from scrapy.conf import settings
|
||||
from scrapy.core import signals
|
||||
from scrapy.core.exceptions import NotConfigured
|
||||
|
||||
class StatsMailer(object):
|
||||
|
||||
def __init__(self):
|
||||
self.recipients = settings.getlist("STATSMAILER_RCPTS")
|
||||
if not self.recipients:
|
||||
raise NotConfigured
|
||||
dispatcher.connect(self.send_stats, signal=signals.domain_closed)
|
||||
|
||||
def send_stats(self, domain):
|
||||
mail = MailSender()
|
||||
body = pprint.pformat(stats[domain])
|
||||
mail.send(self.recipients, "Scrapy stats for: %s" % domain, body)
|
||||
Loading…
Reference in New Issue