emmental/curds/tags.py

35 lines
1.1 KiB
Python

# Copyright 2019 (c) Anna Schumaker.
import re
tag_map = dict()
class Tag:
def extract(info, key, default):
return info.get(key, [ default ])[0]
def lookup(tag):
return tag_map.setdefault(hash(tag), tag)
class Album(Tag):
def __init__(self, info):
self.album = Tag.extract(info, "album", "Unknown Album")
self.genre = Tag.extract(info, "genre", "Unknown")
self.date = int(Tag.extract(info, "date", 0))
self.tracktotal = int(Tag.extract(info, "tracktotal", 0))
self.albumartist = Tag.extract(info, "albumartist",
Tag.extract(info, "album artist",
Tag.extract(info, "artist", "Unknown Artist")))
# Try to detect album names that have a discnumber embedded in them
match = re.search("(cd|dis[c|k])(\s)*(([0-9]+)|one|two|three|four|five)", self.album.lower())
if match and match.start() > 0:
self.album = self.album[:match.start()].strip(" ;({[-")
def __hash__(self):
return hash((self.album, self.albumartist, self.date))
def lookup(info):
return Tag.lookup(Album(info))