Config changes us hud income limits#2084
Conversation
There was a problem hiding this comment.
Code Review
This pull request updates the HUD income processing script to include the current year in both the download and processing loops. The reviewer noted that including the current year can cause the script to crash early in the year if the current year's data is not yet released (resulting in a 404 error and a fatal log). They suggest checking the availability of the current year's file with a lightweight request before attempting to download it.
| for year in range(2006, today.year + 1): | ||
| url = get_url(year) | ||
| if url: | ||
| filename = f"Section8-FY{year}.xlsx" if year > 2016 else f"Section8-FY{year}.xls" | ||
| download_file(url, filename, input_folder) |
There was a problem hiding this comment.
Including the current year (today.year) in the download loop is a good addition to fetch the latest data. However, HUD income limits for the current year are typically not released until April or May. During the first few months of any year, attempting to download the current year's file will result in a 404 error.
Because download_file uses logging.fatal on failure, any download failure will immediately terminate the entire script. This means the script will crash and fail to process even the historical years' data if run early in the year.
To prevent this, we should check if the current year's file is available using a quick GET request with stream=True before calling download_file. If it's not available, we can log a warning and gracefully continue.
| for year in range(2006, today.year + 1): | |
| url = get_url(year) | |
| if url: | |
| filename = f"Section8-FY{year}.xlsx" if year > 2016 else f"Section8-FY{year}.xls" | |
| download_file(url, filename, input_folder) | |
| for year in range(2006, today.year + 1): | |
| url = get_url(year) | |
| if url: | |
| filename = f"Section8-FY{year}.xlsx" if year > 2016 else f"Section8-FY{year}.xls" | |
| if year == today.year: | |
| try: | |
| headers = { | |
| 'User-Agent': | |
| 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36' | |
| } | |
| resp = requests.get(url, headers=headers, timeout=10, stream=True) | |
| if resp.status_code != 200: | |
| logging.warning(f"HUD income limits for {year} are not yet available. Skipping.") | |
| continue | |
| except Exception as e: | |
| logging.warning(f"Could not check availability for {year}: {e}. Skipping.") | |
| continue | |
| download_file(url, filename, input_folder) |
Made changes in downloading script to get the recent date.
Logic:
Resolving the range() function which has stop value, so Python terminates and does not execute the loop for this value .So skipping the recent year.