mirror of
https://github.com/Tautulli/Tautulli.git
synced 2025-07-07 21:51:14 -07:00
Make export work on Python 2 and set failed state
This commit is contained in:
parent
58292067f0
commit
e256d2080d
2 changed files with 62 additions and 23 deletions
|
@ -16,6 +16,7 @@
|
||||||
# along with Tautulli. If not, see <http://www.gnu.org/licenses/>.
|
# along with Tautulli. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
from future.builtins import str
|
||||||
|
|
||||||
import csv
|
import csv
|
||||||
import json
|
import json
|
||||||
|
@ -898,11 +899,15 @@ def export(section_id=None, rating_key=None, file_format='json'):
|
||||||
else:
|
else:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
if media_type not in MEDIA_TYPES:
|
||||||
|
logger.error("Tautulli Exporter :: Cannot export media type '%s'", media_type)
|
||||||
|
return
|
||||||
|
|
||||||
filename = helpers.clean_filename(filename)
|
filename = helpers.clean_filename(filename)
|
||||||
filepath = get_export_filepath(filename)
|
filepath = get_export_filepath(filename)
|
||||||
logger.info("Tautulli Exporter :: Starting export for '%s'...", filename)
|
logger.info("Tautulli Exporter :: Starting export for '%s'...", filename)
|
||||||
|
|
||||||
export_id = set_export_state(timestamp=timestamp,
|
export_id = add_export(timestamp=timestamp,
|
||||||
section_id=section_id,
|
section_id=section_id,
|
||||||
rating_key=rating_key,
|
rating_key=rating_key,
|
||||||
media_type=media_type,
|
media_type=media_type,
|
||||||
|
@ -914,23 +919,49 @@ def export(section_id=None, rating_key=None, file_format='json'):
|
||||||
|
|
||||||
attrs = MEDIA_TYPES[media_type]
|
attrs = MEDIA_TYPES[media_type]
|
||||||
part = partial(helpers.get_attrs_to_dict, attrs=attrs)
|
part = partial(helpers.get_attrs_to_dict, attrs=attrs)
|
||||||
|
pool = ThreadPool(processes=4)
|
||||||
|
success = True
|
||||||
|
|
||||||
with ThreadPool(processes=4) as pool:
|
try:
|
||||||
result = pool.map(part, items)
|
result = pool.map(part, items)
|
||||||
|
|
||||||
if file_format == 'json':
|
if file_format == 'json':
|
||||||
with open(filepath, 'w', encoding='utf-8') as outfile:
|
if plexpy.PYTHON2:
|
||||||
|
kw = {'mode': 'wb'}
|
||||||
|
else:
|
||||||
|
kw = {'mode': 'w', 'encoding': 'utf-8'}
|
||||||
|
|
||||||
|
with open(filepath, **kw) as outfile:
|
||||||
json.dump(result, outfile, indent=4, ensure_ascii=False, sort_keys=True)
|
json.dump(result, outfile, indent=4, ensure_ascii=False, sort_keys=True)
|
||||||
|
|
||||||
elif file_format == 'csv':
|
elif file_format == 'csv':
|
||||||
|
if plexpy.PYTHON2:
|
||||||
|
kw = {'mode': 'wb'}
|
||||||
|
else:
|
||||||
|
kw = {'mode': 'w', 'encoding': 'utf-8', 'newline': ''}
|
||||||
|
|
||||||
flatten_result = helpers.flatten_dict(result)
|
flatten_result = helpers.flatten_dict(result)
|
||||||
flatten_attrs = set().union(*flatten_result)
|
flatten_attrs = set().union(*flatten_result)
|
||||||
with open(filepath, 'w', encoding='utf-8', newline='') as outfile:
|
with open(filepath, **kw) as outfile:
|
||||||
writer = csv.DictWriter(outfile, sorted(flatten_attrs))
|
writer = csv.DictWriter(outfile, sorted(flatten_attrs))
|
||||||
writer.writeheader()
|
writer.writeheader()
|
||||||
writer.writerows(flatten_result)
|
writer.writerows(flatten_result)
|
||||||
|
|
||||||
set_export_complete(export_id=export_id)
|
except Exception as e:
|
||||||
|
import traceback
|
||||||
|
traceback.print_exc()
|
||||||
|
set_export_state(export_id=export_id, success=False)
|
||||||
|
logger.error("Tautulli Exporter :: Failed exported to '%s': %s", filename, e)
|
||||||
|
success = False
|
||||||
|
|
||||||
|
finally:
|
||||||
|
pool.close()
|
||||||
|
pool.join()
|
||||||
|
|
||||||
|
if not success:
|
||||||
|
return
|
||||||
|
|
||||||
|
set_export_state(export_id=export_id)
|
||||||
logger.info("Tautulli Exporter :: Successfully exported to '%s'", filepath)
|
logger.info("Tautulli Exporter :: Successfully exported to '%s'", filepath)
|
||||||
|
|
||||||
|
|
||||||
|
@ -946,7 +977,7 @@ def get_export(export_id):
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def set_export_state(timestamp, section_id, rating_key, media_type, file_format, filename):
|
def add_export(timestamp, section_id, rating_key, media_type, file_format, filename):
|
||||||
keys = {'timestamp': timestamp,
|
keys = {'timestamp': timestamp,
|
||||||
'section_id': section_id,
|
'section_id': section_id,
|
||||||
'rating_key': rating_key,
|
'rating_key': rating_key,
|
||||||
|
@ -964,9 +995,14 @@ def set_export_state(timestamp, section_id, rating_key, media_type, file_format,
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def set_export_complete(export_id):
|
def set_export_state(export_id, success=True):
|
||||||
|
if success:
|
||||||
|
complete = 1
|
||||||
|
else:
|
||||||
|
complete = -1
|
||||||
|
|
||||||
keys = {'id': export_id}
|
keys = {'id': export_id}
|
||||||
values = {'complete': 1}
|
values = {'complete': complete}
|
||||||
|
|
||||||
db = database.MonitorDatabase()
|
db = database.MonitorDatabase()
|
||||||
db.upsert(table_name='exports', key_dict=keys, value_dict=values)
|
db.upsert(table_name='exports', key_dict=keys, value_dict=values)
|
||||||
|
|
|
@ -919,6 +919,7 @@ class WebInterface(object):
|
||||||
dt_columns = [("timestamp", True, False),
|
dt_columns = [("timestamp", True, False),
|
||||||
("media_type_title", True, True),
|
("media_type_title", True, True),
|
||||||
("rating_key", True, True),
|
("rating_key", True, True),
|
||||||
|
("file_format", True, True),
|
||||||
("filename", True, True),
|
("filename", True, True),
|
||||||
("complete", True, False)]
|
("complete", True, False)]
|
||||||
kwargs['json_data'] = build_datatables_json(kwargs, dt_columns, "timestamp")
|
kwargs['json_data'] = build_datatables_json(kwargs, dt_columns, "timestamp")
|
||||||
|
@ -6524,11 +6525,13 @@ class WebInterface(object):
|
||||||
```
|
```
|
||||||
"""
|
"""
|
||||||
result = exporter.get_export(export_id=row_id)
|
result = exporter.get_export(export_id=row_id)
|
||||||
if result and result['complete'] and result['exists']:
|
if result and result['complete'] == 1 and result['exists']:
|
||||||
return serve_download(exporter.get_export_filepath(result['filename']), name=result['filename'])
|
return serve_download(exporter.get_export_filepath(result['filename']), name=result['filename'])
|
||||||
else:
|
else:
|
||||||
if result and not result.get('complete'):
|
if result and result.get('complete') == 0:
|
||||||
msg = 'Export is still being processed.'
|
msg = 'Export is still being processed.'
|
||||||
|
elif result and result.get('complete') == -1:
|
||||||
|
msg = 'Export failed to process.'
|
||||||
elif result and not result.get('exists'):
|
elif result and not result.get('exists'):
|
||||||
msg = 'Export file does not exist.'
|
msg = 'Export file does not exist.'
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue