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 11, 2021
1 parent b8fbf27 commit f071bf2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
28 changes: 20 additions & 8 deletions mediafile.py
Original file line number Diff line number Diff line change
Expand Up @@ -840,30 +840,33 @@ class MP3DescStorageStyle(MP3StorageStyle):
"""Store data in a TXXX (or similar) ID3 frame. The frame is
selected based its ``desc`` field.
"""
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 +875,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 +1754,17 @@ def update(self, dict):
ASFStorageStyle('TotalDiscs'),
out_type=int,
)

url = MediaField(
MP3DescStorageStyle(key='WXXX', attr='url', multispec=False),
MP4StorageStyle('\xa9url'),
StorageStyle('URL'),
StorageStyle('WWW'),
StorageStyle('Website'),
ASFStorageStyle('WM/URL'),
)
lyrics = MediaField(
MP3DescStorageStyle(key='USLT'),
MP3DescStorageStyle(key='USLT', multispec=False),
MP4StorageStyle('\xa9lyr'),
StorageStyle('LYRICS'),
ASFStorageStyle('WM/Lyrics'),
Expand Down
3 changes: 2 additions & 1 deletion test/test_mediafile.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ class ReadWriteTestBase(ArtTestMixin, GenreListTestMixin,
'tracktotal',
'disc',
'disctotal',
'url',
'lyrics',
'comments',
'copyright',
Expand Down Expand Up @@ -708,7 +709,7 @@ def _generate_tags(self, base=None):
# R128 is int
tags[key] = -1
else:
tags[key] = 'value\u2010%s' % key
tags[key] = 'value-%s' % key

for key in ['disc', 'disctotal', 'track', 'tracktotal', 'bpm']:
tags[key] = 1
Expand Down

0 comments on commit f071bf2

Please sign in to comment.