Ability to stop a stream using session key

This commit is contained in:
JonnyWong16 2018-06-05 20:30:07 -07:00
parent c18ee81130
commit 8c0bcd0059
6 changed files with 46 additions and 12 deletions

8
API.md
View file

@ -2624,15 +2624,15 @@ Returns:
### terminate_session
Add a new notification agent.
Stop a streaming session.
```
Required parameters:
session_id (str): The id of the session to terminate
message (str): A custom message to send to the client
session_key (int): The session key of the session to terminate, OR
session_id (str): The session id of the session to terminate
Optional parameters:
None
message (str): A custom message to send to the client
Returns:
None

View file

@ -675,6 +675,7 @@
$.ajax({
url: 'terminate_session',
data: {
session_key: key,
session_id: session_id,
message: message
},

View file

@ -518,7 +518,7 @@ def dbcheck():
# sessions table :: This is a temp table that logs currently active sessions
c_db.execute(
'CREATE TABLE IF NOT EXISTS sessions (id INTEGER PRIMARY KEY AUTOINCREMENT, session_key INTEGER, '
'CREATE TABLE IF NOT EXISTS sessions (id INTEGER PRIMARY KEY AUTOINCREMENT, session_key INTEGER, session_id TEXT, '
'transcode_key TEXT, rating_key INTEGER, section_id INTEGER, media_type TEXT, started INTEGER, stopped INTEGER, '
'paused_counter INTEGER DEFAULT 0, state TEXT, user_id INTEGER, user TEXT, friendly_name TEXT, '
'ip_address TEXT, machine_id TEXT, player TEXT, product TEXT, platform TEXT, title TEXT, parent_title TEXT, '
@ -1081,6 +1081,15 @@ def dbcheck():
'ALTER TABLE sessions ADD COLUMN live_uuid TEXT'
)
# Upgrade sessions table from earlier versions
try:
c_db.execute('SELECT session_id FROM sessions')
except sqlite3.OperationalError:
logger.debug(u"Altering database. Updating database table sessions.")
c_db.execute(
'ALTER TABLE sessions ADD COLUMN session_id TEXT'
)
# Upgrade sessions table from earlier versions
try:
c_db.execute('SELECT original_title FROM sessions')

View file

@ -34,6 +34,7 @@ class ActivityProcessor(object):
def write_session(self, session=None, notify=True):
if session:
values = {'session_key': session.get('session_key', ''),
'session_id': session.get('session_id', ''),
'transcode_key': session.get('transcode_key', ''),
'section_id': session.get('section_id', ''),
'rating_key': session.get('rating_key', ''),
@ -466,6 +467,16 @@ class ActivityProcessor(object):
return None
def get_session_by_id(self, session_id=None):
if session_id:
session = self.db.select_single('SELECT * FROM sessions '
'WHERE session_id = ? ',
args=[session_id])
if session:
return session
return None
def set_session_state(self, session_key=None, state=None, **kwargs):
if str(session_key).isdigit():
values = {}

View file

@ -19,6 +19,7 @@ import time
import urllib
import plexpy
import activity_processor
import common
import helpers
import http_handler
@ -1919,7 +1920,7 @@ class PmsConnect(object):
return session_output
def terminate_session(self, session_id='', message=''):
def terminate_session(self, session_key='', session_id='', message=''):
"""
Terminates a streaming session.
@ -1927,10 +1928,22 @@ class PmsConnect(object):
"""
message = message or 'The server owner has ended the stream.'
if session_key and not session_id:
ap = activity_processor.ActivityProcessor()
session = ap.get_session_by_key(session_key=session_key)
session_id = session['session_id']
elif session_id and not session_key:
ap = activity_processor.ActivityProcessor()
session = ap.get_session_by_id(session_id=session_id)
session_key = session['session_key']
if session_id:
logger.info(u"Tautulli Pmsconnect :: Terminating session %s (session_id %s)." % (session_key, session_id))
result = self.get_sessions_terminate(session_id=session_id, reason=urllib.quote_plus(message))
return result
else:
logger.warn(u"Tautulli Pmsconnect :: Failed to terminate session %s. Missing session_id." % session_key)
return False
def get_item_children(self, rating_key='', get_grandchildren=False):

View file

@ -248,23 +248,23 @@ class WebInterface(object):
@cherrypy.tools.json_out()
@requireAuth(member_of("admin"))
@addtoapi()
def terminate_session(self, session_id=None, message=None, **kwargs):
""" Add a new notification agent.
def terminate_session(self, session_key=None, session_id=None, message=None, **kwargs):
""" Stop a streaming session.
```
Required parameters:
session_id (str): The id of the session to terminate
message (str): A custom message to send to the client
session_key (int): The session key of the session to terminate, OR
session_id (str): The session id of the session to terminate
Optional parameters:
None
message (str): A custom message to send to the client
Returns:
None
```
"""
pms_connect = pmsconnect.PmsConnect()
result = pms_connect.terminate_session(session_id=session_id, message=message)
result = pms_connect.terminate_session(session_key=session_key, session_id=session_id, message=message)
if result:
return {'result': 'success', 'message': 'Session terminated.'}