1
0
This commit is contained in:
Jon Michael Aanes 2024-08-25 20:50:03 +02:00
parent aebf3c7df4
commit 595640efdf
Signed by: Jmaa
SSH Key Fingerprint: SHA256:Ab0GfHGCblESJx7JRE4fj4bFy/KRpeLhi41y4pF3sNA
5 changed files with 44 additions and 27 deletions

View File

@ -71,7 +71,11 @@ def has_data_attribute(e) -> bool:
def normalize_soup_slightly( def normalize_soup_slightly(
soup, classes=True, scripts=True, comments=True, data_attributes=True, soup,
classes=True,
scripts=True,
comments=True,
data_attributes=True,
): ):
"""Perform soup normalization.""" """Perform soup normalization."""
# Little if any content # Little if any content

View File

@ -1,18 +1,12 @@
import csv
import datetime
import decimal
import inspect import inspect
import io
from pathlib import Path
import logging import logging
from collections.abc import Iterable, Mapping, Sequence from collections.abc import Sequence
from decimal import Decimal from pathlib import Path
import requests import requests
import requests_cache import requests_cache
from frozendict import frozendict
from . import notification, data from . import data, notification
from .util import * from .util import *
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)

View File

@ -31,7 +31,8 @@ def parse_duration(text: str) -> datetime.timedelta:
def parse_response_datetime(response) -> datetime.datetime: def parse_response_datetime(response) -> datetime.datetime:
return datetime.datetime.strptime( return datetime.datetime.strptime(
response.headers['Date'], FORMAT_DATE_HEADER, response.headers['Date'],
FORMAT_DATE_HEADER,
).replace(tzinfo=datetime.UTC) ).replace(tzinfo=datetime.UTC)

View File

@ -2,13 +2,13 @@ import csv
import datetime import datetime
import decimal import decimal
import io import io
import urllib.parse
from pathlib import Path
import logging import logging
from collections.abc import Iterable, Mapping, Sequence, Callable
from decimal import Decimal
import typing import typing
import urllib.parse
from collections.abc import Callable, Iterable, Mapping, Sequence
from decimal import Decimal
from pathlib import Path
from frozendict import frozendict from frozendict import frozendict
from . import data from . import data
@ -20,14 +20,25 @@ csv.register_dialect(CSV_DIALECT, lineterminator='\n', skipinitialspace=True)
T = typing.TypeVar('T') T = typing.TypeVar('T')
def try_value(fn: Callable[[str],T], s: str) -> T | None:
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) -> str | Decimal | datetime.date | datetime.datetime | urllib.parse.ParseResult | bool | None: 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
@ -139,6 +150,7 @@ def load_csv_file(csv_file: Path) -> list[frozendict]:
del csvfile del csvfile
return dicts return dicts
def extend_csv_file( def extend_csv_file(
csv_file: Path, csv_file: Path,
new_dicts: list[dict], new_dicts: list[dict],
@ -175,7 +187,7 @@ def extend_csv_file(
output_csv = csvfile_in_memory.getvalue() output_csv = csvfile_in_memory.getvalue()
del writer, csvfile_in_memory del writer, csvfile_in_memory
csv_file.parent.mkdir(parents=True,exist_ok=True) csv_file.parent.mkdir(parents=True, exist_ok=True)
with open(csv_file, 'w') as csvfile: with open(csv_file, 'w') as csvfile:
csvfile.write(output_csv) csvfile.write(output_csv)
del csvfile del csvfile

View File

@ -1,13 +1,13 @@
import argparse import argparse
import logging
import icalendar
import urllib.parse
import datetime import datetime
import csv import urllib.parse
import icalendar
from personal_data.util import load_csv_file from personal_data.util import load_csv_file
NOW = datetime.datetime.now(tz = datetime.UTC) NOW = datetime.datetime.now(tz=datetime.UTC)
def parse_arguments(): def parse_arguments():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
@ -15,6 +15,7 @@ def parse_arguments():
parser.add_argument('output_file') parser.add_argument('output_file')
return parser.parse_args() return parser.parse_args()
def generate_calendar(rows: list[dict]) -> icalendar.Calendar: def generate_calendar(rows: list[dict]) -> icalendar.Calendar:
cal = icalendar.Calendar() cal = icalendar.Calendar()
cal.add('prodid', '-//personal_data_calendar//example.org//') cal.add('prodid', '-//personal_data_calendar//example.org//')
@ -24,9 +25,13 @@ def generate_calendar(rows: list[dict]) -> icalendar.Calendar:
print(event_data) print(event_data)
# Select data # Select data
possible_time_keys = [k for k,v in event_data.items() if isinstance(v, datetime.date)] possible_time_keys = [
possible_name_keys = [k for k,v in event_data.items() if isinstance(v, str)] k for k, v in event_data.items() if isinstance(v, datetime.date)
possible_image_keys = [k for k,v in event_data.items() if isinstance(v, urllib.parse.ParseResult)] ]
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 date = event_data[possible_time_keys[0]] if possible_time_keys else None
title = event_data[possible_name_keys[0]] title = event_data[possible_name_keys[0]]
@ -64,5 +69,6 @@ def main():
with open(args.output_file, 'wb') as f: with open(args.output_file, 'wb') as f:
f.write(calendar.to_ical()) f.write(calendar.to_ical())
if __name__ == '__main__': if __name__ == '__main__':
main() main()