Skip to content

Commit

Permalink
set RELATIVE_BASE for dateparser
Browse files Browse the repository at this point in the history
  • Loading branch information
neon-ninja committed May 18, 2022
1 parent f17d144 commit 6acbb2f
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions facebook_scraper/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ def make_html_element(html: str, url=DEFAULT_URL) -> Element:
datetime_regex = re.compile(fr"({exact_time}|{relative_time})", re.IGNORECASE)
day_of_week_regex = re.compile(fr"({day_of_week})", re.IGNORECASE)


def parse_datetime(text: str, search=True) -> Optional[datetime]:
"""Looks for a string that looks like a date and parses it into a datetime object.
Expand All @@ -187,6 +186,7 @@ def parse_datetime(text: str, search=True) -> Optional[datetime]:
Returns:
The datetime object, or None if it couldn't find a date.
"""
settings = {'RELATIVE_BASE': datetime.today().replace(minute=0, hour=0, second=0, microsecond=0)}
if search:
time_match = datetime_regex.search(text)
dow_match = day_of_week_regex.search(text)
Expand All @@ -197,9 +197,9 @@ def parse_datetime(text: str, search=True) -> Optional[datetime]:
today = calendar.day_abbr[datetime.today().weekday()]
if text == today:
# Fix for dateparser misinterpreting "last Monday" as today if today is Monday
return dateparser.parse(text) - timedelta(days=7)
return dateparser.parse(text, settings=settings) - timedelta(days=7)

result = dateparser.parse(text)
result = dateparser.parse(text, settings=settings)
if result:
return result.replace(microsecond=0)
return None
Expand Down

0 comments on commit 6acbb2f

Please sign in to comment.