Skip to content

Instantly share code, notes, and snippets.

@lukaville
Created April 5, 2015 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukaville/7f9669720e7d54e73a63 to your computer and use it in GitHub Desktop.
Save lukaville/7f9669720e7d54e73a63 to your computer and use it in GitHub Desktop.
MobileUniversity to iCalendar
# encoding: utf-8
import click
import json
import os
from datetime import datetime
from datetime import timedelta
from icalendar import *
SUBJECT_DEFAULT_TIMES = [{
"time_start": timedelta(hours=8, minutes=30),
"time_end": timedelta(hours=10, minutes=05)
}, {
"time_start": timedelta(hours=10, minutes=15),
"time_end": timedelta(hours=11, minutes=50)
}, {
"time_start": timedelta(hours=12, minutes=00),
"time_end": timedelta(hours=13, minutes=35)
}, {
"time_start": timedelta(hours=13, minutes=50),
"time_end": timedelta(hours=15, minutes=25)
}, {
"time_start": timedelta(hours=15, minutes=40),
"time_end": timedelta(hours=17, minutes=15)
}, {
"time_start": timedelta(hours=17, minutes=25),
"time_end": timedelta(hours=19, minutes=00)
}, {
"time_start": timedelta(hours=19, minutes=10),
"time_end": timedelta(hours=20, minutes=45)
}]
SUBJECT_LENGTH = timedelta(hours=1, minutes=35)
WEEKDAYS = ['MO', 'TU', 'WE', 'TH', 'FR', 'SA', 'SU']
SUBJECT_TYPES = ["Семинар", "Лабораторная работа", "Лекция", "Практическое занятие", "Консультация", "Зачет", "Экзамен"]
@click.command()
@click.option('--input', prompt='Input JSON mobile university file', default='input.json')
@click.option('--output', prompt='Output iCalendar file', default='output.ics')
def main(input, output):
"""Converts mobile university JSON to iCal"""
with open(input) as json_file:
muniversity = json.load(json_file)
ical = convert(muniversity)
f = open(os.path.join(output), 'wb')
f.write(ical)
f.close()
def convert(json):
subjects = json['subjects']
date_start = datetime.fromtimestamp(json['date_start'])
date_end = datetime.fromtimestamp(json['date_end'])
cal = Calendar()
cal.add('dtstart', date_start)
cal.add('dtend', date_end)
for subject in subjects:
parity = subject['subject_parity']
weekday = subject['subject_weekday']
subject_times = SUBJECT_DEFAULT_TIMES[subject['subject_number'] - 1]
event = Event()
event.add('summary', subject['subject_name'])
event.add('description', SUBJECT_TYPES[subject['subject_type']])
event.add('location', subject['subject_groups'][0]['auditory_name'])
# Start and end time
event_start = date_start + timedelta(days=weekday) + subject_times['time_start']
if parity == 2:
event_start += timedelta(weeks=1)
event_end = event_start + SUBJECT_LENGTH
# Subject interval
event_interval = 1
if parity == 2 or parity == 1:
event_interval = 2
event.add('dtstart', event_start)
event.add('dtend', event_end)
event.add('rrule', {
'freq': 'weekly',
'interval': str(event_interval),
'until': date_end,
'byday': WEEKDAYS[weekday]
})
cal.add_component(event)
return cal.to_ical()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment