-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
74 lines (57 loc) · 2.14 KB
/
main.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
import datetime
import os
from il_supermarket_scarper import ScarpingTask, ScraperFactory, FileTypesFilters
def load_params():
"""load params from env variables with validation"""
kwargs = {"suppress_exception": True, "lookup_in_db": True}
# validate scrapers
enabled_scrapers = os.getenv("ENABLED_SCRAPERS", None)
if enabled_scrapers:
enabled_scrapers = enabled_scrapers.split(",")
not_valid = list(
filter(
lambda scraper: scraper not in ScraperFactory.all_scrapers_name(),
enabled_scrapers,
)
)
if not_valid:
raise ValueError(f"ENABLED_SCRAPERS contains invalid {not_valid}")
kwargs["enabled_scrapers"] = enabled_scrapers
# validate file types
enabled_file_types = os.getenv("ENABLED_FILE_TYPES", None)
if enabled_file_types:
enabled_file_types = enabled_file_types.split(",")
not_valid = list(
filter(
lambda f_types: f_types not in FileTypesFilters.all_types(),
enabled_file_types,
)
)
if not_valid:
raise ValueError(f"ENABLED_FILE_TYPES contains invalid {not_valid}")
kwargs["files_types"] = enabled_file_types
# validate number of processes
number_of_processes = os.getenv("NUMBER_OF_PROCESSES", None)
if number_of_processes:
try:
kwargs["multiprocessing"] = int(number_of_processes)
except ValueError:
raise ValueError("NUMBER_OF_PROCESSES must be an integer")
# validate limit
limit = os.getenv("LIMIT", None)
if limit:
try:
kwargs["limit"] = int(limit)
except ValueError:
raise ValueError(f"LIMIT must be an integer, but got {limit}")
# validate today
today = os.getenv("TODAY", None)
if today:
try:
kwargs["when_date"] = datetime.datetime.strptime(today, "%Y-%m-%d %H:%M")
except ValueError:
raise ValueError("TODAY must be in the format 'YYYY-MM-DD HH:MM'")
return kwargs
if __name__ == "__main__":
args = load_params()
ScarpingTask(**args).start()