-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathxenotifier.py
executable file
·86 lines (78 loc) · 2.52 KB
/
xenotifier.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
import smtplib
from datetime import datetime
import requests
from bs4 import BeautifulSoup as bs
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
import os
#
# URL απο το website της ΧΕ με οτι filters είναι αναγκαία
#
URL = os.environ.get('XE_URL')
# gmail username
UNAME = os.environ.get('GMAIL_USERNAME')
# gmail password
PWD = os.environ.get('GMAIL_PWD')
# sender address
FROM = '[email protected]'
# comma separated list of recipients
TO = os.environ.get('XE_RECIPIENTS')
soup = bs(requests.get(URL).content, 'lxml')
houses = soup.findAll('div', class_='lazy r')
houses_list = []
with open(
os.path.join(
os.path.dirname(os.path.abspath(__file__)),
'seen_houses.txt'
),
'ra+'
) as logfile:
seen = logfile.read().splitlines()
for house in houses:
price = 'Not Available'
date_posted = 'Not Available'
if house.find('li', class_='r_price'):
price = house.find('li', class_='r_price').text.encode('utf-8')
if house.find('p', class_='r_date'):
date_posted = house.find('p', class_='r_date').text.encode('utf-8')
url = house.find(href=re.compile('enoikiaseis')).attrs['href']
if url not in seen:
logfile.write(url+'\n')
houses_list.append({
'url': url,
'price': price,
'description': house.find('p').text.encode('utf-8'),
'date_posted': date_posted,
'tm': [
li for li in house.findAll('li') if u' τ.μ.' in li.text
][0].text.encode('utf-8')
})
body = ''
for house in houses_list:
house_str = """Σπίτι με τιμή %(price)s,
%(tm)s τ.μ.
%(description)s
Δημοσιεύθηκε στις %(date_posted)s
Link στην Χ.Ε http://www.xe.gr%(url)s
""" % house
body += house_str
if houses_list:
if UNAME and PWD and FROM and TO:
body = "Γειά ! \n" + body
msg = MIMEMultipart()
msg['From'] = FROM
msg['To'] = TO
msg['Subject'] = "Σπίτια απο Χρυσή ευκαιρία %s" % datetime.isoformat(
datetime.now()
)
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(UNAME, PWD)
server.sendmail(FROM, TO.split(","), msg.as_string())
server.quit()
else:
print body