CC-2633: added new encode_to()
- added encode_to() to api_client - replaced .encode() with new encode_to()
This commit is contained in:
parent
a199585cb9
commit
8995e828ff
|
@ -11,6 +11,12 @@ def to_unicode(obj, encoding='utf-8'):
|
|||
obj = unicode(obj, encoding)
|
||||
return obj
|
||||
|
||||
def encode_to(obj, encoding='utf-8'):
|
||||
if isinstance(obj, basestring):
|
||||
if not isinstance(obj, str):
|
||||
obj = obj.encode(encoding)
|
||||
return obj
|
||||
|
||||
"""
|
||||
list of supported easy tags in mutagen version 1.20
|
||||
['albumartistsort', 'musicbrainz_albumstatus', 'lyricist', 'releasecountry', 'date', 'performer', 'musicbrainz_albumartistid', 'composer', 'encodedby', 'tracknumber', 'musicbrainz_albumid', 'album', 'asin', 'musicbrainz_artistid', 'mood', 'copyright', 'author', 'media', 'length', 'version', 'artistsort', 'titlesort', 'discsubtitle', 'website', 'musicip_fingerprint', 'conductor', 'compilation', 'barcode', 'performer:*', 'composersort', 'musicbrainz_discid', 'musicbrainz_albumtype', 'genre', 'isrc', 'discnumber', 'musicbrainz_trmid', 'replaygain_*_gain', 'musicip_puid', 'artist', 'title', 'bpm', 'musicbrainz_trackid', 'arranger', 'albumsort', 'replaygain_*_peak', 'organization']
|
||||
|
@ -186,7 +192,7 @@ class AirtimeMetadata:
|
|||
if (isinstance(md[key], basestring)):
|
||||
#self.logger.info("Converting md[%s] = '%s' ", key, md[key])
|
||||
md[key] = to_unicode(md[key])
|
||||
md[key] = md[key].encode('utf-8')
|
||||
md[key] = encode_to(md[key], 'utf-8')
|
||||
#self.logger.info("Converting complete: md[%s] = '%s' ", key, md[key])
|
||||
|
||||
return md
|
||||
|
|
|
@ -6,6 +6,12 @@ import logging
|
|||
from subprocess import Popen, PIPE
|
||||
from airtimemetadata import AirtimeMetadata
|
||||
|
||||
def encode_to(obj, encoding='utf-8'):
|
||||
if isinstance(obj, basestring):
|
||||
if not isinstance(obj, string):
|
||||
obj = obj.encode(encoding)
|
||||
return obj
|
||||
|
||||
class MediaMonitorCommon:
|
||||
|
||||
timestamp_file = "/var/tmp/airtime/last_index"
|
||||
|
@ -164,14 +170,14 @@ class MediaMonitorCommon:
|
|||
try:
|
||||
#will be in the format .ext
|
||||
file_ext = os.path.splitext(original_path)[1]
|
||||
file_ext = file_ext.encode('utf-8')
|
||||
file_ext = encode_to(file_ext, 'utf-8')
|
||||
|
||||
path_md = ['MDATA_KEY_TITLE', 'MDATA_KEY_CREATOR', 'MDATA_KEY_SOURCE', 'MDATA_KEY_TRACKNUMBER', 'MDATA_KEY_BITRATE']
|
||||
|
||||
md = {}
|
||||
for m in path_md:
|
||||
if m not in orig_md:
|
||||
md[m] = u'unknown'.encode('utf-8')
|
||||
md[m] = encode_to(u'unknown', 'utf-8')
|
||||
else:
|
||||
#get rid of any "/" which will interfere with the filepath.
|
||||
if isinstance(orig_md[m], basestring):
|
||||
|
@ -189,10 +195,10 @@ class MediaMonitorCommon:
|
|||
filepath = None
|
||||
#file is recorded by Airtime
|
||||
#/srv/airtime/stor/recorded/year/month/year-month-day-time-showname-bitrate.ext
|
||||
if(md['MDATA_KEY_CREATOR'] == "Airtime Show Recorder".encode('utf-8')):
|
||||
if(md['MDATA_KEY_CREATOR'] == encode_to("Airtime Show Recorder", 'utf-8')):
|
||||
#yyyy-mm-dd-hh-MM-ss
|
||||
y = orig_md['MDATA_KEY_YEAR'].split("-")
|
||||
filepath = '%s/%s/%s/%s/%s-%s-%s%s' % (storage_directory, "recorded".encode('utf-8'), y[0], y[1], orig_md['MDATA_KEY_YEAR'], md['MDATA_KEY_TITLE'], md['MDATA_KEY_BITRATE'], file_ext)
|
||||
filepath = '%s/%s/%s/%s/%s-%s-%s%s' % (storage_directory, encode_to("recorded", 'utf-8'), y[0], y[1], orig_md['MDATA_KEY_YEAR'], md['MDATA_KEY_TITLE'], md['MDATA_KEY_BITRATE'], file_ext)
|
||||
|
||||
#"Show-Title-2011-03-28-17:15:00"
|
||||
title = md['MDATA_KEY_TITLE'].split("-")
|
||||
|
@ -206,10 +212,10 @@ class MediaMonitorCommon:
|
|||
new_md['MDATA_KEY_TITLE'] = '%s-%s-%s:%s:%s' % (show_name, orig_md['MDATA_KEY_YEAR'], show_hour, show_min, show_sec)
|
||||
self.md_manager.save_md_to_file(new_md)
|
||||
|
||||
elif(md['MDATA_KEY_TRACKNUMBER'] == u'unknown'.encode('utf-8')):
|
||||
filepath = '%s/%s/%s/%s/%s-%s%s' % (storage_directory, "imported".encode('utf-8'), md['MDATA_KEY_CREATOR'], md['MDATA_KEY_SOURCE'], md['MDATA_KEY_TITLE'], md['MDATA_KEY_BITRATE'], file_ext)
|
||||
elif(md['MDATA_KEY_TRACKNUMBER'] == encode_to(u'unknown', 'utf-8')):
|
||||
filepath = '%s/%s/%s/%s/%s-%s%s' % (storage_directory, encode_to("imported", 'utf-8'), md['MDATA_KEY_CREATOR'], md['MDATA_KEY_SOURCE'], md['MDATA_KEY_TITLE'], md['MDATA_KEY_BITRATE'], file_ext)
|
||||
else:
|
||||
filepath = '%s/%s/%s/%s/%s-%s-%s%s' % (storage_directory, "imported".encode('utf-8'), md['MDATA_KEY_CREATOR'], md['MDATA_KEY_SOURCE'], md['MDATA_KEY_TRACKNUMBER'], md['MDATA_KEY_TITLE'], md['MDATA_KEY_BITRATE'], file_ext)
|
||||
filepath = '%s/%s/%s/%s/%s-%s-%s%s' % (storage_directory, encode_to("imported", 'utf-8'), md['MDATA_KEY_CREATOR'], md['MDATA_KEY_SOURCE'], md['MDATA_KEY_TRACKNUMBER'], md['MDATA_KEY_TITLE'], md['MDATA_KEY_BITRATE'], file_ext)
|
||||
|
||||
filepath = self.create_unique_filename(filepath, original_path)
|
||||
self.logger.info('Unique filepath: %s', filepath)
|
||||
|
|
|
@ -38,7 +38,13 @@ def to_unicode(obj, encoding='utf-8'):
|
|||
if not isinstance(obj, unicode):
|
||||
obj = unicode(obj, encoding)
|
||||
return obj
|
||||
|
||||
|
||||
def encode_to(obj, encoding='utf-8'):
|
||||
if isinstance(obj, basestring):
|
||||
if not isinstance(obj, str):
|
||||
obj = obj.encode(encoding)
|
||||
return obj
|
||||
|
||||
class ApiClientInterface:
|
||||
|
||||
# Implementation: optional
|
||||
|
|
|
@ -6,7 +6,7 @@ import sys
|
|||
import os
|
||||
import signal
|
||||
|
||||
from api_clients import api_client
|
||||
from api_clients import api_client as apc
|
||||
|
||||
from multiprocessing import Process, Queue as mpQueue
|
||||
|
||||
|
@ -34,7 +34,7 @@ logger.info("\n\n*** Media Monitor bootup ***\n\n")
|
|||
|
||||
try:
|
||||
config = AirtimeMediaConfig(logger)
|
||||
api_client = api_client.api_client_factory(config.cfg)
|
||||
api_client = apc.api_client_factory(config.cfg)
|
||||
|
||||
logger.info("Setting up monitor")
|
||||
response = None
|
||||
|
@ -42,7 +42,7 @@ try:
|
|||
response = api_client.setup_media_monitor()
|
||||
time.sleep(5)
|
||||
|
||||
storage_directory = response["stor"].encode('utf-8')
|
||||
storage_directory = apc.encode_to(response["stor"], 'utf-8')
|
||||
logger.info("Storage Directory is: %s", storage_directory)
|
||||
config.storage_directory = os.path.normpath(storage_directory)
|
||||
config.imported_directory = os.path.normpath(storage_directory + '/imported')
|
||||
|
|
|
@ -2,6 +2,7 @@ import os
|
|||
import time
|
||||
|
||||
from subprocess import Popen, PIPE
|
||||
from api_clients import api_client
|
||||
|
||||
class AirtimeMediaMonitorBootstrap():
|
||||
|
||||
|
@ -29,7 +30,7 @@ class AirtimeMediaMonitorBootstrap():
|
|||
|
||||
for id, dir in directories.iteritems():
|
||||
self.logger.debug("%s, %s", id, dir)
|
||||
self.sync_database_to_filesystem(id, dir.encode("utf-8"))
|
||||
self.sync_database_to_filesystem(id, api_client.encode_to(dir, "utf-8"))
|
||||
|
||||
"""Gets a list of files that the Airtime database knows for a specific directory.
|
||||
You need to provide the directory's row ID, which is obtained when calling
|
||||
|
@ -69,7 +70,7 @@ class AirtimeMediaMonitorBootstrap():
|
|||
db_known_files_set = set()
|
||||
files = self.list_db_files(dir_id)
|
||||
for file in files['files']:
|
||||
db_known_files_set.add(file.encode('utf-8'))
|
||||
db_known_files_set.add(api_client.encode_to(file, 'utf-8'))
|
||||
|
||||
new_files = self.mmc.scan_dir_for_new_files(dir)
|
||||
|
||||
|
|
|
@ -232,7 +232,7 @@ class AirtimeMetadata:
|
|||
if (isinstance(md[key], basestring)):
|
||||
#self.logger.info("Converting md[%s] = '%s' ", key, md[key])
|
||||
md[key] = api_client.to_unicode(md[key])
|
||||
md[key] = md[key].encode('utf-8')
|
||||
md[key] = api_client.encode_to(md[key], 'utf-8')
|
||||
#self.logger.info("Converting complete: md[%s] = '%s' ", key, md[key])
|
||||
|
||||
return md
|
||||
|
|
|
@ -11,6 +11,7 @@ import pyinotify
|
|||
from pyinotify import Notifier
|
||||
|
||||
#from api_clients import api_client
|
||||
from api_clients import api_client
|
||||
from airtimemetadata import AirtimeMetadata
|
||||
|
||||
class AirtimeNotifier(Notifier):
|
||||
|
@ -74,7 +75,7 @@ class AirtimeNotifier(Notifier):
|
|||
self.watch_directory(m['directory'])
|
||||
|
||||
elif m['event_type'] == "remove_watch":
|
||||
watched_directory = m['directory'].encode('utf-8')
|
||||
watched_directory = api_client.encode_to(m['directory'], 'utf-8')
|
||||
|
||||
mm = self.proc_fun()
|
||||
wd = mm.wm.get_wd(watched_directory)
|
||||
|
@ -83,8 +84,8 @@ class AirtimeNotifier(Notifier):
|
|||
|
||||
elif m['event_type'] == "change_stor":
|
||||
storage_directory = self.config.storage_directory
|
||||
new_storage_directory = m['directory'].encode('utf-8')
|
||||
new_storage_directory_id = str(m['dir_id']).encode('utf-8')
|
||||
new_storage_directory = api_client.encode_to(m['directory'], 'utf-8')
|
||||
new_storage_directory_id = api_client.encode_to(str(m['dir_id']), 'utf-8')
|
||||
|
||||
mm = self.proc_fun()
|
||||
|
||||
|
@ -108,7 +109,7 @@ class AirtimeNotifier(Notifier):
|
|||
|
||||
self.watch_directory(new_storage_directory)
|
||||
elif m['event_type'] == "file_delete":
|
||||
filepath = m['filepath'].encode('utf-8')
|
||||
filepath = api_client.encode_to(m['filepath'], 'utf-8')
|
||||
|
||||
mm = self.proc_fun()
|
||||
self.logger.info("Adding file to ignore: %s ", filepath)
|
||||
|
|
|
@ -5,6 +5,7 @@ import logging
|
|||
|
||||
from subprocess import Popen, PIPE
|
||||
from airtimemetadata import AirtimeMetadata
|
||||
from api_clients import api_client
|
||||
|
||||
class MediaMonitorCommon:
|
||||
|
||||
|
@ -164,14 +165,14 @@ class MediaMonitorCommon:
|
|||
try:
|
||||
#will be in the format .ext
|
||||
file_ext = os.path.splitext(original_path)[1]
|
||||
file_ext = file_ext.encode('utf-8')
|
||||
file_ext = api_client.encode_to(file_ext, 'utf-8')
|
||||
|
||||
path_md = ['MDATA_KEY_TITLE', 'MDATA_KEY_CREATOR', 'MDATA_KEY_SOURCE', 'MDATA_KEY_TRACKNUMBER', 'MDATA_KEY_BITRATE']
|
||||
|
||||
md = {}
|
||||
for m in path_md:
|
||||
if m not in orig_md:
|
||||
md[m] = u'unknown'.encode('utf-8')
|
||||
md[m] = api_client.encode_to(u'unknown', 'utf-8')
|
||||
else:
|
||||
#get rid of any "/" which will interfere with the filepath.
|
||||
if isinstance(orig_md[m], basestring):
|
||||
|
@ -189,10 +190,10 @@ class MediaMonitorCommon:
|
|||
filepath = None
|
||||
#file is recorded by Airtime
|
||||
#/srv/airtime/stor/recorded/year/month/year-month-day-time-showname-bitrate.ext
|
||||
if(md['MDATA_KEY_CREATOR'] == "Airtime Show Recorder".encode('utf-8')):
|
||||
if(md['MDATA_KEY_CREATOR'] == api_client.encode_to("Airtime Show Recorder", 'utf-8')):
|
||||
#yyyy-mm-dd-hh-MM-ss
|
||||
y = orig_md['MDATA_KEY_YEAR'].split("-")
|
||||
filepath = '%s/%s/%s/%s/%s-%s-%s%s' % (storage_directory, "recorded".encode('utf-8'), y[0], y[1], orig_md['MDATA_KEY_YEAR'], md['MDATA_KEY_TITLE'], md['MDATA_KEY_BITRATE'], file_ext)
|
||||
filepath = '%s/%s/%s/%s/%s-%s-%s%s' % (storage_directory, api_client.encode_to("recorded", 'utf-8'), y[0], y[1], orig_md['MDATA_KEY_YEAR'], md['MDATA_KEY_TITLE'], md['MDATA_KEY_BITRATE'], file_ext)
|
||||
|
||||
#"Show-Title-2011-03-28-17:15:00"
|
||||
title = md['MDATA_KEY_TITLE'].split("-")
|
||||
|
@ -206,10 +207,10 @@ class MediaMonitorCommon:
|
|||
new_md['MDATA_KEY_TITLE'] = '%s-%s-%s:%s:%s' % (show_name, orig_md['MDATA_KEY_YEAR'], show_hour, show_min, show_sec)
|
||||
self.md_manager.save_md_to_file(new_md)
|
||||
|
||||
elif(md['MDATA_KEY_TRACKNUMBER'] == u'unknown'.encode('utf-8')):
|
||||
filepath = '%s/%s/%s/%s/%s-%s%s' % (storage_directory, "imported".encode('utf-8'), md['MDATA_KEY_CREATOR'], md['MDATA_KEY_SOURCE'], md['MDATA_KEY_TITLE'], md['MDATA_KEY_BITRATE'], file_ext)
|
||||
elif(md['MDATA_KEY_TRACKNUMBER'] == api_client.encode_to(u'unknown', 'utf-8')):
|
||||
filepath = '%s/%s/%s/%s/%s-%s%s' % (storage_directory, api_client.encode_to("imported", 'utf-8'), md['MDATA_KEY_CREATOR'], md['MDATA_KEY_SOURCE'], md['MDATA_KEY_TITLE'], md['MDATA_KEY_BITRATE'], file_ext)
|
||||
else:
|
||||
filepath = '%s/%s/%s/%s/%s-%s-%s%s' % (storage_directory, "imported".encode('utf-8'), md['MDATA_KEY_CREATOR'], md['MDATA_KEY_SOURCE'], md['MDATA_KEY_TRACKNUMBER'], md['MDATA_KEY_TITLE'], md['MDATA_KEY_BITRATE'], file_ext)
|
||||
filepath = '%s/%s/%s/%s/%s-%s-%s%s' % (storage_directory, api_client.encode_to("imported", 'utf-8'), md['MDATA_KEY_CREATOR'], md['MDATA_KEY_SOURCE'], md['MDATA_KEY_TRACKNUMBER'], md['MDATA_KEY_TITLE'], md['MDATA_KEY_BITRATE'], file_ext)
|
||||
|
||||
filepath = self.create_unique_filename(filepath, original_path)
|
||||
self.logger.info('Unique filepath: %s', filepath)
|
||||
|
|
|
@ -131,7 +131,7 @@ class ShowRecorder(Thread):
|
|||
self.logger.info("time: %s" % time)
|
||||
|
||||
name = time+"-"+self.show_name
|
||||
artist = "Airtime Show Recorder".encode('utf-8')
|
||||
artist = api_client.encode_to("Airtime Show Recorder",'utf-8')
|
||||
|
||||
#set some metadata for our file daemon
|
||||
recorded_file = mutagen.File(filepath, easy=True)
|
||||
|
|
|
@ -38,9 +38,9 @@ def copy_or_move_files_to(paths, dest, flag):
|
|||
else:
|
||||
path = currentDir+path
|
||||
path = apc.to_unicode(path)
|
||||
path = path.encode('utf-8')
|
||||
path = apc.encode_to(path, 'utf-8')
|
||||
dest = apc.to_unicode(dest)
|
||||
dest = dest.encode('utf-8')
|
||||
dest = apc.encode_to(dest, 'utf-8')
|
||||
if(os.path.exists(path)):
|
||||
if(os.path.isdir(path)):
|
||||
path = format_dir_string(path)
|
||||
|
@ -148,8 +148,8 @@ def WatchAddAction(option, opt, value, parser):
|
|||
path = os.path.realpath(path)
|
||||
else:
|
||||
path = currentDir+path
|
||||
path = api_client.apc.to_unicode(path)
|
||||
path = path.encode('utf-8')
|
||||
path = apc.to_unicode(path)
|
||||
path = apc.encode_to(path, 'utf-8')
|
||||
if(os.path.isdir(path)):
|
||||
res = api_client.add_watched_dir(path)
|
||||
if(res is None):
|
||||
|
@ -190,7 +190,7 @@ def WatchRemoveAction(option, opt, value, parser):
|
|||
else:
|
||||
path = currentDir+path
|
||||
path = apc.to_unicode(path)
|
||||
path = path.encode('utf-8')
|
||||
path = apc.encode_to(path, 'utf-8')
|
||||
if(os.path.isdir(path)):
|
||||
res = api_client.remove_watched_dir(path)
|
||||
if(res is None):
|
||||
|
@ -236,7 +236,7 @@ def StorageSetAction(option, opt, value, parser):
|
|||
else:
|
||||
path = currentDir+path
|
||||
path = apc.to_unicode(path)
|
||||
path = path.encode('utf-8')
|
||||
path = apc.encode_to(path, 'utf-8')
|
||||
if(os.path.isdir(path)):
|
||||
res = api_client.set_storage_dir(path)
|
||||
if(res is None):
|
||||
|
|
Loading…
Reference in New Issue