Last active
July 1, 2022 10:34
-
-
Save bitnulleins/2f48d0e254fdeb5c8c64f1dd8019ac52 to your computer and use it in GitHub Desktop.
Merge iCal / ICS Files
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
<?php | |
function merge_ics($token, ...$ics_urls) { | |
/** | |
* Merge list of ICS files to one ICS file. Use ICS header from first ICS file. | |
* Download ICS file from remote server with *file_get_contents* | |
*/ | |
$ical_list = ""; | |
foreach($ics_urls as $key => $ics_file) { | |
$curl = curl_init(); | |
curl_setopt($curl, CURLOPT_URL, $ics_file); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Authorization: Basic ".$token]); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); | |
$ics_data = curl_exec($curl); | |
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
if($httpcode != 200 || curl_exec($ch) === false) { | |
die ('Error for url ' . $ics_file . ': ' . curl_error($ch)); | |
} else { | |
if (count($ics_urls) == 1) return $ics_data; | |
$events_begin = strpos($ics_data, 'BEGIN:VEVENT'); | |
$events_end = strpos($ics_data, 'END:VCALENDAR'); | |
if ($key == 0) { | |
$ical_header = substr($ics_data, 0, $events_begin); | |
$ical_footer = 'END:VCALENDAR'; | |
} | |
$ical_list .= substr($ics_data, $events_begin, $events_end - $events_begin); | |
} | |
} | |
return $ical_header . $ical_list . $ical_footer; | |
} |
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 | |
import requests | |
""" | |
Merge list of ICS files to one ICS file. Use ICS header from first ICS file. | |
Download ICS file from remote server with *file_get_contents* | |
""" | |
def merge_ics(header: dict, **urls: list[str]) -> str: | |
ical_list = "" | |
for key, ics_file in enumerate(urls): | |
try: | |
r = requests.get(ics_file, header=header) | |
r.raise_for_status() | |
ics_data = r.response.content | |
except requests.exceptions.HTTPError as err: | |
raise SystemExit(err) | |
events_begin = ics_data.find('BEGIN:VEVENT') | |
events_end = ics_data.find('END:VCALENDAR') | |
if key == 0: | |
ical_header = ics_data[0, events_begin] | |
ical_footer = 'END:VCALENDAR' | |
ical_list.append(ics_data[events_begin, events_end - events_begin]) | |
return f"{ical_header}{ical_list}{ical_footer}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment