Commit 23bbee82 authored by Benjamin Bouvier's avatar Benjamin Bouvier Committed by Phyks

Adds basic email notifications when new flats have been found (fixes #47).

parent 4c07fc8b
......@@ -9,6 +9,7 @@ import logging
import flatisfy.filters
from flatisfy import database
from flatisfy import email
from flatisfy.models import flat as flat_model
from flatisfy.models import postal_code as postal_code_model
from flatisfy.models import public_transport as public_transport_model
......@@ -143,6 +144,8 @@ def import_and_filter(config, load_from_db=False):
# Create database connection
get_session = database.init_db(config["database"], config["search_index"])
new_flats = []"Merging fetched flats in database...")
# Flatten the flats_by_status dict
flatten_flats_by_status = collections.defaultdict(list)
......@@ -182,8 +185,14 @@ def import_and_filter(config, load_from_db=False):
# just set the status field without worrying
for flat in flats_objects.values():
flat.status = getattr(flat_model.FlatStatus, status)
if flat.status ==
if config["send_email"]:
email.send_notification(config, new_flats)"Done!")
......@@ -62,6 +62,15 @@ DEFAULT_CONFIG = {
"webserver": None,
# List of Weboob backends to use (default to any backend available)
"backends": None,
# Should email notifications be sent?
"send_email": False,
"smtp_server": 'localhost',
"smtp_port": 25,
"smtp_from": "",
"smtp_to": [],
# The web site url, to be used in email notifications. (doesn't matter
# whether the trailing slash is present or not)
"website_url": ""
LOGGER = logging.getLogger(__name__)
......@@ -149,6 +158,11 @@ def validate_config(config):
assert config["webserver"] is None or isinstance(config["webserver"], str) # noqa: E501
assert config["backends"] is None or isinstance(config["backends"], list) # noqa: E501
assert isinstance(config["send_email"], bool)
assert config["smtp_server"] is None or isinstance(config["smtp_server"], (str, unicode))
assert config["smtp_port"] is None or isinstance(config["smtp_port"], int)
assert config["smtp_to"] is None or isinstance(config["smtp_to"], list)
return True
except (AssertionError, KeyError):
_, _, exc_traceback = sys.exc_info()
......@@ -242,6 +256,11 @@ def load_config(args=None):
if k in constraints_filter
# Sanitize website url
if config_data["website_url"] is not None:
if config_data["website_url"][-1] != '/':
config_data["website_url"] += '/'
config_validation = validate_config(config_data)
if config_validation is True:"Config has been fully initialized.")
# coding: utf-8
Email notifications.
from __future__ import absolute_import, print_function, unicode_literals
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def send_email(server, port, subject, _from, to, txt, html):
if len(to) == 0:
LOGGER.warn("No recipients for the email notifications, aborting.")
server = smtplib.SMTP(server, port)
msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = _from
msg['To'] = ', '.join(to)
msg.attach(MIMEText(txt, 'plain', 'utf-8'))
msg.attach(MIMEText(html, 'html', 'utf-8'))
server.sendmail(_from, to, msg.as_string())
def send_notification(config, flats):
# Don't send an email if there are no new flats.
if len(flats) == 0:
txt = u'Hello dear user,\n\nThe following new flats have been found:\n\n'
html = """
<p>Hello dear user!</p>
<p>The following new flats have been found:
website_url = config["website_url"]
for flat in flats:
title = unicode(flat.title)
flat_id = unicode(
area = unicode(flat.area)
cost = unicode(flat.cost)
currency = unicode(flat.currency)
txt += '- {}: {}#/flat/{} (area: {}, cost: {} {})\n' \
.format(title, website_url, flat_id, area, cost, currency)
html += """
<a href="{}#/flat/{}">{}</a>
(area: {}, cost: {} {})
""".format(website_url, flat_id, title, area, cost, currency)
html += "</ul>"
SIGNATURE = u"\nHope you'll find what you were looking for.\n\nBye!\nFlatisfy"
html += SIGNATURE.replace('\n', '<br>')
html += """</p>
"New flats found!",
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment