def _setDates(self): dates = list() try: # Set locale time from en_US to de_DE for formatting calendar dates. locale.setlocale(locale.LC_TIME, 'de_DE.utf-8') dates_string = self.play_item_soup.findAll('p')[1].span.nextSibling # dates_string = "Neues Schauspielhaus / Termine: 10. | 14. | 18. September 2011, 01. | 16. | 22. Oktober 2011, 18. | 29. November 2011, 08. | 11. | 16. | 29. Dezember 2011, 27. Januar 2012, 03. | 10. | 15. | 26. Februar 2012, 27. März 2012" (location_part, seperator, dates_part) = dates_string.partition(':') dates_per_month = dates_part.split(',') # dates_per_month = "[' 10. | 14. | 18. September 2011', ' 01. | 16. | 22. Oktober 2011', ' 18. | 29. November 2011', ' 08. | 11. | 16. | 29. Dezember 2011', ' 27. Januar 2012', ' 03. | 10. | 15. | 26. Februar 2012', ' 27. M\xc3\xa4rz 2012']" for dates_of_one_month in dates_per_month: # dates_of_one_month = " 10. | 14. | 18. September 2011" raw_dates = dates_of_one_month.split('|') # raw_dates = "[' 10. ', ' 14. ', ' 18. September 2011']" raw_date_with_month_name = raw_dates[-1] # raw_date_with_month_name = " 18. September 2011" month = raw_date_with_month_name.split()[1] # month = "September" year = raw_date_with_month_name.split()[2] # year = "2011" for i in range(len(raw_dates) - 1): # i = "0" # date = "10. September 2011" date_string_raw = raw_dates[i].lstrip() + month + ' ' + year date_time = datetime.datetime.strptime(date_string_raw, '%d. %B %Y') # For json serialization we have to save the date as a string. date_string = Lepistant.formatDatetimeToString(date_time) dates.append(date_string) # Formatting the last date in the list: "18. September 2011" last_date_unformatted = raw_dates[-1].lstrip().rstrip() last_date_time = datetime.datetime.strptime(last_date_unformatted, '%d. %B %Y') last_date_string = Lepistant.formatDatetimeToString(last_date_time) # Converting the date_string to ISO 8601 format: YYYY-MM-DDThh:mm+01 # iso_8601_date = Lepistant.formatDateToISO8601(last_date_string) dates.append(last_date_string) logger.info('%s - set dates: %s', self.title, dates) except ValueError as verr: logger.error('Failed to format date "%s" for play "%s" due to: %s', date_string_raw, self.title, str(verr)) except: logger.error('') logger.error('Failed to set dates for play "%s". Therefore setting dates to an empty list.', self.title) logger.error('') # Set locale time back from de_DE to en_US. locale.setlocale(locale.LC_TIME, 'en_US.utf-8') logger.info('') return self._setKey('dates', dates)
def _getFurtherPerformances(self): # Set locale time from en_US to de_DE for formatting calendar dates. locale.setlocale(locale.LC_TIME, 'de_DE.utf-8') perfomance_tuples = list() try: performance_tags = self.play_detail_soup.find('div', {"class": "further-performances"}).findAll('div') for perfomance_tag in performance_tags: date_link_tag = perfomance_tag.findAll('a', text=True)[0].parent date_string = date_link_tag.string date_time = datetime.datetime.strptime(date_string, '%a, %d.%m.%Y / %H.%M Uhr') date = Lepistant.formatDatetimeToString(date_time) url = Lepistant.getURLFromLinkTag(date_link_tag) performance_tuple = (date, url) perfomance_tuples.append(performance_tuple) except: logger.warning('Failed to get further performances for play "%s". Therefore returning an empty list.', self.title) # Set locale time back from de_DE to en_US. locale.setlocale(locale.LC_TIME, 'en_US.utf-8') return perfomance_tuples