diff --git a/plexpy/__init__.py b/plexpy/__init__.py index f7f25fdc..8ddbf9bc 100644 --- a/plexpy/__init__.py +++ b/plexpy/__init__.py @@ -1,4 +1,4 @@ -# This file is part of Tautulli. +# This file is part of Tautulli. # # Tautulli is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -650,7 +650,8 @@ def dbcheck(): 'ip_address TEXT, paused_counter INTEGER DEFAULT 0, player TEXT, product TEXT, product_version TEXT, ' 'platform TEXT, platform_version TEXT, profile TEXT, machine_id TEXT, ' 'bandwidth INTEGER, location TEXT, quality_profile TEXT, secure INTEGER, relayed INTEGER, ' - 'parent_rating_key INTEGER, grandparent_rating_key INTEGER, media_type TEXT, view_offset INTEGER DEFAULT 0)' + 'parent_rating_key INTEGER, grandparent_rating_key INTEGER, media_type TEXT, section_id INTEGER, ' + 'view_offset INTEGER DEFAULT 0)' ) # session_history_media_info table :: This is a table which logs each session's media info @@ -682,7 +683,7 @@ def dbcheck(): 'CREATE TABLE IF NOT EXISTS session_history_metadata (id INTEGER PRIMARY KEY, ' 'rating_key INTEGER, parent_rating_key INTEGER, grandparent_rating_key INTEGER, ' 'title TEXT, parent_title TEXT, grandparent_title TEXT, original_title TEXT, full_title TEXT, ' - 'media_index INTEGER, parent_media_index INTEGER, section_id INTEGER, ' + 'media_index INTEGER, parent_media_index INTEGER, ' 'thumb TEXT, parent_thumb TEXT, grandparent_thumb TEXT, ' 'art TEXT, media_type TEXT, year INTEGER, originally_available_at TEXT, added_at INTEGER, updated_at INTEGER, ' 'last_viewed_at INTEGER, content_rating TEXT, summary TEXT, tagline TEXT, rating TEXT, ' @@ -1421,6 +1422,56 @@ def dbcheck(): except sqlite3.OperationalError: logger.warn("Unable to capitalize Windows platform values in session_history table.") + # Upgrade session_history table from earlier versions + try: + c_db.execute('SELECT section_id FROM session_history') + except sqlite3.OperationalError: + logger.debug("Altering database. Updating database table session_history.") + c_db.execute( + 'ALTER TABLE session_history ADD COLUMN section_id INTEGER' + ) + c_db.execute( + 'UPDATE session_history SET section_id = (' + 'SELECT section_id FROM session_history_metadata ' + 'WHERE session_history_metadata.id = session_history.id)' + ) + c_db.execute( + 'CREATE TABLE IF NOT EXISTS session_history_metadata_temp (id INTEGER PRIMARY KEY, ' + 'rating_key INTEGER, parent_rating_key INTEGER, grandparent_rating_key INTEGER, ' + 'title TEXT, parent_title TEXT, grandparent_title TEXT, original_title TEXT, full_title TEXT, ' + 'media_index INTEGER, parent_media_index INTEGER, ' + 'thumb TEXT, parent_thumb TEXT, grandparent_thumb TEXT, ' + 'art TEXT, media_type TEXT, year INTEGER, originally_available_at TEXT, added_at INTEGER, updated_at INTEGER, ' + 'last_viewed_at INTEGER, content_rating TEXT, summary TEXT, tagline TEXT, rating TEXT, ' + 'duration INTEGER DEFAULT 0, guid TEXT, directors TEXT, writers TEXT, actors TEXT, genres TEXT, studio TEXT, ' + 'labels TEXT, live INTEGER DEFAULT 0, channel_call_sign TEXT, channel_identifier TEXT, channel_thumb TEXT)' + ) + c_db.execute( + 'INSERT INTO session_history_metadata_temp (id, 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, live, channel_call_sign, channel_identifier, channel_thumb) ' + 'SELECT id, 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, live, channel_call_sign, channel_identifier, channel_thumb ' + 'FROM session_history_metadata' + ) + c_db.execute( + 'DROP TABLE session_history_metadata' + ) + c_db.execute( + 'ALTER TABLE session_history_metadata_temp RENAME TO session_history_metadata' + ) + # Upgrade session_history_metadata table from earlier versions try: c_db.execute('SELECT full_title FROM session_history_metadata') @@ -1439,15 +1490,6 @@ def dbcheck(): 'ALTER TABLE session_history_metadata ADD COLUMN tagline TEXT' ) - # Upgrade session_history_metadata table from earlier versions - try: - c_db.execute('SELECT section_id FROM session_history_metadata') - except sqlite3.OperationalError: - logger.debug("Altering database. Updating database table session_history_metadata.") - c_db.execute( - 'ALTER TABLE session_history_metadata ADD COLUMN section_id INTEGER' - ) - # Upgrade session_history_metadata table from earlier versions try: c_db.execute('SELECT labels FROM session_history_metadata') diff --git a/plexpy/activity_processor.py b/plexpy/activity_processor.py index ca50e91a..f4e58329 100644 --- a/plexpy/activity_processor.py +++ b/plexpy/activity_processor.py @@ -311,6 +311,7 @@ class ActivityProcessor(object): 'location': session['location'], 'quality_profile': session['quality_profile'], 'view_offset': session['view_offset'], + 'section_id': metadata['section_id'], 'secure': session['secure'], 'relayed': session['relayed'] } @@ -495,7 +496,6 @@ class ActivityProcessor(object): 'full_title': session['full_title'], 'media_index': metadata['media_index'], 'parent_media_index': metadata['parent_media_index'], - 'section_id': metadata['section_id'], 'thumb': metadata['thumb'], 'parent_thumb': metadata['parent_thumb'], 'grandparent_thumb': metadata['grandparent_thumb'], diff --git a/plexpy/database.py b/plexpy/database.py index cb89e465..aa5869bb 100644 --- a/plexpy/database.py +++ b/plexpy/database.py @@ -270,9 +270,7 @@ def delete_library_history(section_id=None): monitor_db = MonitorDatabase() # Get all history associated with the section_id - result = monitor_db.select('SELECT session_history.id FROM session_history ' - 'JOIN session_history_metadata ON session_history.id = session_history_metadata.id ' - 'WHERE session_history_metadata.section_id = ?', + result = monitor_db.select('SELECT id FROM session_history WHERE section_id = ?', [section_id]) row_ids = [row['id'] for row in result] diff --git a/plexpy/datafactory.py b/plexpy/datafactory.py index 41d70ec4..57705eaf 100644 --- a/plexpy/datafactory.py +++ b/plexpy/datafactory.py @@ -335,7 +335,7 @@ class DataFactory(object): if stat == 'top_movies': top_movies = [] try: - query = 'SELECT sh.id, shm.full_title, shm.year, sh.rating_key, shm.thumb, shm.section_id, ' \ + 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) - ' \ @@ -387,7 +387,7 @@ class DataFactory(object): elif stat == 'popular_movies': popular_movies = [] try: - query = 'SELECT sh.id, shm.full_title, shm.year, sh.rating_key, shm.thumb, shm.section_id, ' \ + 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 ' \ @@ -439,7 +439,7 @@ class DataFactory(object): top_tv = [] try: query = 'SELECT sh.id, shm.grandparent_title, sh.grandparent_rating_key, ' \ - 'shm.grandparent_thumb, shm.section_id, ' \ + '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 ' \ @@ -493,7 +493,7 @@ class DataFactory(object): popular_tv = [] try: query = 'SELECT sh.id, shm.grandparent_title, sh.grandparent_rating_key, ' \ - 'shm.grandparent_thumb, shm.section_id, ' \ + '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, ' \ @@ -546,7 +546,7 @@ class DataFactory(object): top_music = [] try: query = 'SELECT sh.id, shm.grandparent_title, shm.original_title, shm.year, ' \ - 'sh.grandparent_rating_key, shm.grandparent_thumb, shm.section_id, ' \ + '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) - ' \ @@ -599,7 +599,7 @@ class DataFactory(object): popular_music = [] try: query = 'SELECT sh.id, shm.grandparent_title, shm.original_title, shm.year, ' \ - 'sh.grandparent_rating_key, shm.grandparent_thumb, shm.section_id, ' \ + '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 ' \ @@ -829,7 +829,7 @@ class DataFactory(object): 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, shm.section_id, ' \ + '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 THEN u.username ELSE u.friendly_name END) ' \ ' AS friendly_name, ' \ @@ -1170,14 +1170,14 @@ class DataFactory(object): where = 'session_history_metadata.rating_key = ?' args = [rating_key] - query = 'SELECT session_history_metadata.id, ' \ + 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.section_id, session_history_metadata.thumb, ' \ + '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, ' \ @@ -1195,7 +1195,8 @@ class DataFactory(object): '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_metadata.section_id = library_sections.section_id ' \ + '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 ' \ diff --git a/plexpy/libraries.py b/plexpy/libraries.py index 4c97927f..e5a3df96 100644 --- a/plexpy/libraries.py +++ b/plexpy/libraries.py @@ -373,12 +373,12 @@ class Libraries(object): join_types=['LEFT OUTER JOIN', 'LEFT OUTER JOIN', 'LEFT OUTER JOIN'], - join_tables=['session_history_metadata', - 'session_history', + join_tables=['session_history', + 'session_history_metadata', 'session_history_media_info'], - join_evals=[['session_history_metadata.section_id', 'library_sections.section_id'], - ['session_history_metadata.id', 'session_history.id'], - ['session_history_metadata.id', 'session_history_media_info.id']], + join_evals=[['session_history.section_id', 'library_sections.section_id'], + ['session_history.id', 'session_history_metadata.id'], + ['session_history.id', 'session_history_media_info.id']], kwargs=kwargs) except Exception as e: logger.warn("Tautulli Libraries :: Unable to execute database query for get_list: %s." % e) @@ -496,12 +496,11 @@ class Libraries(object): group_by = 'rating_key' try: - query = 'SELECT MAX(session_history.started) AS last_played, COUNT(DISTINCT session_history.%s) AS play_count, ' \ - 'session_history.rating_key, session_history.parent_rating_key, session_history.grandparent_rating_key ' \ + query = 'SELECT MAX(started) AS last_played, COUNT(DISTINCT %s) AS play_count, ' \ + 'rating_key, parent_rating_key, grandparent_rating_key ' \ 'FROM session_history ' \ - 'JOIN session_history_metadata ON session_history.id = session_history_metadata.id ' \ - 'WHERE session_history_metadata.section_id = ? ' \ - 'GROUP BY session_history.%s ' % (count_by, group_by) + 'WHERE section_id = ? ' \ + 'GROUP BY %s ' % (count_by, group_by) result = monitor_db.select(query, args=[section_id]) except Exception as e: logger.warn("Tautulli Libraries :: Unable to execute database query for get_datatables_media_info2: %s." % e) @@ -830,10 +829,7 @@ class Libraries(object): join = '' if include_last_accessed: last_accessed = 'MAX(session_history.started)' - join = 'LEFT OUTER JOIN session_history_metadata ' \ - 'ON library_sections.section_id == session_history_metadata.section_id ' \ - 'LEFT OUTER JOIN session_history ' \ - 'ON session_history_metadata.id == session_history.id' + join = 'LEFT OUTER JOIN session_history ON library_sections.section_id = session_history.section_id ' \ monitor_db = database.MonitorDatabase() diff --git a/plexpy/plexivity_import.py b/plexpy/plexivity_import.py index 49e1d214..2c413cc5 100644 --- a/plexpy/plexivity_import.py +++ b/plexpy/plexivity_import.py @@ -368,6 +368,7 @@ def import_from_plexivity(database_file=None, table_name=None, import_ignore_int 'grandparent_rating_key': extracted_xml['grandparent_rating_key'], 'media_type': extracted_xml['media_type'], 'view_offset': extracted_xml['view_offset'], + 'section_id': extracted_xml['section_id'], 'video_decision': extracted_xml['video_decision'], 'audio_decision': extracted_xml['audio_decision'], 'transcode_decision': extracted_xml['transcode_decision'], @@ -416,7 +417,6 @@ def import_from_plexivity(database_file=None, table_name=None, import_ignore_int 'rating': extracted_xml['rating'], 'duration': extracted_xml['duration'], 'guid': extracted_xml['guid'], - 'section_id': extracted_xml['section_id'], 'directors': extracted_xml['directors'], 'writers': extracted_xml['writers'], 'actors': extracted_xml['actors'], diff --git a/plexpy/plexwatch_import.py b/plexpy/plexwatch_import.py index a942deb7..ac0fd7b0 100644 --- a/plexpy/plexwatch_import.py +++ b/plexpy/plexwatch_import.py @@ -361,6 +361,7 @@ def import_from_plexwatch(database_file=None, table_name=None, import_ignore_int 'grandparent_rating_key': row['grandparent_rating_key'], 'media_type': extracted_xml['media_type'], 'view_offset': extracted_xml['view_offset'], + 'section_id': extracted_xml['section_id'], 'video_decision': extracted_xml['video_decision'], 'audio_decision': extracted_xml['audio_decision'], 'transcode_decision': extracted_xml['transcode_decision'], diff --git a/plexpy/users.py b/plexpy/users.py index 9d546759..5c32d58c 100644 --- a/plexpy/users.py +++ b/plexpy/users.py @@ -413,7 +413,7 @@ class Users(object): join = '' if include_last_seen: last_seen = 'MAX(session_history.started)' - join = 'LEFT OUTER JOIN session_history ON users.user_id == session_history.user_id' + join = 'LEFT OUTER JOIN session_history ON users.user_id = session_history.user_id' monitor_db = database.MonitorDatabase() diff --git a/plexpy/webserve.py b/plexpy/webserve.py index ce9ce6ef..f445c0cb 100644 --- a/plexpy/webserve.py +++ b/plexpy/webserve.py @@ -1953,7 +1953,7 @@ class WebInterface(object): custom_where.append(['session_history.reference_id', reference_id]) if 'section_id' in kwargs: section_id = kwargs.get('section_id', '') - custom_where.append(['session_history_metadata.section_id', section_id]) + custom_where.append(['session_history.section_id', section_id]) if 'media_type' in kwargs: media_type = kwargs.get('media_type', '') if media_type not in ('all', 'live'):