Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Giving user option to generate ICS / add events / delete events within gyft.py #96

Merged
merged 11 commits into from Aug 12, 2023
96 changes: 30 additions & 66 deletions generate_ics.py
@@ -1,58 +1,32 @@
## Adds your timetable from `data.txt` to Google Calendar.
from __future__ import print_function
import os

import json
import datetime
import sys

# this script works only with Python 3
if sys.version_info[0] != 3:
print("This script works only with Python 3")
sys.exit(1)

import re
proffapt marked this conversation as resolved.
Show resolved Hide resolved
from icalendar import Calendar, Event

from icalendar import Calendar
import dates

WORKING_DAYS = dates.get_dates()

import build_event

import argparse
import getpass

parser = argparse.ArgumentParser()
parser.add_argument("-i", "--input")
parser.add_argument("-o", "--output")
args = parser.parse_args()

WORKING_DAYS = dates.get_dates()
DEBUG = False
GENERATE_ICS = True
TIMETABLE_DICT_RE = (
"([0-9]{1,2}):([0-9]{1,2}):([AP])M-([0-9]{1,2}):([0-9]{1,2}):([AP])M"
)
timetable_dict_parser = re.compile(TIMETABLE_DICT_RE)

INPUT_FILENAME = args.input if args.input else "data.txt"
if not os.path.exists(INPUT_FILENAME):
print("Input file", INPUT_FILENAME, "does not exist.")
os._exit(1)

OUTPUT_FILENAME = "timetable.ics" if args.output is None else args.output

cal = Calendar()
cal.add("prodid", "-//Your Timetable generated by GYFT//mxm.dk//")
cal.add("version", "1.0")

"""
Given a starting timestamp d and a weekday number d (0-6), return the timestamp
of the next time this weekday is going to happen
"""
### days to number
days = {}
days["Monday"] = 0
days["Tuesday"] = 1
days["Wednesday"] = 2
days["Thursday"] = 3
days["Friday"] = 4
days["Saturday"] = 5
###


def next_weekday(d, weekday):
"""
Given a starting timestamp d and a weekday number d (0-6), return the timestamp
of the next time this weekday is going to happen
"""
days_ahead = weekday - d.weekday()
if days_ahead < 0: # Target day already happened this week
days_ahead += 7
Expand Down Expand Up @@ -82,27 +56,21 @@ def get_stamp(argument, date):
date.year, date.month, date.day, hours_24_format, int(argument[1])
)

def generate_ICS(input_filename, output_filename):
"""
Creates an ICS file `timetable.ics` with the timetable data present inside the
input file `data.txt`
"""

### days to number
days = {}
days["Monday"] = 0
days["Tuesday"] = 1
days["Wednesday"] = 2
days["Thursday"] = 3
days["Friday"] = 4
days["Saturday"] = 5
###

"""
Creates an ICS file `timetable.ics` with the timetable data present inside the
input file `data.txt`
"""


def main():

TIMETABLE_DICT_RE = (
"([0-9]{1,2}):([0-9]{1,2}):([AP])M-([0-9]{1,2}):([0-9]{1,2}):([AP])M"
)
timetable_dict_parser = re.compile(TIMETABLE_DICT_RE)
cal = Calendar()
cal.add("prodid", "-//Your Timetable generated by GYFT//mxm.dk//")
cal.add("version", "1.0")
# Get your timetable
with open(INPUT_FILENAME) as data_file:
with open(input_filename) as data_file:
data = json.load(data_file)

found_missing_sub = False
Expand Down Expand Up @@ -166,10 +134,6 @@ def main():
print(event)


with open(OUTPUT_FILENAME, "wb") as f:
with open(output_filename, "wb") as f:
f.write(cal.to_ical())
print("INFO: Your timetable has been written to %s" % OUTPUT_FILENAME)


if __name__ == "__main__":
main()
print("INFO: Your timetable has been written to %s" % output_filename)
proffapt marked this conversation as resolved.
Show resolved Hide resolved