Skip to content

Commit 292b53c

Browse files
authored
Merge pull request #1444 from Russell-IO/master
Add ICS calendar generation feature
2 parents b680b34 + 0692468 commit 292b53c

File tree

2 files changed

+458
-0
lines changed

2 files changed

+458
-0
lines changed

README.md

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,184 @@ Open the map viewer in VS Code:
374374
![Test Results Map](test_results_map.png)
375375
376376
---
377+
## ICS Calendar Generation
378+
379+
You can convert bin collection data to an ICS calendar file that can be imported into calendar applications like Google Calendar, Apple Calendar, Microsoft Outlook, etc.
380+
381+
### Overview
382+
383+
The `bin_to_ics.py` script allows you to:
384+
- Convert JSON output from bin collection data into ICS calendar events
385+
- Group multiple bin collections on the same day into a single event
386+
- Create all-day events (default) or timed events
387+
- Add optional reminders/alarms to events
388+
- Customize the calendar name
389+
390+
### Requirements
391+
392+
- Python 3.6 or higher
393+
- The `icalendar` package, which can be installed with:
394+
```bash
395+
pip install icalendar
396+
```
397+
398+
### Basic Usage
399+
400+
```bash
401+
# Basic usage with stdin input and default output file (bin.ics)
402+
python bin_to_ics.py < bin_data.json
403+
404+
# Specify input and output files
405+
python bin_to_ics.py -i bin_data.json -o my_calendar.ics
406+
407+
# Custom calendar name
408+
python bin_to_ics.py -i bin_data.json -o my_calendar.ics -n "My Bin Collections"
409+
```
410+
411+
### Options
412+
413+
```
414+
--input, -i Input JSON file (if not provided, read from stdin)
415+
--output, -o Output ICS file (default: bin.ics)
416+
--name, -n Calendar name (default: Bin Collections)
417+
--alarms, -a Comma-separated list of alarm times before event (e.g., "1d,2h,30m")
418+
--no-all-day Create timed events instead of all-day events
419+
```
420+
421+
### Examples
422+
423+
#### Adding Reminders (Alarms)
424+
425+
Add reminders 1 day and 2 hours before each collection:
426+
427+
```bash
428+
python bin_to_ics.py -i bin_data.json -a "1d,2h"
429+
```
430+
431+
The time format supports:
432+
- Days: `1d`, `2day`, `3days`
433+
- Hours: `1h`, `2hour`, `3hours`
434+
- Minutes: `30m`, `45min`, `60mins`, `90minutes`
435+
436+
#### Creating Timed Events
437+
438+
By default, events are created as all-day events. To create timed events instead (default time: 7:00 AM):
439+
440+
```bash
441+
python bin_to_ics.py -i bin_data.json --no-all-day
442+
```
443+
444+
### Integration with Bin Collection Data Retriever
445+
446+
You can pipe the output from the bin collection data retriever directly to the ICS generator. The required parameters (postcode, house number, UPRN, etc.) depend on the specific council implementation - refer to the [Quickstart](#quickstart) section above or check the [project wiki](https://github.com/robbrad/UKBinCollectionData/wiki) for details about your council.
447+
448+
```bash
449+
python uk_bin_collection/uk_bin_collection/collect_data.py CouncilName "URL" [OPTIONS] |
450+
python bin_to_ics.py [OPTIONS]
451+
```
452+
453+
#### Complete Example for a Council
454+
455+
```bash
456+
python uk_bin_collection/uk_bin_collection/collect_data.py CouncilName \
457+
"council_url" \
458+
-p "YOUR_POSTCODE" \
459+
-n "YOUR_HOUSE_NUMBER" \
460+
-w "http://localhost:4444/wd/hub" |
461+
python bin_to_ics.py \
462+
--name "My Bin Collections" \
463+
--output my_bins.ics \
464+
--alarms "1d,12h"
465+
```
466+
467+
This will:
468+
1. Fetch bin collection data for your address from your council's website
469+
2. Convert it to an ICS file named "my_bins.ics"
470+
3. Set the calendar name to "My Bin Collections"
471+
4. Add reminders 1 day and 12 hours before each collection
472+
473+
For postcode lookup and UPRN information, please check the [UPRN Finder](#uprn-finder) section above.
474+
475+
### Using the Calendar
476+
477+
You have two options for using the generated ICS file:
478+
479+
#### 1. Importing the Calendar
480+
481+
You can directly import the ICS file into your calendar application:
482+
483+
- **Google Calendar**: Go to Settings > Import & export > Import
484+
- **Apple Calendar**: File > Import
485+
- **Microsoft Outlook**: File > Open & Export > Import/Export > Import an iCalendar (.ics)
486+
487+
Note: Importing creates a static copy of the calendar events. If bin collection dates change, you'll need to re-import the calendar.
488+
489+
#### 2. Subscribing to the Calendar
490+
491+
If you host the ICS file on a publicly accessible web server, you can subscribe to it as an internet calendar:
492+
493+
- **Google Calendar**: Go to "Other calendars" > "+" > "From URL" > Enter the URL of your hosted ICS file
494+
- **Apple Calendar**: File > New Calendar Subscription > Enter the URL
495+
- **Microsoft Outlook**: File > Account Settings > Internet Calendars > New > Enter the URL
496+
497+
Benefits of subscribing:
498+
- Calendar automatically updates when the source file changes
499+
- No need to manually re-import when bin collection dates change
500+
- Easily share the calendar with household members
501+
502+
You can set up a cron job or scheduled task to regularly:
503+
1. Retrieve the latest bin collection data
504+
2. Generate a fresh ICS file
505+
3. Publish it to a web-accessible location
506+
507+
### Additional Examples and Use Cases
508+
509+
#### Automation with Cron Jobs
510+
511+
Create a weekly update script on a Linux/Mac system:
512+
513+
```bash
514+
#!/bin/bash
515+
# File: update_bin_calendar.sh
516+
517+
# Set variables
518+
COUNCIL="YourCouncilName"
519+
COUNCIL_URL="https://your-council-website.gov.uk/bins"
520+
POSTCODE="YOUR_POSTCODE"
521+
HOUSE_NUMBER="YOUR_HOUSE_NUMBER"
522+
OUTPUT_DIR="/var/www/html/calendars" # Web-accessible directory
523+
CALENDAR_NAME="Household Bins"
524+
525+
# Ensure output directory exists
526+
mkdir -p $OUTPUT_DIR
527+
528+
# Run the collector and generate the calendar
529+
cd /path/to/UKBinCollectionData && \
530+
python uk_bin_collection/uk_bin_collection/collect_data.py $COUNCIL "$COUNCIL_URL" \
531+
-p "$POSTCODE" -n "$HOUSE_NUMBER" | \
532+
python bin_to_ics.py --name "$CALENDAR_NAME" --output "$OUTPUT_DIR/bins.ics" --alarms "1d,6h"
533+
534+
# Add timestamp to show last update time
535+
echo "Calendar last updated: $(date)" > "$OUTPUT_DIR/last_update.txt"
536+
```
537+
538+
Make the script executable:
539+
```bash
540+
chmod +x update_bin_calendar.sh
541+
```
542+
543+
Add to crontab to run weekly (every Monday at 2 AM):
544+
```bash
545+
0 2 * * 1 /path/to/update_bin_calendar.sh
546+
```
547+
548+
**Google Assistant/Alexa Integration**
549+
550+
If you have your calendar connected to Google Calendar or Outlook, you can ask your smart assistant about upcoming bin collections:
551+
552+
- "Hey Google, when is my next bin collection?"
553+
- "Alexa, what's on my calendar tomorrow?" (will include bin collections)
554+
377555
## Docker API Server
378556
We have created an API for this located under [uk_bin_collection_api_server](https://github.com/robbrad/UKBinCollectionData/uk_bin_collection_api_server)
379557

0 commit comments

Comments
 (0)