Demangle "Morgen" too

This commit is contained in:
I-Al-Istannen 2020-04-30 12:00:21 +02:00
parent 920d521d68
commit 42345ecc61
2 changed files with 19 additions and 11 deletions

View File

@ -139,7 +139,7 @@ class IliasCrawler:
all_properties_text all_properties_text
) )
if modification_date_match is None: if modification_date_match is None:
modification_date = datetime.datetime.now() modification_date = None
LOGGER.warning("Could not extract start date from %r", all_properties_text) LOGGER.warning("Could not extract start date from %r", all_properties_text)
else: else:
modification_date_str = modification_date_match.group(1) modification_date_str = modification_date_match.group(1)
@ -331,14 +331,6 @@ class IliasCrawler:
LOGGER.debug("Found exercise container %r", container_name) LOGGER.debug("Found exercise container %r", container_name)
# Parse the end date to use as modification date
# TODO: Return None?
end_date: datetime.datetime = datetime.datetime.now()
end_date_header: bs4.Tag = container.find(name="div", text="Abgabetermin")
if end_date_header is not None:
end_date_text = end_date_header.findNext("div").getText().strip()
end_date = demangle_date(end_date_text)
# Grab each file as you now have the link # Grab each file as you now have the link
for file_link in files: for file_link in files:
# Two divs, side by side. Left is the name, right is the link ==> get left # Two divs, side by side. Left is the name, right is the link ==> get left
@ -351,7 +343,7 @@ class IliasCrawler:
results.append(IliasDownloadInfo( results.append(IliasDownloadInfo(
Path(element_path, container_name, file_name), Path(element_path, container_name, file_name),
url, url,
end_date None # We do not have any timestamp
)) ))
return results return results

View File

@ -4,14 +4,22 @@ Helper methods to demangle an ILIAS date.
import datetime import datetime
import locale import locale
import logging
import re import re
from typing import Optional
from ..logging import PrettyLogger
LOGGER = logging.getLogger(__name__)
PRETTY = PrettyLogger(LOGGER)
def demangle_date(date: str) -> datetime.datetime: def demangle_date(date: str) -> Optional[datetime.datetime]:
""" """
Demangle a given date in one of the following formats: Demangle a given date in one of the following formats:
"Gestern, HH:MM" "Gestern, HH:MM"
"Heute, HH:MM" "Heute, HH:MM"
"Morgen, HH:MM"
"dd. mon.yyyy, HH:MM "dd. mon.yyyy, HH:MM
""" """
saved = locale.setlocale(locale.LC_ALL) saved = locale.setlocale(locale.LC_ALL)
@ -21,10 +29,18 @@ def demangle_date(date: str) -> datetime.datetime:
date = re.sub(r"\s+", " ", date) date = re.sub(r"\s+", " ", date)
date = date.replace("Gestern", _yesterday().strftime("%d. %b %Y")) date = date.replace("Gestern", _yesterday().strftime("%d. %b %Y"))
date = date.replace("Heute", datetime.date.today().strftime("%d. %b %Y")) date = date.replace("Heute", datetime.date.today().strftime("%d. %b %Y"))
date = date.replace("Morgen", _tomorrow().strftime("%d. %b %Y"))
return datetime.datetime.strptime(date, "%d. %b %Y, %H:%M") return datetime.datetime.strptime(date, "%d. %b %Y, %H:%M")
except ValueError:
PRETTY.warning(f"Could not parse date {date!r}")
return None
finally: finally:
locale.setlocale(locale.LC_ALL, saved) locale.setlocale(locale.LC_ALL, saved)
def _yesterday() -> datetime.date: def _yesterday() -> datetime.date:
return datetime.date.today() - datetime.timedelta(days=1) return datetime.date.today() - datetime.timedelta(days=1)
def _tomorrow() -> datetime.date:
return datetime.date.today() + datetime.timedelta(days=1)