def gen_ics(): data = json.load(open("assets/data/timings.json")) os.system("rm -rf assets/calendars") os.system("mkdir assets/calendars") for offset in time_offsets: for fiqh in data.keys(): os.system("mkdir assets/calendars/" + fiqh) c = Calendar() for d in data[fiqh].keys(): es = Event() ei = Event() stime = arrow.get( dt.fromtimestamp(data[fiqh][d]["sehri_timestamp"] + (offset * 60)), 'Asia/Kolkata').strftime("%Y-%m-%d %H:%M:00") itime = arrow.get( dt.fromtimestamp(data[fiqh][d]["iftar_timestamp"] + (offset * 60)), "Asia/Kolkata").strftime("%Y-%m-%d %H:%M:00") time = dt.now() es.name = "Sahar Ending" es.begin = stime es.created = time es.end = stime ei.name = "Iftar Beginning" ei.created = time ei.begin = itime ei.end = itime c.events.add(es) c.events.add(ei) with open( "assets/calendars/%s/timings%s.ics" % (fiqh, get_offset_string(offset)), "w") as my_file: my_file.writelines(c)
def save_ics_file(self, ics_file: str, n_events: int = 50) -> None: """ Saves n_events random events in the ics file with path ics_file :param ics_file (str): path of the ics file :param n_events (int): number of events to generate """ c = Calendar() for i in range(0, n_events): sys.stdout.write("\rCreating event %i of %i" % (i, n_events)) sys.stdout.flush() e = Event() e.name = self.get_rnd_title() (start, end, duration, created) = self.get_rnd_event_time() e.begin = start e.end = end e.duration = duration e.created = created e.description = self.get_rnd_description() e.url = self.get_rnd_url() e.location = self.get_rnd_address() c.events.add(e) with open(ics_file, 'w') as f: f.writelines(c) sys.stdout.write("\rDone") sys.stdout.flush()
def generate(self): c = Calendar() c.scale = 'GREGORIAN' c.method = 'PUBLISH' c.creator = f'fb2cal v{__version__} ({__status__}) [{__website__}]' c.extra.append( ContentLine(name='X-WR-CALNAME', value='Facebook Birthdays (fb2cal)')) c.extra.append(ContentLine(name='X-PUBLISHED-TTL', value='PT12H')) c.extra.append( ContentLine(name='X-ORIGINAL-URL', value='/events/birthdays/')) cur_date = datetime.now() for facebook_user in self.facebook_users: # Don't add extra 's' if name already ends with 's' formatted_username = f"{facebook_user.name}'s" if facebook_user.name[ -1] != 's' else f"{facebook_user.name}'" formatted_username = f'{formatted_username} Birthday' # Set date components day = facebook_user.birthday_day month = facebook_user.birthday_month year = facebook_user.birthday_year # Feb 29 special case: # If event year is not a leap year, use Feb 28 as birthday date instead if facebook_user.birthday_month == 2 and facebook_user.birthday_day == 29 and not calendar.isleap( year): day = 28 # The birth year may not be visible due to privacy settings # In this case, calculate the year as this year or next year based on if its past current month or not if year is None: year = cur_date.year if facebook_user.birthday_month >= cur_date.month else ( cur_date + relativedelta(years=1)).year # Format date components as needed month = f'{month:02}' day = f'{day:02}' # Event meta data e = Event() e.uid = facebook_user.id e.name = formatted_username e.created = cur_date e.description = f'{facebook_user}\n{generate_facebook_profile_url_permalink(facebook_user)}' e.begin = f'{year}-{month}-{day} 00:00:00' e.make_all_day() e.duration = timedelta(days=1) e.extra.append(ContentLine(name='RRULE', value='FREQ=YEARLY')) c.events.add(e) self.birthday_calendar = c
def generate(self): c = Calendar() c.scale = 'GREGORIAN' c.method = 'PUBLISH' c.creator = f'fb2cal v{__version__} ({__status__}) [{__website__}]' c.extra.append( ContentLine(name='X-WR-CALNAME', value='Facebook Birthdays (fb2cal)')) c.extra.append(ContentLine(name='X-PUBLISHED-TTL', value='PT12H')) c.extra.append( ContentLine(name='X-ORIGINAL-URL', value='/events/birthdays/')) cur_date = datetime.now() for facebook_user in self.facebook_users: e = Event() e.uid = facebook_user.id e.created = cur_date # Don't add extra 's' if name already ends with 's' formatted_username = f"{facebook_user.name}'s" if facebook_user.name[ -1] != 's' else f"{facebook_user.name}'" e.name = f"{formatted_username} Birthday" # Calculate the year as this year or next year based on if its past current month or not # Also pad day, month with leading zeros to 2dp year = cur_date.year if facebook_user.birthday_month >= cur_date.month else ( cur_date + relativedelta(years=1)).year # Feb 29 special case: # If event year is not a leap year, use Feb 28 as birthday date instead if facebook_user.birthday_month == 2 and facebook_user.birthday_day == 29 and not calendar.isleap( year): facebook_user.birthday_day = 28 month = '{:02d}'.format(facebook_user.birthday_month) day = '{:02d}'.format(facebook_user.birthday_day) e.begin = f'{year}-{month}-{day} 00:00:00' e.make_all_day() e.duration = timedelta(days=1) e.extra.append(ContentLine(name='RRULE', value='FREQ=YEARLY')) c.events.add(e) self.birthday_calendar = c
def create_ics(self): print('Please wait a moment...') self.calender.extra.append(ContentLine(name='METHOD', value='PUBLISH')) for course in self.processed_schedule_json['courses']: week = 1 # begin_week, week and weekday start at 1 date = self._construct_date(course['begin_week'], course['weekday']) end_date = self._construct_date(course['end_week'], course['weekday']) while date <= end_date: if course['week_mask'][week - 1] == '1': e = Event() e.name = course['name'] e.location = course['classroom'] e.description = '课程提醒' e.created = datetime.datetime.utcnow() e.begin = self._construct_datetime( date, self.class_time[course['begin_class'] - 1]['begin_time']) e.end = self._construct_datetime( date, self.class_time[course['end_class'] - 1]['end_time']) if self.is_reminder_needed: if self.is_relative_trigger_time and self.trigger_time: alarm = DisplayAlarm(trigger=self.trigger_time, display_text='课程提醒') elif not self.is_relative_trigger_time: # set trigger as the end time of *previous* class # For the first class of day, reminder time is at # the night of previous day if course['begin_class'] == 1: trigger_time = self._construct_datetime \ (date - datetime.timedelta(days=1), self.class_time[course['begin_class'] - 1 - 1]['end_time']) else: trigger_time = self._construct_datetime \ (date, self.class_time[course['begin_class'] - 1 - 1]['end_time']) alarm = DisplayAlarm(trigger=trigger_time, display_text='课程提醒') e.alarms.append(alarm) self.calender.events.add(e) date = date + datetime.timedelta(days=7.0) week = week + 1
def populate_birthdays_calendar(birthdays): """ Populate a birthdays calendar using birthday objects """ c = Calendar() c.scale = 'GREGORIAN' c.method = 'PUBLISH' c.creator = f'fb2cal v{__version__} ({__status__}) [{__website__}]' c.extra.append( ContentLine(name='X-WR-CALNAME', value='Facebook Birthdays (fb2cal)')) c.extra.append(ContentLine(name='X-PUBLISHED-TTL', value='PT12H')) c.extra.append( ContentLine(name='X-ORIGINAL-URL', value='/events/birthdays/')) cur_date = datetime.now() for birthday in birthdays: e = Event() e.uid = birthday.uid e.created = cur_date e.name = f"{birthday.name}'s Birthday" # Calculate the year as this year or next year based on if its past current month or not # Also pad day, month with leading zeros to 2dp year = cur_date.year if birthday.month >= cur_date.month else ( cur_date + relativedelta(years=1)).year # Feb 29 special case: # If event year is not a leap year, use Feb 28 as birthday date instead if birthday.month == 2 and birthday.day == 29 and not calendar.isleap( year): birthday.day = 28 month = '{:02d}'.format(birthday.month) day = '{:02d}'.format(birthday.day) e.begin = f'{year}-{month}-{day} 00:00:00' e.make_all_day() e.duration = timedelta(days=1) e.extra.append(ContentLine(name='RRULE', value='FREQ=YEARLY')) c.events.add(e) return c
def create_lecture_event(lecture): ''' Create the calendar event given the lecture. ''' try: course = [x for x in courses if int(x[FIELD_TEACHING_ID]) == \ lecture[LECTURE_COURSE_ID]][0] # assume there is only one except IndexError as ie: print("Something gone wrong, I can't find the course with this id: {}".format(lecture[LECTURE_COURSE_ID])) exit(2) e = Event() e.name = sub(SUBJECT_REGEX, '', course[FIELD_TEACHING_SUBJECT_DESCRIPTION].capitalize()) if course[FIELD_TEACHING_TEACHER_NAME]: e.description = "Tenuto da {}".format(course[FIELD_TEACHING_TEACHER_NAME].title()) e.begin = lecture[LECTURE_START] e.end = lecture[LECTURE_END] e.created = datetime.today().astimezone() if lecture[LECTURE_LOCATION]: e.location = lecture[LECTURE_LOCATION] if course[FIELD_TEACHING_URL]: e.url = course[FIELD_TEACHING_URL] return e
schedule[name] = [fp1, fp2, fp3, q, r] for race in races: event = Event() today = datetime.datetime.now() gp, circuit, coords, name = race.split(',') if gp != 'JAPAN': dates = get_dates(gp) event.name = f'{name} Grand Prix' event.begin = dates['R'] # event.end = '' event.location = circuit event.geo = [float(i) for i in coords.split()] event.transparent = False event.created = today event.last_modified = today event.extra.extend([ContentLine(name='SEQUENCE', value='0')]) else: event.name = f'{name} Grand Prix' event.begin = '2020-10-11 00:00:00' # event.end = '' event.location = circuit event.geo = [float(i) for i in coords.split()] event.transparent = False event.created = today event.last_modified = today event.extra.extend([ContentLine(name='SEQUENCE', value='0')]) calendar.events.add(event) print(f'Added {event.name !r} to calendar')
description = "Website: https://www.linux-ag.uni-tuebingen.de" location = room + ", Sand, 72076 Tübingen, Germany" time_end = "21:00:00" def get_date(date, time): # We use a strange format (can be copied directly from the website): date_format = "DD.MM.YYYY HH:mm:ss" date = arrow.get(date + " " + time, date_format) date = date.replace(tzinfo='Europe/Berlin') return date # Create the calendar with all events and save the result (iCalendar): c = Calendar() for date in dates: e = Event() e.name = name e.description = description e.location = location e.begin = get_date(date, time_start) e.end = get_date(date, time_end) # To make the output deterministic: e.created = arrow.get("1970-01-01 00:00:00+00:00") e.uid = date + "@www.linux-ag.uni-tuebingen.de" c.events.add(e) with open("linux-ag.ics", "w") as f: f.writelines(c)