Example #1
0
def process_file(f, source_id):
    extension = splitext(f)[1].lower()
    if extension == '.mp3':
        # found a MP3!
        id3 = id3reader.Reader(f)
        album = id3.getValue('album')
        artist = id3.getValue('performer') or album
        title = id3.getValue('title')
        try:
            m = Music.objects.get(filename=f)
        except Music.DoesNotExist:
            # new one
            m = Music()
        if title is not None and artist is not None:
            m.album = album or "Unknown Album"
            m.artist = artist
            m.title = title
        else:
            info = MUSIC_MATCH_REGEX.search(f)
            if info is None:
                return 0
            m.album = "Unknown Album"
            m.artist = "%s - %s" % (info.groupdict()['folder'], info.groupdict()['subfolder'])
            m.title = info.groupdict()['title']
        m.filename = f
        m.source_id = source_id
        try:
            m.save()
            return 1
        except IntegrityError:
            # probably missing information, skip it
            pass
    return 0