Add ability to export custom fields

This commit is contained in:
JonnyWong16 2020-09-27 13:26:04 -07:00
parent 112811190e
commit 4ee9dbab41
No known key found for this signature in database
GPG key ID: B1F1F9807184697A
2 changed files with 48 additions and 8 deletions

View file

@ -55,15 +55,26 @@ class Export(object):
)
METADATA_LEVELS = (1, 2, 3, 9)
MEDIA_INFO_LEVELS = (1, 2, 3, 9)
CUSTOM_FIELD_KEYS = (
'movies',
'shows', 'seasons', 'episodes',
'artists', 'albums', 'tracks',
'photoalbums', 'photos',
'collections', 'children',
'playlists', 'items'
)
def __init__(self, section_id=None, rating_key=None, file_format='json',
metadata_level=1, media_info_level=1, include_images=False):
metadata_level=1, media_info_level=1, include_images=False,
custom_fields=''):
self.section_id = helpers.cast_to_int(section_id)
self.rating_key = helpers.cast_to_int(rating_key)
self.file_format = file_format
self.metadata_level = helpers.cast_to_int(metadata_level)
self.media_info_level = helpers.cast_to_int(media_info_level)
self.include_images = include_images
self.custom_fields = custom_fields
self._custom_fields = {}
self.timestamp = helpers.timestamp()
@ -1442,6 +1453,8 @@ class Export(object):
if self.include_images and self.media_type not in ('movie', 'show', 'season', 'artist', 'album'):
self.include_images = False
self._process_custom_fields()
self.filename = '{}.{}'.format(helpers.clean_filename(filename), self.file_format)
self.export_id = self.add_export()
if not self.export_id:
@ -1452,6 +1465,22 @@ class Export(object):
return True
def _process_custom_fields(self):
for field in self.custom_fields.split(','):
field = field.strip()
media_type = self.media_type
for key in self.CUSTOM_FIELD_KEYS:
if field.startswith(key + '.'):
media_type, field = field.split('.', maxsplit=1)
if key != 'children':
media_type = media_type[:-1]
if media_type in self._custom_fields:
self._custom_fields[media_type].add(field)
else:
self._custom_fields[media_type] = {field}
def add_export(self):
keys = {'timestamp': self.timestamp,
'section_id': self.section_id,
@ -1559,6 +1588,12 @@ class Export(object):
if image_attr in media_attrs:
export_attrs_set.add(image_attr)
export_attrs_set.update(self._custom_fields.get(media_type, []))
if self.media_type == 'collection' and 'children' in self._custom_fields:
export_attrs_set.update(self._custom_fields['children'])
elif self.media_type == 'playlist' and 'items' in self._custom_fields:
export_attrs_set.update(self._custom_fields['items'])
for attr in export_attrs_set:
try:
value = helpers.get_dict_value_by_path(media_attrs, attr)

View file

@ -6492,7 +6492,8 @@ class WebInterface(object):
@requireAuth(member_of("admin"))
@addtoapi()
def export_metadata(self, section_id=None, rating_key=None, file_format='json',
metadata_level=1, media_info_level=1, include_images=False, **kwargs):
metadata_level=1, media_info_level=1, include_images=False,
custom_fields='', **kwargs):
""" Export library or media metadata to a file
```
@ -6504,6 +6505,9 @@ class WebInterface(object):
file_format (str): 'json' (default) or 'csv'
metadata_level (int): The level of metadata to export (default 1)
media_info_level (int): The level of media info to export (default 1)
include_images (bool): True or False to export artwork and posters images
custom_fields (str): Comma separated list of custom fields to export
in addition to the export level selected
Returns:
json:
@ -6517,7 +6521,8 @@ class WebInterface(object):
file_format=file_format,
metadata_level=metadata_level,
media_info_level=media_info_level,
include_images=helpers.bool_true(include_images)).export()
include_images=helpers.bool_true(include_images),
custom_fields=custom_fields).export()
if result:
return {'result': 'success', 'message': 'Metadata export has started.'}