1
0

Parse urls

This commit is contained in:
Jon Michael Aanes 2024-08-25 20:38:16 +02:00
parent 8ca9c36a6e
commit 7c1d6003f4
Signed by: Jmaa
SSH Key Fingerprint: SHA256:Ab0GfHGCblESJx7JRE4fj4bFy/KRpeLhi41y4pF3sNA
2 changed files with 34 additions and 17 deletions

View File

@ -1,41 +1,44 @@
import csv import csv
import datetime import datetime
import decimal import decimal
import inspect
import io import io
import urllib.parse
from pathlib import Path from pathlib import Path
import logging import logging
from collections.abc import Iterable, Mapping, Sequence from collections.abc import Iterable, Mapping, Sequence, Callable
from decimal import Decimal from decimal import Decimal
import requests import typing
import requests_cache
from frozendict import frozendict from frozendict import frozendict
from . import notification, data from . import data
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
CSV_DIALECT = 'one_true_dialect' CSV_DIALECT = 'one_true_dialect'
csv.register_dialect(CSV_DIALECT, lineterminator='\n', skipinitialspace=True) csv.register_dialect(CSV_DIALECT, lineterminator='\n', skipinitialspace=True)
def try_value(fn, s: str) -> object: T = typing.TypeVar('T')
def try_value(fn: Callable[[str],T], s: str) -> T | None:
try: try:
return fn(s) return fn(s)
except (ValueError, decimal.InvalidOperation): except (ValueError, decimal.InvalidOperation):
return None return None
def to_value(s: str) -> object: def to_value(s: str) -> str | Decimal | datetime.date | datetime.datetime | urllib.parse.ParseResult | bool | None:
s = s.strip() s = s.strip()
if len(s) == 0: if len(s) == 0:
return None return None
if (v := try_value(Decimal, s)) is not None: if (v_decimal := try_value(Decimal, s)) is not None:
return v return v_decimal
if v := try_value(datetime.date.fromisoformat, s): if (v_date := try_value(datetime.date.fromisoformat, s)) is not None:
return v return v_date
if v := try_value(datetime.datetime.fromisoformat, s): if (v_datetime := try_value(datetime.datetime.fromisoformat, s)) is not None:
return v return v_datetime
if s.startswith(('http://', 'https://')):
return urllib.parse.urlparse(s)
if s.lower() == 'false': if s.lower() == 'false':
return False return False
if s.lower() == 'true': if s.lower() == 'true':

View File

@ -1,6 +1,7 @@
import argparse import argparse
import logging import logging
import icalendar import icalendar
import urllib.parse
import datetime import datetime
import csv import csv
@ -20,16 +21,29 @@ def generate_calendar(rows: list[dict]) -> icalendar.Calendar:
cal.add('version', '2.0') cal.add('version', '2.0')
for event_data in rows: for event_data in rows:
# Select data
print(event_data) print(event_data)
# Select data
possible_time_keys = [k for k,v in event_data.items() if isinstance(v, datetime.date)]
possible_name_keys = [k for k,v in event_data.items() if isinstance(v, str)]
possible_image_keys = [k for k,v in event_data.items() if isinstance(v, urllib.parse.ParseResult)]
date = event_data[possible_time_keys[0]] if possible_time_keys else None
title = event_data[possible_name_keys[0]]
image = event_data[possible_image_keys[0]] if possible_image_keys else None
if date is None:
continue
# Create event # Create event
event = icalendar.Event() event = icalendar.Event()
event.add('summary', f'Event {i}') event.add('summary', title)
event.add('dtstart', datetime.datetime(2005,4,4,8,0,0,tzinfo=datetime.UTC)) event.add('dtstart', date)
event.add('dtend', datetime.datetime(2005,4,4,10,0,0,tzinfo=datetime.UTC)) event.add('dtend', date)
event.add('created', NOW) event.add('created', NOW)
event.add('dtstamp', NOW) event.add('dtstamp', NOW)
if image:
event.add('image', image.geturl())
cal.add_component(event) cal.add_component(event)
del event del event