-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimmoscout_agent.py
112 lines (103 loc) · 4.62 KB
/
immoscout_agent.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
import requests
import json
class ImmoScout:
def __init__(self, config):
self.config = config
self.url = self._build_immo_url_from_config()
self.map = set()
self.first_run = True
def _build_immo_url_from_config(self):
"""
Builds immobilienscout url using config values to set request parameters
"""
config = self.config
if hasattr(config, 'whole_url') and config.whole_url:
# adding sorting parameter, bc it doesn't work without it
return config.whole_url + "&sorting=2"
with_balcony = "-mit-balkon" if hasattr(config, 'with_balcony') and config.with_balcony else ""
url = f"{config.url}{config.city}/{config.city}/wohnung{with_balcony}-mieten?"
#Build parameters
url += f"haspromotion={config.wbs}&" if hasattr(config, 'wbs') and config.wbs is not None else ""
url += f"shape={config.shape}&" if hasattr(config, 'shape') and config.shape is not None else ""
url += f"numberofrooms={config.num_of_rooms}&" if hasattr(config, 'wbs') and config.num_of_rooms is not None else ""
price_from = config.price_from if (hasattr(config, 'price_from') and config.price_from is not None) else ""
price_up_to = config.price_up_to if (hasattr(config, 'price_up_to') and config.price_up_to is not None) else ""
url += f"price={price_from}-{price_up_to}&"
url += f"livingspace={config.livingspace}&" if hasattr(config, 'livingspace') and config.livingspace else ""
url += f"price_type={config.price_type}&" if hasattr(config, 'price_type') and config.price_type else ""
url += f"floor={config.floor_from}" if hasattr(config, 'floor_from') and config.floor_from else ""
url += "&sorting=2"
return url
def _get_raw_result_list(self):
"""
Returns the available listings as an array of raw results
"""
response = requests.post(self.url)
immoscout_ads_content = json.loads(response.content)
raw_result_list = immoscout_ads_content["searchResponseModel"]["resultlist.resultlist"]["resultlistEntries"][0]["resultlistEntry"]
return raw_result_list
@staticmethod
def parse_result(result):
"""
Parses the relevant info from the result
json to a string for Telegram message
"""
parsed_result = result['resultlist.realEstate']['address']['quarter'] +'\n'
rent = result['resultlist.realEstate']["calculatedTotalRent"]["totalRent"]["value"]
parsed_result +=f"Total Rent: {rent}'Eur\n'"
attributes = result['attributes'][0]['attribute']
for attribute in attributes:
label = str(attribute['label'])
value = str(attribute['value'])
parsed_result += f" {label}: {value}\n'"
url = "https://www.immobilienscout24.de/expose/"
parsed_result += url + result['@id']
return parsed_result
@staticmethod
def get_message_suggestion(message_config, result):
"""
Prepares a message suggestions to send
"""
contact = result['resultlist.realEstate']['contactDetails']
salutation = "Sehr geehrte"
if 'lastname' in contact:
if contact['salutation'] == "FEMALE":
salutation += " Frau "
else:
salutation += "r Herr "
salutation += contact['lastname']
else:
salutation += " Damen und Herren"
address = result['resultlist.realEstate']['address']
address = address['description']['text']
message_text = f"{salutation},\n \
{message_config.text_before_address} \
{address} {message_config.text_after_address}"
print(message_text)
return message_text
@staticmethod
def log_result(result):
"""
Logs the relevant info from the result
"""
print("id: "+result['@id'])
print("title")
print(result['resultlist.realEstate']['title'])
print("address")
print(result['resultlist.realEstate']['address'])
print("contact")
print(result['resultlist.realEstate']['contactDetails'])
def get_new_ads_results(self):
"""
Gets new ads starting from the second run,
ignores the already existing ads
"""
results = []
raw_result_list = self._get_raw_result_list()
for result in raw_result_list:
if result['@id'] not in self.map:
if not self.first_run:
results.append(result)
self.map.add(result['@id'])
self.first_run = False
return results