-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlatFinder.py
293 lines (223 loc) · 9.87 KB
/
FlatFinder.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
from bs4 import BeautifulSoup
import requests
import re
import os.path
from discord import SyncWebhook
from tkinter import *
from tkinter import messagebox
from PIL import ImageTk, Image
from datetime import datetime
base_dir = os.path.dirname(__file__)
lists_dir = os.path.join(base_dir, "property_lists")
#Converts list to txt
def list_to_txt(list_in, filename):
path = os.path.join(lists_dir, filename)
file = open(path, "w")
for i in list_in:
file.write("%s\n" % i)
print("Writing to file complete\n\n")
#Converts txt to list
def txt_to_list(filename):
path = os.path.join(lists_dir, filename)
file = open(path, "r")
file_list = []
for line in file:
l = line[:-1]
file_list.append(l)
return file_list
#Finds properties with desired specs
def rm_get_properties (LocationID, min_price, max_price, radius, isFurnished, min_room, max_room):
#If furnished only was selected, add parameter to url
if isFurnished == True:
rm_url = f'https://www.rightmove.co.uk/property-to-rent/find.html?locationIdentifier=REGION%{LocationID}&maxBedrooms={max_room}&minBedrooms={min_room}&maxPrice={max_price}&minPrice={min_price}&radius={radius}&propertyTypes=&mustHave=&dontShow=&furnishTypes=furnished%2CpartFurnished&keywords='
else:
rm_url = f'https://www.rightmove.co.uk/property-to-rent/find.html?locationIdentifier=REGION%{LocationID}&maxBedrooms={max_room}&minBedrooms={min_room}&maxPrice={max_price}&minPrice={min_price}&radius={radius}&propertyTypes=&mustHave=&dontShow=&furnishTypes=&keywords='
rm = requests.get(rm_url)
rm_doc = BeautifulSoup(rm.text, "html.parser")
#Find all properties on first page
tiles = rm_doc.find_all(["div"], id=re.compile("property-"))
id_list = []
#Extract property ids
for i in range (1, len(tiles)):
id = tiles[i]['id'].removeprefix("property-")
if id != '0':
id_list.append(id)
return id_list
#Compare the old list with the new
def rm_compare(new_list):
old_list = txt_to_list("rm_old.txt")
new_flats = list(set(new_list).difference(old_list))
#Save the new list as the old list name to complete the cycle
list_to_txt(new_list, "rm_old.txt")
return new_flats
#Find the URL of the new properties
def rm_find_URL(list):
url_list = []
for i in range (0,len(list)-1):
id = list[i]
url_list.append(f"https://www.rightmove.co.uk/properties/{id}#/?channel=RES_LET")
return url_list
#Post all new properties to discord
def discord_post(url_list):
webhook = SyncWebhook.from_url(discord_wh)
for url in url_list:
webhook.send("A new flat has been found!\n\n"+ url + "\n")
print("Posted to discord\n")
def main():
rm_list = rm_get_properties(LocationID, min_price, max_price, radius, isFurnished, min_room, max_room)
#print(rm_list, "\n\n")
print(len(rm_list), "results found\n")
rm_new_flats = rm_compare(rm_list)
if len(rm_new_flats) > 0:
rm_url_list = rm_find_URL(rm_new_flats)
discord_post(rm_url_list)
res_num = len(rm_list)
res_label = Label(page2, text=f"{res_num} flats found!")
res_label.grid(row=1, column=1, sticky='S')
now = datetime.now()
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
last_scan_label = Label(page2, text=f"Last scan: {dt_string}")
last_scan_label.grid(row=2, column=1, sticky='N')
window.after(int(refresh_rate)*60000, main)
#Holborn = 5E87513
#Bloomsbury = 5E87494
def submit_form():
global LocationID
global min_price
global max_price
global radius
global min_room
global max_room
global isFurnished
global refresh_rate
global discord_wh
# Get the values from the input
LocationID = LocID_entry.get().upper()
min_price = min_price_entry.get()
max_price = max_price_entry.get()
radius = radius_variable.get()
min_room = min_room_variable.get()
max_room = max_room_variable.get()
isFurnished = furnished_var.get()
refresh_rate = refresh_entry.get()
discord_wh = discord_entry.get()
### ERROR HANDLING ###
#Initialise string for error message
errors = ""
#Check http status code is correct
url = f"https://www.rightmove.co.uk/property-to-rent/find.html?searchType=RENT&locationIdentifier=REGION%{LocationID}&insId=1&radius=0.0&minPrice=&maxPrice=&minBedrooms=&maxBedrooms=&displayPropertyType=&maxDaysSinceAdded=&sortByPriceDescending=&_includeLetAgreed=on&primaryDisplayPropertyType=&secondaryDisplayPropertyType=&oldDisplayPropertyType=&oldPrimaryDisplayPropertyType=&letType=&letFurnishType=&houseFlatShare="
r = requests.head(url)
print(r.status_code)
if r.status_code == 400:
errors = errors + "Invalid location ID\n"
#Check min price is valid
if not min_price.isdigit():
errors = errors + "Invalid minimum price\n"
#Check max price is valid
if not max_price.isdigit():
errors = errors + "Invalid maximum price\n"
#Check refresh rate is valid
if not refresh_rate.isdigit():
errors = errors + "Invalid refresh rate\n"
#Check if any errors exist, if so print error message
if errors != "":
error_msg = "The following inputs are invalid:\n" + errors
messagebox.showinfo("Error", error_msg)
elif r.status_code != 200:
messagebox.showinfo("Could not connect", "Could not connect to Rightmove.com")
elif min_price >= max_price:
messagebox.showinfo("Error", "The min price cannot exceed the max price. Please enter new values.")
elif min_room > max_room:
messagebox.showinfo("Error", "The min bedrooms must not be greater than the max bedrooms. Please enter new values.")
#If no errors exist show page 2
else:
### PAGE 2 CONTENTS ###
params = Label(page2, text=f" Location: {LocationID}\n Minimum Price: £{min_price}\n Maximum Price: £{max_price}\n Radius: {radius} miles\n Minimum Bedrooms: {min_room}\n Maximum Bedrooms: {max_room}\n Only searching for furnished properties: {isFurnished}", anchor="e", justify=LEFT)
params.grid(row=1, column=0, rowspan=2, sticky='N')
page2.tkraise()
window.after(int(refresh_rate)*60000, main)
# Create the main window
window = Tk()
window.title("FlatFinder")
page1 = Frame(window)
page2 = Frame(window)
page1.grid(row=0, column=0, sticky="nsew")
page2.grid(row=0, column=0, sticky="nsew")
page1.lift()
for i in range(3):
page1.columnconfigure(i, weight=1, minsize=200)
page1.rowconfigure(i, weight=1, minsize=35)
for i in range(3):
page2.columnconfigure(i, weight=1, minsize=200)
page2.rowconfigure(i, weight=1, minsize=35)
# PAGE 1 LABELS
LocID_label = Label(page1, text="Location ID:")
LocID_label.grid(row=0, column=0)
min_price_label = Label(page1, text="Minimum Price:")
min_price_label.grid(row=1, column=0)
max_price_label = Label(page1, text="Maximum Price:")
max_price_label.grid(row=2, column=0)
radius_label = Label(page1, text="Radius:")
radius_label.grid(row=3, column=0)
min_room_label = Label(page1, text="Minimum beds (0 for studio):")
min_room_label.grid(row=4, column=0)
max_room_label = Label(page1, text="Maximum beds (0 for studio):")
max_room_label.grid(row=5, column=0)
furnished_label = Label(page1, text="Only view furnished properties?")
furnished_label.grid(row=6, column=0)
discord_label = Label(page1, text="Discord webhook:")
discord_label.grid(row=7, column=0)
#PAGE 1 INPUTS
LocID_entry = Entry(page1)
LocID_entry.grid(row=0, column=1)
min_price_entry = Entry(page1)
min_price_entry.grid(row=1, column=1)
max_price_entry = Entry(page1)
max_price_entry.grid(row=2, column=1)
radius_options = [0, 0.25, 0.5, 1, 3, 5, 10, 15, 20, 30, 40,]
radius_variable = DoubleVar()
radius_variable.set(radius_options[0])
radius_chosen = OptionMenu(page1, radius_variable, *radius_options)
radius_chosen.grid(row=3, column=1)
room_options = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
min_room_variable = IntVar()
min_room_variable.set(room_options[0])
min_room_chosen = OptionMenu(page1, min_room_variable, *room_options)
min_room_chosen.grid(row=4, column=1)
max_room_variable = IntVar()
max_room_variable.set(room_options[0])
max_room_chosen = OptionMenu(page1, max_room_variable, *room_options)
max_room_chosen.grid(row=5, column=1)
furnished_var = BooleanVar()
furnished_entry = Checkbutton(page1, variable=furnished_var)
furnished_entry.grid(row=6, column=1)
discord_entry = Entry(page1, width=78)
discord_entry.grid(row=7, column=1, columnspan=3)
#Refresh rate labels/input
refresh_label = Label(page1, text="Refresh frequency (in mins):")
refresh_label.grid(row=6, column=2)
refresh_entry = Entry(page1)
refresh_entry.grid(row=6, column=3)
#Title and info text
title = Label(page1, text="Welcome to FlatFinder!", font=('Helvetica', 18, 'bold'))
title.grid(row=0, column=2, columnspan=3)
ID_desc = Label(page1, text="To find the location ID, follow these steps:\n1. On Rightmove, search for properties in the desired location.\n2. Look for the values after REGION% (shown below).\n3. Copy these values into the Location ID box in FlatFinder.", anchor="e", justify=LEFT)
ID_desc.grid(row=1, column=2, columnspan=3, rowspan=3)
#Image of location ID
img_frame = Frame(page1)
img_frame.grid(row=4, column=2, columnspan=2, rowspan=1)
img = ImageTk.PhotoImage(Image.open("location_id_img.jpg"))
img_label = Label(img_frame, image = img)
img_label.pack(padx=(15, 15))
# Search button
submit_button = Button(page1, text="Search", command=submit_form)
submit_button.grid(row=8, column=0, columnspan=5)
### PAGE 2 INDEPENDENT CONTENTS ###
pg2_subtitle = Label(page2, text="Your chosen parameters:", font=('Helvetica', 14, 'bold'))
pg2_subtitle.grid(row=0, column=0)
pg2_title = Label(page2, text="Flatfinder is Running", font=('Helvetica', 18, 'bold'))
pg2_title.grid(row=0, column=1)
back_button = Button(page2, text="Back", command=page1.tkraise)
back_button.grid(row=3, column=0, sticky='N')
# Start the main loop
window.mainloop()