This repository has been archived by the owner on Apr 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotifier.py
79 lines (62 loc) · 2.95 KB
/
notifier.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
import os
import requests
import sqlite3
import smtplib
import unicodedata
import urllib.parse as urlparse
from bs4 import BeautifulSoup
from dotenv import load_dotenv
from urllib.parse import parse_qs
load_dotenv()
connection = sqlite3.connect('housinglist.db')
cursor = connection.cursor()
def make_db():
makeTable = """CREATE TABLE IF NOT EXISTS houses(
ID INT,
timeadded DATETIME DEFAULT CURRENT_TIMESTAMP
);"""
cursor.execute(makeTable)
connection.commit()
headers = {"User-Agent": os.getenv('SENDER_USERAGENT')}
def find_houses():
housesPage = requests.get(os.getenv('BASEURL'), headers = headers)
housesSoup = BeautifulSoup(housesPage.content, 'html.parser')
houses = housesSoup.findAll("article", {"class": 'house-thumb'})
for house in houses:
for a in house.findAll('a', href=True):
houseID = parse_qs(urlparse.urlparse(a['href']).query)['detailId'][0]
cursor.execute("SELECT COUNT(*) FROM houses WHERE ID = ?", (houseID,))
if cursor.fetchone()[0] == 0:
scan_housepage(houseID)
cursor.execute("INSERT INTO houses (ID) VALUES (?)", (houseID,))
connection.commit()
def scan_housepage(ID):
houseURL = '{}{}'.format(os.getenv('HOUSEURL'), ID)
housePage = requests.get(houseURL, headers = headers)
houseSoup = BeautifulSoup(housePage.content, 'html.parser')
#This could potentially break as Vestide updates their website.
houseAddress = houseSoup.find("p", {"class":'details-address'}).get_text().strip()
houseDescription = houseSoup.find("p", {"class":'details-intro'}).get_text().strip()
houseAreaPrice = houseSoup.find("p", {"class":'details-figures'})
houseAreaPriceElts = houseAreaPrice.findAll('span')
houseArea = houseAreaPriceElts[0].get_text().strip()
housePrice = unicodedata.normalize(u'NFKD', houseAreaPriceElts[2].get_text().strip()).encode('ascii', 'ignore').decode('utf8')
send_mail(houseURL, houseAddress, houseDescription, houseArea, housePrice)
def send_mail(houseURL, houseAddress, houseDescription, houseArea, housePrice):
server = smtplib.SMTP(os.getenv('SMTP_SERVER'), os.getenv('SMTP_PORT'))
server.ehlo()
server.starttls()
server.ehlo()
server.login(os.getenv('SENDER_EMAIL'), os.getenv('SENDER_PASSWORD'))
subject = 'A new room has been added to Vestide!'
body = 'Hi {}!\n\nA new room has been added to Vestide! Information about the room: \n\nAddress: {}\nDescription: {}\nSurface area: {}\nRent: {}\n\nURL: {}\n\n\n\nWarning! This is an automated message, the information could be wrong. Always verify the information manually!'.format(os.getenv('RECEIVER_NAME'), houseAddress, houseDescription, houseArea, housePrice, houseURL)
msg = f"Subject: {subject}\n\n{body}"
server.sendmail(
os.getenv('SENDER_EMAIL'),
os.getenv('RECEIVER_EMAIL'),
msg
)
server.quit()
make_db()
find_houses()
connection.close()