Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issues with chi_metro_pier_exposition spyder in parsing date #1134

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions city_scrapers/spiders/chi_metro_pier_exposition.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import datetime, time
import dateutil.parser

from city_scrapers_core.constants import BOARD, COMMITTEE
from city_scrapers_core.items import Meeting
Expand Down Expand Up @@ -72,8 +73,8 @@ def _parse_classification(self, title):

def _parse_start(self, item, classification):
"""Parse start datetime as a naive datetime object."""
date_str = item.css("td::text").extract_first().strip()
date_obj = datetime.strptime(date_str, "%B %d, %Y").date()
date_str = item.css("td::text").extract_first().strip().split("*")[-1]
date_obj = dateutil.parser.parse(date_str)
Comment on lines +76 to +77
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add error handling for date parsing

While using dateutil.parser improves date format handling, we should add error handling to gracefully handle unparseable dates.

-        date_str = item.css("td::text").extract_first().strip().split("*")[-1]
-        date_obj = dateutil.parser.parse(date_str)
+        date_str = item.css("td::text").extract_first()
+        if not date_str:
+            raise ValueError("No date string found")
+        date_str = date_str.strip().split("*")[-1]
+        try:
+            date_obj = dateutil.parser.parse(date_str)
+        except (ValueError, TypeError) as e:
+            raise ValueError(f"Failed to parse date '{date_str}': {str(e)}")
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
date_str = item.css("td::text").extract_first().strip().split("*")[-1]
date_obj = dateutil.parser.parse(date_str)
date_str = item.css("td::text").extract_first()
if not date_str:
raise ValueError("No date string found")
date_str = date_str.strip().split("*")[-1]
try:
date_obj = dateutil.parser.parse(date_str)
except (ValueError, TypeError) as e:
raise ValueError(f"Failed to parse date '{date_str}': {str(e)}")

time_obj = time(9)
if classification == COMMITTEE:
time_obj = time(13, 30)
Expand Down