Created
April 5, 2023 09:19
-
-
Save chr5tphr/4e25f04f76389afb4ff3c456fe22facf to your computer and use it in GitHub Desktop.
Export multiple events from khal in .ics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from datetime import datetime | |
import click | |
from khal.settings import get_config | |
from khal.cli import build_collection | |
from icalendar import Calendar | |
@click.command() | |
@click.option('--search', 'search_string') | |
@click.option('--allow-past/--future-only', default=False) | |
def main(search_string, allow_past): | |
conf = get_config(None) | |
collection = build_collection(conf, None) | |
now = conf['locale']['local_timezone'].localize(datetime.now()) | |
vevents = [ | |
Calendar.from_ical(event.raw) | |
for event in sorted(collection.search(search_string)) | |
if ( | |
allow_past | |
or event.allday and event.end >= now.date() | |
or not event.allday and event.end_local >= now | |
) | |
] | |
event_collection = Calendar(next(iter(vevents), {})) | |
for event in vevents: | |
for component in event.subcomponents: | |
event_collection.add_component(component) | |
click.echo(event_collection.to_ical()) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment