On factorise la gestion des valeurs par défaut

This commit is contained in:
Jean-Marie Favreau 2024-10-19 11:14:32 +02:00
parent 7a46bf4733
commit 58ca1a7f85
11 changed files with 27 additions and 19 deletions

View File

@ -107,14 +107,13 @@ class CExtractor(TwoStepsExtractor):
self.downloader.pause = pause self.downloader.pause = pause
category = None category = None
if "category" in default_values:
category = default_values["category"]
if len(categories) > 0: if len(categories) > 0:
category = categories[0] category = categories[0]
for dt in datetimes: for dt in datetimes:
self.add_event_with_props( self.add_event_with_props(
default_values,
event_url, event_url,
title, title,
category, category,

View File

@ -41,7 +41,5 @@ class CExtractor(TwoStepsExtractor):
for event in fevent.build_events(event_url): for event in fevent.build_events(event_url):
event["published"] = published event["published"] = published
if "category" in default_values: self.add_event(default_values, **event)
event["category"] = default_values["category"]
self.add_event(**event)

View File

@ -93,6 +93,7 @@ class CExtractor(TwoStepsExtractor):
url_human = event_url url_human = event_url
self.add_event_with_props( self.add_event_with_props(
default_values,
event_url, event_url,
None, None,
None, None,

View File

@ -68,6 +68,7 @@ class CExtractor(TwoStepsExtractor):
url_human = event_url url_human = event_url
self.add_event_with_props( self.add_event_with_props(
default_values,
event_url, event_url,
title, title,
category, category,

View File

@ -76,6 +76,7 @@ class CExtractor(TwoStepsExtractor):
description = None description = None
self.add_event_with_props( self.add_event_with_props(
default_values,
event_url, event_url,
title, title,
"Concert", "Concert",

View File

@ -69,6 +69,7 @@ class CExtractor(TwoStepsExtractor):
url_human = event_url url_human = event_url
self.add_event_with_props( self.add_event_with_props(
default_values,
event_url, event_url,
None, None,
None, None,

View File

@ -81,11 +81,11 @@ class CExtractor(TwoStepsExtractor):
url_human = event_url url_human = event_url
self.add_event_with_props( self.add_event_with_props(
default_values,
event_url, event_url,
None, None,
None, None,
start_day, start_day,
None if "location" not in default_values else default_values["location"],
description, description,
None, None,
recurrences=None, recurrences=None,

View File

@ -152,6 +152,7 @@ class Extractor(ABC):
def add_event( def add_event(
self, self,
default_values,
title, title,
category, category,
start_day, start_day,
@ -176,14 +177,18 @@ class Extractor(ABC):
print("ERROR: cannot import an event without start day") print("ERROR: cannot import an event without start day")
return return
tags_default = self.default_value_if_exists(default_values, "tags")
if not tags_default:
tags_default = []
event = { event = {
"title": title, "title": title,
"category": category, "category": category if category else self.default_value_if_exists(default_values, "category"),
"start_day": start_day, "start_day": start_day,
"uuids": uuids, "uuids": uuids,
"location": location, "location": location if location else self.default_value_if_exists(default_values, "location"),
"description": description, "description": description,
"tags": tags, "tags": tags + tags_default,
"published": published, "published": published,
"image": image, "image": image,
"image_alt": image_alt, "image_alt": image_alt,

View File

@ -278,7 +278,7 @@ class FacebookEventExtractor(Extractor):
if default_values and "category" in default_values: if default_values and "category" in default_values:
event["category"] = default_values["category"] event["category"] = default_values["category"]
self.add_event(**event) self.add_event(default_values, **event)
return self.get_structure() return self.get_structure()
else: else:
logger.warning("cannot find any event in page") logger.warning("cannot find any event in page")

View File

@ -19,7 +19,6 @@ class GoogleCalendarLinkEventExtractor(Extractor):
def extract( def extract(
self, content, url, url_human=None, default_values=None, published=False self, content, url, url_human=None, default_values=None, published=False
): ):
# default_values are not used
soup = BeautifulSoup(content, "html.parser") soup = BeautifulSoup(content, "html.parser")
for ggu in self.possible_urls: for ggu in self.possible_urls:
@ -41,12 +40,10 @@ class GoogleCalendarLinkEventExtractor(Extractor):
self.set_header(url) self.set_header(url)
if "category" in default_values: category = None
category = default_values["category"]
else:
category = None
self.add_event( self.add_event(
default_values,
title=title, title=title,
category=category, category=category,
start_day=start_day, start_day=start_day,

View File

@ -78,7 +78,7 @@ class ICALExtractor(Extractor):
for event in calendar.walk("VEVENT"): for event in calendar.walk("VEVENT"):
title = self.get_item_from_vevent(event, "SUMMARY") title = self.get_item_from_vevent(event, "SUMMARY")
category = self.default_value_if_exists(default_values, "category") category = None
start_day, start_time = self.get_dt_item_from_vevent(event, "DTSTART") start_day, start_time = self.get_dt_item_from_vevent(event, "DTSTART")
@ -91,8 +91,8 @@ class ICALExtractor(Extractor):
end_day = end_day + timedelta(days=-1) end_day = end_day + timedelta(days=-1)
location = self.get_item_from_vevent(event, "LOCATION") location = self.get_item_from_vevent(event, "LOCATION")
if location is None or location.replace(" ", "") == "": if location.replace(" ", "") == "":
location = self.default_value_if_exists(default_values, "location") location = None
description = self.get_item_from_vevent(event, "DESCRIPTION") description = self.get_item_from_vevent(event, "DESCRIPTION")
if description is not None: if description is not None:
@ -127,7 +127,7 @@ class ICALExtractor(Extractor):
) )
# possible limitation: if the ordering is not original then related # possible limitation: if the ordering is not original then related
tags = self.default_value_if_exists(default_values, "tags") tags = None
last_modified = self.get_item_from_vevent(event, "LAST-MODIFIED", raw=True) last_modified = self.get_item_from_vevent(event, "LAST-MODIFIED", raw=True)
@ -158,6 +158,7 @@ class ICALExtractor(Extractor):
if uuidrel is not None: if uuidrel is not None:
luuids += [uuidrel] luuids += [uuidrel]
self.add_event( self.add_event(
default_values,
title, title,
category, category,
start_day, start_day,
@ -182,6 +183,7 @@ class ICALExtractor(Extractor):
class ICALNoBusyExtractor(ICALExtractor): class ICALNoBusyExtractor(ICALExtractor):
def add_event( def add_event(
self, self,
default_values,
title, title,
category, category,
start_day, start_day,
@ -201,6 +203,7 @@ class ICALNoBusyExtractor(ICALExtractor):
): ):
if title != "Busy" and title != "Accueils bénévoles": if title != "Busy" and title != "Accueils bénévoles":
super().add_event( super().add_event(
default_values,
title, title,
category, category,
start_day, start_day,
@ -241,6 +244,7 @@ class ICALNoVCExtractor(ICALExtractor):
def add_event( def add_event(
self, self,
default_values,
title, title,
category, category,
start_day, start_day,
@ -259,6 +263,7 @@ class ICALNoVCExtractor(ICALExtractor):
image_alt=None, image_alt=None,
): ):
super().add_event( super().add_event(
default_values,
title, title,
category, category,
start_day, start_day,