Skip to content

Commit

Permalink
New tag: URL
Browse files Browse the repository at this point in the history
  • Loading branch information
JuniorJPDJ committed May 16, 2021
1 parent 7bbc937 commit 42300c7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
30 changes: 22 additions & 8 deletions mediafile.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,31 +839,38 @@ def store(self, mutagen_file, value):
class MP3DescStorageStyle(MP3StorageStyle):
"""Store data in a TXXX (or similar) ID3 frame. The frame is
selected based its ``desc`` field.
``attr`` allows to specify name of data accessor property in the frame.
Most of frames use `text`.
``multispec`` specifies if frame data is ``mutagen.id3.MultiSpec``
which means that the data is being packed in the list.
"""
def __init__(self, desc=u'', key='TXXX', **kwargs):
def __init__(self, desc=u'', key='TXXX', attr='text', multispec=True,
**kwargs):
assert isinstance(desc, six.text_type)
self.description = desc
self.attr = attr
self.multispec = multispec
super(MP3DescStorageStyle, self).__init__(key=key, **kwargs)

def store(self, mutagen_file, value):
frames = mutagen_file.tags.getall(self.key)
if self.key != 'USLT':
if self.multispec:
value = [value]

# Try modifying in place.
found = False
for frame in frames:
if frame.desc.lower() == self.description.lower():
frame.text = value
setattr(frame, self.attr, value)
frame.encoding = mutagen.id3.Encoding.UTF8
found = True

# Try creating a new frame.
if not found:
frame = mutagen.id3.Frames[self.key](
desc=self.description,
text=value,
encoding=mutagen.id3.Encoding.UTF8,
**{self.attr: value}
)
if self.id3_lang:
frame.lang = self.id3_lang
Expand All @@ -872,10 +879,10 @@ def store(self, mutagen_file, value):
def fetch(self, mutagen_file):
for frame in mutagen_file.tags.getall(self.key):
if frame.desc.lower() == self.description.lower():
if self.key == 'USLT':
return frame.text
if not self.multispec:
return getattr(frame, self.attr)
try:
return frame.text[0]
return getattr(frame, self.attr)[0]
except IndexError:
return None

Expand Down Expand Up @@ -1751,8 +1758,15 @@ def update(self, dict):
ASFStorageStyle('TotalDiscs'),
out_type=int,
)

url = MediaField(
MP3DescStorageStyle(key='WXXX', attr='url', multispec=False),
MP4StorageStyle('\xa9url'),
StorageStyle('URL'),
ASFStorageStyle('WM/URL'),
)
lyrics = MediaField(
MP3DescStorageStyle(key='USLT'),
MP3DescStorageStyle(key='USLT', multispec=False),
MP4StorageStyle('\xa9lyr'),
StorageStyle('LYRICS'),
ASFStorageStyle('WM/Lyrics'),
Expand Down
4 changes: 3 additions & 1 deletion test/test_mediafile.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,8 @@ def _generate_tags(self, base=None):
tags['art'] = self.jpg_data
tags['comp'] = True

tags['url'] = "https://example.com/"

date = datetime.date(2001, 4, 3)
tags['date'] = date
tags['year'] = date.year
Expand Down Expand Up @@ -990,7 +992,7 @@ def test_properties_from_readable_fields(self):
def test_known_fields(self):
fields = list(ReadWriteTestBase.tag_fields)
fields.extend(('encoder', 'images', 'genres', 'albumtype', 'artists',
'albumartists'))
'albumartists', 'url'))
assertCountEqual(self, MediaFile.fields(), fields)

def test_fields_in_readable_fields(self):
Expand Down

0 comments on commit 42300c7

Please sign in to comment.