From f1edb4e487f81f2540c4840b1153b54907db0a13 Mon Sep 17 00:00:00 2001 From: JonnyWong16 <9099342+JonnyWong16@users.noreply.github.com> Date: Fri, 21 Apr 2023 16:40:50 -0700 Subject: [PATCH] Fix datafactory.py --- plexpy/datafactory.py | 1104 ++++++++++++++++++++--------------------- 1 file changed, 552 insertions(+), 552 deletions(-) diff --git a/plexpy/datafactory.py b/plexpy/datafactory.py index e2ee0c2b..3db32a51 100644 --- a/plexpy/datafactory.py +++ b/plexpy/datafactory.py @@ -93,61 +93,61 @@ class DataFactory(object): group_by = ['session_history.reference_id'] if grouping else ['session_history.id'] columns = [ - 'session_history.reference_id', - 'session_history.id AS row_id', - 'MAX(started) AS date', - 'MIN(started) AS started', - 'MAX(stopped) AS stopped', - 'SUM(CASE WHEN stopped > 0 THEN (stopped - started) ELSE 0 END) - \ - SUM(CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) AS play_duration', - 'SUM(CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) AS paused_counter', - 'session_history.view_offset', - 'session_history.user_id', - 'session_history.user', - '(CASE WHEN users.friendly_name IS NULL OR TRIM(users.friendly_name) = "" \ - THEN users.username ELSE users.friendly_name END) AS friendly_name', - 'users.thumb AS user_thumb', - 'users.custom_avatar_url AS custom_thumb', - 'platform', - 'product', - 'player', - 'ip_address', - 'machine_id', - 'location', - 'secure', - 'relayed', - 'session_history.media_type', - '(CASE WHEN session_history_metadata.live = 1 THEN \'live\' ELSE session_history.media_type END) \ - AS media_type_live', - 'session_history_metadata.rating_key', - 'session_history_metadata.parent_rating_key', - 'session_history_metadata.grandparent_rating_key', - 'session_history_metadata.full_title', - 'session_history_metadata.title', - 'session_history_metadata.parent_title', - 'session_history_metadata.grandparent_title', - 'session_history_metadata.original_title', - 'session_history_metadata.year', - 'session_history_metadata.media_index', - 'session_history_metadata.parent_media_index', - 'session_history_metadata.thumb', - 'session_history_metadata.parent_thumb', - 'session_history_metadata.grandparent_thumb', - 'session_history_metadata.live', - 'session_history_metadata.added_at', - 'session_history_metadata.originally_available_at', - 'session_history_metadata.guid', - 'MAX((CASE WHEN (view_offset IS NULL OR view_offset = "") THEN 0.1 ELSE view_offset * 1.0 END) / \ - (CASE WHEN (session_history_metadata.duration IS NULL OR session_history_metadata.duration = "") \ - THEN 1.0 ELSE session_history_metadata.duration * 1.0 END) * 100) AS percent_complete', - 'session_history_metadata.duration', - 'session_history_metadata.marker_credits_first', - 'session_history_metadata.marker_credits_final', - 'session_history_media_info.transcode_decision', - 'COUNT(*) AS group_count', - 'GROUP_CONCAT(session_history.id) AS group_ids', - 'NULL AS state', - 'NULL AS session_key' + "session_history.reference_id", + "session_history.id AS row_id", + "MAX(started) AS date", + "MIN(started) AS started", + "MAX(stopped) AS stopped", + "SUM(CASE WHEN stopped > 0 THEN (stopped - started) ELSE 0 END) - \ + SUM(CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) AS play_duration", + "SUM(CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) AS paused_counter", + "session_history.view_offset", + "session_history.user_id", + "session_history.user", + "(CASE WHEN users.friendly_name IS NULL OR TRIM(users.friendly_name) = '' \ + THEN users.username ELSE users.friendly_name END) AS friendly_name", + "users.thumb AS user_thumb", + "users.custom_avatar_url AS custom_thumb", + "platform", + "product", + "player", + "ip_address", + "machine_id", + "location", + "secure", + "relayed", + "session_history.media_type", + "(CASE WHEN session_history_metadata.live = 1 THEN 'live' ELSE session_history.media_type END) \ + AS media_type_live", + "session_history_metadata.rating_key", + "session_history_metadata.parent_rating_key", + "session_history_metadata.grandparent_rating_key", + "session_history_metadata.full_title", + "session_history_metadata.title", + "session_history_metadata.parent_title", + "session_history_metadata.grandparent_title", + "session_history_metadata.original_title", + "session_history_metadata.year", + "session_history_metadata.media_index", + "session_history_metadata.parent_media_index", + "session_history_metadata.thumb", + "session_history_metadata.parent_thumb", + "session_history_metadata.grandparent_thumb", + "session_history_metadata.live", + "session_history_metadata.added_at", + "session_history_metadata.originally_available_at", + "session_history_metadata.guid", + "MAX((CASE WHEN (view_offset IS NULL OR view_offset = '') THEN 0.1 ELSE view_offset * 1.0 END) / \ + (CASE WHEN (session_history_metadata.duration IS NULL OR session_history_metadata.duration = '') \ + THEN 1.0 ELSE session_history_metadata.duration * 1.0 END) * 100) AS percent_complete", + "session_history_metadata.duration", + "session_history_metadata.marker_credits_first", + "session_history_metadata.marker_credits_final", + "session_history_media_info.transcode_decision", + "COUNT(*) AS group_count", + "GROUP_CONCAT(session_history.id) AS group_ids", + "NULL AS state", + "NULL AS session_key" ] if include_activity: @@ -157,60 +157,60 @@ class DataFactory(object): group_by_union = ['session_key'] columns_union = [ - 'NULL AS reference_id', - 'NULL AS row_id', - 'started AS date', - 'started', - 'stopped', - 'SUM(CASE WHEN stopped > 0 THEN (stopped - started) ELSE (strftime("%s", "now") - started) END) - \ - SUM(CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) AS play_duration', - 'SUM(CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) AS paused_counter', - 'view_offset', - 'user_id', - 'user', - '(CASE WHEN friendly_name IS NULL OR TRIM(friendly_name) = "" \ - THEN user ELSE friendly_name END) AS friendly_name', - 'NULL AS user_thumb', - 'NULL AS custom_thumb', - 'platform', - 'product', - 'player', - 'ip_address', - 'machine_id', - 'location', - 'secure', - 'relayed', - 'media_type', - '(CASE WHEN live = 1 THEN \'live\' ELSE media_type END) AS media_type_live', - 'rating_key', - 'parent_rating_key', - 'grandparent_rating_key', - 'full_title', - 'title', - 'parent_title', - 'grandparent_title', - 'original_title', - 'year', - 'media_index', - 'parent_media_index', - 'thumb', - 'parent_thumb', - 'grandparent_thumb', - 'live', - 'added_at', - 'originally_available_at', - 'guid', - 'MAX((CASE WHEN (view_offset IS NULL OR view_offset = "") THEN 0.1 ELSE view_offset * 1.0 END) / \ - (CASE WHEN (duration IS NULL OR duration = "") \ - THEN 1.0 ELSE duration * 1.0 END) * 100) AS percent_complete', - 'duration', - 'NULL AS marker_credits_first', - 'NULL AS marker_credits_final', - 'transcode_decision', - 'NULL AS group_count', - 'NULL AS group_ids', - 'state', - 'session_key' + "NULL AS reference_id", + "NULL AS row_id", + "started AS date", + "started", + "stopped", + "SUM(CASE WHEN stopped > 0 THEN (stopped - started) ELSE (strftime('%s', 'now') - started) END) - \ + SUM(CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) AS play_duration", + "SUM(CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) AS paused_counter", + "view_offset", + "user_id", + "user", + "(CASE WHEN friendly_name IS NULL OR TRIM(friendly_name) = '' \ + THEN user ELSE friendly_name END) AS friendly_name", + "NULL AS user_thumb", + "NULL AS custom_thumb", + "platform", + "product", + "player", + "ip_address", + "machine_id", + "location", + "secure", + "relayed", + "media_type", + "(CASE WHEN live = 1 THEN 'live' ELSE media_type END) AS media_type_live", + "rating_key", + "parent_rating_key", + "grandparent_rating_key", + "full_title", + "title", + "parent_title", + "grandparent_title", + "original_title", + "year", + "media_index", + "parent_media_index", + "thumb", + "parent_thumb", + "grandparent_thumb", + "live", + "added_at", + "originally_available_at", + "guid", + "MAX((CASE WHEN (view_offset IS NULL OR view_offset = '') THEN 0.1 ELSE view_offset * 1.0 END) / \ + (CASE WHEN (duration IS NULL OR duration = '') \ + THEN 1.0 ELSE duration * 1.0 END) * 100) AS percent_complete", + "duration", + "NULL AS marker_credits_first", + "NULL AS marker_credits_final", + "transcode_decision", + "NULL AS group_count", + "NULL AS group_ids", + "state", + "session_key" ] else: @@ -392,20 +392,20 @@ class DataFactory(object): if stat == 'top_movies': top_movies = [] try: - query = 'SELECT sh.id, shm.full_title, shm.year, sh.rating_key, shm.thumb, sh.section_id, ' \ - 'shm.art, sh.media_type, shm.content_rating, shm.labels, sh.started, shm.live, shm.guid, ' \ - 'MAX(sh.started) AS last_watch, COUNT(sh.id) AS total_plays, SUM(sh.d) AS total_duration ' \ - 'FROM (SELECT *, SUM(CASE WHEN stopped > 0 THEN (stopped - started) - ' \ - ' (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) ' \ - ' AS d ' \ - ' FROM session_history ' \ - ' WHERE session_history.stopped >= %s ' \ - ' AND session_history.media_type = "movie" %s ' \ - ' GROUP BY %s) AS sh ' \ - 'JOIN session_history_metadata AS shm ON shm.id = sh.id ' \ - 'GROUP BY shm.full_title, shm.year ' \ - 'ORDER BY %s DESC, sh.started DESC ' \ - 'LIMIT %s OFFSET %s ' % (timestamp, where_id, group_by, sort_type, stats_count, stats_start) + query = "SELECT sh.id, shm.full_title, shm.year, sh.rating_key, shm.thumb, sh.section_id, " \ + "shm.art, sh.media_type, shm.content_rating, shm.labels, sh.started, shm.live, shm.guid, " \ + "MAX(sh.started) AS last_watch, COUNT(sh.id) AS total_plays, SUM(sh.d) AS total_duration " \ + "FROM (SELECT *, SUM(CASE WHEN stopped > 0 THEN (stopped - started) - " \ + " (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) " \ + " AS d " \ + " FROM session_history " \ + " WHERE session_history.stopped >= %s " \ + " AND session_history.media_type = 'movie' %s " \ + " GROUP BY %s) AS sh " \ + "JOIN session_history_metadata AS shm ON shm.id = sh.id " \ + "GROUP BY shm.full_title, shm.year " \ + "ORDER BY %s DESC, sh.started DESC " \ + "LIMIT %s OFFSET %s " % (timestamp, where_id, group_by, sort_type, stats_count, stats_start) result = monitor_db.select(query) except Exception as e: logger.warn("Tautulli DataFactory :: Unable to execute database query for get_home_stats: top_movies: %s." % e) @@ -444,21 +444,21 @@ class DataFactory(object): elif stat == 'popular_movies': popular_movies = [] try: - query = 'SELECT sh.id, shm.full_title, shm.year, sh.rating_key, shm.thumb, sh.section_id, ' \ - 'shm.art, sh.media_type, shm.content_rating, shm.labels, sh.started, shm.live, shm.guid, ' \ - 'COUNT(DISTINCT sh.user_id) AS users_watched, ' \ - 'MAX(sh.started) AS last_watch, COUNT(sh.id) as total_plays, SUM(sh.d) AS total_duration ' \ - 'FROM (SELECT *, SUM(CASE WHEN stopped > 0 THEN (stopped - started) - ' \ - ' (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) ' \ - ' AS d ' \ - ' FROM session_history ' \ - ' WHERE session_history.stopped >= %s ' \ - ' AND session_history.media_type = "movie" %s ' \ - ' GROUP BY %s) AS sh ' \ - 'JOIN session_history_metadata AS shm ON shm.id = sh.id ' \ - 'GROUP BY shm.full_title, shm.year ' \ - 'ORDER BY users_watched DESC, %s DESC, sh.started DESC ' \ - 'LIMIT %s OFFSET %s ' % (timestamp, where_id, group_by, sort_type, stats_count, stats_start) + query = "SELECT sh.id, shm.full_title, shm.year, sh.rating_key, shm.thumb, sh.section_id, " \ + "shm.art, sh.media_type, shm.content_rating, shm.labels, sh.started, shm.live, shm.guid, " \ + "COUNT(DISTINCT sh.user_id) AS users_watched, " \ + "MAX(sh.started) AS last_watch, COUNT(sh.id) as total_plays, SUM(sh.d) AS total_duration " \ + "FROM (SELECT *, SUM(CASE WHEN stopped > 0 THEN (stopped - started) - " \ + " (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) " \ + " AS d " \ + " FROM session_history " \ + " WHERE session_history.stopped >= %s " \ + " AND session_history.media_type = 'movie' %s " \ + " GROUP BY %s) AS sh " \ + "JOIN session_history_metadata AS shm ON shm.id = sh.id " \ + "GROUP BY shm.full_title, shm.year " \ + "ORDER BY users_watched DESC, %s DESC, sh.started DESC " \ + "LIMIT %s OFFSET %s " % (timestamp, where_id, group_by, sort_type, stats_count, stats_start) result = monitor_db.select(query) except Exception as e: logger.warn("Tautulli DataFactory :: Unable to execute database query for get_home_stats: popular_movies: %s." % e) @@ -495,22 +495,22 @@ class DataFactory(object): elif stat == 'top_tv': top_tv = [] try: - query = 'SELECT sh.id, shm.grandparent_title, sh.grandparent_rating_key, ' \ - 'shm.grandparent_thumb, sh.section_id, ' \ - 'shm.year, sh.rating_key, shm.art, sh.media_type, ' \ - 'shm.content_rating, shm.labels, sh.started, shm.live, shm.guid, ' \ - 'MAX(sh.started) AS last_watch, COUNT(sh.id) AS total_plays, SUM(sh.d) AS total_duration ' \ - 'FROM (SELECT *, SUM(CASE WHEN stopped > 0 THEN (stopped - started) - ' \ - ' (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) ' \ - ' AS d ' \ - ' FROM session_history ' \ - ' WHERE session_history.stopped >= %s ' \ - ' AND session_history.media_type = "episode" %s ' \ - ' GROUP BY %s) AS sh ' \ - 'JOIN session_history_metadata AS shm ON shm.id = sh.id ' \ - 'GROUP BY shm.grandparent_title ' \ - 'ORDER BY %s DESC, sh.started DESC ' \ - 'LIMIT %s OFFSET %s ' % (timestamp, where_id, group_by, sort_type, stats_count, stats_start) + query = "SELECT sh.id, shm.grandparent_title, sh.grandparent_rating_key, " \ + "shm.grandparent_thumb, sh.section_id, " \ + "shm.year, sh.rating_key, shm.art, sh.media_type, " \ + "shm.content_rating, shm.labels, sh.started, shm.live, shm.guid, " \ + "MAX(sh.started) AS last_watch, COUNT(sh.id) AS total_plays, SUM(sh.d) AS total_duration " \ + "FROM (SELECT *, SUM(CASE WHEN stopped > 0 THEN (stopped - started) - " \ + " (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) " \ + " AS d " \ + " FROM session_history " \ + " WHERE session_history.stopped >= %s " \ + " AND session_history.media_type = 'episode' %s " \ + " GROUP BY %s) AS sh " \ + "JOIN session_history_metadata AS shm ON shm.id = sh.id " \ + "GROUP BY shm.grandparent_title " \ + "ORDER BY %s DESC, sh.started DESC " \ + "LIMIT %s OFFSET %s " % (timestamp, where_id, group_by, sort_type, stats_count, stats_start) result = monitor_db.select(query) except Exception as e: logger.warn("Tautulli DataFactory :: Unable to execute database query for get_home_stats: top_tv: %s." % e) @@ -549,23 +549,23 @@ class DataFactory(object): elif stat == 'popular_tv': popular_tv = [] try: - query = 'SELECT sh.id, shm.grandparent_title, sh.grandparent_rating_key, ' \ - 'shm.grandparent_thumb, sh.section_id, ' \ - 'shm.year, sh.rating_key, shm.art, sh.media_type, ' \ - 'shm.content_rating, shm.labels, sh.started, shm.live, shm.guid, ' \ - 'COUNT(DISTINCT sh.user_id) AS users_watched, ' \ - 'MAX(sh.started) AS last_watch, COUNT(sh.id) as total_plays, SUM(sh.d) AS total_duration ' \ - 'FROM (SELECT *, SUM(CASE WHEN stopped > 0 THEN (stopped - started) - ' \ - ' (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) ' \ - ' AS d ' \ - ' FROM session_history ' \ - ' WHERE session_history.stopped >= %s ' \ - ' AND session_history.media_type = "episode" %s ' \ - ' GROUP BY %s) AS sh ' \ - 'JOIN session_history_metadata AS shm ON shm.id = sh.id ' \ - 'GROUP BY shm.grandparent_title ' \ - 'ORDER BY users_watched DESC, %s DESC, sh.started DESC ' \ - 'LIMIT %s OFFSET %s ' % (timestamp, where_id, group_by, sort_type, stats_count, stats_start) + query = "SELECT sh.id, shm.grandparent_title, sh.grandparent_rating_key, " \ + "shm.grandparent_thumb, sh.section_id, " \ + "shm.year, sh.rating_key, shm.art, sh.media_type, " \ + "shm.content_rating, shm.labels, sh.started, shm.live, shm.guid, " \ + "COUNT(DISTINCT sh.user_id) AS users_watched, " \ + "MAX(sh.started) AS last_watch, COUNT(sh.id) as total_plays, SUM(sh.d) AS total_duration " \ + "FROM (SELECT *, SUM(CASE WHEN stopped > 0 THEN (stopped - started) - " \ + " (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) " \ + " AS d " \ + " FROM session_history " \ + " WHERE session_history.stopped >= %s " \ + " AND session_history.media_type = 'episode' %s " \ + " GROUP BY %s) AS sh " \ + "JOIN session_history_metadata AS shm ON shm.id = sh.id " \ + "GROUP BY shm.grandparent_title " \ + "ORDER BY users_watched DESC, %s DESC, sh.started DESC " \ + "LIMIT %s OFFSET %s " % (timestamp, where_id, group_by, sort_type, stats_count, stats_start) result = monitor_db.select(query) except Exception as e: logger.warn("Tautulli DataFactory :: Unable to execute database query for get_home_stats: popular_tv: %s." % e) @@ -602,21 +602,21 @@ class DataFactory(object): elif stat == 'top_music': top_music = [] try: - query = 'SELECT sh.id, shm.grandparent_title, shm.original_title, shm.year, ' \ - 'sh.grandparent_rating_key, shm.grandparent_thumb, sh.section_id, ' \ - 'shm.art, sh.media_type, shm.content_rating, shm.labels, sh.started, shm.live, shm.guid, ' \ - 'MAX(sh.started) AS last_watch, COUNT(sh.id) AS total_plays, SUM(sh.d) AS total_duration ' \ - 'FROM (SELECT *, SUM(CASE WHEN stopped > 0 THEN (stopped - started) - ' \ - ' (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) ' \ - ' AS d ' \ - ' FROM session_history ' \ - ' WHERE session_history.stopped >= %s ' \ - ' AND session_history.media_type = "track" %s ' \ - ' GROUP BY %s) AS sh ' \ - 'JOIN session_history_metadata AS shm ON shm.id = sh.id ' \ - 'GROUP BY shm.original_title, shm.grandparent_title ' \ - 'ORDER BY %s DESC, sh.started DESC ' \ - 'LIMIT %s OFFSET %s ' % (timestamp, where_id, group_by, sort_type, stats_count, stats_start) + query = "SELECT sh.id, shm.grandparent_title, shm.original_title, shm.year, " \ + "sh.grandparent_rating_key, shm.grandparent_thumb, sh.section_id, " \ + "shm.art, sh.media_type, shm.content_rating, shm.labels, sh.started, shm.live, shm.guid, " \ + "MAX(sh.started) AS last_watch, COUNT(sh.id) AS total_plays, SUM(sh.d) AS total_duration " \ + "FROM (SELECT *, SUM(CASE WHEN stopped > 0 THEN (stopped - started) - " \ + " (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) " \ + " AS d " \ + " FROM session_history " \ + " WHERE session_history.stopped >= %s " \ + " AND session_history.media_type = 'track' %s " \ + " GROUP BY %s) AS sh " \ + "JOIN session_history_metadata AS shm ON shm.id = sh.id " \ + "GROUP BY shm.original_title, shm.grandparent_title " \ + "ORDER BY %s DESC, sh.started DESC " \ + "LIMIT %s OFFSET %s " % (timestamp, where_id, group_by, sort_type, stats_count, stats_start) result = monitor_db.select(query) except Exception as e: logger.warn("Tautulli DataFactory :: Unable to execute database query for get_home_stats: top_music: %s." % e) @@ -655,22 +655,22 @@ class DataFactory(object): elif stat == 'popular_music': popular_music = [] try: - query = 'SELECT sh.id, shm.grandparent_title, shm.original_title, shm.year, ' \ - 'sh.grandparent_rating_key, shm.grandparent_thumb, sh.section_id, ' \ - 'shm.art, sh.media_type, shm.content_rating, shm.labels, sh.started, shm.live, shm.guid, ' \ - 'COUNT(DISTINCT sh.user_id) AS users_watched, ' \ - 'MAX(sh.started) AS last_watch, COUNT(sh.id) as total_plays, SUM(sh.d) AS total_duration ' \ - 'FROM (SELECT *, SUM(CASE WHEN stopped > 0 THEN (stopped - started) - ' \ - ' (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) ' \ - ' AS d ' \ - ' FROM session_history ' \ - ' WHERE session_history.stopped >= %s ' \ - ' AND session_history.media_type = "track" %s ' \ - ' GROUP BY %s) AS sh ' \ - 'JOIN session_history_metadata AS shm ON shm.id = sh.id ' \ - 'GROUP BY shm.original_title, shm.grandparent_title ' \ - 'ORDER BY users_watched DESC, %s DESC, sh.started DESC ' \ - 'LIMIT %s OFFSET %s ' % (timestamp, where_id, group_by, sort_type, stats_count, stats_start) + query = "SELECT sh.id, shm.grandparent_title, shm.original_title, shm.year, " \ + "sh.grandparent_rating_key, shm.grandparent_thumb, sh.section_id, " \ + "shm.art, sh.media_type, shm.content_rating, shm.labels, sh.started, shm.live, shm.guid, " \ + "COUNT(DISTINCT sh.user_id) AS users_watched, " \ + "MAX(sh.started) AS last_watch, COUNT(sh.id) as total_plays, SUM(sh.d) AS total_duration " \ + "FROM (SELECT *, SUM(CASE WHEN stopped > 0 THEN (stopped - started) - " \ + " (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) " \ + " AS d " \ + " FROM session_history " \ + " WHERE session_history.stopped >= %s " \ + " AND session_history.media_type = 'track' %s " \ + " GROUP BY %s) AS sh " \ + "JOIN session_history_metadata AS shm ON shm.id = sh.id " \ + "GROUP BY shm.original_title, shm.grandparent_title " \ + "ORDER BY users_watched DESC, %s DESC, sh.started DESC " \ + "LIMIT %s OFFSET %s " % (timestamp, where_id, group_by, sort_type, stats_count, stats_start) result = monitor_db.select(query) except Exception as e: logger.warn("Tautulli DataFactory :: Unable to execute database query for get_home_stats: popular_music: %s." % e) @@ -707,28 +707,28 @@ class DataFactory(object): elif stat == 'top_libraries': top_libraries = [] try: - query = 'SELECT sh.id, shm.title, shm.grandparent_title, shm.full_title, shm.year, ' \ - 'shm.media_index, shm.parent_media_index, ' \ - 'sh.rating_key, shm.grandparent_rating_key, shm.thumb, shm.grandparent_thumb, ' \ - 'sh.user, sh.user_id, sh.player, sh.section_id, ' \ - 'shm.art, sh.media_type, shm.content_rating, shm.labels, shm.live, shm.guid, ' \ - 'ls.section_name, ls.section_type, ' \ - 'ls.thumb AS library_thumb, ls.custom_thumb_url AS custom_thumb, ' \ - 'ls.art AS library_art, ls.custom_art_url AS custom_art, ' \ - 'sh.started, ' \ - 'MAX(sh.started) AS last_watch, COUNT(sh.id) AS total_plays, SUM(sh.d) AS total_duration ' \ - 'FROM (SELECT *, SUM(CASE WHEN stopped > 0 THEN (stopped - started) - ' \ - ' (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) ' \ - ' AS d ' \ - ' FROM session_history ' \ - ' WHERE session_history.stopped >= %s %s ' \ - ' GROUP BY %s) AS sh ' \ - 'JOIN session_history_metadata AS shm ON shm.id = sh.id ' \ - 'LEFT OUTER JOIN (SELECT * FROM library_sections WHERE deleted_section = 0) ' \ - ' AS ls ON sh.section_id = ls.section_id ' \ - 'GROUP BY sh.section_id ' \ - 'ORDER BY %s DESC, sh.started DESC ' \ - 'LIMIT %s OFFSET %s ' % (timestamp, where_id, group_by, sort_type, stats_count, stats_start) + query = "SELECT sh.id, shm.title, shm.grandparent_title, shm.full_title, shm.year, " \ + "shm.media_index, shm.parent_media_index, " \ + "sh.rating_key, shm.grandparent_rating_key, shm.thumb, shm.grandparent_thumb, " \ + "sh.user, sh.user_id, sh.player, sh.section_id, " \ + "shm.art, sh.media_type, shm.content_rating, shm.labels, shm.live, shm.guid, " \ + "ls.section_name, ls.section_type, " \ + "ls.thumb AS library_thumb, ls.custom_thumb_url AS custom_thumb, " \ + "ls.art AS library_art, ls.custom_art_url AS custom_art, " \ + "sh.started, " \ + "MAX(sh.started) AS last_watch, COUNT(sh.id) AS total_plays, SUM(sh.d) AS total_duration " \ + "FROM (SELECT *, SUM(CASE WHEN stopped > 0 THEN (stopped - started) - " \ + " (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) " \ + " AS d " \ + " FROM session_history " \ + " WHERE session_history.stopped >= %s %s " \ + " GROUP BY %s) AS sh " \ + "JOIN session_history_metadata AS shm ON shm.id = sh.id " \ + "LEFT OUTER JOIN (SELECT * FROM library_sections WHERE deleted_section = 0) " \ + " AS ls ON sh.section_id = ls.section_id " \ + "GROUP BY sh.section_id " \ + "ORDER BY %s DESC, sh.started DESC " \ + "LIMIT %s OFFSET %s " % (timestamp, where_id, group_by, sort_type, stats_count, stats_start) result = monitor_db.select(query) except Exception as e: logger.warn("Tautulli DataFactory :: Unable to execute database query for get_home_stats: top_libraries: %s." % e) @@ -795,28 +795,28 @@ class DataFactory(object): elif stat == 'top_users': top_users = [] try: - query = 'SELECT sh.id, shm.title, shm.grandparent_title, shm.full_title, shm.year, ' \ - 'shm.media_index, shm.parent_media_index, ' \ - 'sh.rating_key, shm.grandparent_rating_key, shm.thumb, shm.grandparent_thumb, ' \ - 'sh.user, sh.user_id, sh.player, sh.section_id, ' \ - 'shm.art, sh.media_type, shm.content_rating, shm.labels, shm.live, shm.guid, ' \ - 'u.thumb AS user_thumb, u.custom_avatar_url AS custom_thumb, ' \ - 'sh.started, ' \ - '(CASE WHEN u.friendly_name IS NULL OR TRIM(u.friendly_name) = ""' \ - ' THEN u.username ELSE u.friendly_name END) ' \ - ' AS friendly_name, ' \ - 'MAX(sh.started) AS last_watch, COUNT(sh.id) AS total_plays, SUM(sh.d) AS total_duration ' \ - 'FROM (SELECT *, SUM(CASE WHEN stopped > 0 THEN (stopped - started) - ' \ - ' (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) ' \ - ' AS d ' \ - ' FROM session_history ' \ - ' WHERE session_history.stopped >= %s %s ' \ - ' GROUP BY %s) AS sh ' \ - 'JOIN session_history_metadata AS shm ON shm.id = sh.id ' \ - 'LEFT OUTER JOIN users AS u ON sh.user_id = u.user_id ' \ - 'GROUP BY sh.user_id ' \ - 'ORDER BY %s DESC, sh.started DESC ' \ - 'LIMIT %s OFFSET %s ' % (timestamp, where_id, group_by, sort_type, stats_count, stats_start) + query = "SELECT sh.id, shm.title, shm.grandparent_title, shm.full_title, shm.year, " \ + "shm.media_index, shm.parent_media_index, " \ + "sh.rating_key, shm.grandparent_rating_key, shm.thumb, shm.grandparent_thumb, " \ + "sh.user, sh.user_id, sh.player, sh.section_id, " \ + "shm.art, sh.media_type, shm.content_rating, shm.labels, shm.live, shm.guid, " \ + "u.thumb AS user_thumb, u.custom_avatar_url AS custom_thumb, " \ + "sh.started, " \ + "(CASE WHEN u.friendly_name IS NULL OR TRIM(u.friendly_name) = ''" \ + " THEN u.username ELSE u.friendly_name END) " \ + " AS friendly_name, " \ + "MAX(sh.started) AS last_watch, COUNT(sh.id) AS total_plays, SUM(sh.d) AS total_duration " \ + "FROM (SELECT *, SUM(CASE WHEN stopped > 0 THEN (stopped - started) - " \ + " (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) " \ + " AS d " \ + " FROM session_history " \ + " WHERE session_history.stopped >= %s %s " \ + " GROUP BY %s) AS sh " \ + "JOIN session_history_metadata AS shm ON shm.id = sh.id " \ + "LEFT OUTER JOIN users AS u ON sh.user_id = u.user_id " \ + "GROUP BY sh.user_id " \ + "ORDER BY %s DESC, sh.started DESC " \ + "LIMIT %s OFFSET %s " % (timestamp, where_id, group_by, sort_type, stats_count, stats_start) result = monitor_db.select(query) except Exception as e: logger.warn("Tautulli DataFactory :: Unable to execute database query for get_home_stats: top_users: %s." % e) @@ -873,17 +873,17 @@ class DataFactory(object): top_platform = [] try: - query = 'SELECT sh.platform, sh.started, ' \ - 'MAX(sh.started) AS last_watch, COUNT(sh.id) AS total_plays, SUM(sh.d) AS total_duration ' \ - 'FROM (SELECT *, SUM(CASE WHEN stopped > 0 THEN (stopped - started) - ' \ - ' (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) ' \ - ' AS d ' \ - ' FROM session_history ' \ - ' WHERE session_history.stopped >= %s %s ' \ - ' GROUP BY %s) AS sh ' \ - 'GROUP BY sh.platform ' \ - 'ORDER BY %s DESC, sh.started DESC ' \ - 'LIMIT %s OFFSET %s ' % (timestamp, where_id, group_by, sort_type, stats_count, stats_start) + query = "SELECT sh.platform, sh.started, " \ + "MAX(sh.started) AS last_watch, COUNT(sh.id) AS total_plays, SUM(sh.d) AS total_duration " \ + "FROM (SELECT *, SUM(CASE WHEN stopped > 0 THEN (stopped - started) - " \ + " (CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) ELSE 0 END) " \ + " AS d " \ + " FROM session_history " \ + " WHERE session_history.stopped >= %s %s " \ + " GROUP BY %s) AS sh " \ + "GROUP BY sh.platform " \ + "ORDER BY %s DESC, sh.started DESC " \ + "LIMIT %s OFFSET %s " % (timestamp, where_id, group_by, sort_type, stats_count, stats_start) result = monitor_db.select(query) except Exception as e: logger.warn("Tautulli DataFactory :: Unable to execute database query for get_home_stats: top_platforms: %s." % e) @@ -924,65 +924,65 @@ class DataFactory(object): if plexpy.CONFIG.WATCHED_MARKER == 1: watched_threshold = ( - '(CASE WHEN shm.marker_credits_final IS NULL ' - 'THEN sh._duration * (CASE WHEN sh.media_type = "movie" THEN %d ELSE %d END) / 100.0 ' - 'ELSE shm.marker_credits_final END) ' - 'AS watched_threshold' + "(CASE WHEN shm.marker_credits_final IS NULL " + "THEN sh._duration * (CASE WHEN sh.media_type = 'movie' THEN %d ELSE %d END) / 100.0 " + "ELSE shm.marker_credits_final END) " + "AS watched_threshold" ) % (movie_watched_percent, tv_watched_percent) - watched_where = '_view_offset >= watched_threshold' + watched_where = "_view_offset >= watched_threshold" elif plexpy.CONFIG.WATCHED_MARKER == 2: watched_threshold = ( - '(CASE WHEN shm.marker_credits_first IS NULL ' - 'THEN sh._duration * (CASE WHEN sh.media_type = "movie" THEN %d ELSE %d END) / 100.0 ' - 'ELSE shm.marker_credits_first END) ' - 'AS watched_threshold' + "(CASE WHEN shm.marker_credits_first IS NULL " + "THEN sh._duration * (CASE WHEN sh.media_type = 'movie' THEN %d ELSE %d END) / 100.0 " + "ELSE shm.marker_credits_first END) " + "AS watched_threshold" ) % (movie_watched_percent, tv_watched_percent) - watched_where = '_view_offset >= watched_threshold' + watched_where = "_view_offset >= watched_threshold" elif plexpy.CONFIG.WATCHED_MARKER == 3: watched_threshold = ( - 'MIN(' - '(CASE WHEN shm.marker_credits_first IS NULL ' - 'THEN sh._duration * (CASE WHEN sh.media_type = "movie" THEN %d ELSE %d END) / 100.0 ' - 'ELSE shm.marker_credits_first END), ' - 'sh._duration * (CASE WHEN sh.media_type = "movie" THEN %d ELSE %d END) / 100.0) ' - 'AS watched_threshold' + "MIN(" + "(CASE WHEN shm.marker_credits_first IS NULL " + "THEN sh._duration * (CASE WHEN sh.media_type = 'movie' THEN %d ELSE %d END) / 100.0 " + "ELSE shm.marker_credits_first END), " + "sh._duration * (CASE WHEN sh.media_type = 'movie' THEN %d ELSE %d END) / 100.0) " + "AS watched_threshold" ) % (movie_watched_percent, tv_watched_percent, movie_watched_percent, tv_watched_percent) - watched_where = '_view_offset >= watched_threshold' + watched_where = "_view_offset >= watched_threshold" else: - watched_threshold = 'NULL AS watched_threshold' + watched_threshold = "NULL AS watched_threshold" watched_where = ( - 'sh.media_type == "movie" AND percent_complete >= %d ' - 'OR sh.media_type == "episode" AND percent_complete >= %d' + "sh.media_type == 'movie' AND percent_complete >= %d " + "OR sh.media_type == 'episode' AND percent_complete >= %d" ) % (movie_watched_percent, tv_watched_percent) last_watched = [] try: - query = 'SELECT sh.id, shm.title, shm.grandparent_title, shm.full_title, shm.year, ' \ - 'shm.media_index, shm.parent_media_index, ' \ - 'sh.rating_key, shm.grandparent_rating_key, shm.thumb, shm.grandparent_thumb, ' \ - 'sh.user, sh.user_id, u.custom_avatar_url as user_thumb, sh.player, sh.section_id, ' \ - 'shm.art, sh.media_type, shm.content_rating, shm.labels, shm.live, shm.guid, ' \ - '(CASE WHEN u.friendly_name IS NULL OR TRIM(u.friendly_name) = ""' \ - ' THEN u.username ELSE u.friendly_name END) ' \ - ' AS friendly_name, ' \ - 'MAX(sh.started) AS last_watch, sh._view_offset, sh._duration, ' \ - '(sh._view_offset / sh._duration * 100) AS percent_complete, ' \ - '%s ' \ - 'FROM (SELECT *, MAX(session_history.id), ' \ - ' (CASE WHEN view_offset IS NULL THEN 0.1 ELSE view_offset * 1.0 END) AS _view_offset, ' \ - ' (CASE WHEN duration IS NULL THEN 1.0 ELSE duration * 1.0 END) AS _duration ' \ - ' FROM session_history ' \ - ' JOIN session_history_metadata ON session_history_metadata.id = session_history.id ' \ - ' WHERE session_history.stopped >= %s ' \ - ' AND (session_history.media_type = "movie" ' \ - ' OR session_history.media_type = "episode") %s ' \ - ' GROUP BY %s) AS sh ' \ - 'JOIN session_history_metadata AS shm ON shm.id = sh.id ' \ - 'LEFT OUTER JOIN users AS u ON sh.user_id = u.user_id ' \ - 'WHERE %s ' \ - 'GROUP BY sh.id ' \ - 'ORDER BY last_watch DESC ' \ - 'LIMIT %s OFFSET %s' % (watched_threshold, + query = "SELECT sh.id, shm.title, shm.grandparent_title, shm.full_title, shm.year, " \ + "shm.media_index, shm.parent_media_index, " \ + "sh.rating_key, shm.grandparent_rating_key, shm.thumb, shm.grandparent_thumb, " \ + "sh.user, sh.user_id, u.custom_avatar_url as user_thumb, sh.player, sh.section_id, " \ + "shm.art, sh.media_type, shm.content_rating, shm.labels, shm.live, shm.guid, " \ + "(CASE WHEN u.friendly_name IS NULL OR TRIM(u.friendly_name) = ''" \ + " THEN u.username ELSE u.friendly_name END) " \ + " AS friendly_name, " \ + "MAX(sh.started) AS last_watch, sh._view_offset, sh._duration, " \ + "(sh._view_offset / sh._duration * 100) AS percent_complete, " \ + "%s " \ + "FROM (SELECT *, MAX(session_history.id), " \ + " (CASE WHEN view_offset IS NULL THEN 0.1 ELSE view_offset * 1.0 END) AS _view_offset, " \ + " (CASE WHEN duration IS NULL THEN 1.0 ELSE duration * 1.0 END) AS _duration " \ + " FROM session_history " \ + " JOIN session_history_metadata ON session_history_metadata.id = session_history.id " \ + " WHERE session_history.stopped >= %s " \ + " AND (session_history.media_type = 'movie' " \ + " OR session_history.media_type = 'episode') %s " \ + " GROUP BY %s) AS sh " \ + "JOIN session_history_metadata AS shm ON shm.id = sh.id " \ + "LEFT OUTER JOIN users AS u ON sh.user_id = u.user_id " \ + "WHERE %s " \ + "GROUP BY sh.id " \ + "ORDER BY last_watch DESC " \ + "LIMIT %s OFFSET %s" % (watched_threshold, timestamp, where_id, group_by, watched_where, stats_count, stats_start) result = monitor_db.select(query) @@ -1068,10 +1068,10 @@ class DataFactory(object): most_concurrent = [] try: - base_query = 'SELECT sh.started, sh.stopped ' \ - 'FROM session_history AS sh ' \ - 'JOIN session_history_media_info AS shmi ON sh.id = shmi.id ' \ - 'WHERE sh.stopped >= %s ' % timestamp + base_query = "SELECT sh.started, sh.stopped " \ + "FROM session_history AS sh " \ + "JOIN session_history_media_info AS shmi ON sh.id = shmi.id " \ + "WHERE sh.stopped >= %s " % timestamp title = 'Concurrent Streams' query = base_query @@ -1081,21 +1081,21 @@ class DataFactory(object): title = 'Concurrent Transcodes' query = base_query \ - + 'AND shmi.transcode_decision = "transcode" ' + + "AND shmi.transcode_decision = 'transcode' " result = monitor_db.select(query) if result: most_concurrent.append(calc_most_concurrent(title, result)) title = 'Concurrent Direct Streams' query = base_query \ - + 'AND shmi.transcode_decision = "copy" ' + + "AND shmi.transcode_decision = 'copy' " result = monitor_db.select(query) if result: most_concurrent.append(calc_most_concurrent(title, result)) title = 'Concurrent Direct Plays' query = base_query \ - + 'AND shmi.transcode_decision = "direct play" ' + + "AND shmi.transcode_decision = 'direct play' " result = monitor_db.select(query) if result: most_concurrent.append(calc_most_concurrent(title, result)) @@ -1120,21 +1120,21 @@ class DataFactory(object): library_stats = [] try: - query = 'SELECT ls.id, ls.section_id, ls.section_name, ls.section_type, ls.thumb AS library_thumb, ' \ - 'ls.custom_thumb_url AS custom_thumb, ls.art AS library_art, ls.custom_art_url AS custom_art, ' \ - 'ls.count, ls.parent_count, ls.child_count, ' \ - 'sh.id, shm.title, shm.grandparent_title, shm.full_title, shm.year, ' \ - 'shm.media_index, shm.parent_media_index, ' \ - 'sh.rating_key, shm.grandparent_rating_key, shm.thumb, shm.grandparent_thumb, ' \ - 'sh.user, sh.user_id, sh.player, ' \ - 'shm.art, sh.media_type, shm.content_rating, shm.labels, shm.live, shm.guid, ' \ - 'MAX(sh.started) AS last_watch ' \ - 'FROM library_sections AS ls ' \ - 'LEFT OUTER JOIN session_history AS sh ON ls.section_id = sh.section_id ' \ - 'LEFT OUTER JOIN session_history_metadata AS shm ON sh.id = shm.id ' \ - 'WHERE ls.section_id IN (%s) AND ls.deleted_section = 0 ' \ - 'GROUP BY ls.id ' \ - 'ORDER BY ls.section_type, ls.count DESC, ls.parent_count DESC, ls.child_count DESC ' % ','.join(library_cards) + query = "SELECT ls.id, ls.section_id, ls.section_name, ls.section_type, ls.thumb AS library_thumb, " \ + "ls.custom_thumb_url AS custom_thumb, ls.art AS library_art, ls.custom_art_url AS custom_art, " \ + "ls.count, ls.parent_count, ls.child_count, " \ + "sh.id, shm.title, shm.grandparent_title, shm.full_title, shm.year, " \ + "shm.media_index, shm.parent_media_index, " \ + "sh.rating_key, shm.grandparent_rating_key, shm.thumb, shm.grandparent_thumb, " \ + "sh.user, sh.user_id, sh.player, " \ + "shm.art, sh.media_type, shm.content_rating, shm.labels, shm.live, shm.guid, " \ + "MAX(sh.started) AS last_watch " \ + "FROM library_sections AS ls " \ + "LEFT OUTER JOIN session_history AS sh ON ls.section_id = sh.section_id " \ + "LEFT OUTER JOIN session_history_metadata AS shm ON sh.id = shm.id " \ + "WHERE ls.section_id IN (%s) AND ls.deleted_section = 0 " \ + "GROUP BY ls.id " \ + "ORDER BY ls.section_type, ls.count DESC, ls.parent_count DESC, ls.child_count DESC " % ",".join(library_cards) result = monitor_db.select(query) except Exception as e: logger.warn("Tautulli DataFactory :: Unable to execute database query for get_library_stats: %s." % e) @@ -1228,15 +1228,15 @@ class DataFactory(object): try: if days > 0: if str(rating_key).isdigit(): - query = 'SELECT (SUM(stopped - started) - ' \ - 'SUM(CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END)) AS total_time, ' \ - 'COUNT(DISTINCT %s) AS total_plays, section_id ' \ - 'FROM session_history ' \ - 'JOIN session_history_metadata ON session_history_metadata.id = session_history.id ' \ - 'WHERE stopped >= ? ' \ - 'AND (session_history.grandparent_rating_key IN (%s) ' \ - 'OR session_history.parent_rating_key IN (%s) ' \ - 'OR session_history.rating_key IN (%s))' % ( + query = "SELECT (SUM(stopped - started) - " \ + "SUM(CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END)) AS total_time, " \ + "COUNT(DISTINCT %s) AS total_plays, section_id " \ + "FROM session_history " \ + "JOIN session_history_metadata ON session_history_metadata.id = session_history.id " \ + "WHERE stopped >= ? " \ + "AND (session_history.grandparent_rating_key IN (%s) " \ + "OR session_history.parent_rating_key IN (%s) " \ + "OR session_history.rating_key IN (%s))" % ( group_by, rating_keys_arg, rating_keys_arg, rating_keys_arg ) @@ -1245,14 +1245,14 @@ class DataFactory(object): result = [] else: if str(rating_key).isdigit(): - query = 'SELECT (SUM(stopped - started) - ' \ - 'SUM(CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END)) AS total_time, ' \ - 'COUNT(DISTINCT %s) AS total_plays, section_id ' \ - 'FROM session_history ' \ - 'JOIN session_history_metadata ON session_history_metadata.id = session_history.id ' \ - 'WHERE (session_history.grandparent_rating_key IN (%s) ' \ - 'OR session_history.parent_rating_key IN (%s) ' \ - 'OR session_history.rating_key IN (%s))' % ( + query = "SELECT (SUM(stopped - started) - " \ + "SUM(CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END)) AS total_time, " \ + "COUNT(DISTINCT %s) AS total_plays, section_id " \ + "FROM session_history " \ + "JOIN session_history_metadata ON session_history_metadata.id = session_history.id " \ + "WHERE (session_history.grandparent_rating_key IN (%s) " \ + "OR session_history.parent_rating_key IN (%s) " \ + "OR session_history.rating_key IN (%s))" % ( group_by, rating_keys_arg, rating_keys_arg, rating_keys_arg ) @@ -1308,20 +1308,20 @@ class DataFactory(object): try: if str(rating_key).isdigit(): - 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, ' \ - 'users.user_id, users.username, users.thumb, users.custom_avatar_url AS custom_thumb, ' \ - '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, ' \ - 'section_id ' \ - 'FROM session_history ' \ - 'JOIN session_history_metadata ON session_history_metadata.id = session_history.id ' \ - 'JOIN users ON users.user_id = session_history.user_id ' \ - 'WHERE (session_history.grandparent_rating_key IN (%s) ' \ - 'OR session_history.parent_rating_key IN (%s) ' \ - 'OR session_history.rating_key IN (%s)) ' \ - 'GROUP BY users.user_id ' \ - 'ORDER BY total_plays DESC, total_time DESC' % ( + 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, " \ + "users.user_id, users.username, users.thumb, users.custom_avatar_url AS custom_thumb, " \ + "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, " \ + "section_id " \ + "FROM session_history " \ + "JOIN session_history_metadata ON session_history_metadata.id = session_history.id " \ + "JOIN users ON users.user_id = session_history.user_id " \ + "WHERE (session_history.grandparent_rating_key IN (%s) " \ + "OR session_history.parent_rating_key IN (%s) " \ + "OR session_history.rating_key IN (%s)) " \ + "GROUP BY users.user_id " \ + "ORDER BY total_plays DESC, total_time DESC" % ( group_by, rating_keys_arg, rating_keys_arg, rating_keys_arg ) @@ -1362,53 +1362,53 @@ class DataFactory(object): user_cond = '' table = 'session_history' if row_id else 'sessions' if session.get_session_user_id(): - user_cond = 'AND %s.user_id = %s ' % (table, session.get_session_user_id()) + user_cond = "AND %s.user_id = %s " % (table, session.get_session_user_id()) if row_id: - query = 'SELECT bitrate, video_full_resolution, ' \ - 'optimized_version, optimized_version_profile, optimized_version_title, ' \ - 'synced_version, synced_version_profile, ' \ - 'container, video_codec, video_bitrate, video_width, video_height, video_framerate, ' \ - 'video_dynamic_range, aspect_ratio, ' \ - 'audio_codec, audio_bitrate, audio_channels, audio_language, audio_language_code, ' \ - 'subtitle_codec, subtitle_forced, subtitle_language, ' \ - 'stream_bitrate, stream_video_full_resolution, quality_profile, stream_container_decision, stream_container, ' \ - 'stream_video_decision, stream_video_codec, stream_video_bitrate, stream_video_width, stream_video_height, ' \ - 'stream_video_framerate, stream_video_dynamic_range, ' \ - 'stream_audio_decision, stream_audio_codec, stream_audio_bitrate, stream_audio_channels, ' \ - 'stream_audio_language, stream_audio_language_code, ' \ - 'subtitles, stream_subtitle_decision, stream_subtitle_codec, stream_subtitle_forced, stream_subtitle_language, ' \ - 'transcode_hw_decoding, transcode_hw_encoding, ' \ - 'video_decision, audio_decision, transcode_decision, width, height, container, ' \ - 'transcode_container, transcode_video_codec, transcode_audio_codec, transcode_audio_channels, ' \ - 'transcode_width, transcode_height, ' \ - 'session_history_metadata.media_type, title, grandparent_title, original_title ' \ - 'FROM session_history_media_info ' \ - 'JOIN session_history ON session_history_media_info.id = session_history.id ' \ - 'JOIN session_history_metadata ON session_history_media_info.id = session_history_metadata.id ' \ - 'WHERE session_history_media_info.id = ? %s' % user_cond + query = "SELECT bitrate, video_full_resolution, " \ + "optimized_version, optimized_version_profile, optimized_version_title, " \ + "synced_version, synced_version_profile, " \ + "container, video_codec, video_bitrate, video_width, video_height, video_framerate, " \ + "video_dynamic_range, aspect_ratio, " \ + "audio_codec, audio_bitrate, audio_channels, audio_language, audio_language_code, " \ + "subtitle_codec, subtitle_forced, subtitle_language, " \ + "stream_bitrate, stream_video_full_resolution, quality_profile, stream_container_decision, stream_container, " \ + "stream_video_decision, stream_video_codec, stream_video_bitrate, stream_video_width, stream_video_height, " \ + "stream_video_framerate, stream_video_dynamic_range, " \ + "stream_audio_decision, stream_audio_codec, stream_audio_bitrate, stream_audio_channels, " \ + "stream_audio_language, stream_audio_language_code, " \ + "subtitles, stream_subtitle_decision, stream_subtitle_codec, stream_subtitle_forced, stream_subtitle_language, " \ + "transcode_hw_decoding, transcode_hw_encoding, " \ + "video_decision, audio_decision, transcode_decision, width, height, container, " \ + "transcode_container, transcode_video_codec, transcode_audio_codec, transcode_audio_channels, " \ + "transcode_width, transcode_height, " \ + "session_history_metadata.media_type, title, grandparent_title, original_title " \ + "FROM session_history_media_info " \ + "JOIN session_history ON session_history_media_info.id = session_history.id " \ + "JOIN session_history_metadata ON session_history_media_info.id = session_history_metadata.id " \ + "WHERE session_history_media_info.id = ? %s" % user_cond result = monitor_db.select(query, args=[row_id]) elif session_key: - query = 'SELECT bitrate, video_full_resolution, ' \ - 'optimized_version, optimized_version_profile, optimized_version_title, ' \ - 'synced_version, synced_version_profile, ' \ - 'container, video_codec, video_bitrate, video_width, video_height, video_framerate, ' \ - 'video_dynamic_range, aspect_ratio, ' \ - 'audio_codec, audio_bitrate, audio_channels, audio_language, audio_language_code, ' \ - 'subtitle_codec, subtitle_forced, subtitle_language, ' \ - 'stream_bitrate, stream_video_full_resolution, quality_profile, stream_container_decision, stream_container, ' \ - 'stream_video_decision, stream_video_codec, stream_video_bitrate, stream_video_width, stream_video_height, ' \ - 'stream_video_framerate, stream_video_dynamic_range, ' \ - 'stream_audio_decision, stream_audio_codec, stream_audio_bitrate, stream_audio_channels, ' \ - 'stream_audio_language, stream_audio_language_code, ' \ - 'subtitles, stream_subtitle_decision, stream_subtitle_codec, stream_subtitle_forced, stream_subtitle_language, ' \ - 'transcode_hw_decoding, transcode_hw_encoding, ' \ - 'video_decision, audio_decision, transcode_decision, width, height, container, ' \ - 'transcode_container, transcode_video_codec, transcode_audio_codec, transcode_audio_channels, ' \ - 'transcode_width, transcode_height, ' \ - 'media_type, title, grandparent_title, original_title ' \ - 'FROM sessions ' \ - 'WHERE session_key = ? %s' % user_cond + query = "SELECT bitrate, video_full_resolution, " \ + "optimized_version, optimized_version_profile, optimized_version_title, " \ + "synced_version, synced_version_profile, " \ + "container, video_codec, video_bitrate, video_width, video_height, video_framerate, " \ + "video_dynamic_range, aspect_ratio, " \ + "audio_codec, audio_bitrate, audio_channels, audio_language, audio_language_code, " \ + "subtitle_codec, subtitle_forced, subtitle_language, " \ + "stream_bitrate, stream_video_full_resolution, quality_profile, stream_container_decision, stream_container, " \ + "stream_video_decision, stream_video_codec, stream_video_bitrate, stream_video_width, stream_video_height, " \ + "stream_video_framerate, stream_video_dynamic_range, " \ + "stream_audio_decision, stream_audio_codec, stream_audio_bitrate, stream_audio_channels, " \ + "stream_audio_language, stream_audio_language_code, " \ + "subtitles, stream_subtitle_decision, stream_subtitle_codec, stream_subtitle_forced, stream_subtitle_language, " \ + "transcode_hw_decoding, transcode_hw_encoding, " \ + "video_decision, audio_decision, transcode_decision, width, height, container, " \ + "transcode_container, transcode_video_codec, transcode_audio_codec, transcode_audio_channels, " \ + "transcode_width, transcode_height, " \ + "media_type, title, grandparent_title, original_title " \ + "FROM sessions " \ + "WHERE session_key = ? %s" % user_cond result = monitor_db.select(query, args=[session_key]) else: return None @@ -1499,43 +1499,43 @@ class DataFactory(object): if rating_key or guid: if guid: - where = 'session_history_metadata.guid LIKE ?' + where = "session_history_metadata.guid LIKE ?" args = [guid.split('?')[0] + '%'] # SQLite LIKE wildcard else: - where = 'session_history_metadata.rating_key = ?' + where = "session_history_metadata.rating_key = ?" args = [rating_key] - query = 'SELECT session_history.section_id, session_history_metadata.id, ' \ - 'session_history_metadata.rating_key, session_history_metadata.parent_rating_key, ' \ - 'session_history_metadata.grandparent_rating_key, session_history_metadata.title, ' \ - 'session_history_metadata.parent_title, session_history_metadata.grandparent_title, ' \ - 'session_history_metadata.original_title, session_history_metadata.full_title, ' \ - 'library_sections.section_name, ' \ - 'session_history_metadata.media_index, session_history_metadata.parent_media_index, ' \ - 'session_history_metadata.thumb, ' \ - 'session_history_metadata.parent_thumb, session_history_metadata.grandparent_thumb, ' \ - 'session_history_metadata.art, session_history_metadata.media_type, session_history_metadata.year, ' \ - 'session_history_metadata.originally_available_at, session_history_metadata.added_at, ' \ - 'session_history_metadata.updated_at, session_history_metadata.last_viewed_at, ' \ - 'session_history_metadata.content_rating, session_history_metadata.summary, ' \ - 'session_history_metadata.tagline, session_history_metadata.rating, session_history_metadata.duration, ' \ - 'session_history_metadata.guid, session_history_metadata.directors, session_history_metadata.writers, ' \ - 'session_history_metadata.actors, session_history_metadata.genres, session_history_metadata.studio, ' \ - 'session_history_metadata.labels, ' \ - 'session_history_media_info.container, session_history_media_info.bitrate, ' \ - 'session_history_media_info.video_codec, session_history_media_info.video_resolution, ' \ - 'session_history_media_info.video_full_resolution, ' \ - 'session_history_media_info.video_framerate, session_history_media_info.audio_codec, ' \ - 'session_history_media_info.audio_channels, session_history_metadata.live, ' \ - 'session_history_metadata.channel_call_sign, session_history_metadata.channel_identifier, ' \ - 'session_history_metadata.channel_thumb ' \ - 'FROM session_history_metadata ' \ - 'JOIN library_sections ON session_history.section_id = library_sections.section_id ' \ - 'JOIN session_history ON session_history_metadata.id = session_history.id ' \ - 'JOIN session_history_media_info ON session_history_metadata.id = session_history_media_info.id ' \ - 'WHERE %s ' \ - 'ORDER BY session_history_metadata.id DESC ' \ - 'LIMIT 1' % where + query = "SELECT session_history.section_id, session_history_metadata.id, " \ + "session_history_metadata.rating_key, session_history_metadata.parent_rating_key, " \ + "session_history_metadata.grandparent_rating_key, session_history_metadata.title, " \ + "session_history_metadata.parent_title, session_history_metadata.grandparent_title, " \ + "session_history_metadata.original_title, session_history_metadata.full_title, " \ + "library_sections.section_name, " \ + "session_history_metadata.media_index, session_history_metadata.parent_media_index, " \ + "session_history_metadata.thumb, " \ + "session_history_metadata.parent_thumb, session_history_metadata.grandparent_thumb, " \ + "session_history_metadata.art, session_history_metadata.media_type, session_history_metadata.year, " \ + "session_history_metadata.originally_available_at, session_history_metadata.added_at, " \ + "session_history_metadata.updated_at, session_history_metadata.last_viewed_at, " \ + "session_history_metadata.content_rating, session_history_metadata.summary, " \ + "session_history_metadata.tagline, session_history_metadata.rating, session_history_metadata.duration, " \ + "session_history_metadata.guid, session_history_metadata.directors, session_history_metadata.writers, " \ + "session_history_metadata.actors, session_history_metadata.genres, session_history_metadata.studio, " \ + "session_history_metadata.labels, " \ + "session_history_media_info.container, session_history_media_info.bitrate, " \ + "session_history_media_info.video_codec, session_history_media_info.video_resolution, " \ + "session_history_media_info.video_full_resolution, " \ + "session_history_media_info.video_framerate, session_history_media_info.audio_codec, " \ + "session_history_media_info.audio_channels, session_history_metadata.live, " \ + "session_history_metadata.channel_call_sign, session_history_metadata.channel_identifier, " \ + "session_history_metadata.channel_thumb " \ + "FROM session_history_metadata " \ + "JOIN library_sections ON session_history.section_id = library_sections.section_id " \ + "JOIN session_history ON session_history_metadata.id = session_history.id " \ + "JOIN session_history_media_info ON session_history_metadata.id = session_history_media_info.id " \ + "WHERE %s " \ + "ORDER BY session_history_metadata.id DESC " \ + "LIMIT 1" % where result = monitor_db.select(query=query, args=args) else: result = [] @@ -1614,14 +1614,14 @@ class DataFactory(object): where, args = datatables.build_custom_where(custom_where=custom_where) try: - query = 'SELECT SUM(CASE WHEN stopped > 0 THEN (stopped - started) ELSE 0 END) - ' \ - 'SUM(CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) AS total_duration, ' \ - '(CASE WHEN session_history_metadata.live = 1 THEN "live" ELSE session_history.media_type END) ' \ - 'AS media_type_live ' \ - 'FROM session_history ' \ - 'JOIN session_history_metadata ON session_history_metadata.id = session_history.id ' \ - 'JOIN session_history_media_info ON session_history_media_info.id = session_history.id ' \ - '%s ' % where + query = "SELECT SUM(CASE WHEN stopped > 0 THEN (stopped - started) ELSE 0 END) - " \ + "SUM(CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END) AS total_duration, " \ + "(CASE WHEN session_history_metadata.live = 1 THEN 'live' ELSE session_history.media_type END) " \ + "AS media_type_live " \ + "FROM session_history " \ + "JOIN session_history_metadata ON session_history_metadata.id = session_history.id " \ + "JOIN session_history_media_info ON session_history_media_info.id = session_history.id " \ + "%s " % where result = monitor_db.select(query, args=args) except Exception as e: logger.warn("Tautulli DataFactory :: Unable to execute database query for get_total_duration: %s." % e) @@ -1644,7 +1644,7 @@ class DataFactory(object): if session_key: try: - query = 'SELECT ip_address FROM sessions WHERE session_key = %d %s' % (int(session_key), user_cond) + query = "SELECT ip_address FROM sessions WHERE session_key = %d %s" % (int(session_key), user_cond) result = monitor_db.select(query) except Exception as e: logger.warn("Tautulli DataFactory :: Unable to execute database query for get_session_ip: %s." % e) @@ -1694,19 +1694,19 @@ class DataFactory(object): where = '' if where_params: - where = 'WHERE ' + ' AND '.join([w + ' = ?' for w in where_params]) + where = "WHERE " + " AND ".join([w + " = ?" for w in where_params]) if order_by: - order_by = 'ORDER BY ' + order_by + ' DESC' + order_by = "ORDER BY " + order_by + " DESC" if service == 'imgur': - query = 'SELECT imgur_title AS img_title, imgur_url AS img_url FROM imgur_lookup ' \ - 'JOIN image_hash_lookup ON imgur_lookup.img_hash = image_hash_lookup.img_hash ' \ - '%s %s' % (where, order_by) + query = "SELECT imgur_title AS img_title, imgur_url AS img_url FROM imgur_lookup " \ + "JOIN image_hash_lookup ON imgur_lookup.img_hash = image_hash_lookup.img_hash " \ + "%s %s" % (where, order_by) elif service == 'cloudinary': - query = 'SELECT cloudinary_title AS img_title, cloudinary_url AS img_url FROM cloudinary_lookup ' \ - 'JOIN image_hash_lookup ON cloudinary_lookup.img_hash = image_hash_lookup.img_hash ' \ - '%s %s' % (where, order_by) + query = "SELECT cloudinary_title AS img_title, cloudinary_url AS img_url FROM cloudinary_lookup " \ + "JOIN image_hash_lookup ON cloudinary_lookup.img_hash = image_hash_lookup.img_hash " \ + "%s %s" % (where, order_by) else: logger.warn("Tautulli DataFactory :: Unable to execute database query for get_img_info: " "service not provided.") @@ -1754,14 +1754,14 @@ class DataFactory(object): args = [] log_msg = '' if rating_key: - where = 'WHERE rating_key = ?' + where = "WHERE rating_key = ?" args = [rating_key] log_msg = ' for rating_key %s' % rating_key if service.lower() == 'imgur': # Delete from Imgur - query = 'SELECT imgur_title, delete_hash, fallback FROM imgur_lookup ' \ - 'JOIN image_hash_lookup ON imgur_lookup.img_hash = image_hash_lookup.img_hash %s' % where + query = "SELECT imgur_title, delete_hash, fallback FROM imgur_lookup " \ + "JOIN image_hash_lookup ON imgur_lookup.img_hash = image_hash_lookup.img_hash %s" % where results = monitor_db.select(query, args=args) for imgur_info in results: @@ -1772,15 +1772,15 @@ class DataFactory(object): logger.info("Tautulli DataFactory :: Deleting Imgur info%s from the database." % log_msg) - result = monitor_db.action('DELETE FROM imgur_lookup WHERE img_hash ' - 'IN (SELECT img_hash FROM image_hash_lookup %s)' % where, + result = monitor_db.action("DELETE FROM imgur_lookup WHERE img_hash " + "IN (SELECT img_hash FROM image_hash_lookup %s)" % where, args) elif service.lower() == 'cloudinary': # Delete from Cloudinary - query = 'SELECT cloudinary_title, rating_key, fallback FROM cloudinary_lookup ' \ - 'JOIN image_hash_lookup ON cloudinary_lookup.img_hash = image_hash_lookup.img_hash %s ' \ - 'GROUP BY rating_key' % where + query = "SELECT cloudinary_title, rating_key, fallback FROM cloudinary_lookup " \ + "JOIN image_hash_lookup ON cloudinary_lookup.img_hash = image_hash_lookup.img_hash %s " \ + "GROUP BY rating_key" % where results = monitor_db.select(query, args=args) if delete_all: @@ -1791,8 +1791,8 @@ class DataFactory(object): logger.info("Tautulli DataFactory :: Deleting Cloudinary info%s from the database." % log_msg) - result = monitor_db.action('DELETE FROM cloudinary_lookup WHERE img_hash ' - 'IN (SELECT img_hash FROM image_hash_lookup %s)' % where, + result = monitor_db.action("DELETE FROM cloudinary_lookup WHERE img_hash " + "IN (SELECT img_hash FROM image_hash_lookup %s)" % where, args) else: @@ -1883,15 +1883,15 @@ class DataFactory(object): if rating_key: logger.info("Tautulli DataFactory :: Deleting lookup info for rating_key %s from the database." % rating_key) - result_themoviedb = monitor_db.action('DELETE FROM themoviedb_lookup WHERE rating_key = ?', [rating_key]) - result_tvmaze = monitor_db.action('DELETE FROM tvmaze_lookup WHERE rating_key = ?', [rating_key]) - result_musicbrainz = monitor_db.action('DELETE FROM musicbrainz_lookup WHERE rating_key = ?', [rating_key]) + result_themoviedb = monitor_db.action("DELETE FROM themoviedb_lookup WHERE rating_key = ?", [rating_key]) + result_tvmaze = monitor_db.action("DELETE FROM tvmaze_lookup WHERE rating_key = ?", [rating_key]) + result_musicbrainz = monitor_db.action("DELETE FROM musicbrainz_lookup WHERE rating_key = ?", [rating_key]) return bool(result_themoviedb or result_tvmaze or result_musicbrainz) elif service and delete_all: if service.lower() in ('themoviedb', 'tvmaze', 'musicbrainz'): logger.info("Tautulli DataFactory :: Deleting all lookup info for '%s' from the database." % service) - result = monitor_db.action('DELETE FROM %s_lookup' % service.lower()) + result = monitor_db.action("DELETE FROM %s_lookup" % service.lower()) return bool(result) else: logger.error("Tautulli DataFactory :: Unable to delete lookup info: invalid service '%s' provided." @@ -1901,13 +1901,13 @@ class DataFactory(object): monitor_db = database.MonitorDatabase() if rating_key: - query = 'SELECT rating_key, parent_rating_key, grandparent_rating_key, title, parent_title, grandparent_title, ' \ - 'media_index, parent_media_index, year, media_type ' \ - 'FROM session_history_metadata ' \ - 'WHERE rating_key = ? ' \ - 'OR parent_rating_key = ? ' \ - 'OR grandparent_rating_key = ? ' \ - 'LIMIT 1' + query = "SELECT rating_key, parent_rating_key, grandparent_rating_key, title, parent_title, grandparent_title, " \ + "media_index, parent_media_index, year, media_type " \ + "FROM session_history_metadata " \ + "WHERE rating_key = ? " \ + "OR parent_rating_key = ? " \ + "OR grandparent_rating_key = ? " \ + "LIMIT 1" result = monitor_db.select(query=query, args=[rating_key, rating_key, rating_key]) else: result = [] @@ -1974,12 +1974,12 @@ class DataFactory(object): # Get the grandparent rating key try: - query = 'SELECT rating_key, parent_rating_key, grandparent_rating_key ' \ - 'FROM session_history_metadata ' \ - 'WHERE rating_key = ? ' \ - 'OR parent_rating_key = ? ' \ - 'OR grandparent_rating_key = ? ' \ - 'LIMIT 1' + query = "SELECT rating_key, parent_rating_key, grandparent_rating_key " \ + "FROM session_history_metadata " \ + "WHERE rating_key = ? " \ + "OR parent_rating_key = ? " \ + "OR grandparent_rating_key = ? " \ + "LIMIT 1" result = monitor_db.select(query=query, args=[rating_key, rating_key, rating_key]) grandparent_rating_key = result[0]['grandparent_rating_key'] @@ -1988,12 +1988,12 @@ class DataFactory(object): logger.warn("Tautulli DataFactory :: Unable to execute database query for get_rating_keys_list: %s." % e) return {} - query = 'SELECT rating_key, parent_rating_key, grandparent_rating_key, title, parent_title, grandparent_title, ' \ - 'media_index, parent_media_index ' \ - 'FROM session_history_metadata ' \ - 'WHERE {0} = ? ' \ - 'GROUP BY {1} ' \ - 'ORDER BY {1} DESC ' + query = "SELECT rating_key, parent_rating_key, grandparent_rating_key, title, parent_title, grandparent_title, " \ + "media_index, parent_media_index " \ + "FROM session_history_metadata " \ + "WHERE {0} = ? " \ + "GROUP BY {1} " \ + "ORDER BY {1} DESC " # get grandparent_rating_keys grandparents = {} @@ -2070,13 +2070,13 @@ class DataFactory(object): if metadata['media_type'] == 'show' or metadata['media_type'] == 'artist': # check grandparent_rating_key (2 tables) query = ( - 'SELECT id FROM session_history ' - 'WHERE grandparent_rating_key = ? ' + "SELECT id FROM session_history " + "WHERE grandparent_rating_key = ? " ) args = [old_key] if _UPDATE_METADATA_IDS['grandparent_rating_key_ids']: - query += 'AND id NOT IN (%s)' % ','.join(_UPDATE_METADATA_IDS['grandparent_rating_key_ids']) + query += "AND id NOT IN (%s)" % ",".join(_UPDATE_METADATA_IDS['grandparent_rating_key_ids']) ids = [str(row['id']) for row in monitor_db.select(query, args)] if ids: @@ -2085,26 +2085,26 @@ class DataFactory(object): continue monitor_db.action( - 'UPDATE session_history SET grandparent_rating_key = ? ' - 'WHERE id IN (%s)' % ','.join(ids), + "UPDATE session_history SET grandparent_rating_key = ? " + "WHERE id IN (%s)" % ",".join(ids), [new_key] ) monitor_db.action( - 'UPDATE session_history_metadata SET grandparent_rating_key = ? ' - 'WHERE id IN (%s)' % ','.join(ids), + "UPDATE session_history_metadata SET grandparent_rating_key = ? " + "WHERE id IN (%s)" % ",".join(ids), [new_key] ) elif metadata['media_type'] == 'season' or metadata['media_type'] == 'album': # check parent_rating_key (2 tables) query = ( - 'SELECT id FROM session_history ' - 'WHERE parent_rating_key = ? ' + "SELECT id FROM session_history " + "WHERE parent_rating_key = ? " ) args = [old_key] if _UPDATE_METADATA_IDS['parent_rating_key_ids']: - query += 'AND id NOT IN (%s)' % ','.join(_UPDATE_METADATA_IDS['parent_rating_key_ids']) + query += "AND id NOT IN (%s)" % ",".join(_UPDATE_METADATA_IDS['parent_rating_key_ids']) ids = [str(row['id']) for row in monitor_db.select(query, args)] if ids: @@ -2113,26 +2113,26 @@ class DataFactory(object): continue monitor_db.action( - 'UPDATE session_history SET parent_rating_key = ? ' - 'WHERE id IN (%s)' % ','.join(ids), + "UPDATE session_history SET parent_rating_key = ? " + "WHERE id IN (%s)" % ",".join(ids), [new_key] ) monitor_db.action( - 'UPDATE session_history_metadata SET parent_rating_key = ? ' - 'WHERE id IN (%s)' % ','.join(ids), + "UPDATE session_history_metadata SET parent_rating_key = ? " + "WHERE id IN (%s)" % ",".join(ids), [new_key] ) else: # check rating_key (2 tables) query = ( - 'SELECT id FROM session_history ' - 'WHERE rating_key = ? ' + "SELECT id FROM session_history " + "WHERE rating_key = ? " ) args = [old_key] if _UPDATE_METADATA_IDS['rating_key_ids']: - query += 'AND id NOT IN (%s)' % ','.join(_UPDATE_METADATA_IDS['rating_key_ids']) + query += "AND id NOT IN (%s)" % ",".join(_UPDATE_METADATA_IDS['rating_key_ids']) ids = [str(row['id']) for row in monitor_db.select(query, args)] if ids: @@ -2141,13 +2141,13 @@ class DataFactory(object): continue monitor_db.action( - 'UPDATE session_history SET rating_key = ? ' - 'WHERE id IN (%s)' % ','.join(ids), + "UPDATE session_history SET rating_key = ? " + "WHERE id IN (%s)" % ",".join(ids), [new_key] ) monitor_db.action( - 'UPDATE session_history_media_info SET rating_key = ? ' - 'WHERE id IN (%s)' % ','.join(ids), + "UPDATE session_history_media_info SET rating_key = ? " + "WHERE id IN (%s)" % ",".join(ids), [new_key] ) @@ -2181,21 +2181,21 @@ class DataFactory(object): monitor_db = database.MonitorDatabase() - query = 'UPDATE session_history SET section_id = ? ' \ - 'WHERE id IN (%s)' % ','.join(ids) + query = "UPDATE session_history SET section_id = ? " \ + "WHERE id IN (%s)" % ",".join(ids) args = [metadata['section_id']] monitor_db.action(query=query, args=args) # Update the session_history_metadata table - query = 'UPDATE session_history_metadata SET rating_key = ?, parent_rating_key = ?, ' \ - 'grandparent_rating_key = ?, title = ?, parent_title = ?, grandparent_title = ?, ' \ - 'original_title = ?, full_title = ?, ' \ - 'media_index = ?, parent_media_index = ?, thumb = ?, parent_thumb = ?, ' \ - 'grandparent_thumb = ?, art = ?, media_type = ?, year = ?, originally_available_at = ?, ' \ - 'added_at = ?, updated_at = ?, last_viewed_at = ?, content_rating = ?, summary = ?, ' \ - 'tagline = ?, rating = ?, duration = ?, guid = ?, directors = ?, writers = ?, actors = ?, ' \ - 'genres = ?, studio = ?, labels = ? ' \ - 'WHERE id IN (%s)' % ','.join(ids) + query = "UPDATE session_history_metadata SET rating_key = ?, parent_rating_key = ?, " \ + "grandparent_rating_key = ?, title = ?, parent_title = ?, grandparent_title = ?, " \ + "original_title = ?, full_title = ?, " \ + "media_index = ?, parent_media_index = ?, thumb = ?, parent_thumb = ?, " \ + "grandparent_thumb = ?, art = ?, media_type = ?, year = ?, originally_available_at = ?, " \ + "added_at = ?, updated_at = ?, last_viewed_at = ?, content_rating = ?, summary = ?, " \ + "tagline = ?, rating = ?, duration = ?, guid = ?, directors = ?, writers = ?, actors = ?, " \ + "genres = ?, studio = ?, labels = ? " \ + "WHERE id IN (%s)" % ",".join(ids) args = [metadata['rating_key'], metadata['parent_rating_key'], metadata['grandparent_rating_key'], metadata['title'], metadata['parent_title'], metadata['grandparent_title'], @@ -2212,19 +2212,19 @@ class DataFactory(object): def get_notification_log(self, kwargs=None): data_tables = datatables.DataTables() - columns = ['notify_log.id', - 'notify_log.timestamp', - 'notify_log.session_key', - 'notify_log.rating_key', - 'notify_log.user_id', - 'notify_log.user', - 'notify_log.notifier_id', - 'notify_log.agent_id', - 'notify_log.agent_name', - 'notify_log.notify_action', - 'notify_log.subject_text', - 'notify_log.body_text', - 'notify_log.success' + columns = ["notify_log.id", + "notify_log.timestamp", + "notify_log.session_key", + "notify_log.rating_key", + "notify_log.user_id", + "notify_log.user", + "notify_log.notifier_id", + "notify_log.agent_id", + "notify_log.agent_name", + "notify_log.notify_action", + "notify_log.subject_text", + "notify_log.body_text", + "notify_log.success" ] try: query = data_tables.ssp_query(table_name='notify_log', @@ -2281,8 +2281,8 @@ class DataFactory(object): try: logger.info("Tautulli DataFactory :: Clearing notification logs from database.") - monitor_db.action('DELETE FROM notify_log') - monitor_db.action('VACUUM') + monitor_db.action("DELETE FROM notify_log") + monitor_db.action("VACUUM") return True except Exception as e: logger.warn("Tautulli DataFactory :: Unable to execute database query for delete_notification_log: %s." % e) @@ -2291,18 +2291,18 @@ class DataFactory(object): def get_newsletter_log(self, kwargs=None): data_tables = datatables.DataTables() - columns = ['newsletter_log.id', - 'newsletter_log.timestamp', - 'newsletter_log.newsletter_id', - 'newsletter_log.agent_id', - 'newsletter_log.agent_name', - 'newsletter_log.notify_action', - 'newsletter_log.subject_text', - 'newsletter_log.body_text', - 'newsletter_log.start_date', - 'newsletter_log.end_date', - 'newsletter_log.uuid', - 'newsletter_log.success' + columns = ["newsletter_log.id", + "newsletter_log.timestamp", + "newsletter_log.newsletter_id", + "newsletter_log.agent_id", + "newsletter_log.agent_name", + "newsletter_log.notify_action", + "newsletter_log.subject_text", + "newsletter_log.body_text", + "newsletter_log.start_date", + "newsletter_log.end_date", + "newsletter_log.uuid", + "newsletter_log.success" ] try: query = data_tables.ssp_query(table_name='newsletter_log', @@ -2353,8 +2353,8 @@ class DataFactory(object): try: logger.info("Tautulli DataFactory :: Clearing newsletter logs from database.") - monitor_db.action('DELETE FROM newsletter_log') - monitor_db.action('VACUUM') + monitor_db.action("DELETE FROM newsletter_log") + monitor_db.action("VACUUM") return True except Exception as e: logger.warn("Tautulli DataFactory :: Unable to execute database query for delete_newsletter_log: %s." % e) @@ -2365,15 +2365,15 @@ class DataFactory(object): if user_id: if history_only: - query = 'SELECT machine_id FROM session_history ' \ - 'WHERE user_id = ? ' \ - 'GROUP BY machine_id' + query = "SELECT machine_id FROM session_history " \ + "WHERE user_id = ? " \ + "GROUP BY machine_id" else: - query = 'SELECT * FROM (' \ - 'SELECT user_id, machine_id FROM session_history ' \ - 'UNION SELECT user_id, machine_id from sessions_continued) ' \ - 'WHERE user_id = ? ' \ - 'GROUP BY machine_id' + query = "SELECT * FROM (" \ + "SELECT user_id, machine_id FROM session_history " \ + "UNION SELECT user_id, machine_id from sessions_continued) " \ + "WHERE user_id = ? " \ + "GROUP BY machine_id" try: result = monitor_db.select(query=query, args=[user_id]) @@ -2390,7 +2390,7 @@ class DataFactory(object): if rating_key: try: - query = 'SELECT * FROM recently_added WHERE rating_key = ?' + query = "SELECT * FROM recently_added WHERE rating_key = ?" result = monitor_db.select(query=query, args=[rating_key]) except Exception as e: logger.warn("Tautulli DataFactory :: Unable to execute database query for get_recently_added_item: %s." % e)