CC-2633: moving to_unicode() to api_client.py
- move to_unicode() to api_client.py - fixed how it decodes path in airtime-import.py
This commit is contained in:
parent
e2a5344e6e
commit
375cfb2c2d
|
@ -5,11 +5,16 @@ import logging
|
||||||
import math
|
import math
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
def to_unicode(obj, encoding='utf-8'):
|
||||||
|
if isinstance(obj, basestring):
|
||||||
|
if not isinstance(obj, unicode):
|
||||||
|
obj = unicode(obj, encoding)
|
||||||
|
return obj
|
||||||
|
|
||||||
"""
|
"""
|
||||||
list of supported easy tags in mutagen version 1.20
|
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']
|
['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']
|
||||||
"""
|
"""
|
||||||
|
|
||||||
class AirtimeMetadata:
|
class AirtimeMetadata:
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
@ -108,7 +113,6 @@ class AirtimeMetadata:
|
||||||
|
|
||||||
def get_md_from_file(self, filepath):
|
def get_md_from_file(self, filepath):
|
||||||
|
|
||||||
self.logger.debug("testing upgrade")
|
|
||||||
self.logger.info("getting info from filepath %s", filepath)
|
self.logger.info("getting info from filepath %s", filepath)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -125,7 +129,8 @@ class AirtimeMetadata:
|
||||||
|
|
||||||
|
|
||||||
self.logger.info(file_info)
|
self.logger.info(file_info)
|
||||||
|
if file_info is None:
|
||||||
|
return None
|
||||||
#check if file has any metadata
|
#check if file has any metadata
|
||||||
if file_info is not None:
|
if file_info is not None:
|
||||||
for key in file_info.keys() :
|
for key in file_info.keys() :
|
||||||
|
@ -134,11 +139,13 @@ class AirtimeMetadata:
|
||||||
|
|
||||||
if 'MDATA_KEY_TITLE' not in md:
|
if 'MDATA_KEY_TITLE' not in md:
|
||||||
#get rid of file extention from original name, name might have more than 1 '.' in it.
|
#get rid of file extention from original name, name might have more than 1 '.' in it.
|
||||||
|
#filepath = to_unicode(filepath)
|
||||||
|
#filepath = filepath.encode('utf-8')
|
||||||
original_name = os.path.basename(filepath)
|
original_name = os.path.basename(filepath)
|
||||||
original_name = original_name.split(".")[0:-1]
|
original_name = original_name.split(".")[0:-1]
|
||||||
original_name = ''.join(original_name)
|
original_name = ''.join(original_name)
|
||||||
md['MDATA_KEY_TITLE'] = original_name
|
md['MDATA_KEY_TITLE'] = original_name
|
||||||
|
|
||||||
#incase track number is in format u'4/11'
|
#incase track number is in format u'4/11'
|
||||||
#need to also check that the tracknumber is even a tracknumber (cc-2582)
|
#need to also check that the tracknumber is even a tracknumber (cc-2582)
|
||||||
if 'MDATA_KEY_TRACKNUMBER' in md:
|
if 'MDATA_KEY_TRACKNUMBER' in md:
|
||||||
|
@ -175,10 +182,11 @@ class AirtimeMetadata:
|
||||||
|
|
||||||
#do this so object can be urlencoded properly.
|
#do this so object can be urlencoded properly.
|
||||||
for key in md.keys():
|
for key in md.keys():
|
||||||
if(isinstance(md[key], basestring)):
|
|
||||||
|
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] = md[key].encode('utf-8')
|
||||||
|
#self.logger.info("Converting complete: md[%s] = '%s' ", key, md[key])
|
||||||
self.logger.info("MD after parsing.")
|
|
||||||
self.logger.debug(md)
|
|
||||||
|
|
||||||
return md
|
return md
|
||||||
|
|
|
@ -32,7 +32,13 @@ def api_client_factory(config):
|
||||||
else:
|
else:
|
||||||
logger.info('API Client "'+config["api_client"]+'" not supported. Please check your config file.\n')
|
logger.info('API Client "'+config["api_client"]+'" not supported. Please check your config file.\n')
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
|
def to_unicode(obj, encoding='utf-8'):
|
||||||
|
if isinstance(obj, basestring):
|
||||||
|
if not isinstance(obj, unicode):
|
||||||
|
obj = unicode(obj, encoding)
|
||||||
|
return obj
|
||||||
|
|
||||||
class ApiClientInterface:
|
class ApiClientInterface:
|
||||||
|
|
||||||
# Implementation: optional
|
# Implementation: optional
|
||||||
|
@ -515,7 +521,6 @@ class AirTimeApiClient(ApiClientInterface):
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
################################################################################
|
################################################################################
|
||||||
# OpenBroadcast API Client
|
# OpenBroadcast API Client
|
||||||
################################################################################
|
################################################################################
|
||||||
|
|
|
@ -4,12 +4,7 @@ import mutagen
|
||||||
import logging
|
import logging
|
||||||
import math
|
import math
|
||||||
import re
|
import re
|
||||||
|
from api_clients import api_client
|
||||||
def to_unicode(obj, encoding='utf-8'):
|
|
||||||
if isinstance(obj, basestring):
|
|
||||||
if not isinstance(obj, unicode):
|
|
||||||
obj = unicode(obj, encoding)
|
|
||||||
return obj
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
list of supported easy tags in mutagen version 1.20
|
list of supported easy tags in mutagen version 1.20
|
||||||
|
@ -139,8 +134,8 @@ class AirtimeMetadata:
|
||||||
|
|
||||||
if 'MDATA_KEY_TITLE' not in md:
|
if 'MDATA_KEY_TITLE' not in md:
|
||||||
#get rid of file extention from original name, name might have more than 1 '.' in it.
|
#get rid of file extention from original name, name might have more than 1 '.' in it.
|
||||||
filepath = to_unicode(filepath)
|
#filepath = to_unicode(filepath)
|
||||||
filepath = filepath.encode('utf-8')
|
#filepath = filepath.encode('utf-8')
|
||||||
original_name = os.path.basename(filepath)
|
original_name = os.path.basename(filepath)
|
||||||
original_name = original_name.split(".")[0:-1]
|
original_name = original_name.split(".")[0:-1]
|
||||||
original_name = ''.join(original_name)
|
original_name = ''.join(original_name)
|
||||||
|
@ -185,7 +180,7 @@ class AirtimeMetadata:
|
||||||
|
|
||||||
if (isinstance(md[key], basestring)):
|
if (isinstance(md[key], basestring)):
|
||||||
#self.logger.info("Converting md[%s] = '%s' ", key, md[key])
|
#self.logger.info("Converting md[%s] = '%s' ", key, md[key])
|
||||||
md[key] = to_unicode(md[key])
|
md[key] = api_client.to_unicode(md[key])
|
||||||
md[key] = md[key].encode('utf-8')
|
md[key] = md[key].encode('utf-8')
|
||||||
#self.logger.info("Converting complete: md[%s] = '%s' ", key, md[key])
|
#self.logger.info("Converting complete: md[%s] = '%s' ", key, md[key])
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ import os
|
||||||
import logging
|
import logging
|
||||||
from configobj import ConfigObj
|
from configobj import ConfigObj
|
||||||
from optparse import OptionParser, OptionValueError
|
from optparse import OptionParser, OptionValueError
|
||||||
from api_clients import api_client
|
from api_clients import api_client as apc
|
||||||
import json
|
import json
|
||||||
import shutil
|
import shutil
|
||||||
|
|
||||||
|
@ -26,7 +26,7 @@ except Exception, e:
|
||||||
print('Error loading config file: %s', e)
|
print('Error loading config file: %s', e)
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
api_client = api_client.api_client_factory(config)
|
api_client = apc.api_client_factory(config)
|
||||||
|
|
||||||
#helper functions
|
#helper functions
|
||||||
# copy or move files
|
# copy or move files
|
||||||
|
@ -37,9 +37,9 @@ def copy_or_move_files_to(paths, dest, flag):
|
||||||
path = os.path.realpath(path)
|
path = os.path.realpath(path)
|
||||||
else:
|
else:
|
||||||
path = currentDir+path
|
path = currentDir+path
|
||||||
path = path.decode('utf-8')
|
path = apc.to_unicode(path)
|
||||||
path = path.encode('utf-8')
|
path = path.encode('utf-8')
|
||||||
dest = dest.decode('utf-8')
|
dest = apc.to_unicode(dest)
|
||||||
dest = dest.encode('utf-8')
|
dest = dest.encode('utf-8')
|
||||||
if(os.path.exists(path)):
|
if(os.path.exists(path)):
|
||||||
if(os.path.isdir(path)):
|
if(os.path.isdir(path)):
|
||||||
|
@ -148,7 +148,7 @@ def WatchAddAction(option, opt, value, parser):
|
||||||
path = os.path.realpath(path)
|
path = os.path.realpath(path)
|
||||||
else:
|
else:
|
||||||
path = currentDir+path
|
path = currentDir+path
|
||||||
path = path.decode('utf-8')
|
path = api_client.apc.to_unicode(path)
|
||||||
path = path.encode('utf-8')
|
path = path.encode('utf-8')
|
||||||
if(os.path.isdir(path)):
|
if(os.path.isdir(path)):
|
||||||
res = api_client.add_watched_dir(path)
|
res = api_client.add_watched_dir(path)
|
||||||
|
@ -189,7 +189,7 @@ def WatchRemoveAction(option, opt, value, parser):
|
||||||
path = os.path.realpath(path)
|
path = os.path.realpath(path)
|
||||||
else:
|
else:
|
||||||
path = currentDir+path
|
path = currentDir+path
|
||||||
path = path.decode('utf-8')
|
path = apc.to_unicode(path)
|
||||||
path = path.encode('utf-8')
|
path = path.encode('utf-8')
|
||||||
if(os.path.isdir(path)):
|
if(os.path.isdir(path)):
|
||||||
res = api_client.remove_watched_dir(path)
|
res = api_client.remove_watched_dir(path)
|
||||||
|
@ -235,7 +235,7 @@ def StorageSetAction(option, opt, value, parser):
|
||||||
path = os.path.realpath(path)
|
path = os.path.realpath(path)
|
||||||
else:
|
else:
|
||||||
path = currentDir+path
|
path = currentDir+path
|
||||||
path = path.decode('utf-8')
|
path = apc.to_unicode(path)
|
||||||
path = path.encode('utf-8')
|
path = path.encode('utf-8')
|
||||||
if(os.path.isdir(path)):
|
if(os.path.isdir(path)):
|
||||||
res = api_client.set_storage_dir(path)
|
res = api_client.set_storage_dir(path)
|
||||||
|
|
Loading…
Reference in New Issue