"""Deals with reading/writing MP3 tags and metadata in sermon filename"""fromdatetimeimportdate,timeimportosimportreimportsysfrommutagen.id3importID3,TIT2,TDRC,TPE1,TIME,COMMfromsermons.modelsimportBIBLE_BOOKS,BIBLE_NAME_TO_VAL,Speaker,Series,Topicexpected_filename_re=re.compile(ur'(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2}) (?P<hour>\d{2})(?P<minute>\d{2}) (?P<speaker>[^-]+)( - (?P<title>.*))?\.mp3')defset_attrs_from_filename(sermon):""" Fills in the details that can be found from the filename """filename=os.path.basename(sermon.sermon.name)m_filename=expected_filename_re.match(filename)ifm_filenameisnotNone:d=m_filename.groupdict()sermon.date_delivered=date(int(d['year']),int(d['month']),int(d['day']))sermon.time_delivered=time(int(d['hour']),int(d['minute']))sermon.speaker=Speaker.objects.get_or_create(name=d['speaker'])[0]passage=Nonetitle=d['title']iftitleisNone:title=''# Now see if we can parse title to get passage infobook=Noneposs_book=title.split(' ')[0]poss_book_2=' '.join(title.split(' ')[0:2])ifposs_bookinBIBLE_BOOKS:book=poss_bookifposs_book_2inBIBLE_BOOKS:book=poss_book_2ifbookisnotNone:# Assume 'title' is actually a passagepassage=titletitle=''sermon.bible_book=BIBLE_NAME_TO_VAL[book]elif' - 'intitle:title_part,passage_part=title.split(' - ',1)poss_book=passage_part.split(' ')[0]poss_book_2=' '.join(passage_part.split(' ')[0:2])ifposs_bookinBIBLE_BOOKS:book=poss_bookifposs_book_2inBIBLE_BOOKS:book=poss_book_2ifbookisnotNone:title=title_partpassage=passage_partsermon.bible_book=BIBLE_NAME_TO_VAL[book]sermon.title=titleifpassageisnotNone:sermon.passage=passagereturnTrueelse:returnFalsedefwrite_id3_tags(sermon):fname=sermon.sermon.file.nameUTF8=3# mutagen.id3 says so.try:tags=ID3(fname)exceptException:# Could be missing or malformed headertags=ID3()tags.filename=fnametags.add(TIT2(encoding=UTF8,text=sermon.title))tags.add(TDRC(encoding=UTF8,text=sermon.date_delivered.strftime('%Y-%m-%d')+' '+sermon.time_delivered.strftime('%H:%M')))tags.add(TPE1(encoding=UTF8,text=sermon.speaker.name))topics=sermon.topics.all()comment=("""Sermon: Speaker: %(speaker)s Bible book: %(bible_book)s Text: %(passage)s Series: %(series)s Topics: %(topics)s"""%dict(speaker=sermon.speaker.name,bible_book=sermon.bible_book,passage=sermon.passage,series=sermon.series.nameifsermon.seriesisnotNoneelse'',topics=', '.join(t.namefortintopics)iftopicselse''))tags.add(COMM(encoding=UTF8,lang='eng',text=comment))tags.save()defset_attrs_from_id3_tags(sermon):fname=sermon.sermon.file.nametags=ID3(fname)title='TIT2'date_delivered='TDRC'speaker='TPE1'bible_book="COMM:Sermon.bible_book:'eng'"passage="COMM:Sermon.passage:'eng'"series="COMM:Sermon.series:'eng'"topics="COMM:Sermon.topics:'eng'"has_time=Falseiftitleintags:sermon.title=tags[title].text[0]ifdate_deliveredintags:timestamp=tags[date_delivered].text[0]iftimestamp.yearandtimestamp.monthandtimestamp.day:sermon.date_delivered=date(timestamp.year,timestamp.month,timestamp.day)iftimestamp.hourisnotNoneandtimestamp.minuteisnotNone:has_time=Truesermon.time_delivered=time(timestamp.hour,timestamp.minute)ifspeakerintags:sermon.speaker=Speaker.objects.get_or_create(name=tags[speaker].text[0])[0]ifbible_bookintags:sermon.bible_book=tags[bible_book].text[0]ifpassageintags:sermon.passage=tags[passage].text[0]ifseriesintags:sermon.series=Series.objects.get_or_create(name=tags[series].text[0])[0]topics_to_add=[]iftopicsintags:fortintags[topics].text[0].split(','):# Topics have to be added later, after savingtopics_to_add.append(Topic.objects.get_or_create(name=t)[0])ifhas_time:# Obviously have done export to ID3, so ID3 has good info.good_info=Trueelse:good_info=Falsereturngood_info,topics_to_add