-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHouseFinder.py
72 lines (63 loc) · 2.53 KB
/
HouseFinder.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
import json
import requests
from bs4 import BeautifulSoup
import datetime
import time
import smtplib
import os
from email.message import EmailMessage
import re
website_url = "https://www.thunderbayhouses.com/"
EMAIL_ADDRESS = os.environ.get('EMAIL_ADDRESS')
EMAIL_PASSWORD = os.environ.get('EMAIL_PASSWORD')
EMAIL_TO = [EMAIL_ADDRESS] ## change this to the emails you and to send too
def website_call(url)-> str:
# Call the website
page = requests.get(url)
soup = BeautifulSoup(page.content, "html.parser")
return soup
def json_convert(add_soup):
soup_dict = json.loads(add_soup.text)
return soup_dict
def modify_json_listing_date(house_soup): #convert the listing date to a integer for sorting.
for house in house_soup:
if house['Listing Date']:
remove_hyph = re.sub("-", "", house['Listing Date'])
convert_to_int = int(remove_hyph[:8]) ##removes the first 8 characters of the string
house['Listing Date'] = convert_to_int
return house_soup
def sort_houses(sorted_soup):
sorted_houses = sorted(sorted_soup, key=lambda x: x['Listing Date'], reverse=True)
return sorted_houses
def houses_to_list(json) -> str:
all_houses = []
for house in json:
if house["Property Status"] == "New":
#all_houses.append(website_url + house['Thumbnail'])
all_houses.append(house["Address"])
all_houses.append(house['Price'])
all_houses.append(website_url + house['Permalink'])
all_houses.append("House Status: " + house['Property Status'])
all_houses.append('\n')
return all_houses
def houses_list_to_string(house_list):
house_content = '\n'.join(house_list)
return house_content
def send_email(email_from, email_password, email_to, houses_string):
msg = EmailMessage()
msg['Subject'] = 'Good Morning Homie 🏡🤖'
msg['From'] = email_from
msg['To'] = email_to
msg.set_content(houses_string)
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
smtp.send_message(msg)
def main():
website_raw_data = website_call("https://www.thunderbayhouses.com/cache/listings.json?nocache")
json_file = json_convert(website_raw_data)
modified_file = modify_json_listing_date(json_file)
sorted_houses = sort_houses(modified_file)
houses_ready_to_send = houses_list_to_string(houses_to_list(sorted_houses))
send_email(EMAIL_ADDRESS, EMAIL_PASSWORD, EMAIL_TO, houses_ready_to_send)
if __name__ == "__main__":
main()