Skip to content

Commit

Permalink
Merge pull request flathunters#369 from mrclrchtr/main
Browse files Browse the repository at this point in the history
Add possibility to start main.py with config.yml
  • Loading branch information
codders authored Apr 20, 2023
2 parents f1cca1a + 31c13d1 commit 425bfa8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 27 deletions.
29 changes: 3 additions & 26 deletions flathunt.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
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 time
from datetime import time as dtime

from flathunter.argument_parser import parse
from flathunter.logging import logger, configure_logging
from flathunter.idmaintainer import IdMaintainer
from flathunter.hunter import Hunter
from flathunter.config import Config, Env
from flathunter.config import Config
from flathunter.heartbeat import Heartbeat
from flathunter.time_utils import wait_during_period

Expand Down Expand Up @@ -47,30 +46,8 @@ def launch_flat_hunt(config, heartbeat: Heartbeat):

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"
)
if Env.FLATHUNTER_TARGET_URLS is not None:
default_config_path = None
else:
default_config_path = f"{os.path.dirname(os.path.abspath(__file__))}/config.yaml"
parser.add_argument('--config', '-c',
type=argparse.FileType('r', encoding='UTF-8'),
default=default_config_path,
help=f'Config file to use. If not set, try to use "{default_config_path}"'
)
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
args = parse()
config_handle = args.config
if config_handle is not None:
config = Config(config_handle.name)
Expand Down
33 changes: 33 additions & 0 deletions flathunter/argument_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""Parser for some startup arguments"""

import argparse
import os

from flathunter.config import Env


def parse():
"""Processes and return command-line arguments"""
parser = argparse.ArgumentParser(
description=("Searches for flats on Immobilienscout24.de and wg-gesucht.de"
" and sends results to Telegram User"),
epilog="Designed by Nody"
)
if Env.FLATHUNTER_TARGET_URLS is not None:
default_config_path = None
else:
root_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
default_config_path = f"{root_dir}/config.yaml"
parser.add_argument('--config', '-c',
type=argparse.FileType('r', encoding='UTF-8'),
default=default_config_path,
help=f'Config file to use. If not set, try to use "{default_config_path}"'
)
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.')
)
return parser.parse_args()
9 changes: 8 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
""" Startup file for Google Cloud deployment or local webserver"""
import os

from flathunter.argument_parser import parse
from flathunter.idmaintainer import IdMaintainer
from flathunter.googlecloud_idmaintainer import GoogleCloudIdMaintainer
from flathunter.web_hunter import WebHunter
Expand All @@ -9,7 +10,13 @@

from flathunter.web import app

config = Config()
# load config
args = parse()
config_handle = args.config
if config_handle is not None:
config = Config(config_handle.name)
else:
config = Config()

if __name__ == '__main__':
# Use the SQLite DB file if we are running locally
Expand Down

0 comments on commit 425bfa8

Please sign in to comment.