forked from open-austin/eviction-hearing-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_all_filings_settings_since_date.py
More file actions
112 lines (87 loc) · 3.58 KB
/
Copy pathget_all_filings_settings_since_date.py
File metadata and controls
112 lines (87 loc) · 3.58 KB
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
"""Script to get all filings and settings since a given dates. To use: python get_all_filings_since_date.py (m)m-(d)d-yyyy
All dates in this script are in the format (m)m-(d)d-yyyy"""
from datetime import date, datetime, timedelta
import sys
from typing import List, Tuple
import logging
from colorama import Fore, Style
import click
from emailing import send_email
import parse_filings
import parse_settings
logger = logging.getLogger()
logging.basicConfig(stream=sys.stdout)
logger.setLevel(logging.INFO)
def split_into_weeks(start: str, end: str) -> List[Tuple[str, str]]:
"""Returns a list of tuples representing the start and end dates for all weeks between the specified start and end dates"""
start_date = datetime.strptime(start, "%m-%d-%Y").date()
end_date = datetime.strptime(end, "%m-%d-%Y").date()
days_in_range = ((end_date - start_date).days) + 1
if days_in_range > 7:
first_end_date = start_date + timedelta(days=6)
first_end_date_str = first_end_date.strftime("%-m-%-d-%Y")
next_start_date_str = (first_end_date + timedelta(days=1)).strftime(
"%-m-%-d-%Y"
)
return [(start, first_end_date_str)] + split_into_weeks(
next_start_date_str, end
)
else:
return [(start, end)]
def try_to_parse(start: str, end: str, tries: int) -> str:
"""
Parses filings and settings between start and end dates.
Tries `tries` times before giving up.
If all attempts fail, returns the start and end date, otherwise returns 'success'.
"""
for attempt in range(1, tries + 1):
try:
parse_filings.parse_filings_on_cloud(start, end, get_old_active=False)
parse_settings.parse_settings_on_cloud(start, end, write_to_sheets=False)
logger.info(
Fore.GREEN
+ f"Successfully parsed filings and settings between {start} and {end} on attempt {attempt}.\n"
+ Style.RESET_ALL
)
return "success"
except Exception as error:
if attempt == tries:
logger.error(f"Error message: {error}")
message = f"{start}, {end}"
logger.error(
Fore.RED
+ f"Failed to parse filings and settings between {start} and {end} on all {tries} attempts.\n"
+ Style.RESET_ALL
)
return message
def get_all_filings_settings_since_date(start_date: str):
"""Gets all filings and settings since `start_date` but splits it up by week. Logs the weeks that failed."""
yesterdays_date = (date.today() - timedelta(days=1)).strftime("%-m-%-d-%Y")
weeks = split_into_weeks(start_date, yesterdays_date)
logger.info(
f"Will get all filings and settings between {start_date} and {yesterdays_date}\n"
)
failures = []
for week_start, week_end in weeks:
msg = try_to_parse(week_start, week_end, 5)
if msg != "success":
failures.append(msg)
if failures:
failures_str = "\n".join(failures)
logger.info("All failures:")
logger.info(Fore.RED + failures_str + Style.RESET_ALL)
send_email(
failures_str, "Date ranges for which parsing filings and settings failed"
)
else:
logger.info(
Fore.GREEN
+ f"There were no failures when getting all filings between {start_date} and {yesterdays_date} - yay!!"
+ Style.RESET_ALL
)
if __name__ == "__main__":
@click.command()
@click.argument("date", nargs=1)
def get_all_since_date(date):
get_all_filings_settings_since_date(date)
get_all_since_date()