Created
November 28, 2019 17:14
-
-
Save wolkenarchitekt/d63a70092c4377cbae1a8c665f857b54 to your computer and use it in GitHub Desktop.
Using popularimeter with mediafile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import collections | |
import mutagen | |
import six | |
from mediafile import StorageStyle, MediaField, MediaFile | |
Popularimeter = collections.namedtuple('Popularimeter', ['rating', 'count']) | |
class PopmMediaField(MediaField): | |
def __init__(self, *styles, **kwargs): | |
kwargs['out_type'] = dict | |
super().__init__(*styles, **kwargs) | |
def __get__(self, mediafile, owner=None): | |
return super().__get__(mediafile=mediafile, owner=owner) | |
def __set__(self, mediafile, value): | |
return super().__set__(mediafile=mediafile, value=value) | |
def __delete__(self, mediafile): | |
return super().__delete__(mediafile) | |
class MP3PopmStorageStyle(StorageStyle): | |
"""Store data in ID3 frames. | |
""" | |
formats = ['MP3', 'AIFF', 'DSF'] | |
def __init__(self, key, **kwargs): | |
super(MP3PopmStorageStyle, self).__init__(key, as_type=dict, **kwargs) | |
def fetch(self, mutagen_file): | |
ratings = mutagen_file.tags.getall('POPM') | |
return {popm.email: Popularimeter(rating=popm.rating, count=popm.count) | |
for popm in ratings} | |
popm_field = PopmMediaField( | |
MP3PopmStorageStyle('POPM'), | |
) | |
MediaFile.add_field('popm', popm_field) | |
f = MediaFile('/tmp/popm.mp3') | |
print(f.popm['traktor@native-instruments.de'].rating) | |
print(f.popm['traktor@native-instruments.de'].count) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment