Skip to content

Commit

Permalink
Merge pull request #96 from ashwinpra/master
Browse files Browse the repository at this point in the history
Giving user option to generate ICS / add events / delete events within gyft.py
  • Loading branch information
proffapt committed Aug 12, 2023
2 parents b32a0b6 + 4a837a1 commit 2a7715f
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 197 deletions.
13 changes: 1 addition & 12 deletions add_events.py
Expand Up @@ -14,13 +14,6 @@
import datetime
DEBUG = False

try:
import argparse

flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None

SCOPES = "https://www.googleapis.com/auth/calendar"
CLIENT_SECRET_FILE = "client_secret.json"
APPLICATION_NAME = "gyft"
Expand Down Expand Up @@ -72,7 +65,7 @@ def get_credentials():
###


def main():
def create_calendar():
"""Shows basic usage of the Google Calendar API.
Creates a Google Calendar API service object and outputs a list of the next
Expand Down Expand Up @@ -154,7 +147,3 @@ def main():
if DEBUG:
break
print("\n\nEvents added to calendar\n")


if __name__ == "__main__":
main()
12 changes: 1 addition & 11 deletions del_events.py
Expand Up @@ -13,12 +13,6 @@
from oauth2client import file
import datetime

try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None

SCOPES = 'https://www.googleapis.com/auth/calendar'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'gyft'
Expand Down Expand Up @@ -53,7 +47,7 @@ def get_credentials():
return credentials


def main():
def delete_calendar():
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('calendar', 'v3', http=http)
Expand All @@ -72,7 +66,3 @@ def main():
eventId=event["id"]).execute()
print("Deleted: ", event["summary"], event["start"])
print("Deletion done!")


if __name__ == '__main__':
main()
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
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("\nYour timetable has been written to %s" % output_filename)

0 comments on commit 2a7715f

Please sign in to comment.