forked from Zaunei/flathunter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
flathunt.py
111 lines (94 loc) · 4.09 KB
/
flathunt.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Flathunter - search for flats by crawling property portals, and receive telegram
messages about them. This is the main command-line executable, for running on the
console. To run as a webservice, look at main.py"""
import argparse
import os
import logging
import sys
import time
from pprint import pformat
from flathunter.idmaintainer import IdMaintainer
from flathunter.hunter import Hunter
from flathunter.config import Config
from flathunter.heartbeat import Heartbeat
__author__ = "Jan Harrie"
__version__ = "1.0"
__maintainer__ = "Nody"
__email__ = "[email protected]"
__status__ = "Production"
# init logging
if os.name == 'posix':
# coloring on linux
CYELLOW = '\033[93m'
CBLUE = '\033[94m'
COFF = '\033[0m'
LOG_FORMAT = '[' + CBLUE + '%(asctime)s' + COFF + '|' + CBLUE + '%(filename)-18s' + COFF + \
'|' + CYELLOW + '%(levelname)-8s' + COFF + ']: %(message)s'
else:
# else without color
LOG_FORMAT = '[%(asctime)s|%(filename)-18s|%(levelname)-8s]: %(message)s'
logging.basicConfig(
format=LOG_FORMAT,
datefmt='%Y/%m/%d %H:%M:%S',
level=logging.INFO)
__log__ = logging.getLogger('flathunt')
def launch_flat_hunt(config, heartbeat=None):
"""Starts the crawler / notification loop"""
id_watch = IdMaintainer('%s/processed_ids.db' % config.database_location())
hunter = Hunter(config, id_watch)
hunter.hunt_flats()
counter = 0
while config.get('loop', dict()).get('active', False):
counter += 1
counter = heartbeat.send_heartbeat(counter)
time.sleep(config.get('loop', dict()).get('sleeping_time', 60 * 10))
hunter.hunt_flats()
def main():
"""Processes command-line arguments, loads the config, launches the flathunter"""
parser = argparse.ArgumentParser(description= \
"Searches for flats on Immobilienscout24.de and wg-gesucht.de and sends " + \
"results to Telegram User", epilog="Designed by Nody")
parser.add_argument('--config', '-c',
type=argparse.FileType('r', encoding='UTF-8'),
default='%s/config.yaml' % os.path.dirname(os.path.abspath(__file__)),
help="Config file to use. If not set, try to use '%s/config.yaml' " %
os.path.dirname(os.path.abspath(__file__))
)
parser.add_argument('--heartbeat', '-hb',
action='store',
default=None,
help='Set the interval time to receive heartbeat messages to check that the bot is' + \
'alive. Accepted strings are "hour", "day", "week". Defaults to None.'
)
args = parser.parse_args()
# load config
config_handle = args.config
config = Config(config_handle.name)
# check config
notifiers = config.get('notifiers', list())
if 'mattermost' in notifiers \
and not config.get('mattermost', dict()).get('webhook_url'):
__log__.error("No mattermost webhook configured. Starting like this would be pointless...")
return
if 'telegram' in notifiers:
if not config.get('telegram', dict()).get('bot_token'):
__log__.error("No telegram bot token configured. Starting like this would be pointless...")
return
if not config.get('telegram', dict()).get('receiver_ids'):
__log__.warning("No telegram receivers configured - nobody will get notifications.")
if not config.get('urls'):
__log__.error("No urls configured. Starting like this would be meaningless...")
return
# get heartbeat instructions
heartbeat_interval = args.heartbeat
heartbeat = Heartbeat(config, heartbeat_interval)
# adjust log level, if required
if config.get('verbose'):
__log__.setLevel(logging.DEBUG)
__log__.debug("Settings from config: %s", pformat(config))
# start hunting for flats
launch_flat_hunt(config, heartbeat)
if __name__ == "__main__":
main()