import ics
import json
import requests
from datetime import datetime

def parse_time(isc_time):
    return datetime.strptime(isc_time, "%Y%m%dT%H%M%SZ").strftime("%Y-%m-%dT%H:%M:%S.000")


def convert(cal):
    data = []
    for e in cal.events:
        e_dict = {}
        e_dict['title'] = e.name
        e_dict['start'] = str(e.begin)[:-6]
        e_dict['end'] = str(e.end)[:-6]
        e_dict['location'] = e.location
        e_dict['description'] = e.description
        data.append(e_dict)
    return json.dumps(data)


def convert_url(url):
    cal = ics.Calendar(requests.get(url).text)
    return convert(cal)