Skip to content

Commit

Permalink
Minor refactor of ID3 tag writing code
Browse files Browse the repository at this point in the history
  • Loading branch information
malept committed Apr 16, 2015
1 parent 9e2b2da commit 2cc7491
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
22 changes: 22 additions & 0 deletions gmusicprocurator/id3.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,26 @@ def set_albumart(id3, key, urls):

EasyID3.RegisterKey('albumart', setter=set_albumart)


def convert_google_field_to_mp3(google_value):
"""
Convert a Google metadata field value to the equivalent ID3 tag value.
:rtype: str
"""
if isinstance(google_value, basestring):
return google_value
elif isinstance(google_value, list):
# take the first value, see what it is
item = google_value[0]
if isinstance(item, basestring):
return item
elif isinstance(item, dict) and 'url' in item:
# e.g., albumArtRef
return item['url']
else:
return str(item)
else:
return str(google_value)

__all__ = ('EasyMP3',)
22 changes: 5 additions & 17 deletions gmusicprocurator/views/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from xspf import Xspf

from ..app import app, music
from ..id3 import EasyMP3
from ..id3 import convert_google_field_to_mp3, EasyMP3

HALF_MB = 1024 * 512 # in bytes

Expand Down Expand Up @@ -166,22 +166,10 @@ def add_id3_tags_to_mp3(song_id, input_data):
f.write(input_data.getvalue())
f.flush()
audio = EasyMP3(f.name)
for gmf, id3f in METADATA_FIELDS.iteritems():
if gmf in song_info:
if isinstance(song_info[gmf], basestring):
audio[id3f] = song_info[gmf]
elif isinstance(song_info[gmf], list):
# take the first value, see what it is
val = song_info[gmf][0]
if isinstance(val, basestring):
audio[id3f] = val
elif isinstance(val, dict) and 'url' in val:
# e.g., albumArtRef
audio[id3f] = val['url']
else:
audio[id3f] = str(val)
else:
audio[id3f] = str(song_info[gmf])
audio.update(dict(
[(id3_field, convert_google_field_to_mp3(song_info[google_field]))
for google_field, id3_field in METADATA_FIELDS.iteritems()
if google_field in song_info]))
audio.save()
copyfileobj(open(f.name, 'rb'), output)
return output
Expand Down

0 comments on commit 2cc7491

Please sign in to comment.