From b2d724dc7a58fc150b203ea129ab8af6e9e5e36f Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sun, 30 Jun 2013 16:22:52 -0400 Subject: [PATCH] test/aggregate.py: Add an example aggregation script This is how you use the library :). --- MANIFEST.in | 2 +- README | 5 +++++ test/aggregate.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100755 test/aggregate.py diff --git a/MANIFEST.in b/MANIFEST.in index 7c95064..b300d1b 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,2 +1,2 @@ include COPYING -recursive-include test README *.ics +recursive-include test README *.py *.ics diff --git a/README b/README index 053a1f9..2818559 100644 --- a/README +++ b/README @@ -31,6 +31,11 @@ Test with nose_:: $ nosetests --with-doctest --doctest-tests pycalendar +There is also a example aggregation script that prints aggregate feed +to stdout and geographic positions to stderr. Run it with:: + + $ PYTHONPATH=. test/aggregate.py + .. _Calagator: http://calagator.org/ .. _distutils: http://docs.python.org/3/distutils/ diff --git a/test/aggregate.py b/test/aggregate.py new file mode 100755 index 0000000..d2cd738 --- /dev/null +++ b/test/aggregate.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python +# +# Copyright + +"""An example aggregation script + +This script grabs all the ``.ics`` files in the ``test/`` directory +and aggregates them into a single iCalendar feed. While it's doing +this, it also prints and geographic positions to stderr. In a live +site, you could use a different version of ``get_geo`` to build +javascript that renders a map showing event locations. +""" + +import os as _os +import sys as _sys + +import pycalendar.aggregator as _pycalendar_aggregator +import pycalendar.feed as _pycalendar_feed + + +def get_geo(feed): + for event in feed['VEVENT']: + if 'GEO' in event: + lat,lon = [float(x) for x in event['GEO'].split(';')] + _sys.stderr.write('{} at lat {}, lon {}\n'.format( + event['UID'], lat, lon)) + + +def get_urls(root=_os.path.dirname(__file__)): + for dirpath, dirnames, filenames in _os.walk(root): + for filename in filenames: + base,ext = _os.path.splitext(filename) + if ext == '.ics': + path = _os.path.abspath(_os.path.join(dirpath, filename)) + yield 'file://{}'.format(path.replace(_os.sep, '/')) + + +def aggregate(): + aggregator = _pycalendar_aggregator.Aggregator( + prodid='-//pycalendar//NONSGML testing//EN', + feeds=[_pycalendar_feed.Feed(url=url) + for url in get_urls()], + processors=[get_geo], + ) + aggregator.fetch() + return aggregator + + +if __name__ == '__main__': + aggregator = aggregate() + aggregator.write(stream=_sys.stdout) -- 2.26.2