mirror of
https://github.com/Tautulli/Tautulli.git
synced 2025-08-22 14:13:40 -07:00
Fix libraries.py
This commit is contained in:
parent
f3624e979b
commit
de09c9f481
1 changed files with 109 additions and 109 deletions
|
@ -95,8 +95,8 @@ def refresh_libraries():
|
||||||
|
|
||||||
add_live_tv_library(refresh=True)
|
add_live_tv_library(refresh=True)
|
||||||
|
|
||||||
query = 'UPDATE library_sections SET is_active = 0 WHERE server_id != ? OR ' \
|
query = "UPDATE library_sections SET is_active = 0 WHERE server_id != ? OR " \
|
||||||
'section_id NOT IN ({})'.format(', '.join(['?'] * len(section_ids)))
|
"section_id NOT IN ({})".format(", ".join(["?"] * len(section_ids)))
|
||||||
monitor_db.action(query=query, args=[plexpy.CONFIG.PMS_IDENTIFIER] + section_ids)
|
monitor_db.action(query=query, args=[plexpy.CONFIG.PMS_IDENTIFIER] + section_ids)
|
||||||
|
|
||||||
new_keys = plexpy.CONFIG.HOME_LIBRARY_CARDS + new_keys
|
new_keys = plexpy.CONFIG.HOME_LIBRARY_CARDS + new_keys
|
||||||
|
@ -112,8 +112,8 @@ def refresh_libraries():
|
||||||
|
|
||||||
def add_live_tv_library(refresh=False):
|
def add_live_tv_library(refresh=False):
|
||||||
monitor_db = database.MonitorDatabase()
|
monitor_db = database.MonitorDatabase()
|
||||||
result = monitor_db.select_single('SELECT * FROM library_sections '
|
result = monitor_db.select_single("SELECT * FROM library_sections "
|
||||||
'WHERE section_id = ? and server_id = ?',
|
"WHERE section_id = ? and server_id = ?",
|
||||||
[common.LIVE_TV_SECTION_ID, plexpy.CONFIG.PMS_IDENTIFIER])
|
[common.LIVE_TV_SECTION_ID, plexpy.CONFIG.PMS_IDENTIFIER])
|
||||||
|
|
||||||
if result and not refresh or not result and refresh:
|
if result and not refresh or not result and refresh:
|
||||||
|
@ -138,7 +138,7 @@ def add_live_tv_library(refresh=False):
|
||||||
|
|
||||||
def has_library_type(section_type):
|
def has_library_type(section_type):
|
||||||
monitor_db = database.MonitorDatabase()
|
monitor_db = database.MonitorDatabase()
|
||||||
query = 'SELECT * FROM library_sections WHERE section_type = ? AND deleted_section = 0'
|
query = "SELECT * FROM library_sections WHERE section_type = ? AND deleted_section = 0"
|
||||||
args = [section_type]
|
args = [section_type]
|
||||||
result = monitor_db.select_single(query=query, args=args)
|
result = monitor_db.select_single(query=query, args=args)
|
||||||
return bool(result)
|
return bool(result)
|
||||||
|
@ -328,44 +328,44 @@ class Libraries(object):
|
||||||
|
|
||||||
group_by = 'session_history.reference_id' if grouping else 'session_history.id'
|
group_by = 'session_history.reference_id' if grouping else 'session_history.id'
|
||||||
|
|
||||||
columns = ['library_sections.id AS row_id',
|
columns = ["library_sections.id AS row_id",
|
||||||
'library_sections.server_id',
|
"library_sections.server_id",
|
||||||
'library_sections.section_id',
|
"library_sections.section_id",
|
||||||
'library_sections.section_name',
|
"library_sections.section_name",
|
||||||
'library_sections.section_type',
|
"library_sections.section_type",
|
||||||
'library_sections.count',
|
"library_sections.count",
|
||||||
'library_sections.parent_count',
|
"library_sections.parent_count",
|
||||||
'library_sections.child_count',
|
"library_sections.child_count",
|
||||||
'library_sections.thumb AS library_thumb',
|
"library_sections.thumb AS library_thumb",
|
||||||
'library_sections.custom_thumb_url AS custom_thumb',
|
"library_sections.custom_thumb_url AS custom_thumb",
|
||||||
'library_sections.art AS library_art',
|
"library_sections.art AS library_art",
|
||||||
'library_sections.custom_art_url AS custom_art',
|
"library_sections.custom_art_url AS custom_art",
|
||||||
'COUNT(DISTINCT %s) AS plays' % group_by,
|
"COUNT(DISTINCT %s) AS plays" % group_by,
|
||||||
'SUM(CASE WHEN session_history.stopped > 0 THEN (session_history.stopped - session_history.started) \
|
"SUM(CASE WHEN session_history.stopped > 0 THEN (session_history.stopped - session_history.started) \
|
||||||
ELSE 0 END) - SUM(CASE WHEN session_history.paused_counter IS NULL THEN 0 ELSE \
|
ELSE 0 END) - SUM(CASE WHEN session_history.paused_counter IS NULL THEN 0 ELSE \
|
||||||
session_history.paused_counter END) AS duration',
|
session_history.paused_counter END) AS duration",
|
||||||
'MAX(session_history.started) AS last_accessed',
|
"MAX(session_history.started) AS last_accessed",
|
||||||
'MAX(session_history.id) AS history_row_id',
|
"MAX(session_history.id) AS history_row_id",
|
||||||
'session_history_metadata.full_title AS last_played',
|
"session_history_metadata.full_title AS last_played",
|
||||||
'session_history.rating_key',
|
"session_history.rating_key",
|
||||||
'session_history_metadata.media_type',
|
"session_history_metadata.media_type",
|
||||||
'session_history_metadata.thumb',
|
"session_history_metadata.thumb",
|
||||||
'session_history_metadata.parent_thumb',
|
"session_history_metadata.parent_thumb",
|
||||||
'session_history_metadata.grandparent_thumb',
|
"session_history_metadata.grandparent_thumb",
|
||||||
'session_history_metadata.parent_title',
|
"session_history_metadata.parent_title",
|
||||||
'session_history_metadata.year',
|
"session_history_metadata.year",
|
||||||
'session_history_metadata.media_index',
|
"session_history_metadata.media_index",
|
||||||
'session_history_metadata.parent_media_index',
|
"session_history_metadata.parent_media_index",
|
||||||
'session_history_metadata.content_rating',
|
"session_history_metadata.content_rating",
|
||||||
'session_history_metadata.labels',
|
"session_history_metadata.labels",
|
||||||
'session_history_metadata.live',
|
"session_history_metadata.live",
|
||||||
'session_history_metadata.added_at',
|
"session_history_metadata.added_at",
|
||||||
'session_history_metadata.originally_available_at',
|
"session_history_metadata.originally_available_at",
|
||||||
'session_history_metadata.guid',
|
"session_history_metadata.guid",
|
||||||
'library_sections.do_notify',
|
"library_sections.do_notify",
|
||||||
'library_sections.do_notify_created',
|
"library_sections.do_notify_created",
|
||||||
'library_sections.keep_history',
|
"library_sections.keep_history",
|
||||||
'library_sections.is_active'
|
"library_sections.is_active"
|
||||||
]
|
]
|
||||||
try:
|
try:
|
||||||
query = data_tables.ssp_query(table_name='library_sections',
|
query = data_tables.ssp_query(table_name='library_sections',
|
||||||
|
@ -499,11 +499,11 @@ class Libraries(object):
|
||||||
group_by = 'rating_key'
|
group_by = 'rating_key'
|
||||||
|
|
||||||
try:
|
try:
|
||||||
query = 'SELECT MAX(started) AS last_played, COUNT(DISTINCT %s) AS play_count, ' \
|
query = "SELECT MAX(started) AS last_played, COUNT(DISTINCT %s) AS play_count, " \
|
||||||
'rating_key, parent_rating_key, grandparent_rating_key ' \
|
"rating_key, parent_rating_key, grandparent_rating_key " \
|
||||||
'FROM session_history ' \
|
"FROM session_history " \
|
||||||
'WHERE section_id = ? ' \
|
"WHERE section_id = ? " \
|
||||||
'GROUP BY %s ' % (count_by, group_by)
|
"GROUP BY %s " % (count_by, group_by)
|
||||||
result = monitor_db.select(query, args=[section_id])
|
result = monitor_db.select(query, args=[section_id])
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warn("Tautulli Libraries :: Unable to execute database query for get_datatables_media_info2: %s." % e)
|
logger.warn("Tautulli Libraries :: Unable to execute database query for get_datatables_media_info2: %s." % e)
|
||||||
|
@ -838,27 +838,27 @@ class Libraries(object):
|
||||||
last_accessed = 'NULL'
|
last_accessed = 'NULL'
|
||||||
join = ''
|
join = ''
|
||||||
if include_last_accessed:
|
if include_last_accessed:
|
||||||
last_accessed = 'MAX(session_history.started)'
|
last_accessed = "MAX(session_history.started)"
|
||||||
join = 'LEFT OUTER JOIN session_history ON library_sections.section_id = session_history.section_id ' \
|
join = "LEFT OUTER JOIN session_history ON library_sections.section_id = session_history.section_id " \
|
||||||
|
|
||||||
monitor_db = database.MonitorDatabase()
|
monitor_db = database.MonitorDatabase()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if str(section_id).isdigit():
|
if str(section_id).isdigit():
|
||||||
where = 'library_sections.section_id = ?'
|
where = "library_sections.section_id = ?"
|
||||||
args = [section_id]
|
args = [section_id]
|
||||||
else:
|
else:
|
||||||
raise Exception('Missing section_id')
|
raise Exception('Missing section_id')
|
||||||
|
|
||||||
query = 'SELECT library_sections.id AS row_id, server_id, library_sections.section_id, ' \
|
query = "SELECT library_sections.id AS row_id, server_id, library_sections.section_id, " \
|
||||||
'section_name, section_type, ' \
|
"section_name, section_type, " \
|
||||||
'count, parent_count, child_count, ' \
|
"count, parent_count, child_count, " \
|
||||||
'library_sections.thumb AS library_thumb, custom_thumb_url AS custom_thumb, ' \
|
"library_sections.thumb AS library_thumb, custom_thumb_url AS custom_thumb, " \
|
||||||
'library_sections.art AS library_art, ' \
|
"library_sections.art AS library_art, " \
|
||||||
'custom_art_url AS custom_art, is_active, ' \
|
"custom_art_url AS custom_art, is_active, " \
|
||||||
'do_notify, do_notify_created, keep_history, deleted_section, %s AS last_accessed ' \
|
"do_notify, do_notify_created, keep_history, deleted_section, %s AS last_accessed " \
|
||||||
'FROM library_sections %s ' \
|
"FROM library_sections %s " \
|
||||||
'WHERE %s AND server_id = ? ' % (last_accessed, join, where)
|
"WHERE %s AND server_id = ? " % (last_accessed, join, where)
|
||||||
result = monitor_db.select(query, args=args + [server_id])
|
result = monitor_db.select(query, args=args + [server_id])
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warn("Tautulli Libraries :: Unable to execute database query for get_library_details: %s." % e)
|
logger.warn("Tautulli Libraries :: Unable to execute database query for get_library_details: %s." % e)
|
||||||
|
@ -924,24 +924,24 @@ class Libraries(object):
|
||||||
try:
|
try:
|
||||||
if days > 0:
|
if days > 0:
|
||||||
if str(section_id).isdigit():
|
if str(section_id).isdigit():
|
||||||
query = 'SELECT (SUM(stopped - started) - ' \
|
query = "SELECT (SUM(stopped - started) - " \
|
||||||
'SUM(CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END)) AS total_time, ' \
|
"SUM(CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END)) AS total_time, " \
|
||||||
'COUNT(DISTINCT %s) AS total_plays ' \
|
"COUNT(DISTINCT %s) AS total_plays " \
|
||||||
'FROM session_history ' \
|
"FROM session_history " \
|
||||||
'JOIN session_history_metadata ON session_history_metadata.id = session_history.id ' \
|
"JOIN session_history_metadata ON session_history_metadata.id = session_history.id " \
|
||||||
'WHERE stopped >= %s ' \
|
"WHERE stopped >= %s " \
|
||||||
'AND section_id = ?' % (group_by, timestamp_query)
|
"AND section_id = ?" % (group_by, timestamp_query)
|
||||||
result = monitor_db.select(query, args=[section_id])
|
result = monitor_db.select(query, args=[section_id])
|
||||||
else:
|
else:
|
||||||
result = []
|
result = []
|
||||||
else:
|
else:
|
||||||
if str(section_id).isdigit():
|
if str(section_id).isdigit():
|
||||||
query = 'SELECT (SUM(stopped - started) - ' \
|
query = "SELECT (SUM(stopped - started) - " \
|
||||||
'SUM(CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END)) AS total_time, ' \
|
"SUM(CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END)) AS total_time, " \
|
||||||
'COUNT(DISTINCT %s) AS total_plays ' \
|
"COUNT(DISTINCT %s) AS total_plays " \
|
||||||
'FROM session_history ' \
|
"FROM session_history " \
|
||||||
'JOIN session_history_metadata ON session_history_metadata.id = session_history.id ' \
|
"JOIN session_history_metadata ON session_history_metadata.id = session_history.id " \
|
||||||
'WHERE section_id = ?' % group_by
|
"WHERE section_id = ?" % group_by
|
||||||
result = monitor_db.select(query, args=[section_id])
|
result = monitor_db.select(query, args=[section_id])
|
||||||
else:
|
else:
|
||||||
result = []
|
result = []
|
||||||
|
@ -981,17 +981,17 @@ class Libraries(object):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if str(section_id).isdigit():
|
if str(section_id).isdigit():
|
||||||
query = 'SELECT (CASE WHEN users.friendly_name IS NULL OR TRIM(users.friendly_name) = "" ' \
|
query = "SELECT (CASE WHEN users.friendly_name IS NULL OR TRIM(users.friendly_name) = '' " \
|
||||||
'THEN users.username ELSE users.friendly_name END) AS friendly_name, ' \
|
"THEN users.username ELSE users.friendly_name END) AS friendly_name, " \
|
||||||
'users.user_id, users.username, users.thumb, users.custom_avatar_url AS custom_thumb, ' \
|
"users.user_id, users.username, users.thumb, users.custom_avatar_url AS custom_thumb, " \
|
||||||
'COUNT(DISTINCT %s) AS total_plays, (SUM(stopped - started) - ' \
|
"COUNT(DISTINCT %s) AS total_plays, (SUM(stopped - started) - " \
|
||||||
'SUM(CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END)) AS total_time ' \
|
"SUM(CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END)) AS total_time " \
|
||||||
'FROM session_history ' \
|
"FROM session_history " \
|
||||||
'JOIN session_history_metadata ON session_history_metadata.id = session_history.id ' \
|
"JOIN session_history_metadata ON session_history_metadata.id = session_history.id " \
|
||||||
'JOIN users ON users.user_id = session_history.user_id ' \
|
"JOIN users ON users.user_id = session_history.user_id " \
|
||||||
'WHERE section_id = ? ' \
|
"WHERE section_id = ? " \
|
||||||
'GROUP BY users.user_id ' \
|
"GROUP BY users.user_id " \
|
||||||
'ORDER BY total_plays DESC, total_time DESC' % group_by
|
"ORDER BY total_plays DESC, total_time DESC" % group_by
|
||||||
result = monitor_db.select(query, args=[section_id])
|
result = monitor_db.select(query, args=[section_id])
|
||||||
else:
|
else:
|
||||||
result = []
|
result = []
|
||||||
|
@ -1030,16 +1030,16 @@ class Libraries(object):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if str(section_id).isdigit():
|
if str(section_id).isdigit():
|
||||||
query = 'SELECT session_history.id, session_history.media_type, guid, ' \
|
query = "SELECT session_history.id, session_history.media_type, guid, " \
|
||||||
'session_history.rating_key, session_history.parent_rating_key, session_history.grandparent_rating_key, ' \
|
"session_history.rating_key, session_history.parent_rating_key, session_history.grandparent_rating_key, " \
|
||||||
'title, parent_title, grandparent_title, original_title, ' \
|
"title, parent_title, grandparent_title, original_title, " \
|
||||||
'thumb, parent_thumb, grandparent_thumb, media_index, parent_media_index, ' \
|
"thumb, parent_thumb, grandparent_thumb, media_index, parent_media_index, " \
|
||||||
'year, originally_available_at, added_at, live, started, user, content_rating, labels, section_id ' \
|
"year, originally_available_at, added_at, live, started, user, content_rating, labels, section_id " \
|
||||||
'FROM session_history_metadata ' \
|
"FROM session_history_metadata " \
|
||||||
'JOIN session_history ON session_history_metadata.id = session_history.id ' \
|
"JOIN session_history ON session_history_metadata.id = session_history.id " \
|
||||||
'WHERE section_id = ? ' \
|
"WHERE section_id = ? " \
|
||||||
'GROUP BY session_history.rating_key ' \
|
"GROUP BY session_history.rating_key " \
|
||||||
'ORDER BY MAX(started) DESC LIMIT ?'
|
"ORDER BY MAX(started) DESC LIMIT ?"
|
||||||
result = monitor_db.select(query, args=[section_id, limit])
|
result = monitor_db.select(query, args=[section_id, limit])
|
||||||
else:
|
else:
|
||||||
result = []
|
result = []
|
||||||
|
@ -1085,8 +1085,8 @@ class Libraries(object):
|
||||||
monitor_db = database.MonitorDatabase()
|
monitor_db = database.MonitorDatabase()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
query = 'SELECT section_id, section_name, section_type, agent ' \
|
query = "SELECT section_id, section_name, section_type, agent " \
|
||||||
'FROM library_sections WHERE deleted_section = 0'
|
"FROM library_sections WHERE deleted_section = 0"
|
||||||
result = monitor_db.select(query=query)
|
result = monitor_db.select(query=query)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warn("Tautulli Libraries :: Unable to execute database query for get_sections: %s." % e)
|
logger.warn("Tautulli Libraries :: Unable to execute database query for get_sections: %s." % e)
|
||||||
|
@ -1110,8 +1110,8 @@ class Libraries(object):
|
||||||
row_ids = list(map(helpers.cast_to_int, row_ids.split(',')))
|
row_ids = list(map(helpers.cast_to_int, row_ids.split(',')))
|
||||||
|
|
||||||
# Get the section_ids corresponding to the row_ids
|
# Get the section_ids corresponding to the row_ids
|
||||||
result = monitor_db.select('SELECT server_id, section_id FROM library_sections '
|
result = monitor_db.select("SELECT server_id, section_id FROM library_sections "
|
||||||
'WHERE id IN ({})'.format(','.join(['?'] * len(row_ids))), row_ids)
|
"WHERE id IN ({})".format(",".join(["?"] * len(row_ids))), row_ids)
|
||||||
|
|
||||||
success = []
|
success = []
|
||||||
for library in result:
|
for library in result:
|
||||||
|
@ -1135,9 +1135,9 @@ class Libraries(object):
|
||||||
logger.info("Tautulli Libraries :: Deleting library with server_id %s and section_id %s from database."
|
logger.info("Tautulli Libraries :: Deleting library with server_id %s and section_id %s from database."
|
||||||
% (server_id, section_id))
|
% (server_id, section_id))
|
||||||
try:
|
try:
|
||||||
monitor_db.action('UPDATE library_sections '
|
monitor_db.action("UPDATE library_sections "
|
||||||
'SET deleted_section = 1, keep_history = 0, do_notify = 0, do_notify_created = 0 '
|
"SET deleted_section = 1, keep_history = 0, do_notify = 0, do_notify_created = 0 "
|
||||||
'WHERE server_id = ? AND section_id = ?', [server_id, section_id])
|
"WHERE server_id = ? AND section_id = ?", [server_id, section_id])
|
||||||
return delete_success
|
return delete_success
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warn("Tautulli Libraries :: Unable to execute database query for delete: %s." % e)
|
logger.warn("Tautulli Libraries :: Unable to execute database query for delete: %s." % e)
|
||||||
|
@ -1150,26 +1150,26 @@ class Libraries(object):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if section_id and section_id.isdigit():
|
if section_id and section_id.isdigit():
|
||||||
query = 'SELECT * FROM library_sections WHERE section_id = ?'
|
query = "SELECT * FROM library_sections WHERE section_id = ?"
|
||||||
result = monitor_db.select(query=query, args=[section_id])
|
result = monitor_db.select(query=query, args=[section_id])
|
||||||
if result:
|
if result:
|
||||||
logger.info("Tautulli Libraries :: Re-adding library with id %s to database." % section_id)
|
logger.info("Tautulli Libraries :: Re-adding library with id %s to database." % section_id)
|
||||||
monitor_db.action('UPDATE library_sections '
|
monitor_db.action("UPDATE library_sections "
|
||||||
'SET deleted_section = 0, keep_history = 1, do_notify = 1, do_notify_created = 1 '
|
"SET deleted_section = 0, keep_history = 1, do_notify = 1, do_notify_created = 1 "
|
||||||
'WHERE section_id = ?',
|
"WHERE section_id = ?",
|
||||||
[section_id])
|
[section_id])
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
elif section_name:
|
elif section_name:
|
||||||
query = 'SELECT * FROM library_sections WHERE section_name = ?'
|
query = "SELECT * FROM library_sections WHERE section_name = ?"
|
||||||
result = monitor_db.select(query=query, args=[section_name])
|
result = monitor_db.select(query=query, args=[section_name])
|
||||||
if result:
|
if result:
|
||||||
logger.info("Tautulli Libraries :: Re-adding library with name %s to database." % section_name)
|
logger.info("Tautulli Libraries :: Re-adding library with name %s to database." % section_name)
|
||||||
monitor_db.action('UPDATE library_sections '
|
monitor_db.action("UPDATE library_sections "
|
||||||
'SET deleted_section = 0, keep_history = 1, do_notify = 1, do_notify_created = 1 '
|
"SET deleted_section = 0, keep_history = 1, do_notify = 1, do_notify_created = 1 "
|
||||||
'WHERE section_name = ?',
|
"WHERE section_name = ?",
|
||||||
[section_name])
|
[section_name])
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
|
@ -1203,7 +1203,7 @@ class Libraries(object):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
logger.debug("Tautulli Libraries :: Deleting libraries where server_id does not match %s." % server_id)
|
logger.debug("Tautulli Libraries :: Deleting libraries where server_id does not match %s." % server_id)
|
||||||
monitor_db.action('DELETE FROM library_sections WHERE server_id != ?', [server_id])
|
monitor_db.action("DELETE FROM library_sections WHERE server_id != ?", [server_id])
|
||||||
|
|
||||||
return 'Deleted duplicate libraries from the database.'
|
return 'Deleted duplicate libraries from the database.'
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue