Skip to content

Instantly share code, notes, and snippets.

@Katakompe
Created October 4, 2020 21:17
Show Gist options
  • Save Katakompe/9ddeb9d57dbe69ebcaff753e7169ddeb to your computer and use it in GitHub Desktop.
Save Katakompe/9ddeb9d57dbe69ebcaff753e7169ddeb to your computer and use it in GitHub Desktop.
ICS-File Editor for Ufind Calendar exports
"""
Short script used to edit ics files from https://ufind.univie.ac.at/. It removes the leading course number. Additionally it can also remove the lv type.
"""
import re
from os import listdir
from os.path import isfile, join, exists
import sys
from typing import List
#Indicates if the LV Type should also be removed (VU, VO, etc.)
remove_lv_type = False
#Regex to match the course number
re_course_number = re.compile(r"(?<=SUMMARY:)\d+-\d+ ")
#Regex to match the course type and the course number
re_course_number_and_type = re.compile(r"(?<=SUMMARY:)\d+-\d+ \w+ ")
def get_flags(args: List[str]):
return [arg for arg in args if arg.startswith("-")]
def print_help_message():
print("Script to remove the leading course number from ufind ical files.")
print("Run the script by typing \"python fix_ical.py <flags> <filenames>\".\n")
print("<filenames> stands for one or more files you want to edit.\nMake sure not to forget the ics file ending in the name.\nYou can also pass folders instead of files. Then the script will take any file with the ics ending in the folder and do the change.\n")
print("<flags> stands for additional flags you can add.\nThere are currently two. \"-h\" prints this message. \"-t\" additionally removes the type of the course (eg. VU, VO, etc.).")
print("This script is not tested thoroughly, so make sure to backup your ics files before usage.\n")
def edit_file(file_or_folder):
if not exists(file_or_folder):
print(f"Datei ${file_or_folder} existiert nicht.")
return
if isfile(file_or_folder):
with open(file_or_folder, "r+") as file:
content = file.read()
regex = re_course_number_and_type if remove_lv_type else re_course_number
result = regex.sub("", content)
file.seek(0)
file.truncate()
file.write(result)
print(f"Changed '${file_or_folder}'.")
else:
#edit all files in the specified subfolder "file_or_folder"
[edit_file(join(file_or_folder, f)) for f in listdir(file_or_folder) if isfile(join(file_or_folder, f)) and f.endswith(".ics")]
args = sys.argv[1:]
flags = get_flags(args)
files = [arg for arg in args if arg not in flags]
if "-h" in flags:
print_help_message()
if "-t" in flags:
remove_lv_type = True
for file in files:
edit_file(file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment