From 493f93c425aec51ed65c70435c9586798793785c Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Tue, 24 Jul 2012 11:18:12 -0400 Subject: [PATCH] cc-4105: added metadata write function --- .../media-monitor2/media/monitor/airtime.py | 14 +++++++++++++- .../media-monitor2/media/monitor/metadata.py | 13 ++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/python_apps/media-monitor2/media/monitor/airtime.py b/python_apps/media-monitor2/media/monitor/airtime.py index 9281b90ae..ec4704204 100644 --- a/python_apps/media-monitor2/media/monitor/airtime.py +++ b/python_apps/media-monitor2/media/monitor/airtime.py @@ -4,6 +4,8 @@ from kombu.connection import BrokerConnection import json import copy +from media.monitor.exception import BadSongFile +from media.monitor.metadata import Metadata from media.monitor.log import Loggable # Do not confuse with media monitor 1's AirtimeNotifier class that more related @@ -88,12 +90,22 @@ class AirtimeMessageReceiver(Loggable): # pydispatcher or do the necessary changes on the filesystem that will fire # the events def md_update(self, msg): - pass + md_path = msg['MDATA_KEY_FILEPATH'] + try: + Metadata.write_unsafe(path=md_path, md=msg) + except BadSongFile as e: + self.logger.info("Cannot find metadata file: '%s'" % e.path) + except Exception as e: + # TODO : add md_path to problem path or something? + self.logger.info("Unknown error when writing metadata to: '%s'" % md_path) def new_watch(self, msg): + # TODO : add new watched directory by bootstrapping it with an empty + # database? pass def remove_watch(self, msg): pass def rescan_watch(self, msg): + # TODO : basically a bootstrap on the directory pass def change_storage(self, msg): pass diff --git a/python_apps/media-monitor2/media/monitor/metadata.py b/python_apps/media-monitor2/media/monitor/metadata.py index a4f406389..41be0fddb 100644 --- a/python_apps/media-monitor2/media/monitor/metadata.py +++ b/python_apps/media-monitor2/media/monitor/metadata.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- import mutagen import math +import os import copy from media.monitor.exceptions import BadSongFile from media.monitor.log import Loggable @@ -93,7 +94,17 @@ def truncate_to_length(item, length): class Metadata(Loggable): @staticmethod def write_unsafe(path,md): - pass + if not os.path.exists(path): + raise BadSongFile(path) + song_file = mutagen.File(path, easy=True) + for airtime_k, airtime_v in md.iteritems(): + if airtime_k in airtime2mutagen: + song_file[ airtime2mutagen[airtime_k] ] = airtime_v + else: + song_file[ airtime2mutagen[airtime_k] ] = u"" + song_file.save() + + def __init__(self, fpath): # Forcing the unicode through try: fpath = fpath.decode("utf-8")