'Provides a subscribeable URL for iCal subscriptions'

def calendar(self):

    logger.log(u'Receiving    iCal    request    from    {0}'.format(self.request.remote_ip))
    ical = u'BEGIN:VCALENDAR\r\n'
    ical += u'VERSION:2.0\r\n'
    ical += u'X-WR-CALNAME:SickRage\r\n'
    ical += u'X-WR-CALDESC:SickRage\r\n'
    ical += u'PRODID://Sick-Beard    Upcoming    Episodes//\r\n'
    future_weeks = try_int(self.get_argument(u'future', 52), 52)
    past_weeks = try_int(self.get_argument(u'past', 52), 52)
    past_date = (datetime.date.today() + datetime.timedelta(weeks=(- past_weeks))).toordinal()
    future_date = (datetime.date.today() + datetime.timedelta(weeks=future_weeks)).toordinal()
    main_db_con = db.DBConnection()
    calendar_shows = main_db_con.select(u"SELECT    show_name,    indexer_id,    network,    airs,    runtime    FROM    tv_shows    WHERE    (    status    =    'Continuing'    OR    status    =    'Returning    Series'    )    AND    paused    !=    '1'")
    for show in calendar_shows:
        episode_list = main_db_con.select(u'SELECT    indexerid,    name,    season,    episode,    description,    airdate    FROM    tv_episodes    WHERE    airdate    >=    ?    AND    airdate    <    ?    AND    showid    =    ?', (past_date, future_date, int(show['indexer_id'])))
        utc = tz.gettz(u'GMT')
        for episode in episode_list:
            air_date_time = network_timezones.parse_date_time(episode['airdate'], show['airs'], show['network']).astimezone(utc)
            air_date_time_end = (air_date_time + datetime.timedelta(minutes=try_int(show['runtime'], 60)))
            ical += u'BEGIN:VEVENT\r\n'
            ical += ((((u'DTSTART:' + air_date_time.strftime(u'%Y%m%d')) + u'T') + air_date_time.strftime(u'%H%M%S')) + u'Z\r\n')
            ical += ((((u'DTEND:' + air_date_time_end.strftime(u'%Y%m%d')) + u'T') + air_date_time_end.strftime(u'%H%M%S')) + u'Z\r\n')
            if sickbeard.CALENDAR_ICONS:
                ical += u'X-GOOGLE-CALENDAR-CONTENT-ICON:https://lh3.googleusercontent.com/-Vp_3ZosvTgg/VjiFu5BzQqI/AAAAAAAA_TY/3ZL_1bC0Pgw/s16-Ic42/SickRage.png\r\n'
                ical += u'X-GOOGLE-CALENDAR-CONTENT-DISPLAY:CHIP\r\n'
            ical += u'SUMMARY:    {0}    -    {1}x{2}    -    {3}\r\n'.format(show['show_name'], episode['season'], episode['episode'], episode['name'])
            ical += ((((((((u'UID:SickRage-' + str(datetime.date.today().isoformat())) + u'-') + show['show_name'].replace(u'    ', u'-')) + u'-E') + str(episode['episode'])) + u'S') + str(episode['season'])) + u'\r\n')
            if episode['description']:
                ical += u'DESCRIPTION:    {0}    on    {1}    \\n\\n    {2}\r\n'.format((show['airs'] or u'(Unknown    airs)'), (show['network'] or u'Unknown    network'), episode['description'].splitlines()[0])
            else:
                ical += ((((u'DESCRIPTION:' + (show['airs'] or u'(Unknown    airs)')) + u'    on    ') + (show['network'] or u'Unknown    network')) + u'\r\n')
            ical += u'END:VEVENT\r\n'
    ical += u'END:VCALENDAR'
    return ical
