def process_item(self, item, spider): if not item.get('date_begin'): raise DropItem('No begin date') if item.get('time_begin'): item['date_begin'] = parse("{date} {time}".format( date=item['date_begin'], time=item['time_begin'])) else: item['date_begin'] = parse(item['date_begin']) if item.get('date_end') and item.get('time_end'): item['date_end'] = parse("{date} {time}".format( date=item['date_end'], time=item['time_end'])) elif item.get('date_end'): item['date_end'] = parse(item['date_end']) #construct ical from the event item eastern = timezone('US/Eastern') cal = Calendar() cal.add('prodid', 'Local Colors') cal.add('version', '2.0') event = Event() u = UIDGenerator() event.add('uid', u.uid('localcolors.us')) event.add('dtstart', eastern.localize(item['date_begin'])) if item.get('date_end'): event.add('dtend', eastern.localize(item['date_end'])) event.add('dtstamp', eastern.localize(datetime.now())) #TODO incorporate venue address; possible get lat/long for GEO if item.get('venue'): event.add('location', item['venue']) if item.get('title'): event.add('summary', item['title']) event.add('description', item['title']) #TODO: re-structure contact info if item.get('contact_name'): event.add('contact', item['contact_name']) event.add('url', item['url_event']) #TODO: add non-standard property for scraped URL if item.get('type'): event.add('categories', item['type']) cal.add_component(event) self.file.write(cal.to_ical()) return item
def test_tools_UIDGenerator(self): # Automatic semi-random uid g = UIDGenerator() uid = g.uid() txt = uid.to_ical() length = 15 + 1 + 16 + 1 + 11 self.assertTrue(len(txt) == length) self.assertTrue('@example.com' in txt) # You should at least insert your own hostname to be more compliant uid = g.uid('Example.ORG') txt = uid.to_ical() self.assertTrue(len(txt) == length) self.assertTrue('@Example.ORG' in txt) # You can also insert a path or similar uid = g.uid('Example.ORG', '/path/to/content') txt = uid.to_ical() self.assertTrue(len(txt) == length) self.assertTrue('-/path/to/[email protected]' in txt)
def get_events_workout(calendar, workout, duration, start_date=None): ''' Creates all necessary events from the given workout and adds them to the calendar. Each event's occurrence ist set to weekly (one event for each training day). :param calendar: calendar to add events to :param workout: Workout :param duration: duration in weeks :param start_date: start date, default: profile default :return: None ''' start_date = start_date if start_date else workout.creation_date end_date = start_date + datetime.timedelta(weeks=duration) generator = UIDGenerator() site = Site.objects.get_current() for day in workout.canonical_representation['day_list']: # Make the description of the event with the day's exercises description_list = [] for set in day['set_list']: for exercise in set['exercise_list']: description_list.append(six.text_type(exercise['obj'])) description = ', '.join( description_list) if description_list else day['obj'].description # Make an event for each weekday for weekday in day['days_of_week']['day_list']: event = Event() event.add('summary', day['obj'].description) event.add('description', description) event.add('dtstart', next_weekday(start_date, weekday.id - 1)) event.add('dtend', next_weekday(start_date, weekday.id - 1)) event.add('rrule', {'freq': 'weekly', 'until': end_date}) event['uid'] = generator.uid(host_name=site.domain) event.add('priority', 5) calendar.add_component(event)
def get_events_workout(calendar, workout, duration, start_date=None): ''' Creates all necessary events from the given workout and adds them to the calendar. Each event's occurrence ist set to weekly (one event for each training day). :param calendar: calendar to add events to :param workout: Workout :param duration: duration in weeks :param start_date: start date, default: profile default :return: None ''' start_date = start_date if start_date else workout.creation_date end_date = start_date + datetime.timedelta(weeks=duration) generator = UIDGenerator() site = Site.objects.get(pk=settings.SITE_ID) for day in workout.canonical_representation['day_list']: # Make the description of the event with the day's exercises description_list = [] for set in day['set_list']: for exercise in set['exercise_list']: description_list.append(unicode(exercise['obj'])) description = ', '.join(description_list) if description_list else day['obj'].description # Make an event for each weekday for weekday in day['days_of_week']['day_list']: event = Event() event.add('summary', day['obj'].description) event.add('description', description) event.add('dtstart', next_weekday(start_date, weekday.id - 1)) event.add('dtend', next_weekday(start_date, weekday.id - 1)) event.add('rrule', {'freq': 'weekly', 'until': end_date}) event['uid'] = generator.uid(host_name=site.domain) event.add('priority', 5) calendar.add_component(event)
def get_events_workout(calendar, workout, duration, start_date=None): """ Creates all necessary events from the given workout and adds them to the calendar. Each event's occurrence ist set to weekly (one event for each training day). :param calendar: calendar to add events to :param workout: Workout :param duration: duration in weeks :param start_date: start date, default: profile default :return: None """ start_date = start_date if start_date else workout.creation_date end_date = start_date + datetime.timedelta(weeks=duration) generator = UIDGenerator() site = Site.objects.get_current() for day in workout.canonical_representation["day_list"]: # Make the description of the event with the day's exercises description_list = [] for set in day["set_list"]: for exercise in set["exercise_list"]: description_list.append(six.text_type(exercise["obj"])) description = ", ".join(description_list) if description_list else day["obj"].description # Make an event for each weekday for weekday in day["days_of_week"]["day_list"]: event = Event() event.add("summary", day["obj"].description) event.add("description", description) event.add("dtstart", next_weekday(start_date, weekday.id - 1)) event.add("dtend", next_weekday(start_date, weekday.id - 1)) event.add("rrule", {"freq": "weekly", "until": end_date}) event["uid"] = generator.uid(host_name=site.domain) event.add("priority", 5) calendar.add_component(event)
# calendar = calendars[0] # print "Using calendar", calendar # # print "Looking for events" # results = calendar.date_search(datetime(2013, 12, 1)) # for event in results: # print "Found", event cal = Calendar() cal.add('calscale', 'GREGORIAN') cal.add('version', '2.0') cal.add('method', 'REQUEST') cal.add('prodid', '-//MeetingGroom//iroazh.eu//') freebusy = FreeBusy() freebusy.add('uid', UIDGenerator().uid(gethostname())) freebusy.add('dtstart', datetime(2013, 12, 12)) freebusy.add('dtend', datetime(2013, 12, 13)) cal.add_component(freebusy) request = cal.to_ical() request = \ """BEGIN:VCALENDAR CALSCALE:GREGORIAN VERSION:2.0 METHOD:REQUEST PRODID:-//Apple Inc.//Mac OS X 10.9//EN BEGIN:VFREEBUSY UID:4790384D-220D-4B73-86E4-CF639975EC2B