-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcondos_scraper.py
164 lines (135 loc) · 4.96 KB
/
condos_scraper.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python3
"""
Copyright Andrew Wang, 2019
Distributed under the terms of the GNU General Public License.
This is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this file. If not, see <http://www.gnu.org/licenses/>.
"""
import datetime
import json
import pyhtgen
import requests
import smtplib
from bs4 import BeautifulSoup
from email.mime.text import MIMEText
from email.utils import formataddr
def download(url):
data = requests.get(url)
return data.text
def send_email(from_name, from_email, password, to_name, to_email, subject,
body, smtp_server, port):
try:
msg = MIMEText(body, 'html', 'utf-8')
msg['From'] = formataddr([from_name, from_email])
msg['To'] = formataddr([to_name, to_email])
msg['Subject'] = subject
server = smtplib.SMTP_SSL(smtp_server, port)
server.login(from_email, password)
server.sendmail(from_email, [to_email, ], msg.as_string())
server.quit()
except Exception as e:
print(e)
def do_parse(html):
condos = []
soup = BeautifulSoup(html, 'html.parser')
list_row = soup.find(id='listRow')
list_row_children = list_row.children
for next_child in list_row_children:
next_condo = next_child.get_text(separator=', ', strip=True)
if 'Login' not in next_condo and next_condo.count(', ') == 8:
condos.append(next_condo.split(', '))
return condos
def fetch_area(area):
_id = area['id']
_type = area['type']
url = ('https://condos.ca/toronto/north-york/condos-for-sale' +
'?tab=listings&{}_id={}'
).format(_type, _id)
return do_parse(download(url))
def read_lang_file(lang):
try:
with open('lang.json', encoding='utf8') as file:
text = file.read()
return json.loads(text)[lang]
except Exception as e:
print(e)
print('Please make sure lang.json exists and is properly formatted.')
exit(-1)
def read_config_file(config):
try:
with open('config.json', encoding='utf8') as file:
text = file.read()
return json.loads(text)[config]
except Exception as e:
print(e)
print('Please make sure config.json exists and is properly formatted.')
exit(-1)
def build_email(condos, lang):
lang_dict = read_lang_file(lang)
header_row = [
lang_dict['price'],
lang_dict['dom'],
lang_dict['address'],
lang_dict['unit'],
lang_dict['bed'],
lang_dict['shower'],
lang_dict['parking'],
lang_dict['size'],
lang_dict['maint_fee'],
]
htmlcode = pyhtgen.table(condos, header_row=header_row)
return htmlcode
def generate_subject():
response = download('https://api.exchangeratesapi.io/latest?' +
'symbols=CNY&base=CAD')
response_json = json.loads(response)
rate = round(response_json['rates']['CNY'], 3)
return 'Condos {} | $1 = ¥{}'.format(str(datetime.date.today()), rate)
def send_condo_email(following_areas, lang, mailing_list, debug=False):
lang_dict = read_lang_file(lang)
body = ''
for next_area in following_areas:
condos = fetch_area(next_area)
body += '<h2>' + next_area['name'] + '</h2>'
body += build_email(condos, lang)
body += ('<p>' + lang_dict['generate'] + ' ' +
str(datetime.datetime.today()).split('.')[0] + '</p>'
)
if debug:
with open('output.txt', 'w', encoding='utf-8') as file:
file.write(body)
with open('subject.txt', 'w', encoding='utf-8') as file:
file.write(generate_subject())
else:
subject = generate_subject()
smtp_config = read_config_file('smtp')
for next_email in mailing_list:
send_email(
smtp_config['from_name'],
smtp_config['from_email'],
smtp_config['password'],
'',
next_email,
subject,
body,
smtp_config['smtp_server'],
smtp_config['port'],
)
if __name__ == '__main__':
following_areas = read_config_file('following_areas')
mailing_list = read_config_file('mailing_list')
if datetime.datetime.today().day == 1:
mailing_list += read_config_file('monthly_mailing_list')
send_condo_email(following_areas,
read_config_file('language'),
mailing_list,
True
)