42 lines
1.4 KiB
Python
42 lines
1.4 KiB
Python
# Copyright 2019 (c) Anna Schumaker.
|
|
import tags
|
|
import concurrent.futures
|
|
import mutagen
|
|
import os
|
|
import re
|
|
|
|
discno_map = { "one" : "1", "two" : "2", "three" : "3", "four" : "4", "five" : "5" }
|
|
track_list = [ ]
|
|
track_pool = concurrent.futures.ThreadPoolExecutor(max_workers = 1)
|
|
|
|
class Track:
|
|
def __init__(self, path):
|
|
fileinfo = mutagen.File(path)
|
|
|
|
self.path = path
|
|
self.title = fileinfo.get("title", [os.path.basename(path)])[0]
|
|
self.artist = fileinfo.get("artist", ["Unknown Artist"])[0]
|
|
self.tracknumber = int(fileinfo.get("tracknumber", ["0"])[0])
|
|
self.length = fileinfo.info.length
|
|
self.album = tags.Album.lookup(fileinfo)
|
|
self.discnumber = int(fileinfo.get("discnumber", ["1"])[0])
|
|
|
|
# Try to detect discnumbers that are embedded in the album name
|
|
discno = fileinfo.get("album", ["Unknown Album"])[0][len(self.album.album):]
|
|
match = re.search("([1-9][0-9]*)|one|two|three|four|five", discno.lower())
|
|
if match:
|
|
self.discnumber = int(discno_map.get(match.group(), match.group()))
|
|
|
|
|
|
def __add(path):
|
|
try:
|
|
if not any(t.path == path for t in track_list):
|
|
track = Track(path)
|
|
track_list.append(track)
|
|
return track
|
|
except mutagen.MutagenError:
|
|
return None
|
|
|
|
def add(path):
|
|
return track_pool.submit(__add, path)
|