Fix activity_processor.py

This commit is contained in:
JonnyWong16 2023-03-07 16:55:31 -08:00
commit b6f05087c3
No known key found for this signature in database
GPG key ID: B1F1F9807184697A

View file

@ -331,10 +331,10 @@ class ActivityProcessor(object):
if session['live']: if session['live']:
# Check if we should group the session, select the last guid from the user # Check if we should group the session, select the last guid from the user
query = 'SELECT session_history.id, session_history_metadata.guid, session_history.reference_id ' \ query = "SELECT session_history.id, session_history_metadata.guid, session_history.reference_id " \
'FROM session_history ' \ "FROM session_history " \
'JOIN session_history_metadata ON session_history.id == session_history_metadata.id ' \ "JOIN session_history_metadata ON session_history.id == session_history_metadata.id " \
'WHERE session_history.user_id = ? ORDER BY session_history.id DESC LIMIT 1 ' "WHERE session_history.user_id = ? ORDER BY session_history.id DESC LIMIT 1 "
args = [session['user_id']] args = [session['user_id']]
@ -351,8 +351,8 @@ class ActivityProcessor(object):
else: else:
# Check if we should group the session, select the last two rows from the user # Check if we should group the session, select the last two rows from the user
query = 'SELECT id, rating_key, view_offset, reference_id FROM session_history ' \ query = "SELECT id, rating_key, view_offset, reference_id FROM session_history " \
'WHERE user_id = ? AND rating_key = ? ORDER BY id DESC LIMIT 2 ' "WHERE user_id = ? AND rating_key = ? ORDER BY id DESC LIMIT 2 "
args = [session['user_id'], session['rating_key']] args = [session['user_id'], session['rating_key']]
@ -375,7 +375,7 @@ class ActivityProcessor(object):
marker_first, marker_final marker_first, marker_final
) )
query = 'UPDATE session_history SET reference_id = ? WHERE id = ? ' query = "UPDATE session_history SET reference_id = ? WHERE id = ? "
# If previous session view offset less than watched percent, # If previous session view offset less than watched percent,
# and new session view offset is greater, # and new session view offset is greater,
@ -547,12 +547,12 @@ class ActivityProcessor(object):
return session['id'] return session['id']
def get_sessions(self, user_id=None, ip_address=None): def get_sessions(self, user_id=None, ip_address=None):
query = 'SELECT * FROM sessions' query = "SELECT * FROM sessions"
args = [] args = []
if str(user_id).isdigit(): if str(user_id).isdigit():
ip = ' GROUP BY ip_address' if ip_address else '' ip = " GROUP BY ip_address" if ip_address else ""
query += ' WHERE user_id = ?' + ip query += " WHERE user_id = ?" + ip
args.append(user_id) args.append(user_id)
sessions = self.db.select(query, args) sessions = self.db.select(query, args)
@ -560,8 +560,8 @@ class ActivityProcessor(object):
def get_session_by_key(self, session_key=None): def get_session_by_key(self, session_key=None):
if str(session_key).isdigit(): if str(session_key).isdigit():
session = self.db.select_single('SELECT * FROM sessions ' session = self.db.select_single("SELECT * FROM sessions "
'WHERE session_key = ? ', "WHERE session_key = ? ",
args=[session_key]) args=[session_key])
if session: if session:
return session return session
@ -570,8 +570,8 @@ class ActivityProcessor(object):
def get_session_by_id(self, session_id=None): def get_session_by_id(self, session_id=None):
if session_id: if session_id:
session = self.db.select_single('SELECT * FROM sessions ' session = self.db.select_single("SELECT * FROM sessions "
'WHERE session_id = ? ', "WHERE session_id = ? ",
args=[session_id]) args=[session_id])
if session: if session:
return session return session
@ -597,15 +597,15 @@ class ActivityProcessor(object):
def delete_session(self, session_key=None, row_id=None): def delete_session(self, session_key=None, row_id=None):
if str(session_key).isdigit(): if str(session_key).isdigit():
self.db.action('DELETE FROM sessions WHERE session_key = ?', [session_key]) self.db.action("DELETE FROM sessions WHERE session_key = ?", [session_key])
elif str(row_id).isdigit(): elif str(row_id).isdigit():
self.db.action('DELETE FROM sessions WHERE id = ?', [row_id]) self.db.action("DELETE FROM sessions WHERE id = ?", [row_id])
def set_session_last_paused(self, session_key=None, timestamp=None): def set_session_last_paused(self, session_key=None, timestamp=None):
if str(session_key).isdigit(): if str(session_key).isdigit():
result = self.db.select('SELECT last_paused, paused_counter ' result = self.db.select("SELECT last_paused, paused_counter "
'FROM sessions ' "FROM sessions "
'WHERE session_key = ?', args=[session_key]) "WHERE session_key = ?", args=[session_key])
paused_counter = None paused_counter = None
for session in result: for session in result:
@ -626,15 +626,15 @@ class ActivityProcessor(object):
def increment_session_buffer_count(self, session_key=None): def increment_session_buffer_count(self, session_key=None):
if str(session_key).isdigit(): if str(session_key).isdigit():
self.db.action('UPDATE sessions SET buffer_count = buffer_count + 1 ' self.db.action("UPDATE sessions SET buffer_count = buffer_count + 1 "
'WHERE session_key = ?', "WHERE session_key = ?",
[session_key]) [session_key])
def get_session_buffer_count(self, session_key=None): def get_session_buffer_count(self, session_key=None):
if str(session_key).isdigit(): if str(session_key).isdigit():
buffer_count = self.db.select_single('SELECT buffer_count ' buffer_count = self.db.select_single("SELECT buffer_count "
'FROM sessions ' "FROM sessions "
'WHERE session_key = ?', "WHERE session_key = ?",
[session_key]) [session_key])
if buffer_count: if buffer_count:
return buffer_count['buffer_count'] return buffer_count['buffer_count']
@ -643,15 +643,15 @@ class ActivityProcessor(object):
def set_session_buffer_trigger_time(self, session_key=None): def set_session_buffer_trigger_time(self, session_key=None):
if str(session_key).isdigit(): if str(session_key).isdigit():
self.db.action('UPDATE sessions SET buffer_last_triggered = strftime("%s","now") ' self.db.action("UPDATE sessions SET buffer_last_triggered = strftime('%s', 'now') "
'WHERE session_key = ?', "WHERE session_key = ?",
[session_key]) [session_key])
def get_session_buffer_trigger_time(self, session_key=None): def get_session_buffer_trigger_time(self, session_key=None):
if str(session_key).isdigit(): if str(session_key).isdigit():
last_time = self.db.select_single('SELECT buffer_last_triggered ' last_time = self.db.select_single("SELECT buffer_last_triggered "
'FROM sessions ' "FROM sessions "
'WHERE session_key = ?', "WHERE session_key = ?",
[session_key]) [session_key])
if last_time: if last_time:
return last_time['buffer_last_triggered'] return last_time['buffer_last_triggered']
@ -660,12 +660,12 @@ class ActivityProcessor(object):
def set_temp_stopped(self): def set_temp_stopped(self):
stopped_time = helpers.timestamp() stopped_time = helpers.timestamp()
self.db.action('UPDATE sessions SET stopped = ?', [stopped_time]) self.db.action("UPDATE sessions SET stopped = ?", [stopped_time])
def increment_write_attempts(self, session_key=None): def increment_write_attempts(self, session_key=None):
if str(session_key).isdigit(): if str(session_key).isdigit():
session = self.get_session_by_key(session_key=session_key) session = self.get_session_by_key(session_key=session_key)
self.db.action('UPDATE sessions SET write_attempts = ? WHERE session_key = ?', self.db.action("UPDATE sessions SET write_attempts = ? WHERE session_key = ?",
[session['write_attempts'] + 1, session_key]) [session['write_attempts'] + 1, session_key])
def set_marker(self, session_key=None, marker_idx=None, marker_type=None): def set_marker(self, session_key=None, marker_idx=None, marker_type=None):
@ -674,13 +674,13 @@ class ActivityProcessor(object):
int(marker_type == 'commercial'), int(marker_type == 'commercial'),
int(marker_type == 'credits') int(marker_type == 'credits')
] ]
self.db.action('UPDATE sessions SET intro = ?, commercial = ?, credits = ?, marker = ? ' self.db.action("UPDATE sessions SET intro = ?, commercial = ?, credits = ?, marker = ? "
'WHERE session_key = ?', "WHERE session_key = ?",
marker_args + [marker_idx, session_key]) marker_args + [marker_idx, session_key])
def set_watched(self, session_key=None): def set_watched(self, session_key=None):
self.db.action('UPDATE sessions SET watched = ? ' self.db.action("UPDATE sessions SET watched = ? "
'WHERE session_key = ?', "WHERE session_key = ?",
[1, session_key]) [1, session_key])
def write_continued_session(self, user_id=None, machine_id=None, media_type=None, stopped=None): def write_continued_session(self, user_id=None, machine_id=None, media_type=None, stopped=None):
@ -689,9 +689,9 @@ class ActivityProcessor(object):
self.db.upsert(table_name='sessions_continued', key_dict=keys, value_dict=values) self.db.upsert(table_name='sessions_continued', key_dict=keys, value_dict=values)
def is_initial_stream(self, user_id=None, machine_id=None, media_type=None, started=None): def is_initial_stream(self, user_id=None, machine_id=None, media_type=None, started=None):
last_session = self.db.select_single('SELECT stopped ' last_session = self.db.select_single("SELECT stopped "
'FROM sessions_continued ' "FROM sessions_continued "
'WHERE user_id = ? AND machine_id = ? AND media_type = ? ' "WHERE user_id = ? AND machine_id = ? AND media_type = ? "
'ORDER BY stopped DESC', "ORDER BY stopped DESC",
[user_id, machine_id, media_type]) [user_id, machine_id, media_type])
return int(started - last_session.get('stopped', 0) >= plexpy.CONFIG.NOTIFY_CONTINUED_SESSION_THRESHOLD) return int(started - last_session.get('stopped', 0) >= plexpy.CONFIG.NOTIFY_CONTINUED_SESSION_THRESHOLD)