-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
78 lines (59 loc) · 2.67 KB
/
run.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
import os
import argparse
from datetime import datetime
from helpers.watchdog import dog
from helpers.parsers.portals.nehnutelnosti import NehnutelnostiPropertyOffersParser
from helpers.utils.common import ensure_path
from helpers.senders.email_sender import send_email, prepare_email
from helpers.senders.email_builder import get_email_message_html, get_email_message_plain
# Prepare the base paths
THIS_PATH = os.path.realpath(__file__)
BASE_PATH = os.path.dirname(THIS_PATH)
# Prepare the analytical timestamp
TODAY = datetime.now()
today = f'{TODAY.day}. {TODAY.month}. {TODAY.year}'
# Prepare the settings path/presence
ensure_path(os.path.join(BASE_PATH, "settings"))
ensure_path(os.path.join(BASE_PATH, "helpers", "watchdog", "settings"))
# Prepare the database path/presence
ensure_path(os.path.join(BASE_PATH, "helpers", "watchdog", "database"))
if __name__ == '__main__':
# -------------------------
# Parse the input arguments
# -------------------------
arg_parser = argparse.ArgumentParser(description='Property offers parser')
arg_parser.add_argument('-n', '--new', type=bool, help='new watchdog (delete the cache of the visited ids)')
arg_parser.add_argument('-e', '--email', type=str, help='email of the recipient')
args = arg_parser.parse_args()
# ---------------------
# Prepare the machinery
# ---------------------
# Prepare e-mail sending
u_name, d_name = prepare_email(args.email)
# ---------------------------
# Prepare the property parser
# ---------------------------
# Prepare the parser
parser = NehnutelnostiPropertyOffersParser()
# -------------------------
# Parse the property offers
# -------------------------
# Clear the cache with the visited offers if the new run is desired
if args.new:
dog.clear_visited_offers()
# Get the new offers to be checked
offers = dog.watch(parser)
if offers:
# Prepare the message
message_text = get_email_message_plain(offers, parser.weburl)
message_html = get_email_message_html(offers, parser.weburl)
# Send the offers
#
# Needs the less secure access to be turned on: https://myaccount.google.com/u/2/lesssecureapps?pageId=none
# More info: https://stackoverflow.com/questions/16512592/login-credentials-not-working-with-gmail-smtp
# And: https://joequery.me/guides/python-smtp-authenticationerror/
send_email(from_address='[email protected]',
to_address={'username': u_name, 'domain': d_name},
subject=f'nehnutelnosti.sk {today}',
plaintext=message_text,
html=message_html)