-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathturbo_apt.py
247 lines (222 loc) · 9.68 KB
/
turbo_apt.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
import os
import urllib
import urllib.parse
from urllib.parse import urlencode, quote_plus
import time
import datetime
import json
import pprint
import re
from json import JSONEncoder, JSONDecoder
from bs4 import BeautifulSoup
import simplejson
import googlemaps
_g_addresses = []
maps = None
def log(msg, level="info"):
print(level + ": " + str(msg))
class EtuoviApt:
def __init__(self, url, json_file="turbo_apt.json"):
self.filepath = json_file
self.url = url
try:
self.apt_id = url.rstrip("/").split("/")[-1].split("?")[0]
except ValueError as e:
log("Invalid URL: %s" % url)
raise e
self.commute_times = []
self._html = ""
self._soup = None
self.last_updated = 0
self.address = ""
self.floor = (0, 0)
self.price_total = 0
self.price_debt = 0
self.price_selling = 0
self.expense_maint = 0
self.expense_debt = 0
self.rejected = False
if os.path.exists(json_file) and self.load(json_file):
#The URL was already found from file.
log("Loaded %s" %(self.address))
else:
#This is a new URL. Fetch data and save to file.
with urllib.request.urlopen(url) as response:
self._html = response.read()
self._soup = BeautifulSoup(self._html, 'html.parser')
#Address of interest
self.scrape()
coords = map_helper.get_coords(self.address)
self.coords = (coords["lat"], coords["lng"])
self.last_updated = time.time()
for address in _g_addresses:
trips = {
"car": map_helper.get_distance(self.address, address["address"], "driving"),
"bus": map_helper.get_distance(self.address, address["address"], "transit")
}
self.commute_times.append({
"name": address["name"],
"trip_choices": trips})
self.save(json_file)
def scrape(self):
def scrape_price(self, field_name):
try:
self.price_selling = float(soup.find('dt', text=field_name)\
.find_next_sibling().get_text().strip().replace("€", "").replace(" ", "").replace(",", "."))
return True
except AttributeError:
log("%s not found" %field_name)
return False
#Address
soup = self._soup
map_container = soup.find('div', id="mapInfoWindowContent")
self.address = map_container.find_all('label')[0].get_text()
log("Address:" + self.address)
#Floor
basics = soup.find('article', class_="basics").find("dl")
self.floor_raw = basics.find('dt', text="Kerrokset:").find_next_sibling().get_text()
if self.floor_raw and "/" in self.floor_raw:
self.floor = tuple(self.floor_raw.split("/"))
#Price
if not scrape_price(self, "Myyntihinta:"):
if not scrape_price(self, "Velaton lähtöhinta:"):
if not scrape_price(self, "Viimeisin tarjous:"):
log("Failed to find the price for this place.")
self.price_selling = 0
try:
self.price_debt = float(soup.find('dt', text="Velkaosuus:")
.find_next_sibling().get_text().strip().replace("€", "").replace(" ", "").replace(",", "."))
except AttributeError:
log("Debt price not found. Assuming 0.")
self.price_debt = 0
self.price_total = self.price_debt + self.price_selling
#Monthly costs
maint_cost = soup.find(string=re.compile("Hoitovastike"))
if maint_cost:
match = re.match(r".*Hoitovastike (\d{0,7},\d{1,2}).*Rahoitusvastike (\d{0,7},\d{1,2})", maint_cost)
if match:
self.expense_maint = float(match.group(1).replace(",", "."))
self.expense_debt = float(match.group(2).replace(",", "."))
def __str__(self):
string = "\n***************************\n" + self.address + "\n"
string += "Price: {} € ({} € + {} € of debt) €\n"\
.format(self.price_total, self.price_selling, self.price_debt)
string += "Expenses: {}/{}\n".format(self.expense_maint, self.expense_debt)
string += "Floor: {}/{}\n".format(self.floor[0], self.floor[1])
for commute in self.commute_times:
trips = commute["trip_choices"]
string += "%s is %s (%s by car, %s by bus)" \
%(commute["name"], trips["car"][0],
trips["car"][1], trips["bus"][1]) + "\n"
string += "Commute estimates are based on the next monday at 8.30\n"
string += "\nOther information:\n"
already_printed = ["address", "price_debt", "price_selling", "price_total",
"expense_maint", "expense_debt", "floor", "commute_times",
"floor_raw", "_soup", "_html", "last_updated"]
for key, value in self.__dict__.items():
if key not in already_printed and not key.startswith("_"):
string += "%s: %s\n" %(key, value, )
return string
def get_etuovi_info_str(self):
string = ""
for commute in self.commute_times:
trips = commute["trip_choices"]
string += "%s is %s (%s by car, %s by bus)" \
%(commute["name"], trips["car"][0],
trips["car"][1], trips["bus"][1]) + "\n"
string += "Commute estimates are based on the next monday at 8.30\n"
return string
def save(self, filepath=None):
"""Saves the object to JSON file."""
if not filepath:
filepath = self.filepath
log("Saving to file...")
target = []
apt_from_file = {}
if os.path.exists(filepath):
with open(filepath, "r") as fp:
target = json.load(fp)
for obj in target:
if type(obj) != dict:
raise Exception("Invalid JSON! List contained %s, expected only dict" %(type(obj), ))
if obj["address"] == self.address:
#Remove the existing entry, we'll add it back later.
apt_from_file = obj.copy()
target.remove(obj)
log("Removed {}".format(obj["address"]))
#Merge dictionaries
tmp_dict = {**apt_from_file, **self.__dict__.copy()}
#Don't save private keys to file. soup is not serializeable.
for key in self.__dict__.keys():
if key.startswith("_"):
tmp_dict.pop(key)
tmp_dict["etuovi_additional_info"] = self.get_etuovi_info_str()
target.append(tmp_dict)
#log(pprint.pformat(target))
with open(filepath, "w") as fp:
fp.write(json.dumps(target, indent=4, sort_keys=True))
def load(self, filepath=None):
"""Loads an object from JSON file.
Returns:
True for existing EtuoviApt objects. Else False."""
if not filepath:
filepath = self.filepath
if not os.path.exists(filepath):
return False
with open(filepath, "r", encoding="utf-8") as fp:
json_list = json.load(fp)
try:
for obj in json_list:
if "address" in obj and obj["apt_id"] == self.apt_id:
log("Found match in file for %s" %(self.apt_id))
for prop in self.__dict__.keys():
if not prop.startswith("_"):
setattr(self, prop, obj[prop])
return True
except Exception:
log("Failed to load EtuoviApt from file.")
return False
return False
class MapHelper():
def __init__(self, maps):
self.maps = maps
def get_coords(self, address):
"""Converts an address to lat, lng dict"""
geocode = self.maps.geocode(address)[0]
return geocode["geometry"]["location"]
def get_distance(self, origin, destination, mode="driving"):
time_of_travel = int(next_weekday(datetime.datetime.now(), 0, 8, 30).timestamp())
kwargs = {
"origins": origin,
"departure_time": time_of_travel,
"destinations": destination,
"mode": mode,
"language": "en-EN",
}
result = self.maps.distance_matrix(**kwargs)
#log(pprint.pformat(result))
try:
if result["rows"][0]["elements"][0]['status'] == "OK":
travel_time = result['rows'][0]['elements'][0]['duration']['text']
friendly_distance = result['rows'][0]['elements'][0]['distance']['text']
distance = result['rows'][0]['elements'][0]['distance']['value']
else:
log("Warning: Google Maps was unable to calculate distance!")
return None, None, None
except KeyError as e:
log("Invalid response!", "ERROR")
log(pprint.pformat(result), "ERROR")
raise e
return friendly_distance, travel_time, distance
def next_weekday(d, weekday, hour, minute):
"""Returns the next weekday datetime at time_of_day"""
days_ahead = weekday - d.weekday()
if days_ahead <= 0: # Target day already happened this week
days_ahead += 7
date = d + datetime.timedelta(days_ahead)
return datetime.datetime(date.year, date.month, date.day, hour, minute)
with open("../personal.json", encoding='utf-8') as f:
data = json.load(f)
_g_addresses = data["addresses"]
maps = googlemaps.Client(key=data["google_api_key"])
map_helper = MapHelper(maps)