-
Notifications
You must be signed in to change notification settings - Fork 4
/
wg-gesucht.py
140 lines (121 loc) · 5.29 KB
/
wg-gesucht.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
import datetime
import logging
import os.path
import pprint
import sys
import time
from subprocess import call
import yaml
from src import ListingGetter, ListingInfoGetter, submit_wg
logging.basicConfig(
format="[%(asctime)s | %(levelname)s] - %(message)s ",
level=logging.INFO,
datefmt="%Y-%m-%d_%H:%M:%S",
handlers=[logging.FileHandler("../debug.log"), logging.StreamHandler()],
)
logger = logging.getLogger("bot")
def main(config):
"""
Operations:
- Get newest listings
- Compares them with previous listing (if available)
- For all new listings not present in previous listings:
- Checks rental length of listing
- Checks if listing is reupload by comparing to user_name and address
- Gets listing text -> can be used for OpenAI further down the line
- Attemps to submit an application
- Adds listing to 'past_listings.txt'
"""
# initialise old listings for later
old_listings = dict()
while True:
# read previously sent messages:
past_listings_file_name = "past_listings.txt"
if not os.path.exists(past_listings_file_name):
prev_listings = []
else:
with open(past_listings_file_name, "r") as msgs:
prev_listings = msgs.readlines()
# get current listings
url = config["url"]
listing_getter = ListingGetter(url)
info_dict = listing_getter.get_all_infos()
new_listings = info_dict
# get diff: new - old listings
old_values = old_listings.values()
diff_dict = {
k: v for k, v in enumerate(new_listings.values()) if v not in old_values
}
if diff_dict:
logger.info(f"Found {len(diff_dict)} new listings.")
for listing in diff_dict.values():
# unpack listing dict
ref = listing["ref"]
listing_length_months = listing["rental_length_months"]
# add to config for submit_app function
config["ref"] = ref
config["user_name"] = listing["user_name"]
config["address"] = listing["address"]
logger.info(f"Trying to send message to: {listing}")
# check rental start date
desired_rental_params = config["rental_start"]
desired_rental_start = datetime.datetime(
desired_rental_params["year"],
desired_rental_params["month"],
desired_rental_params["day"],
)
earliest_allowable_start = desired_rental_start - datetime.timedelta(
days=desired_rental_params["buffer_days"]
)
latest_allowable_start = desired_rental_start + datetime.timedelta(
days=desired_rental_params["buffer_days"]
)
if (earliest_allowable_start > listing["rental_start"]) or (
listing["rental_start"] > latest_allowable_start
):
logger.info(
f"Rental start ({listing['rental_start']}) is outside of the desired start range. Skipping ..."
)
continue
# check rental length, if below min -> skip this listing
min_rental_length_months = config["min_listing_length_months"]
if (
listing_length_months >= 0
and listing_length_months < min_rental_length_months
):
logger.info(
f"Rental period of {listing_length_months} months is below required {min_rental_length_months} months. Skipping ..."
)
continue
# check if already messaged listing in the past
listings_sent_identifier = (
f"{listing['user_name']}: {listing['address']}\n"
)
if listings_sent_identifier in prev_listings:
logger.info(
"Listing in 'prev_listings' file, therfore contacted in the past! Skipping ..."
)
continue
# get listing text and store in config for later processing
listing_info_getter = ListingInfoGetter(ref)
listing_text = listing_info_getter.get_listing_text()
config["listing_text"] = listing_text
# use selenium to retrieve dynamically loaded info and send message
sending_successful = submit_wg.submit_app(config, logger)
# if new message sent -> store information about listing
if sending_successful:
listing_info_getter.save_listing_text(
"listing_texts.json", listing_text
)
# add listing to past_listings.txt
with open(past_listings_file_name, "a") as msgs:
msgs.write(f"{listings_sent_identifier}")
old_listings = new_listings.copy()
else:
logger.info("No new offers.")
logger.info("Sleep.")
time.sleep(60)
if __name__ == "__main__":
with open("config.yaml", "r") as stream:
config = yaml.safe_load(stream)
main(config)