def load_xml_bytes(xml_bytes): root = etree.fromstring(xml_bytes) # apply compatibility changes to data # change layout (version < 3.0) if root.get('type'): root.set('layout', root.get('type')) # change date format (version < 3.0) creation = root.find('creation') if creation is not None and creation.text: try: date = datetime.datetime.strptime(creation.text, '%a %b %d %H:%M:%S %Y') creation.text = date.strftime('%Y-%m-%d %H:%M:%S') except ValueError: pass # replace speaker identifier speaker = root.find('speaker') if speaker is not None and speaker.get('id'): speaker.set('identifier', speaker.get('id')) # replace chapters tags with index (version < 2.6) chapters = root.find('chapters') if chapters is not None: chapters.tag = 'indexes' for element in chapters: if element.tag == 'item': element.tag = 'index' # convert timecode to time timecode = element.find('timecode') if timecode is not None: timecode.tag = 'time' timecode.text = str(utils.get_time_from_timecode(timecode.text)) # set videos attributes names (version < 3.0) videos = root.find('videos') if videos is not None: for element in videos: for attr in element.attrib: element.set('is_%s' % attr, element.get(attr)) for sub in element: if sub.tag == 'publishid' and sub.get('type'): sub.set('service', sub.get('type')) # load from xml mc = MetaCast.from_xml(root) return mc
def set_time_from_timecode_string(self, timecode_string): self.time = utils.get_time_from_timecode(timecode_string)