mirror of
https://github.com/Tautulli/Tautulli.git
synced 2025-08-19 21:03:21 -07:00
Fix merge conflicts
This commit is contained in:
commit
82b567f15f
50 changed files with 9327 additions and 277 deletions
|
@ -17,16 +17,21 @@ import os
|
|||
import sys
|
||||
import subprocess
|
||||
import threading
|
||||
import webbrowser
|
||||
import sqlite3
|
||||
import cherrypy
|
||||
import datetime
|
||||
import uuid
|
||||
# Some cut down versions of Python may not include this module and it's not critical for us
|
||||
try:
|
||||
import webbrowser
|
||||
no_browser = False
|
||||
except ImportError:
|
||||
no_browser = True
|
||||
|
||||
from apscheduler.schedulers.background import BackgroundScheduler
|
||||
from apscheduler.triggers.interval import IntervalTrigger
|
||||
|
||||
from plexpy import versioncheck, logger, monitor, plextv
|
||||
from plexpy import versioncheck, logger, activity_pinger, plextv
|
||||
import plexpy.config
|
||||
|
||||
PROG_DIR = None
|
||||
|
@ -66,6 +71,7 @@ COMMITS_BEHIND = None
|
|||
|
||||
UMASK = None
|
||||
|
||||
POLLING_FAILOVER = False
|
||||
|
||||
def initialize(config_file):
|
||||
with INIT_LOCK:
|
||||
|
@ -75,6 +81,7 @@ def initialize(config_file):
|
|||
global CURRENT_VERSION
|
||||
global LATEST_VERSION
|
||||
global UMASK
|
||||
global POLLING_FAILOVER
|
||||
|
||||
CONFIG = plexpy.config.Config(config_file)
|
||||
|
||||
|
@ -228,18 +235,19 @@ def daemonize():
|
|||
|
||||
|
||||
def launch_browser(host, port, root):
|
||||
if host == '0.0.0.0':
|
||||
host = 'localhost'
|
||||
if not no_browser:
|
||||
if host == '0.0.0.0':
|
||||
host = 'localhost'
|
||||
|
||||
if CONFIG.ENABLE_HTTPS:
|
||||
protocol = 'https'
|
||||
else:
|
||||
protocol = 'http'
|
||||
if CONFIG.ENABLE_HTTPS:
|
||||
protocol = 'https'
|
||||
else:
|
||||
protocol = 'http'
|
||||
|
||||
try:
|
||||
webbrowser.open('%s://%s:%i%s' % (protocol, host, port, root))
|
||||
except Exception as e:
|
||||
logger.error('Could not launch browser: %s', e)
|
||||
try:
|
||||
webbrowser.open('%s://%s:%i%s' % (protocol, host, port, root))
|
||||
except Exception as e:
|
||||
logger.error('Could not launch browser: %s', e)
|
||||
|
||||
|
||||
def initialize_scheduler():
|
||||
|
@ -273,7 +281,11 @@ def initialize_scheduler():
|
|||
|
||||
if CONFIG.PMS_IP and CONFIG.PMS_TOKEN:
|
||||
schedule_job(plextv.get_real_pms_url, 'Refresh Plex Server URLs', hours=12, minutes=0, seconds=0)
|
||||
schedule_job(monitor.check_active_sessions, 'Check for active sessions', hours=0, minutes=0, seconds=seconds)
|
||||
|
||||
# If we're not using websockets then fall back to polling
|
||||
if not CONFIG.MONITORING_USE_WEBSOCKET or POLLING_FAILOVER:
|
||||
schedule_job(activity_pinger.check_active_sessions, 'Check for active sessions',
|
||||
hours=0, minutes=0, seconds=seconds)
|
||||
|
||||
# Refresh the users list
|
||||
if CONFIG.REFRESH_USERS_INTERVAL:
|
||||
|
@ -349,7 +361,7 @@ def dbcheck():
|
|||
'audio_channels INTEGER, transcode_protocol TEXT, transcode_container TEXT, '
|
||||
'transcode_video_codec TEXT, transcode_audio_codec TEXT, transcode_audio_channels INTEGER,'
|
||||
'transcode_width INTEGER, transcode_height INTEGER, buffer_count INTEGER DEFAULT 0, '
|
||||
'buffer_last_triggered INTEGER)'
|
||||
'buffer_last_triggered INTEGER, last_paused INTEGER)'
|
||||
)
|
||||
|
||||
# session_history table :: This is a history table which logs essential stream details
|
||||
|
@ -597,6 +609,15 @@ def dbcheck():
|
|||
'ALTER TABLE users ADD COLUMN custom_avatar_url TEXT'
|
||||
)
|
||||
|
||||
# Upgrade sessions table from earlier versions
|
||||
try:
|
||||
c_db.execute('SELECT last_paused from sessions')
|
||||
except sqlite3.OperationalError:
|
||||
logger.debug(u"Altering database. Updating database table sessions.")
|
||||
c_db.execute(
|
||||
'ALTER TABLE sessions ADD COLUMN last_paused INTEGER'
|
||||
)
|
||||
|
||||
# Add "Local" user to database as default unauthenticated user.
|
||||
result = c_db.execute('SELECT id FROM users WHERE username = "Local"')
|
||||
if not result.fetchone():
|
||||
|
@ -633,10 +654,6 @@ def shutdown(restart=False, update=False):
|
|||
cherrypy.engine.exit()
|
||||
SCHED.shutdown(wait=False)
|
||||
|
||||
# Clear any sessions in the db - Not sure yet if we should do this. More testing required
|
||||
# logger.debug(u'Clearing Plex sessions.')
|
||||
# monitor.drop_session_db()
|
||||
|
||||
CONFIG.write()
|
||||
|
||||
if not restart and not update:
|
||||
|
|
215
plexpy/activity_handler.py
Normal file
215
plexpy/activity_handler.py
Normal file
|
@ -0,0 +1,215 @@
|
|||
# This file is part of PlexPy.
|
||||
#
|
||||
# PlexPy is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# PlexPy is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with PlexPy. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import time
|
||||
import plexpy
|
||||
|
||||
from plexpy import logger, pmsconnect, activity_processor, threading, notification_handler
|
||||
|
||||
|
||||
class ActivityHandler(object):
|
||||
|
||||
def __init__(self, timeline):
|
||||
self.timeline = timeline
|
||||
# print timeline
|
||||
|
||||
def is_valid_session(self):
|
||||
if 'sessionKey' in self.timeline:
|
||||
if str(self.timeline['sessionKey']).isdigit():
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def get_session_key(self):
|
||||
if self.is_valid_session():
|
||||
return int(self.timeline['sessionKey'])
|
||||
|
||||
return None
|
||||
|
||||
def get_live_session(self):
|
||||
pms_connect = pmsconnect.PmsConnect()
|
||||
session_list = pms_connect.get_current_activity()
|
||||
|
||||
for session in session_list['sessions']:
|
||||
if int(session['session_key']) == self.get_session_key():
|
||||
return session
|
||||
|
||||
return None
|
||||
|
||||
def update_db_session(self):
|
||||
# Update our session temp table values
|
||||
monitor_proc = activity_processor.ActivityProcessor()
|
||||
monitor_proc.write_session(session=self.get_live_session(), notify=False)
|
||||
|
||||
def on_start(self):
|
||||
if self.is_valid_session():
|
||||
logger.debug(u"PlexPy ActivityHandler :: Session %s has started." % str(self.get_session_key()))
|
||||
|
||||
# Fire off notifications
|
||||
threading.Thread(target=notification_handler.notify,
|
||||
kwargs=dict(stream_data=self.get_live_session(), notify_action='play')).start()
|
||||
|
||||
# Write the new session to our temp session table
|
||||
self.update_db_session()
|
||||
|
||||
def on_stop(self, force_stop=False):
|
||||
if self.is_valid_session():
|
||||
logger.debug(u"PlexPy ActivityHandler :: Session %s has stopped." % str(self.get_session_key()))
|
||||
|
||||
# Set the session last_paused timestamp
|
||||
ap = activity_processor.ActivityProcessor()
|
||||
ap.set_session_last_paused(session_key=self.get_session_key(), timestamp=None)
|
||||
|
||||
# Update the session state and viewOffset
|
||||
# Set force_stop to true to disable the state set
|
||||
if not force_stop:
|
||||
ap.set_session_state(session_key=self.get_session_key(),
|
||||
state=self.timeline['state'],
|
||||
view_offset=self.timeline['viewOffset'])
|
||||
|
||||
# Retrieve the session data from our temp table
|
||||
db_session = ap.get_session_by_key(session_key=self.get_session_key())
|
||||
|
||||
# Fire off notifications
|
||||
threading.Thread(target=notification_handler.notify,
|
||||
kwargs=dict(stream_data=db_session, notify_action='stop')).start()
|
||||
|
||||
# Write it to the history table
|
||||
monitor_proc = activity_processor.ActivityProcessor()
|
||||
monitor_proc.write_session_history(session=db_session)
|
||||
|
||||
# Remove the session from our temp session table
|
||||
ap.delete_session(session_key=self.get_session_key())
|
||||
|
||||
def on_pause(self):
|
||||
if self.is_valid_session():
|
||||
logger.debug(u"PlexPy ActivityHandler :: Session %s has been paused." % str(self.get_session_key()))
|
||||
|
||||
# Set the session last_paused timestamp
|
||||
ap = activity_processor.ActivityProcessor()
|
||||
ap.set_session_last_paused(session_key=self.get_session_key(), timestamp=int(time.time()))
|
||||
|
||||
# Update the session state and viewOffset
|
||||
ap.set_session_state(session_key=self.get_session_key(),
|
||||
state=self.timeline['state'],
|
||||
view_offset=self.timeline['viewOffset'])
|
||||
|
||||
# Retrieve the session data from our temp table
|
||||
db_session = ap.get_session_by_key(session_key=self.get_session_key())
|
||||
|
||||
# Fire off notifications
|
||||
threading.Thread(target=notification_handler.notify,
|
||||
kwargs=dict(stream_data=db_session, notify_action='pause')).start()
|
||||
|
||||
def on_resume(self):
|
||||
if self.is_valid_session():
|
||||
logger.debug(u"PlexPy ActivityHandler :: Session %s has been resumed." % str(self.get_session_key()))
|
||||
|
||||
# Set the session last_paused timestamp
|
||||
ap = activity_processor.ActivityProcessor()
|
||||
ap.set_session_last_paused(session_key=self.get_session_key(), timestamp=None)
|
||||
|
||||
# Update the session state and viewOffset
|
||||
ap.set_session_state(session_key=self.get_session_key(),
|
||||
state=self.timeline['state'],
|
||||
view_offset=self.timeline['viewOffset'])
|
||||
|
||||
# Retrieve the session data from our temp table
|
||||
db_session = ap.get_session_by_key(session_key=self.get_session_key())
|
||||
|
||||
# Fire off notifications
|
||||
threading.Thread(target=notification_handler.notify,
|
||||
kwargs=dict(stream_data=db_session, notify_action='resume')).start()
|
||||
|
||||
def on_buffer(self):
|
||||
if self.is_valid_session():
|
||||
logger.debug(u"PlexPy ActivityHandler :: Session %s is buffering." % self.get_session_key())
|
||||
ap = activity_processor.ActivityProcessor()
|
||||
db_stream = ap.get_session_by_key(session_key=self.get_session_key())
|
||||
|
||||
# Increment our buffer count
|
||||
ap.increment_session_buffer_count(session_key=self.get_session_key())
|
||||
|
||||
# Get our current buffer count
|
||||
current_buffer_count = ap.get_session_buffer_count(self.get_session_key())
|
||||
logger.debug(u"PlexPy ActivityHandler :: Session %s buffer count is %s." %
|
||||
(self.get_session_key(), current_buffer_count))
|
||||
|
||||
# Get our last triggered time
|
||||
buffer_last_triggered = ap.get_session_buffer_trigger_time(self.get_session_key())
|
||||
|
||||
time_since_last_trigger = 0
|
||||
if buffer_last_triggered:
|
||||
logger.debug(u"PlexPy ActivityHandler :: Session %s buffer last triggered at %s." %
|
||||
(self.get_session_key(), buffer_last_triggered))
|
||||
time_since_last_trigger = int(time.time()) - int(buffer_last_triggered)
|
||||
|
||||
if current_buffer_count >= plexpy.CONFIG.BUFFER_THRESHOLD and time_since_last_trigger == 0 or \
|
||||
time_since_last_trigger >= plexpy.CONFIG.BUFFER_WAIT:
|
||||
ap.set_session_buffer_trigger_time(session_key=self.get_session_key())
|
||||
threading.Thread(target=notification_handler.notify,
|
||||
kwargs=dict(stream_data=db_stream, notify_action='buffer')).start()
|
||||
|
||||
# This function receives events from our websocket connection
|
||||
def process(self):
|
||||
if self.is_valid_session():
|
||||
from plexpy import helpers
|
||||
|
||||
ap = activity_processor.ActivityProcessor()
|
||||
db_session = ap.get_session_by_key(session_key=self.get_session_key())
|
||||
|
||||
this_state = self.timeline['state']
|
||||
this_key = str(self.timeline['ratingKey'])
|
||||
|
||||
# If we already have this session in the temp table, check for state changes
|
||||
if db_session:
|
||||
last_state = db_session['state']
|
||||
last_key = str(db_session['rating_key'])
|
||||
|
||||
# Make sure the same item is being played
|
||||
if this_key == last_key:
|
||||
# Update the session state and viewOffset
|
||||
if this_state == 'playing':
|
||||
ap.set_session_state(session_key=self.get_session_key(),
|
||||
state=this_state,
|
||||
view_offset=self.timeline['viewOffset'])
|
||||
# Start our state checks
|
||||
if this_state != last_state:
|
||||
if this_state == 'paused':
|
||||
self.on_pause()
|
||||
elif last_state == 'paused' and this_state == 'playing':
|
||||
self.on_resume()
|
||||
elif this_state == 'stopped':
|
||||
self.on_stop()
|
||||
elif this_state == 'buffering':
|
||||
self.on_buffer()
|
||||
# If a client doesn't register stop events (I'm looking at you PHT!) check if the ratingKey has changed
|
||||
else:
|
||||
# Manually stop and start
|
||||
# Set force_stop so that we don't overwrite our last viewOffset
|
||||
self.on_stop(force_stop=True)
|
||||
self.on_start()
|
||||
|
||||
# Monitor if the stream has reached the watch percentage for notifications
|
||||
# The only purpose of this is for notifications
|
||||
progress_percent = helpers.get_percent(self.timeline['viewOffset'], db_session['duration'])
|
||||
if progress_percent >= plexpy.CONFIG.NOTIFY_WATCHED_PERCENT and this_state != 'buffering':
|
||||
threading.Thread(target=notification_handler.notify,
|
||||
kwargs=dict(stream_data=db_session, notify_action='watched')).start()
|
||||
|
||||
else:
|
||||
# We don't have this session in our table yet, start a new one.
|
||||
if this_state != 'buffering':
|
||||
self.on_start()
|
164
plexpy/activity_pinger.py
Normal file
164
plexpy/activity_pinger.py
Normal file
|
@ -0,0 +1,164 @@
|
|||
# This file is part of PlexPy.
|
||||
#
|
||||
# PlexPy is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# PlexPy is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with PlexPy. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from plexpy import logger, pmsconnect, notification_handler, database, helpers, activity_processor
|
||||
|
||||
import threading
|
||||
import plexpy
|
||||
import time
|
||||
|
||||
monitor_lock = threading.Lock()
|
||||
|
||||
|
||||
def check_active_sessions(ws_request=False):
|
||||
|
||||
with monitor_lock:
|
||||
pms_connect = pmsconnect.PmsConnect()
|
||||
session_list = pms_connect.get_current_activity()
|
||||
monitor_db = database.MonitorDatabase()
|
||||
monitor_process = activity_processor.ActivityProcessor()
|
||||
# logger.debug(u"PlexPy Monitor :: Checking for active streams.")
|
||||
|
||||
if session_list:
|
||||
media_container = session_list['sessions']
|
||||
|
||||
# Check our temp table for what we must do with the new streams
|
||||
db_streams = monitor_db.select('SELECT started, session_key, rating_key, media_type, title, parent_title, '
|
||||
'grandparent_title, user_id, user, friendly_name, ip_address, player, '
|
||||
'platform, machine_id, parent_rating_key, grandparent_rating_key, state, '
|
||||
'view_offset, duration, video_decision, audio_decision, width, height, '
|
||||
'container, video_codec, audio_codec, bitrate, video_resolution, '
|
||||
'video_framerate, aspect_ratio, audio_channels, transcode_protocol, '
|
||||
'transcode_container, transcode_video_codec, transcode_audio_codec, '
|
||||
'transcode_audio_channels, transcode_width, transcode_height, '
|
||||
'paused_counter, last_paused '
|
||||
'FROM sessions')
|
||||
for stream in db_streams:
|
||||
if any(d['session_key'] == str(stream['session_key']) and d['rating_key'] == str(stream['rating_key'])
|
||||
for d in media_container):
|
||||
# The user's session is still active
|
||||
for session in media_container:
|
||||
if session['session_key'] == str(stream['session_key']) and \
|
||||
session['rating_key'] == str(stream['rating_key']):
|
||||
# The user is still playing the same media item
|
||||
# Here we can check the play states
|
||||
if session['state'] != stream['state']:
|
||||
if session['state'] == 'paused':
|
||||
# Push any notifications -
|
||||
# Push it on it's own thread so we don't hold up our db actions
|
||||
threading.Thread(target=notification_handler.notify,
|
||||
kwargs=dict(stream_data=stream, notify_action='pause')).start()
|
||||
|
||||
if session['state'] == 'playing' and stream['state'] == 'paused':
|
||||
# Push any notifications -
|
||||
# Push it on it's own thread so we don't hold up our db actions
|
||||
threading.Thread(target=notification_handler.notify,
|
||||
kwargs=dict(stream_data=stream, notify_action='resume')).start()
|
||||
|
||||
if stream['state'] == 'paused' and not ws_request:
|
||||
# The stream is still paused so we need to increment the paused_counter
|
||||
# Using the set config parameter as the interval, probably not the most accurate but
|
||||
# it will have to do for now. If it's a websocket request don't use this method.
|
||||
paused_counter = int(stream['paused_counter']) + plexpy.CONFIG.MONITORING_INTERVAL
|
||||
monitor_db.action('UPDATE sessions SET paused_counter = ? '
|
||||
'WHERE session_key = ? AND rating_key = ?',
|
||||
[paused_counter, stream['session_key'], stream['rating_key']])
|
||||
|
||||
if session['state'] == 'buffering' and plexpy.CONFIG.BUFFER_THRESHOLD > 0:
|
||||
# The stream is buffering so we need to increment the buffer_count
|
||||
# We're going just increment on every monitor ping,
|
||||
# would be difficult to keep track otherwise
|
||||
monitor_db.action('UPDATE sessions SET buffer_count = buffer_count + 1 '
|
||||
'WHERE session_key = ? AND rating_key = ?',
|
||||
[stream['session_key'], stream['rating_key']])
|
||||
|
||||
# Check the current buffer count and last buffer to determine if we should notify
|
||||
buffer_values = monitor_db.select('SELECT buffer_count, buffer_last_triggered '
|
||||
'FROM sessions '
|
||||
'WHERE session_key = ? AND rating_key = ?',
|
||||
[stream['session_key'], stream['rating_key']])
|
||||
|
||||
if buffer_values[0]['buffer_count'] >= plexpy.CONFIG.BUFFER_THRESHOLD:
|
||||
# Push any notifications -
|
||||
# Push it on it's own thread so we don't hold up our db actions
|
||||
# Our first buffer notification
|
||||
if buffer_values[0]['buffer_count'] == plexpy.CONFIG.BUFFER_THRESHOLD:
|
||||
logger.info(u"PlexPy Monitor :: User '%s' has triggered a buffer warning."
|
||||
% stream['user'])
|
||||
# Set the buffer trigger time
|
||||
monitor_db.action('UPDATE sessions '
|
||||
'SET buffer_last_triggered = strftime("%s","now") '
|
||||
'WHERE session_key = ? AND rating_key = ?',
|
||||
[stream['session_key'], stream['rating_key']])
|
||||
|
||||
threading.Thread(target=notification_handler.notify,
|
||||
kwargs=dict(stream_data=stream, notify_action='buffer')).start()
|
||||
else:
|
||||
# Subsequent buffer notifications after wait time
|
||||
if int(time.time()) > buffer_values[0]['buffer_last_triggered'] + \
|
||||
plexpy.CONFIG.BUFFER_WAIT:
|
||||
logger.info(u"PlexPy Monitor :: User '%s' has triggered multiple buffer warnings."
|
||||
% stream['user'])
|
||||
# Set the buffer trigger time
|
||||
monitor_db.action('UPDATE sessions '
|
||||
'SET buffer_last_triggered = strftime("%s","now") '
|
||||
'WHERE session_key = ? AND rating_key = ?',
|
||||
[stream['session_key'], stream['rating_key']])
|
||||
|
||||
threading.Thread(target=notification_handler.notify,
|
||||
kwargs=dict(stream_data=stream, notify_action='buffer')).start()
|
||||
|
||||
logger.debug(u"PlexPy Monitor :: Stream buffering. Count is now %s. Last triggered %s."
|
||||
% (buffer_values[0][0], buffer_values[0][1]))
|
||||
|
||||
# Check if the user has reached the offset in the media we defined as the "watched" percent
|
||||
# Don't trigger if state is buffer as some clients push the progress to the end when
|
||||
# buffering on start.
|
||||
if session['view_offset'] and session['duration'] and session['state'] != 'buffering':
|
||||
if helpers.get_percent(session['view_offset'],
|
||||
session['duration']) > plexpy.CONFIG.NOTIFY_WATCHED_PERCENT:
|
||||
# Push any notifications -
|
||||
# Push it on it's own thread so we don't hold up our db actions
|
||||
threading.Thread(target=notification_handler.notify,
|
||||
kwargs=dict(stream_data=stream, notify_action='watched')).start()
|
||||
|
||||
else:
|
||||
# The user has stopped playing a stream
|
||||
logger.debug(u"PlexPy Monitor :: Removing sessionKey %s ratingKey %s from session queue"
|
||||
% (stream['session_key'], stream['rating_key']))
|
||||
monitor_db.action('DELETE FROM sessions WHERE session_key = ? AND rating_key = ?',
|
||||
[stream['session_key'], stream['rating_key']])
|
||||
|
||||
# Check if the user has reached the offset in the media we defined as the "watched" percent
|
||||
if stream['view_offset'] and stream['duration']:
|
||||
if helpers.get_percent(stream['view_offset'],
|
||||
stream['duration']) > plexpy.CONFIG.NOTIFY_WATCHED_PERCENT:
|
||||
# Push any notifications -
|
||||
# Push it on it's own thread so we don't hold up our db actions
|
||||
threading.Thread(target=notification_handler.notify,
|
||||
kwargs=dict(stream_data=stream, notify_action='watched')).start()
|
||||
|
||||
# Push any notifications - Push it on it's own thread so we don't hold up our db actions
|
||||
threading.Thread(target=notification_handler.notify,
|
||||
kwargs=dict(stream_data=stream, notify_action='stop')).start()
|
||||
|
||||
# Write the item history on playback stop
|
||||
monitor_process.write_session_history(session=stream)
|
||||
|
||||
# Process the newly received session data
|
||||
for session in media_container:
|
||||
monitor_process.write_session(session)
|
||||
else:
|
||||
logger.debug(u"PlexPy Monitor :: Unable to read session list.")
|
364
plexpy/activity_processor.py
Normal file
364
plexpy/activity_processor.py
Normal file
|
@ -0,0 +1,364 @@
|
|||
# This file is part of PlexPy.
|
||||
#
|
||||
# PlexPy is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# PlexPy is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with PlexPy. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from plexpy import logger, pmsconnect, notification_handler, log_reader, database
|
||||
|
||||
import threading
|
||||
import plexpy
|
||||
import re
|
||||
import time
|
||||
|
||||
|
||||
class ActivityProcessor(object):
|
||||
|
||||
def __init__(self):
|
||||
self.db = database.MonitorDatabase()
|
||||
|
||||
def write_session(self, session=None, notify=True):
|
||||
if session:
|
||||
values = {'session_key': session['session_key'],
|
||||
'rating_key': session['rating_key'],
|
||||
'media_type': session['media_type'],
|
||||
'state': session['state'],
|
||||
'user_id': session['user_id'],
|
||||
'user': session['user'],
|
||||
'machine_id': session['machine_id'],
|
||||
'title': session['title'],
|
||||
'parent_title': session['parent_title'],
|
||||
'grandparent_title': session['grandparent_title'],
|
||||
'friendly_name': session['friendly_name'],
|
||||
'player': session['player'],
|
||||
'platform': session['platform'],
|
||||
'parent_rating_key': session['parent_rating_key'],
|
||||
'grandparent_rating_key': session['grandparent_rating_key'],
|
||||
'view_offset': session['view_offset'],
|
||||
'duration': session['duration'],
|
||||
'video_decision': session['video_decision'],
|
||||
'audio_decision': session['audio_decision'],
|
||||
'width': session['width'],
|
||||
'height': session['height'],
|
||||
'container': session['container'],
|
||||
'video_codec': session['video_codec'],
|
||||
'audio_codec': session['audio_codec'],
|
||||
'bitrate': session['bitrate'],
|
||||
'video_resolution': session['video_resolution'],
|
||||
'video_framerate': session['video_framerate'],
|
||||
'aspect_ratio': session['aspect_ratio'],
|
||||
'audio_channels': session['audio_channels'],
|
||||
'transcode_protocol': session['transcode_protocol'],
|
||||
'transcode_container': session['transcode_container'],
|
||||
'transcode_video_codec': session['transcode_video_codec'],
|
||||
'transcode_audio_codec': session['transcode_audio_codec'],
|
||||
'transcode_audio_channels': session['transcode_audio_channels'],
|
||||
'transcode_width': session['transcode_width'],
|
||||
'transcode_height': session['transcode_height']
|
||||
}
|
||||
|
||||
keys = {'session_key': session['session_key'],
|
||||
'rating_key': session['rating_key']}
|
||||
|
||||
result = self.db.upsert('sessions', values, keys)
|
||||
|
||||
if result == 'insert':
|
||||
# Push any notifications - Push it on it's own thread so we don't hold up our db actions
|
||||
if notify:
|
||||
threading.Thread(target=notification_handler.notify,
|
||||
kwargs=dict(stream_data=values, notify_action='play')).start()
|
||||
|
||||
started = int(time.time())
|
||||
|
||||
# Try and grab IP address from logs
|
||||
if plexpy.CONFIG.IP_LOGGING_ENABLE and plexpy.CONFIG.PMS_LOGS_FOLDER:
|
||||
ip_address = self.find_session_ip(rating_key=session['rating_key'],
|
||||
machine_id=session['machine_id'])
|
||||
else:
|
||||
ip_address = None
|
||||
|
||||
timestamp = {'started': started,
|
||||
'ip_address': ip_address}
|
||||
|
||||
# If it's our first write then time stamp it.
|
||||
self.db.upsert('sessions', timestamp, keys)
|
||||
|
||||
def write_session_history(self, session=None, import_metadata=None, is_import=False, import_ignore_interval=0):
|
||||
from plexpy import users
|
||||
|
||||
user_data = users.Users()
|
||||
user_details = user_data.get_user_friendly_name(user=session['user'])
|
||||
|
||||
if session:
|
||||
logging_enabled = False
|
||||
|
||||
if is_import:
|
||||
if str(session['stopped']).isdigit():
|
||||
stopped = int(session['stopped'])
|
||||
else:
|
||||
stopped = int(time.time())
|
||||
else:
|
||||
stopped = int(time.time())
|
||||
|
||||
if plexpy.CONFIG.VIDEO_LOGGING_ENABLE and str(session['rating_key']).isdigit() and \
|
||||
(session['media_type'] == 'movie' or session['media_type'] == 'episode'):
|
||||
logging_enabled = True
|
||||
elif plexpy.CONFIG.MUSIC_LOGGING_ENABLE and str(session['rating_key']).isdigit() and \
|
||||
session['media_type'] == 'track':
|
||||
logging_enabled = True
|
||||
else:
|
||||
logger.debug(u"PlexPy ActivityProcessor :: ratingKey %s not logged. Does not meet logging criteria. "
|
||||
u"Media type is '%s'" % (session['rating_key'], session['media_type']))
|
||||
|
||||
if str(session['paused_counter']).isdigit():
|
||||
real_play_time = stopped - session['started'] - int(session['paused_counter'])
|
||||
else:
|
||||
real_play_time = stopped - session['started']
|
||||
|
||||
if plexpy.CONFIG.LOGGING_IGNORE_INTERVAL and not is_import:
|
||||
if (session['media_type'] == 'movie' or session['media_type'] == 'episode') and \
|
||||
(real_play_time < int(plexpy.CONFIG.LOGGING_IGNORE_INTERVAL)):
|
||||
logging_enabled = False
|
||||
logger.debug(u"PlexPy ActivityProcessor :: Play duration for ratingKey %s is %s secs which is less than %s "
|
||||
u"seconds, so we're not logging it." %
|
||||
(session['rating_key'], str(real_play_time), plexpy.CONFIG.LOGGING_IGNORE_INTERVAL))
|
||||
elif is_import and import_ignore_interval:
|
||||
if (session['media_type'] == 'movie' or session['media_type'] == 'episode') and \
|
||||
(real_play_time < int(import_ignore_interval)):
|
||||
logging_enabled = False
|
||||
logger.debug(u"PlexPy ActivityProcessor :: Play duration for ratingKey %s is %s secs which is less than %s "
|
||||
u"seconds, so we're not logging it." %
|
||||
(session['rating_key'], str(real_play_time),
|
||||
import_ignore_interval))
|
||||
|
||||
if not user_details['keep_history'] and not is_import:
|
||||
logging_enabled = False
|
||||
logger.debug(u"PlexPy ActivityProcessor :: History logging for user '%s' is disabled." % session['user'])
|
||||
|
||||
if logging_enabled:
|
||||
# logger.debug(u"PlexPy ActivityProcessor :: Attempting to write to session_history table...")
|
||||
query = 'INSERT INTO session_history (started, stopped, rating_key, parent_rating_key, ' \
|
||||
'grandparent_rating_key, media_type, user_id, user, ip_address, paused_counter, player, ' \
|
||||
'platform, machine_id, view_offset) VALUES ' \
|
||||
'(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'
|
||||
|
||||
args = [session['started'], stopped, session['rating_key'], session['parent_rating_key'],
|
||||
session['grandparent_rating_key'], session['media_type'], session['user_id'], session['user'],
|
||||
session['ip_address'], session['paused_counter'], session['player'], session['platform'],
|
||||
session['machine_id'], session['view_offset']]
|
||||
|
||||
# logger.debug(u"PlexPy ActivityProcessor :: Writing session_history transaction...")
|
||||
self.db.action(query=query, args=args)
|
||||
|
||||
# logger.debug(u"PlexPy ActivityProcessor :: Successfully written history item, last id for session_history is %s"
|
||||
# % last_id)
|
||||
|
||||
# Write the session_history_media_info table
|
||||
# logger.debug(u"PlexPy ActivityProcessor :: Attempting to write to session_history_media_info table...")
|
||||
query = 'INSERT INTO session_history_media_info (id, rating_key, video_decision, audio_decision, ' \
|
||||
'duration, width, height, container, video_codec, audio_codec, bitrate, video_resolution, ' \
|
||||
'video_framerate, aspect_ratio, audio_channels, transcode_protocol, transcode_container, ' \
|
||||
'transcode_video_codec, transcode_audio_codec, transcode_audio_channels, transcode_width, ' \
|
||||
'transcode_height) VALUES ' \
|
||||
'(last_insert_rowid(), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'
|
||||
|
||||
args = [session['rating_key'], session['video_decision'], session['audio_decision'],
|
||||
session['duration'], session['width'], session['height'], session['container'],
|
||||
session['video_codec'], session['audio_codec'], session['bitrate'],
|
||||
session['video_resolution'], session['video_framerate'], session['aspect_ratio'],
|
||||
session['audio_channels'], session['transcode_protocol'], session['transcode_container'],
|
||||
session['transcode_video_codec'], session['transcode_audio_codec'],
|
||||
session['transcode_audio_channels'], session['transcode_width'], session['transcode_height']]
|
||||
|
||||
# logger.debug(u"PlexPy ActivityProcessor :: Writing session_history_media_info transaction...")
|
||||
self.db.action(query=query, args=args)
|
||||
|
||||
if not is_import:
|
||||
logger.debug(u"PlexPy ActivityProcessor :: Fetching metadata for item ratingKey %s" % session['rating_key'])
|
||||
pms_connect = pmsconnect.PmsConnect()
|
||||
result = pms_connect.get_metadata_details(rating_key=str(session['rating_key']))
|
||||
metadata = result['metadata']
|
||||
else:
|
||||
metadata = import_metadata
|
||||
|
||||
# Write the session_history_metadata table
|
||||
directors = ";".join(metadata['directors'])
|
||||
writers = ";".join(metadata['writers'])
|
||||
actors = ";".join(metadata['actors'])
|
||||
genres = ";".join(metadata['genres'])
|
||||
|
||||
# Build media item title
|
||||
if session['media_type'] == 'episode' or session['media_type'] == 'track':
|
||||
full_title = '%s - %s' % (metadata['grandparent_title'], metadata['title'])
|
||||
elif session['media_type'] == 'movie':
|
||||
full_title = metadata['title']
|
||||
else:
|
||||
full_title = metadata['title']
|
||||
|
||||
# logger.debug(u"PlexPy ActivityProcessor :: Attempting to write to session_history_metadata table...")
|
||||
query = 'INSERT INTO session_history_metadata (id, rating_key, parent_rating_key, ' \
|
||||
'grandparent_rating_key, title, parent_title, grandparent_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) VALUES ' \
|
||||
'(last_insert_rowid(), ' \
|
||||
'?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'
|
||||
|
||||
args = [session['rating_key'], session['parent_rating_key'], session['grandparent_rating_key'],
|
||||
session['title'], session['parent_title'], session['grandparent_title'], full_title,
|
||||
metadata['index'], metadata['parent_index'], metadata['thumb'], metadata['parent_thumb'],
|
||||
metadata['grandparent_thumb'], metadata['art'], session['media_type'], metadata['year'],
|
||||
metadata['originally_available_at'], metadata['added_at'], metadata['updated_at'],
|
||||
metadata['last_viewed_at'], metadata['content_rating'], metadata['summary'], metadata['tagline'],
|
||||
metadata['rating'], metadata['duration'], metadata['guid'], directors, writers, actors, genres, metadata['studio']]
|
||||
|
||||
# logger.debug(u"PlexPy ActivityProcessor :: Writing session_history_metadata transaction...")
|
||||
self.db.action(query=query, args=args)
|
||||
|
||||
def find_session_ip(self, rating_key=None, machine_id=None):
|
||||
|
||||
logger.debug(u"PlexPy ActivityProcessor :: Requesting log lines...")
|
||||
log_lines = log_reader.get_log_tail(window=5000, parsed=False)
|
||||
|
||||
rating_key_line = 'ratingKey=' + rating_key
|
||||
rating_key_line_2 = 'metadata%2F' + rating_key
|
||||
machine_id_line = 'session=' + machine_id
|
||||
|
||||
for line in reversed(log_lines):
|
||||
# We're good if we find a line with both machine id and rating key
|
||||
# This is usually when there is a transcode session
|
||||
if machine_id_line in line and (rating_key_line in line or rating_key_line_2 in line):
|
||||
# Currently only checking for ipv4 addresses
|
||||
ipv4 = re.findall(r'[0-9]+(?:\.[0-9]+){3}', line)
|
||||
if ipv4:
|
||||
# The logged IP will always be the first match and we don't want localhost entries
|
||||
if ipv4[0] != '127.0.0.1':
|
||||
logger.debug(u"PlexPy ActivityProcessor :: Matched IP address (%s) for stream ratingKey %s "
|
||||
u"and machineIdentifier %s."
|
||||
% (ipv4[0], rating_key, machine_id))
|
||||
return ipv4[0]
|
||||
|
||||
logger.debug(u"PlexPy ActivityProcessor :: Unable to find IP address on first pass. "
|
||||
u"Attempting fallback check in 5 seconds...")
|
||||
|
||||
# Wait for the log to catch up and read in new lines
|
||||
time.sleep(5)
|
||||
|
||||
logger.debug(u"PlexPy ActivityProcessor :: Requesting log lines...")
|
||||
log_lines = log_reader.get_log_tail(window=5000, parsed=False)
|
||||
|
||||
for line in reversed(log_lines):
|
||||
if 'GET /:/timeline' in line and (rating_key_line in line or rating_key_line_2 in line):
|
||||
# Currently only checking for ipv4 addresses
|
||||
# This method can return the wrong IP address if more than one user
|
||||
# starts watching the same media item around the same time.
|
||||
ipv4 = re.findall(r'[0-9]+(?:\.[0-9]+){3}', line)
|
||||
if ipv4:
|
||||
# The logged IP will always be the first match and we don't want localhost entries
|
||||
if ipv4[0] != '127.0.0.1':
|
||||
logger.debug(u"PlexPy ActivityProcessor :: Matched IP address (%s) for stream ratingKey %s." %
|
||||
(ipv4[0], rating_key))
|
||||
return ipv4[0]
|
||||
|
||||
logger.debug(u"PlexPy ActivityProcessor :: Unable to find IP address on fallback search. Not logging IP address.")
|
||||
|
||||
return None
|
||||
|
||||
def get_session_by_key(self, session_key=None):
|
||||
if str(session_key).isdigit():
|
||||
result = self.db.select('SELECT started, session_key, rating_key, media_type, title, parent_title, '
|
||||
'grandparent_title, user_id, user, friendly_name, ip_address, player, '
|
||||
'platform, machine_id, parent_rating_key, grandparent_rating_key, state, '
|
||||
'view_offset, duration, video_decision, audio_decision, width, height, '
|
||||
'container, video_codec, audio_codec, bitrate, video_resolution, '
|
||||
'video_framerate, aspect_ratio, audio_channels, transcode_protocol, '
|
||||
'transcode_container, transcode_video_codec, transcode_audio_codec, '
|
||||
'transcode_audio_channels, transcode_width, transcode_height, '
|
||||
'paused_counter, last_paused '
|
||||
'FROM sessions WHERE session_key = ? LIMIT 1', args=[session_key])
|
||||
for session in result:
|
||||
if session:
|
||||
return session
|
||||
|
||||
return None
|
||||
|
||||
def set_session_state(self, session_key=None, state=None, view_offset=0):
|
||||
if str(session_key).isdigit() and str(view_offset).isdigit():
|
||||
values = {'view_offset': int(view_offset)}
|
||||
if state:
|
||||
values['state'] = state
|
||||
|
||||
keys = {'session_key': session_key}
|
||||
result = self.db.upsert('sessions', values, keys)
|
||||
|
||||
return result
|
||||
|
||||
return None
|
||||
|
||||
def delete_session(self, session_key=None):
|
||||
if str(session_key).isdigit():
|
||||
self.db.action('DELETE FROM sessions WHERE session_key = ?', [session_key])
|
||||
|
||||
def set_session_last_paused(self, session_key=None, timestamp=None):
|
||||
if str(session_key).isdigit():
|
||||
result = self.db.select('SELECT last_paused, paused_counter '
|
||||
'FROM sessions '
|
||||
'WHERE session_key = ?', args=[session_key])
|
||||
|
||||
paused_counter = None
|
||||
for session in result:
|
||||
if session['last_paused']:
|
||||
paused_offset = int(time.time()) - int(session['last_paused'])
|
||||
paused_counter = int(session['paused_counter']) + int(paused_offset)
|
||||
|
||||
values = {'state': 'playing',
|
||||
'last_paused': timestamp
|
||||
}
|
||||
if paused_counter:
|
||||
values['paused_counter'] = paused_counter
|
||||
|
||||
keys = {'session_key': session_key}
|
||||
self.db.upsert('sessions', values, keys)
|
||||
|
||||
def increment_session_buffer_count(self, session_key=None):
|
||||
if str(session_key).isdigit():
|
||||
self.db.action('UPDATE sessions SET buffer_count = buffer_count + 1 '
|
||||
'WHERE session_key = ?',
|
||||
[session_key])
|
||||
|
||||
def get_session_buffer_count(self, session_key=None):
|
||||
if str(session_key).isdigit():
|
||||
buffer_count = self.db.select_single('SELECT buffer_count '
|
||||
'FROM sessions '
|
||||
'WHERE session_key = ?',
|
||||
[session_key])
|
||||
if buffer_count:
|
||||
return buffer_count
|
||||
|
||||
return 0
|
||||
|
||||
def set_session_buffer_trigger_time(self, session_key=None):
|
||||
if str(session_key).isdigit():
|
||||
self.db.action('UPDATE sessions SET buffer_last_triggered = strftime("%s","now") '
|
||||
'WHERE session_key = ?',
|
||||
[session_key])
|
||||
|
||||
def get_session_buffer_trigger_time(self, session_key=None):
|
||||
if str(session_key).isdigit():
|
||||
last_time = self.db.select_single('SELECT buffer_last_triggered '
|
||||
'FROM sessions '
|
||||
'WHERE session_key = ?',
|
||||
[session_key])
|
||||
if last_time:
|
||||
return last_time
|
||||
|
||||
return None
|
|
@ -83,9 +83,11 @@ _CONFIG_DEFINITIONS = {
|
|||
'GROWL_ON_RESUME': (int, 'Growl', 0),
|
||||
'GROWL_ON_BUFFER': (int, 'Growl', 0),
|
||||
'GROWL_ON_WATCHED': (int, 'Growl', 0),
|
||||
'HOME_LIBRARY_CARDS': (str, 'General', 'library_statistics_first'),
|
||||
'HOME_STATS_LENGTH': (int, 'General', 30),
|
||||
'HOME_STATS_TYPE': (int, 'General', 0),
|
||||
'HOME_STATS_COUNT': (int, 'General', 5),
|
||||
'HOME_STATS_CARDS': (str, 'General', 'watch_statistics, top_tv, popular_tv, top_movies, popular_movies, top_music, popular_music, top_users, top_platforms, last_watched'),
|
||||
'HTTPS_CERT': (str, 'General', ''),
|
||||
'HTTPS_KEY': (str, 'General', ''),
|
||||
'HTTP_HOST': (str, 'General', '0.0.0.0'),
|
||||
|
@ -110,6 +112,7 @@ _CONFIG_DEFINITIONS = {
|
|||
'MUSIC_NOTIFY_ON_PAUSE': (int, 'Monitoring', 0),
|
||||
'MUSIC_LOGGING_ENABLE': (int, 'Monitoring', 0),
|
||||
'MONITORING_INTERVAL': (int, 'Monitoring', 60),
|
||||
'MONITORING_USE_WEBSOCKET': (int, 'Monitoring', 0),
|
||||
'NMA_APIKEY': (str, 'NMA', ''),
|
||||
'NMA_ENABLED': (int, 'NMA', 0),
|
||||
'NMA_PRIORITY': (int, 'NMA', 0),
|
||||
|
@ -195,8 +198,14 @@ _CONFIG_DEFINITIONS = {
|
|||
'TV_NOTIFY_ON_PAUSE': (int, 'Monitoring', 0),
|
||||
'TWITTER_ENABLED': (int, 'Twitter', 0),
|
||||
'TWITTER_PASSWORD': (str, 'Twitter', ''),
|
||||
'TWITTER_PREFIX': (str, 'Twitter', 'Headphones'),
|
||||
'TWITTER_PREFIX': (str, 'Twitter', 'PlexPy'),
|
||||
'TWITTER_USERNAME': (str, 'Twitter', ''),
|
||||
'TWITTER_ON_PLAY': (int, 'Twitter', 0),
|
||||
'TWITTER_ON_STOP': (int, 'Twitter', 0),
|
||||
'TWITTER_ON_PAUSE': (int, 'Twitter', 0),
|
||||
'TWITTER_ON_RESUME': (int, 'Twitter', 0),
|
||||
'TWITTER_ON_BUFFER': (int, 'Twitter', 0),
|
||||
'TWITTER_ON_WATCHED': (int, 'Twitter', 0),
|
||||
'UPDATE_DB_INTERVAL': (int, 'General', 24),
|
||||
'VERIFY_SSL_CERT': (bool_int, 'Advanced', 1),
|
||||
'VIDEO_LOGGING_ENABLE': (int, 'Monitoring', 1),
|
||||
|
|
|
@ -60,6 +60,8 @@ class DataFactory(object):
|
|||
(CASE WHEN session_history_metadata.duration IS NULL THEN 1.0 ELSE session_history_metadata.duration * 1.0 END) * 100) AS percent_complete',
|
||||
'session_history_media_info.video_decision',
|
||||
'COUNT(*) AS group_count'
|
||||
'session_history_media_info.audio_decision',
|
||||
'session_history.user_id as user_id'
|
||||
]
|
||||
try:
|
||||
query = data_tables.ssp_query(table_name='session_history',
|
||||
|
@ -127,6 +129,8 @@ class DataFactory(object):
|
|||
"video_decision": item["video_decision"],
|
||||
"watched_status": watched_status,
|
||||
"group_count": item["group_count"]
|
||||
"audio_decision": item["audio_decision"],
|
||||
"user_id": item["user_id"]
|
||||
}
|
||||
|
||||
rows.append(row)
|
||||
|
@ -139,22 +143,14 @@ class DataFactory(object):
|
|||
|
||||
return dict
|
||||
|
||||
def get_home_stats(self, time_range='30', stat_type='0', stat_count='5'):
|
||||
def get_home_stats(self, time_range='30', stats_type=0, stats_count='5', stats_cards='', notify_watched_percent='85'):
|
||||
monitor_db = database.MonitorDatabase()
|
||||
|
||||
if not time_range.isdigit():
|
||||
time_range = '30'
|
||||
sort_type = 'total_plays' if stats_type == 0 else 'total_duration'
|
||||
|
||||
sort_type = 'total_plays' if stat_type == '0' else 'total_duration'
|
||||
|
||||
if not time_range.isdigit():
|
||||
stat_count = '5'
|
||||
|
||||
# This actually determines the output order in the home page
|
||||
stats_queries = ["top_tv", "popular_tv", "top_movies", "popular_movies", "top_users", "top_platforms", "last_watched"]
|
||||
home_stats = []
|
||||
|
||||
for stat in stats_queries:
|
||||
for stat in stats_cards:
|
||||
if 'top_tv' in stat:
|
||||
top_tv = []
|
||||
try:
|
||||
|
@ -174,7 +170,7 @@ class DataFactory(object):
|
|||
'>= datetime("now", "-%s days", "localtime") ' \
|
||||
'AND session_history_metadata.media_type = "episode" ' \
|
||||
'GROUP BY session_history_metadata.grandparent_title ' \
|
||||
'ORDER BY %s DESC LIMIT %s' % (time_range, sort_type, stat_count)
|
||||
'ORDER BY %s DESC LIMIT %s' % (time_range, sort_type, stats_count)
|
||||
result = monitor_db.select(query)
|
||||
except:
|
||||
logger.warn("Unable to execute database query.")
|
||||
|
@ -210,6 +206,10 @@ class DataFactory(object):
|
|||
'session_history_metadata.grandparent_rating_key, ' \
|
||||
'MAX(session_history.started) as last_watch, ' \
|
||||
'COUNT(session_history.id) as total_plays, ' \
|
||||
'SUM(case when session_history.stopped > 0 ' \
|
||||
'then (session_history.stopped - session_history.started) ' \
|
||||
' - (case when session_history.paused_counter is NULL then 0 else session_history.paused_counter end) ' \
|
||||
'else 0 end) as total_duration, ' \
|
||||
'session_history_metadata.grandparent_thumb ' \
|
||||
'FROM session_history_metadata ' \
|
||||
'JOIN session_history ON session_history_metadata.id = session_history.id ' \
|
||||
|
@ -217,8 +217,8 @@ class DataFactory(object):
|
|||
'>= datetime("now", "-%s days", "localtime") ' \
|
||||
'AND session_history_metadata.media_type = "episode" ' \
|
||||
'GROUP BY session_history_metadata.grandparent_title ' \
|
||||
'ORDER BY users_watched DESC, total_plays DESC ' \
|
||||
'LIMIT %s' % (time_range, stat_count)
|
||||
'ORDER BY users_watched DESC, %s DESC ' \
|
||||
'LIMIT %s' % (time_range, sort_type, stats_count)
|
||||
result = monitor_db.select(query)
|
||||
except:
|
||||
logger.warn("Unable to execute database query.")
|
||||
|
@ -230,7 +230,7 @@ class DataFactory(object):
|
|||
'rating_key': item[3],
|
||||
'last_play': item[4],
|
||||
'total_plays': item[5],
|
||||
'grandparent_thumb': item[6],
|
||||
'grandparent_thumb': item[7],
|
||||
'thumb': '',
|
||||
'user': '',
|
||||
'friendly_name': '',
|
||||
|
@ -262,7 +262,7 @@ class DataFactory(object):
|
|||
'>= datetime("now", "-%s days", "localtime") ' \
|
||||
'AND session_history_metadata.media_type = "movie" ' \
|
||||
'GROUP BY session_history_metadata.full_title ' \
|
||||
'ORDER BY %s DESC LIMIT %s' % (time_range, sort_type, stat_count)
|
||||
'ORDER BY %s DESC LIMIT %s' % (time_range, sort_type, stats_count)
|
||||
result = monitor_db.select(query)
|
||||
except:
|
||||
logger.warn("Unable to execute database query.")
|
||||
|
@ -298,6 +298,10 @@ class DataFactory(object):
|
|||
'session_history_metadata.rating_key, ' \
|
||||
'MAX(session_history.started) as last_watch, ' \
|
||||
'COUNT(session_history.id) as total_plays, ' \
|
||||
'SUM(case when session_history.stopped > 0 ' \
|
||||
'then (session_history.stopped - session_history.started) ' \
|
||||
' - (case when session_history.paused_counter is NULL then 0 else session_history.paused_counter end) ' \
|
||||
'else 0 end) as total_duration, ' \
|
||||
'session_history_metadata.thumb ' \
|
||||
'FROM session_history_metadata ' \
|
||||
'JOIN session_history ON session_history_metadata.id = session_history.id ' \
|
||||
|
@ -305,8 +309,8 @@ class DataFactory(object):
|
|||
'>= datetime("now", "-%s days", "localtime") ' \
|
||||
'AND session_history_metadata.media_type = "movie" ' \
|
||||
'GROUP BY session_history_metadata.full_title ' \
|
||||
'ORDER BY users_watched DESC, total_plays DESC ' \
|
||||
'LIMIT %s' % (time_range, stat_count)
|
||||
'ORDER BY users_watched DESC, %s DESC ' \
|
||||
'LIMIT %s' % (time_range, sort_type, stats_count)
|
||||
result = monitor_db.select(query)
|
||||
except:
|
||||
logger.warn("Unable to execute database query.")
|
||||
|
@ -319,7 +323,7 @@ class DataFactory(object):
|
|||
'last_play': item[4],
|
||||
'total_plays': item[5],
|
||||
'grandparent_thumb': '',
|
||||
'thumb': item[6],
|
||||
'thumb': item[7],
|
||||
'user': '',
|
||||
'friendly_name': '',
|
||||
'platform_type': '',
|
||||
|
@ -331,6 +335,98 @@ class DataFactory(object):
|
|||
home_stats.append({'stat_id': stat,
|
||||
'rows': popular_movies})
|
||||
|
||||
elif 'top_music' in stat:
|
||||
top_music = []
|
||||
try:
|
||||
query = 'SELECT session_history_metadata.id, ' \
|
||||
'session_history_metadata.grandparent_title, ' \
|
||||
'COUNT(session_history_metadata.grandparent_title) as total_plays, ' \
|
||||
'SUM(case when session_history.stopped > 0 ' \
|
||||
'then (session_history.stopped - session_history.started) ' \
|
||||
' - (case when session_history.paused_counter is NULL then 0 else session_history.paused_counter end) ' \
|
||||
'else 0 end) as total_duration, ' \
|
||||
'session_history_metadata.grandparent_rating_key, ' \
|
||||
'MAX(session_history.started) as last_watch,' \
|
||||
'session_history_metadata.grandparent_thumb ' \
|
||||
'FROM session_history_metadata ' \
|
||||
'JOIN session_history on session_history_metadata.id = session_history.id ' \
|
||||
'WHERE datetime(session_history.stopped, "unixepoch", "localtime") ' \
|
||||
'>= datetime("now", "-%s days", "localtime") ' \
|
||||
'AND session_history_metadata.media_type = "track" ' \
|
||||
'GROUP BY session_history_metadata.grandparent_title ' \
|
||||
'ORDER BY %s DESC LIMIT %s' % (time_range, sort_type, stats_count)
|
||||
result = monitor_db.select(query)
|
||||
except:
|
||||
logger.warn("Unable to execute database query.")
|
||||
return None
|
||||
|
||||
for item in result:
|
||||
row = {'title': item[1],
|
||||
'total_plays': item[2],
|
||||
'total_duration': item[3],
|
||||
'users_watched': '',
|
||||
'rating_key': item[4],
|
||||
'last_play': item[5],
|
||||
'grandparent_thumb': item[6],
|
||||
'thumb': '',
|
||||
'user': '',
|
||||
'friendly_name': '',
|
||||
'platform_type': '',
|
||||
'platform': '',
|
||||
'row_id': item[0]
|
||||
}
|
||||
top_music.append(row)
|
||||
|
||||
home_stats.append({'stat_id': stat,
|
||||
'stat_type': sort_type,
|
||||
'rows': top_music})
|
||||
|
||||
elif 'popular_music' in stat:
|
||||
popular_music = []
|
||||
try:
|
||||
query = 'SELECT session_history_metadata.id, ' \
|
||||
'session_history_metadata.grandparent_title, ' \
|
||||
'COUNT(DISTINCT session_history.user_id) as users_watched, ' \
|
||||
'session_history_metadata.grandparent_rating_key, ' \
|
||||
'MAX(session_history.started) as last_watch, ' \
|
||||
'COUNT(session_history.id) as total_plays, ' \
|
||||
'SUM(case when session_history.stopped > 0 ' \
|
||||
'then (session_history.stopped - session_history.started) ' \
|
||||
' - (case when session_history.paused_counter is NULL then 0 else session_history.paused_counter end) ' \
|
||||
'else 0 end) as total_duration, ' \
|
||||
'session_history_metadata.grandparent_thumb ' \
|
||||
'FROM session_history_metadata ' \
|
||||
'JOIN session_history ON session_history_metadata.id = session_history.id ' \
|
||||
'WHERE datetime(session_history.stopped, "unixepoch", "localtime") ' \
|
||||
'>= datetime("now", "-%s days", "localtime") ' \
|
||||
'AND session_history_metadata.media_type = "track" ' \
|
||||
'GROUP BY session_history_metadata.grandparent_title ' \
|
||||
'ORDER BY users_watched DESC, %s DESC ' \
|
||||
'LIMIT %s' % (time_range, sort_type, stats_count)
|
||||
result = monitor_db.select(query)
|
||||
except:
|
||||
logger.warn("Unable to execute database query.")
|
||||
return None
|
||||
|
||||
for item in result:
|
||||
row = {'title': item[1],
|
||||
'users_watched': item[2],
|
||||
'rating_key': item[3],
|
||||
'last_play': item[4],
|
||||
'total_plays': item[5],
|
||||
'grandparent_thumb': item[7],
|
||||
'thumb': '',
|
||||
'user': '',
|
||||
'friendly_name': '',
|
||||
'platform_type': '',
|
||||
'platform': '',
|
||||
'row_id': item[0]
|
||||
}
|
||||
popular_music.append(row)
|
||||
|
||||
home_stats.append({'stat_id': stat,
|
||||
'rows': popular_music})
|
||||
|
||||
elif 'top_users' in stat:
|
||||
top_users = []
|
||||
try:
|
||||
|
@ -351,7 +447,7 @@ class DataFactory(object):
|
|||
'WHERE datetime(session_history.stopped, "unixepoch", "localtime") >= ' \
|
||||
'datetime("now", "-%s days", "localtime") '\
|
||||
'GROUP BY session_history.user_id ' \
|
||||
'ORDER BY %s DESC LIMIT %s' % (time_range, sort_type, stat_count)
|
||||
'ORDER BY %s DESC LIMIT %s' % (time_range, sort_type, stats_count)
|
||||
result = monitor_db.select(query)
|
||||
except:
|
||||
logger.warn("Unable to execute database query.")
|
||||
|
@ -399,7 +495,7 @@ class DataFactory(object):
|
|||
'WHERE datetime(session_history.stopped, "unixepoch", "localtime") ' \
|
||||
'>= datetime("now", "-%s days", "localtime") ' \
|
||||
'GROUP BY session_history.platform ' \
|
||||
'ORDER BY %s DESC LIMIT %s' % (time_range, sort_type, stat_count)
|
||||
'ORDER BY %s DESC LIMIT %s' % (time_range, sort_type, stats_count)
|
||||
result = monitor_db.select(query)
|
||||
except:
|
||||
logger.warn("Unable to execute database query.")
|
||||
|
@ -440,7 +536,11 @@ class DataFactory(object):
|
|||
'session_history_metadata.thumb, ' \
|
||||
'session_history_metadata.grandparent_thumb, ' \
|
||||
'MAX(session_history.started) as last_watch, ' \
|
||||
'session_history.player as platform ' \
|
||||
'session_history.player as platform, ' \
|
||||
'((CASE WHEN session_history.view_offset IS NULL THEN 0.1 ELSE \
|
||||
session_history.view_offset * 1.0 END) / \
|
||||
(CASE WHEN session_history_metadata.duration IS NULL THEN 1.0 ELSE \
|
||||
session_history_metadata.duration * 1.0 END) * 100) as percent_complete ' \
|
||||
'FROM session_history_metadata ' \
|
||||
'JOIN session_history ON session_history_metadata.id = session_history.id ' \
|
||||
'LEFT OUTER JOIN users ON session_history.user_id = users.user_id ' \
|
||||
|
@ -448,9 +548,10 @@ class DataFactory(object):
|
|||
'>= datetime("now", "-%s days", "localtime") ' \
|
||||
'AND (session_history_metadata.media_type = "movie" ' \
|
||||
'OR session_history_metadata.media_type = "episode") ' \
|
||||
'AND percent_complete >= %s ' \
|
||||
'GROUP BY session_history_metadata.full_title ' \
|
||||
'ORDER BY last_watch DESC ' \
|
||||
'LIMIT %s' % (time_range, stat_count)
|
||||
'LIMIT %s' % (time_range, notify_watched_percent, stats_count)
|
||||
result = monitor_db.select(query)
|
||||
except:
|
||||
logger.warn("Unable to execute database query.")
|
||||
|
@ -688,4 +789,4 @@ class DataFactory(object):
|
|||
|
||||
return 'Deleted all items for user_id %s.' % user_id
|
||||
else:
|
||||
return 'Unable to delete items. Input user_id not valid.'
|
||||
return 'Unable to delete items. Input user_id not valid.'
|
|
@ -81,10 +81,11 @@ class HTTPHandler(object):
|
|||
logger.warn(u"Failed to access uri endpoint %s with error %s" % (uri, e))
|
||||
return None
|
||||
except Exception, e:
|
||||
logger.warn(u"Failed to access uri endpoint %s. Is your server maybe accepting SSL connections only?" % uri)
|
||||
logger.warn(u"Failed to access uri endpoint %s. Is your server maybe accepting SSL connections only? %s" % (uri, e))
|
||||
return None
|
||||
except:
|
||||
logger.warn(u"Failed to access uri endpoint %s with Uncaught exception." % uri)
|
||||
return None
|
||||
|
||||
if request_status == 200:
|
||||
if output_format == 'dict':
|
||||
|
|
|
@ -49,7 +49,8 @@ AGENT_IDS = {"Growl": 0,
|
|||
"Pushover": 7,
|
||||
"OSX Notify": 8,
|
||||
"Boxcar2": 9,
|
||||
"Email": 10}
|
||||
"Email": 10,
|
||||
"Twitter": 11}
|
||||
|
||||
def available_notification_agents():
|
||||
agents = [{'name': 'Growl',
|
||||
|
@ -171,6 +172,18 @@ def available_notification_agents():
|
|||
'on_resume': plexpy.CONFIG.EMAIL_ON_RESUME,
|
||||
'on_buffer': plexpy.CONFIG.EMAIL_ON_BUFFER,
|
||||
'on_watched': plexpy.CONFIG.EMAIL_ON_WATCHED
|
||||
},
|
||||
{'name': 'Twitter',
|
||||
'id': AGENT_IDS['Twitter'],
|
||||
'config_prefix': 'twitter',
|
||||
'has_config': True,
|
||||
'state': checked(plexpy.CONFIG.TWITTER_ENABLED),
|
||||
'on_play': plexpy.CONFIG.TWITTER_ON_PLAY,
|
||||
'on_stop': plexpy.CONFIG.TWITTER_ON_STOP,
|
||||
'on_pause': plexpy.CONFIG.TWITTER_ON_PAUSE,
|
||||
'on_resume': plexpy.CONFIG.TWITTER_ON_RESUME,
|
||||
'on_buffer': plexpy.CONFIG.TWITTER_ON_BUFFER,
|
||||
'on_watched': plexpy.CONFIG.TWITTER_ON_WATCHED
|
||||
}
|
||||
]
|
||||
|
||||
|
@ -229,6 +242,9 @@ def get_notification_agent_config(config_id):
|
|||
elif config_id == 10:
|
||||
email = Email()
|
||||
return email.return_config_options()
|
||||
elif config_id == 11:
|
||||
tweet = TwitterNotifier()
|
||||
return tweet.return_config_options()
|
||||
else:
|
||||
return []
|
||||
else:
|
||||
|
@ -271,6 +287,9 @@ def send_notification(config_id, subject, body):
|
|||
elif config_id == 10:
|
||||
email = Email()
|
||||
email.notify(subject=subject, message=body)
|
||||
elif config_id == 11:
|
||||
tweet = TwitterNotifier()
|
||||
tweet.notify(subject=subject, message=body)
|
||||
else:
|
||||
logger.debug(u"PlexPy Notifier :: Unknown agent id received.")
|
||||
else:
|
||||
|
@ -912,19 +931,17 @@ class TwitterNotifier(object):
|
|||
SIGNIN_URL = 'https://api.twitter.com/oauth/authenticate'
|
||||
|
||||
def __init__(self):
|
||||
self.consumer_key = "oYKnp2ddX5gbARjqX8ZAAg"
|
||||
self.consumer_secret = "A4Xkw9i5SjHbTk7XT8zzOPqivhj9MmRDR9Qn95YA9sk"
|
||||
self.consumer_key = "2LdJKXHDUwJtjYBsdwJisIOsh"
|
||||
self.consumer_secret = "QWbUcZzAIiL4zbDCIhy2EdUkV8yEEav3qMdo5y3FugxCFelWrA"
|
||||
|
||||
def notify_snatch(self, title):
|
||||
if plexpy.CONFIG.TWITTER_ONSNATCH:
|
||||
self._notifyTwitter(common.notifyStrings[common.NOTIFY_SNATCH] + ': ' + title + ' at ' + helpers.now())
|
||||
|
||||
def notify_download(self, title):
|
||||
if plexpy.CONFIG.TWITTER_ENABLED:
|
||||
self._notifyTwitter(common.notifyStrings[common.NOTIFY_DOWNLOAD] + ': ' + title + ' at ' + helpers.now())
|
||||
def notify(self, subject, message):
|
||||
if not subject or not message:
|
||||
return
|
||||
else:
|
||||
self._send_tweet(subject + ': ' + message)
|
||||
|
||||
def test_notify(self):
|
||||
return self._notifyTwitter("This is a test notification from PlexPy at " + helpers.now(), force=True)
|
||||
return self._send_tweet("This is a test notification from PlexPy at " + helpers.now())
|
||||
|
||||
def _get_authorization(self):
|
||||
|
||||
|
@ -958,7 +975,7 @@ class TwitterNotifier(object):
|
|||
logger.info('Generating and signing request for an access token using key ' + key)
|
||||
|
||||
oauth_consumer = oauth.Consumer(key=self.consumer_key, secret=self.consumer_secret)
|
||||
logger.info('oauth_consumer: ' + str(oauth_consumer))
|
||||
# logger.debug('oauth_consumer: ' + str(oauth_consumer))
|
||||
oauth_client = oauth.Client(oauth_consumer, token)
|
||||
logger.info('oauth_client: ' + str(oauth_client))
|
||||
resp, content = oauth_client.request(self.ACCESS_TOKEN_URL, method='POST', body='oauth_verifier=%s' % key)
|
||||
|
@ -979,7 +996,6 @@ class TwitterNotifier(object):
|
|||
return True
|
||||
|
||||
def _send_tweet(self, message=None):
|
||||
|
||||
username = self.consumer_key
|
||||
password = self.consumer_secret
|
||||
access_token_key = plexpy.CONFIG.TWITTER_USERNAME
|
||||
|
@ -997,13 +1013,36 @@ class TwitterNotifier(object):
|
|||
|
||||
return True
|
||||
|
||||
def _notifyTwitter(self, message='', force=False):
|
||||
prefix = plexpy.CONFIG.TWITTER_PREFIX
|
||||
def return_config_options(self):
|
||||
config_option = [{'label': 'Request Authorisation',
|
||||
'value': 'Request Authorisation',
|
||||
'name': 'twitterStep1',
|
||||
'description': 'Step 1: Click Request button above. (Ensure you allow the browser pop-up).',
|
||||
'input_type': 'button'
|
||||
},
|
||||
{'label': 'Authorisation Key',
|
||||
'value': '',
|
||||
'name': 'twitter_key',
|
||||
'description': 'Step 2: Input the authorisation key you received from Step 1.',
|
||||
'input_type': 'text'
|
||||
},
|
||||
{'label': 'Verify Key',
|
||||
'value': 'Verify Key',
|
||||
'name': 'twitterStep2',
|
||||
'description': 'Step 3: Verify the key.',
|
||||
'input_type': 'button'
|
||||
},
|
||||
{'label': 'Test Twitter',
|
||||
'value': 'Test Twitter',
|
||||
'name': 'testTwitter',
|
||||
'description': 'Test if Twitter notifications are working. See logs for troubleshooting.',
|
||||
'input_type': 'button'
|
||||
},
|
||||
{'input_type': 'nosave'
|
||||
}
|
||||
]
|
||||
|
||||
if not plexpy.CONFIG.TWITTER_ENABLED and not force:
|
||||
return False
|
||||
|
||||
return self._send_tweet(prefix + ": " + message)
|
||||
return config_option
|
||||
|
||||
class OSX_NOTIFY(object):
|
||||
|
||||
|
@ -1067,6 +1106,7 @@ class OSX_NOTIFY(object):
|
|||
|
||||
notification_center = NSUserNotificationCenter.defaultUserNotificationCenter()
|
||||
notification_center.deliverNotification_(notification)
|
||||
logger.info(u"OSX Notify notifications sent.")
|
||||
|
||||
del pool
|
||||
return True
|
||||
|
@ -1204,7 +1244,7 @@ class Email(object):
|
|||
'input_type': 'password'
|
||||
},
|
||||
{'label': 'TLS',
|
||||
'value': checked(plexpy.CONFIG.EMAIL_TLS),
|
||||
'value': plexpy.CONFIG.EMAIL_TLS,
|
||||
'name': 'email_tls',
|
||||
'description': 'Does the server use encryption.',
|
||||
'input_type': 'checkbox'
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
|
||||
import sqlite3
|
||||
|
||||
from plexpy import logger, helpers, monitor, users, plextv
|
||||
from plexpy import logger, helpers, activity_pinger, activity_processor, users, plextv
|
||||
from xml.dom import minidom
|
||||
|
||||
import plexpy
|
||||
|
@ -245,9 +245,10 @@ def import_from_plexwatch(database=None, table_name=None, import_ignore_interval
|
|||
logger.debug(u"PlexPy Importer :: PlexWatch data import in progress...")
|
||||
|
||||
logger.debug(u"PlexPy Importer :: Disabling monitoring while import in progress.")
|
||||
plexpy.schedule_job(monitor.check_active_sessions, 'Check for active sessions', hours=0, minutes=0, seconds=0)
|
||||
plexpy.schedule_job(activity_pinger.check_active_sessions, 'Check for active sessions',
|
||||
hours=0, minutes=0, seconds=0)
|
||||
|
||||
monitor_processing = monitor.MonitorProcessing()
|
||||
ap = activity_processor.ActivityProcessor()
|
||||
user_data = users.Users()
|
||||
|
||||
# Get the latest friends list so we can pull user id's
|
||||
|
@ -373,10 +374,10 @@ def import_from_plexwatch(database=None, table_name=None, import_ignore_interval
|
|||
# On older versions of PMS, "clip" items were still classified as "movie" and had bad ratingKey values
|
||||
# Just make sure that the ratingKey is indeed an integer
|
||||
if session_history_metadata['rating_key'].isdigit():
|
||||
monitor_processing.write_session_history(session=session_history,
|
||||
import_metadata=session_history_metadata,
|
||||
is_import=True,
|
||||
import_ignore_interval=import_ignore_interval)
|
||||
ap.write_session_history(session=session_history,
|
||||
import_metadata=session_history_metadata,
|
||||
is_import=True,
|
||||
import_ignore_interval=import_ignore_interval)
|
||||
else:
|
||||
logger.debug(u"PlexPy Importer :: Item has bad rating_key: %s" % session_history_metadata['rating_key'])
|
||||
|
||||
|
|
|
@ -705,9 +705,9 @@ class PmsConnect(object):
|
|||
'transcode_container': transcode_container,
|
||||
'transcode_protocol': transcode_protocol,
|
||||
'duration': duration,
|
||||
'progress': progress,
|
||||
'view_offset': progress,
|
||||
'progress_percent': str(helpers.get_percent(progress, duration)),
|
||||
'type': 'track',
|
||||
'media_type': 'track',
|
||||
'indexes': 0
|
||||
}
|
||||
|
||||
|
@ -826,14 +826,14 @@ class PmsConnect(object):
|
|||
'transcode_container': transcode_container,
|
||||
'transcode_protocol': transcode_protocol,
|
||||
'duration': duration,
|
||||
'progress': progress,
|
||||
'view_offset': progress,
|
||||
'progress_percent': str(helpers.get_percent(progress, duration)),
|
||||
'indexes': use_indexes
|
||||
}
|
||||
if helpers.get_xml_attr(session, 'ratingKey').isdigit():
|
||||
session_output['type'] = helpers.get_xml_attr(session, 'type')
|
||||
session_output['media_type'] = helpers.get_xml_attr(session, 'type')
|
||||
else:
|
||||
session_output['type'] = 'clip'
|
||||
session_output['media_type'] = 'clip'
|
||||
|
||||
elif helpers.get_xml_attr(session, 'type') == 'movie':
|
||||
session_output = {'session_key': helpers.get_xml_attr(session, 'sessionKey'),
|
||||
|
@ -882,14 +882,14 @@ class PmsConnect(object):
|
|||
'transcode_container': transcode_container,
|
||||
'transcode_protocol': transcode_protocol,
|
||||
'duration': duration,
|
||||
'progress': progress,
|
||||
'view_offset': progress,
|
||||
'progress_percent': str(helpers.get_percent(progress, duration)),
|
||||
'indexes': use_indexes
|
||||
}
|
||||
if helpers.get_xml_attr(session, 'ratingKey').isdigit():
|
||||
session_output['type'] = helpers.get_xml_attr(session, 'type')
|
||||
session_output['media_type'] = helpers.get_xml_attr(session, 'type')
|
||||
else:
|
||||
session_output['type'] = 'clip'
|
||||
session_output['media_type'] = 'clip'
|
||||
|
||||
elif helpers.get_xml_attr(session, 'type') == 'clip':
|
||||
session_output = {'session_key': helpers.get_xml_attr(session, 'sessionKey'),
|
||||
|
@ -938,9 +938,9 @@ class PmsConnect(object):
|
|||
'transcode_container': transcode_container,
|
||||
'transcode_protocol': transcode_protocol,
|
||||
'duration': duration,
|
||||
'progress': progress,
|
||||
'view_offset': progress,
|
||||
'progress_percent': str(helpers.get_percent(progress, duration)),
|
||||
'type': helpers.get_xml_attr(session, 'type'),
|
||||
'media_type': helpers.get_xml_attr(session, 'type'),
|
||||
'indexes': 0
|
||||
}
|
||||
|
||||
|
@ -1027,9 +1027,9 @@ class PmsConnect(object):
|
|||
'transcode_container': transcode_container,
|
||||
'transcode_protocol': transcode_protocol,
|
||||
'duration': '',
|
||||
'progress': '',
|
||||
'view_offset': '',
|
||||
'progress_percent': '100',
|
||||
'type': 'photo',
|
||||
'media_type': 'photo',
|
||||
'indexes': 0
|
||||
}
|
||||
|
||||
|
@ -1083,7 +1083,6 @@ class PmsConnect(object):
|
|||
}
|
||||
children_list.append(children_output)
|
||||
|
||||
|
||||
output = {'children_count': helpers.get_xml_attr(xml_head[0], 'size'),
|
||||
'children_type': helpers.get_xml_attr(xml_head[0], 'viewGroup'),
|
||||
'title': helpers.get_xml_attr(xml_head[0], 'title2'),
|
||||
|
@ -1208,7 +1207,7 @@ class PmsConnect(object):
|
|||
'title': helpers.get_xml_attr(xml_head[0], 'title1'),
|
||||
'libraries_list': libraries_list
|
||||
}
|
||||
|
||||
|
||||
return output
|
||||
|
||||
"""
|
||||
|
@ -1270,11 +1269,11 @@ class PmsConnect(object):
|
|||
return output
|
||||
|
||||
"""
|
||||
Return processed and validated server statistics.
|
||||
Return processed and validated library statistics.
|
||||
|
||||
Output: array
|
||||
"""
|
||||
def get_library_stats(self):
|
||||
def get_library_stats(self, library_cards=''):
|
||||
server_libraries = self.get_server_children()
|
||||
|
||||
server_library_stats = []
|
||||
|
@ -1285,7 +1284,10 @@ class PmsConnect(object):
|
|||
for library in libraries_list:
|
||||
library_type = library['type']
|
||||
section_key = library['key']
|
||||
library_list = self.get_library_children(library_type, section_key)
|
||||
if section_key in library_cards:
|
||||
library_list = self.get_library_children(library_type, section_key)
|
||||
else:
|
||||
continue
|
||||
|
||||
if library_list['library_count'] != '0':
|
||||
library_stats = {'title': library['title'],
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
PLEXPY_VERSION = "master"
|
||||
PLEXPY_RELEASE_VERSION = "1.1.9"
|
||||
PLEXPY_RELEASE_VERSION = "1.1.10"
|
||||
|
|
142
plexpy/web_socket.py
Normal file
142
plexpy/web_socket.py
Normal file
|
@ -0,0 +1,142 @@
|
|||
# This file is part of PlexPy.
|
||||
#
|
||||
# PlexPy is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# PlexPy is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with PlexPy. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
# Mostly borrowed from https://github.com/trakt/Plex-Trakt-Scrobbler
|
||||
|
||||
from plexpy import logger, activity_pinger
|
||||
|
||||
import threading
|
||||
import plexpy
|
||||
import json
|
||||
import time
|
||||
import websocket
|
||||
|
||||
name = 'websocket'
|
||||
opcode_data = (websocket.ABNF.OPCODE_TEXT, websocket.ABNF.OPCODE_BINARY)
|
||||
|
||||
|
||||
def start_thread():
|
||||
# Check for any existing sessions on start up
|
||||
activity_pinger.check_active_sessions(ws_request=True)
|
||||
# Start the websocket listener on it's own thread
|
||||
threading.Thread(target=run).start()
|
||||
|
||||
|
||||
def run():
|
||||
from websocket import create_connection
|
||||
|
||||
uri = 'ws://%s:%s/:/websockets/notifications' % (
|
||||
plexpy.CONFIG.PMS_IP,
|
||||
plexpy.CONFIG.PMS_PORT
|
||||
)
|
||||
|
||||
# Set authentication token (if one is available)
|
||||
if plexpy.CONFIG.PMS_TOKEN:
|
||||
uri += '?X-Plex-Token=' + plexpy.CONFIG.PMS_TOKEN
|
||||
|
||||
ws_connected = False
|
||||
reconnects = 0
|
||||
|
||||
# Try an open the websocket connection - if it fails after 5 retries fallback to polling
|
||||
while not ws_connected and reconnects < 15:
|
||||
try:
|
||||
logger.info(u'PlexPy WebSocket :: Opening websocket, connection attempt %s.' % str(reconnects + 1))
|
||||
ws = create_connection(uri)
|
||||
reconnects = 0
|
||||
ws_connected = True
|
||||
logger.info(u'PlexPy WebSocket :: Ready')
|
||||
except IOError, e:
|
||||
logger.error(u'PlexPy WebSocket :: %s.' % e)
|
||||
reconnects += 1
|
||||
time.sleep(5)
|
||||
|
||||
while ws_connected:
|
||||
try:
|
||||
process(*receive(ws))
|
||||
|
||||
# successfully received data, reset reconnects counter
|
||||
reconnects = 0
|
||||
except websocket.WebSocketConnectionClosedException:
|
||||
if reconnects <= 5:
|
||||
reconnects += 1
|
||||
|
||||
# Increasing sleep interval between reconnections
|
||||
if reconnects > 1:
|
||||
time.sleep(2 * (reconnects - 1))
|
||||
|
||||
logger.warn(u'PlexPy WebSocket :: Connection has closed, reconnecting...')
|
||||
try:
|
||||
ws = create_connection(uri)
|
||||
except IOError, e:
|
||||
logger.info(u'PlexPy WebSocket :: %s.' % e)
|
||||
|
||||
else:
|
||||
ws_connected = False
|
||||
break
|
||||
|
||||
if not ws_connected:
|
||||
logger.error(u'PlexPy WebSocket :: Connection unavailable, falling back to polling.')
|
||||
plexpy.POLLING_FAILOVER = True
|
||||
plexpy.initialize_scheduler()
|
||||
|
||||
logger.debug(u'Leaving thread.')
|
||||
|
||||
|
||||
def receive(ws):
|
||||
frame = ws.recv_frame()
|
||||
|
||||
if not frame:
|
||||
raise websocket.WebSocketException("Not a valid frame %s" % frame)
|
||||
elif frame.opcode in opcode_data:
|
||||
return frame.opcode, frame.data
|
||||
elif frame.opcode == websocket.ABNF.OPCODE_CLOSE:
|
||||
ws.send_close()
|
||||
return frame.opcode, None
|
||||
elif frame.opcode == websocket.ABNF.OPCODE_PING:
|
||||
ws.pong("Hi!")
|
||||
|
||||
return None, None
|
||||
|
||||
|
||||
def process(opcode, data):
|
||||
from plexpy import activity_handler
|
||||
|
||||
if opcode not in opcode_data:
|
||||
return False
|
||||
|
||||
try:
|
||||
info = json.loads(data)
|
||||
except Exception as ex:
|
||||
logger.warn(u'PlexPy WebSocket :: Error decoding message from websocket: %s' % ex)
|
||||
logger.debug(data)
|
||||
return False
|
||||
|
||||
type = info.get('type')
|
||||
|
||||
if not type:
|
||||
return False
|
||||
|
||||
if type == 'playing':
|
||||
# logger.debug('%s.playing %s' % (name, info))
|
||||
try:
|
||||
time_line = info.get('_children')
|
||||
except:
|
||||
logger.debug(u"PlexPy WebSocket :: Session found but unable to get timeline data.")
|
||||
return False
|
||||
|
||||
activity = activity_handler.ActivityHandler(timeline=time_line[0])
|
||||
activity.process()
|
||||
|
||||
return True
|
|
@ -66,9 +66,9 @@ class WebInterface(object):
|
|||
def home(self):
|
||||
config = {
|
||||
"home_stats_length": plexpy.CONFIG.HOME_STATS_LENGTH,
|
||||
"home_stats_type": plexpy.CONFIG.HOME_STATS_TYPE,
|
||||
"home_stats_count": plexpy.CONFIG.HOME_STATS_COUNT,
|
||||
"pms_identifier": plexpy.CONFIG.PMS_IDENTIFIER,
|
||||
"home_stats_cards": plexpy.CONFIG.HOME_STATS_CARDS,
|
||||
"home_library_cards": plexpy.CONFIG.HOME_LIBRARY_CARDS,
|
||||
"pms_identifier": plexpy.CONFIG.PMS_IDENTIFIER
|
||||
}
|
||||
return serve_template(templatename="index.html", title="Home", config=config)
|
||||
|
||||
|
@ -99,6 +99,7 @@ class WebInterface(object):
|
|||
# The setup wizard just refreshes the page on submit so we must redirect to home if config set.
|
||||
# Also redirecting to home if a PMS token already exists - will remove this in future.
|
||||
if plexpy.CONFIG.FIRST_RUN_COMPLETE or plexpy.CONFIG.PMS_TOKEN:
|
||||
plexpy.initialize_scheduler()
|
||||
raise cherrypy.HTTPRedirect("home")
|
||||
else:
|
||||
return serve_template(templatename="welcome.html", title="Welcome", config=config)
|
||||
|
@ -121,16 +122,41 @@ class WebInterface(object):
|
|||
return json.dumps(formats)
|
||||
|
||||
@cherrypy.expose
|
||||
def home_stats(self, time_range='30', stat_type='0', stat_count='5', **kwargs):
|
||||
def home_stats(self, **kwargs):
|
||||
data_factory = datafactory.DataFactory()
|
||||
stats_data = data_factory.get_home_stats(time_range=time_range, stat_type=stat_type, stat_count=stat_count)
|
||||
|
||||
time_range = plexpy.CONFIG.HOME_STATS_LENGTH
|
||||
stats_type = plexpy.CONFIG.HOME_STATS_TYPE
|
||||
stats_count = plexpy.CONFIG.HOME_STATS_COUNT
|
||||
stats_cards = plexpy.CONFIG.HOME_STATS_CARDS.split(', ')
|
||||
notify_watched_percent = plexpy.CONFIG.NOTIFY_WATCHED_PERCENT
|
||||
|
||||
stats_data = data_factory.get_home_stats(time_range=time_range,
|
||||
stats_type=stats_type,
|
||||
stats_count=stats_count,
|
||||
stats_cards=stats_cards,
|
||||
notify_watched_percent=notify_watched_percent)
|
||||
|
||||
return serve_template(templatename="home_stats.html", title="Stats", data=stats_data)
|
||||
|
||||
@cherrypy.expose
|
||||
def library_stats(self, **kwargs):
|
||||
pms_connect = pmsconnect.PmsConnect()
|
||||
stats_data = pms_connect.get_library_stats()
|
||||
|
||||
library_cards = plexpy.CONFIG.HOME_LIBRARY_CARDS.split(', ')
|
||||
|
||||
if library_cards == ['library_statistics_first']:
|
||||
library_cards = ['library_statistics']
|
||||
server_children = pms_connect.get_server_children()
|
||||
server_libraries = server_children['libraries_list']
|
||||
|
||||
for library in server_libraries:
|
||||
library_cards.append(library['key'])
|
||||
|
||||
plexpy.CONFIG.HOME_LIBRARY_CARDS = ', '.join(library_cards)
|
||||
plexpy.CONFIG.write()
|
||||
|
||||
stats_data = pms_connect.get_library_stats(library_cards=library_cards)
|
||||
|
||||
return serve_template(templatename="library_stats.html", title="Library Stats", data=stats_data)
|
||||
|
||||
|
@ -144,7 +170,12 @@ class WebInterface(object):
|
|||
|
||||
@cherrypy.expose
|
||||
def graphs(self):
|
||||
return serve_template(templatename="graphs.html", title="Graphs")
|
||||
|
||||
config = {
|
||||
"music_logging_enable": plexpy.CONFIG.MUSIC_LOGGING_ENABLE
|
||||
}
|
||||
|
||||
return serve_template(templatename="graphs.html", title="Graphs", config=config)
|
||||
|
||||
@cherrypy.expose
|
||||
def sync(self):
|
||||
|
@ -373,46 +404,7 @@ class WebInterface(object):
|
|||
"cache_dir": plexpy.CONFIG.CACHE_DIR,
|
||||
"check_github": checked(plexpy.CONFIG.CHECK_GITHUB),
|
||||
"interface_list": interface_list,
|
||||
"growl_enabled": checked(plexpy.CONFIG.GROWL_ENABLED),
|
||||
"growl_host": plexpy.CONFIG.GROWL_HOST,
|
||||
"growl_password": plexpy.CONFIG.GROWL_PASSWORD,
|
||||
"prowl_enabled": checked(plexpy.CONFIG.PROWL_ENABLED),
|
||||
"prowl_keys": plexpy.CONFIG.PROWL_KEYS,
|
||||
"prowl_priority": plexpy.CONFIG.PROWL_PRIORITY,
|
||||
"xbmc_enabled": checked(plexpy.CONFIG.XBMC_ENABLED),
|
||||
"xbmc_host": plexpy.CONFIG.XBMC_HOST,
|
||||
"xbmc_username": plexpy.CONFIG.XBMC_USERNAME,
|
||||
"xbmc_password": plexpy.CONFIG.XBMC_PASSWORD,
|
||||
"plex_enabled": checked(plexpy.CONFIG.PLEX_ENABLED),
|
||||
"plex_client_host": plexpy.CONFIG.PLEX_CLIENT_HOST,
|
||||
"plex_username": plexpy.CONFIG.PLEX_USERNAME,
|
||||
"plex_password": plexpy.CONFIG.PLEX_PASSWORD,
|
||||
"nma_enabled": checked(plexpy.CONFIG.NMA_ENABLED),
|
||||
"nma_apikey": plexpy.CONFIG.NMA_APIKEY,
|
||||
"nma_priority": int(plexpy.CONFIG.NMA_PRIORITY),
|
||||
"pushalot_enabled": checked(plexpy.CONFIG.PUSHALOT_ENABLED),
|
||||
"pushalot_apikey": plexpy.CONFIG.PUSHALOT_APIKEY,
|
||||
"pushover_enabled": checked(plexpy.CONFIG.PUSHOVER_ENABLED),
|
||||
"pushover_keys": plexpy.CONFIG.PUSHOVER_KEYS,
|
||||
"pushover_apitoken": plexpy.CONFIG.PUSHOVER_APITOKEN,
|
||||
"pushover_priority": plexpy.CONFIG.PUSHOVER_PRIORITY,
|
||||
"pushbullet_enabled": checked(plexpy.CONFIG.PUSHBULLET_ENABLED),
|
||||
"pushbullet_apikey": plexpy.CONFIG.PUSHBULLET_APIKEY,
|
||||
"pushbullet_deviceid": plexpy.CONFIG.PUSHBULLET_DEVICEID,
|
||||
"twitter_enabled": checked(plexpy.CONFIG.TWITTER_ENABLED),
|
||||
"osx_notify_enabled": checked(plexpy.CONFIG.OSX_NOTIFY_ENABLED),
|
||||
"osx_notify_app": plexpy.CONFIG.OSX_NOTIFY_APP,
|
||||
"boxcar_enabled": checked(plexpy.CONFIG.BOXCAR_ENABLED),
|
||||
"boxcar_token": plexpy.CONFIG.BOXCAR_TOKEN,
|
||||
"cache_sizemb": plexpy.CONFIG.CACHE_SIZEMB,
|
||||
"email_enabled": checked(plexpy.CONFIG.EMAIL_ENABLED),
|
||||
"email_from": plexpy.CONFIG.EMAIL_FROM,
|
||||
"email_to": plexpy.CONFIG.EMAIL_TO,
|
||||
"email_smtp_server": plexpy.CONFIG.EMAIL_SMTP_SERVER,
|
||||
"email_smtp_user": plexpy.CONFIG.EMAIL_SMTP_USER,
|
||||
"email_smtp_password": plexpy.CONFIG.EMAIL_SMTP_PASSWORD,
|
||||
"email_smtp_port": int(plexpy.CONFIG.EMAIL_SMTP_PORT),
|
||||
"email_tls": checked(plexpy.CONFIG.EMAIL_TLS),
|
||||
"pms_identifier": plexpy.CONFIG.PMS_IDENTIFIER,
|
||||
"pms_ip": plexpy.CONFIG.PMS_IP,
|
||||
"pms_logs_folder": plexpy.CONFIG.PMS_LOGS_FOLDER,
|
||||
|
@ -421,7 +413,6 @@ class WebInterface(object):
|
|||
"pms_ssl": checked(plexpy.CONFIG.PMS_SSL),
|
||||
"pms_use_bif": checked(plexpy.CONFIG.PMS_USE_BIF),
|
||||
"pms_uuid": plexpy.CONFIG.PMS_UUID,
|
||||
"plexwatch_database": plexpy.CONFIG.PLEXWATCH_DATABASE,
|
||||
"date_format": plexpy.CONFIG.DATE_FORMAT,
|
||||
"time_format": plexpy.CONFIG.TIME_FORMAT,
|
||||
"grouping_global_history": checked(plexpy.CONFIG.GROUPING_GLOBAL_HISTORY),
|
||||
|
@ -440,6 +431,7 @@ class WebInterface(object):
|
|||
"movie_notify_on_pause": checked(plexpy.CONFIG.MOVIE_NOTIFY_ON_PAUSE),
|
||||
"music_notify_on_pause": checked(plexpy.CONFIG.MUSIC_NOTIFY_ON_PAUSE),
|
||||
"monitoring_interval": plexpy.CONFIG.MONITORING_INTERVAL,
|
||||
"monitoring_use_websocket": checked(plexpy.CONFIG.MONITORING_USE_WEBSOCKET),
|
||||
"refresh_users_interval": plexpy.CONFIG.REFRESH_USERS_INTERVAL,
|
||||
"refresh_users_on_startup": checked(plexpy.CONFIG.REFRESH_USERS_ON_STARTUP),
|
||||
"ip_logging_enable": checked(plexpy.CONFIG.IP_LOGGING_ENABLE),
|
||||
|
@ -463,6 +455,8 @@ class WebInterface(object):
|
|||
"home_stats_length": plexpy.CONFIG.HOME_STATS_LENGTH,
|
||||
"home_stats_type": checked(plexpy.CONFIG.HOME_STATS_TYPE),
|
||||
"home_stats_count": plexpy.CONFIG.HOME_STATS_COUNT,
|
||||
"home_stats_cards": plexpy.CONFIG.HOME_STATS_CARDS,
|
||||
"home_library_cards": plexpy.CONFIG.HOME_LIBRARY_CARDS,
|
||||
"buffer_threshold": plexpy.CONFIG.BUFFER_THRESHOLD,
|
||||
"buffer_wait": plexpy.CONFIG.BUFFER_WAIT,
|
||||
"group_history_tables": checked(plexpy.CONFIG.GROUP_HISTORY_TABLES)
|
||||
|
@ -475,14 +469,9 @@ class WebInterface(object):
|
|||
# Handle the variable config options. Note - keys with False values aren't getting passed
|
||||
|
||||
checked_configs = [
|
||||
"launch_browser", "enable_https", "api_enabled", "freeze_db", "growl_enabled",
|
||||
"prowl_enabled", "xbmc_enabled", "check_github",
|
||||
"plex_enabled", "nma_enabled", "pushalot_enabled",
|
||||
"pushover_enabled", "pushbullet_enabled",
|
||||
"twitter_enabled", "osx_notify_enabled",
|
||||
"boxcar_enabled", "email_enabled", "email_tls",
|
||||
"launch_browser", "enable_https", "api_enabled", "freeze_db", "check_github",
|
||||
"grouping_global_history", "grouping_user_history", "grouping_charts", "pms_use_bif", "pms_ssl",
|
||||
"tv_notify_enable", "movie_notify_enable", "music_notify_enable",
|
||||
"tv_notify_enable", "movie_notify_enable", "music_notify_enable", "monitoring_use_websocket",
|
||||
"tv_notify_on_start", "movie_notify_on_start", "music_notify_on_start",
|
||||
"tv_notify_on_stop", "movie_notify_on_stop", "music_notify_on_stop",
|
||||
"tv_notify_on_pause", "movie_notify_on_pause", "music_notify_on_pause", "refresh_users_on_startup",
|
||||
|
@ -517,6 +506,14 @@ class WebInterface(object):
|
|||
if kwargs['pms_ip'] != plexpy.CONFIG.PMS_IP:
|
||||
refresh_users = True
|
||||
|
||||
if 'home_stats_cards' in kwargs:
|
||||
if kwargs['home_stats_cards'] != 'watch_statistics':
|
||||
kwargs['home_stats_cards'] = ', '.join(kwargs['home_stats_cards'])
|
||||
|
||||
if 'home_library_cards' in kwargs:
|
||||
if kwargs['home_library_cards'] != 'library_statistics':
|
||||
kwargs['home_library_cards'] = ', '.join(kwargs['home_library_cards'])
|
||||
|
||||
plexpy.CONFIG.process_kwargs(kwargs)
|
||||
|
||||
# Write the config
|
||||
|
@ -537,15 +534,6 @@ class WebInterface(object):
|
|||
|
||||
@cherrypy.expose
|
||||
def set_notification_config(self, **kwargs):
|
||||
# Handle the variable config options. Note - keys with False values aren't getting passed
|
||||
|
||||
checked_configs = [
|
||||
"email_tls"
|
||||
]
|
||||
for checked_config in checked_configs:
|
||||
if checked_config not in kwargs:
|
||||
# checked items should be zero or one. if they were not sent then the item was not checked
|
||||
kwargs[checked_config] = 0
|
||||
|
||||
for plain_config, use_config in [(x[4:], x) for x in kwargs if x.startswith('use_')]:
|
||||
# the use prefix is fairly nice in the html, but does not match the actual config
|
||||
|
@ -1124,6 +1112,18 @@ class WebInterface(object):
|
|||
else:
|
||||
logger.warn('Unable to retrieve data.')
|
||||
|
||||
@cherrypy.expose
|
||||
def get_server_children(self, **kwargs):
|
||||
|
||||
pms_connect = pmsconnect.PmsConnect()
|
||||
result = pms_connect.get_server_children()
|
||||
|
||||
if result:
|
||||
cherrypy.response.headers['Content-type'] = 'application/json'
|
||||
return json.dumps(result)
|
||||
else:
|
||||
logger.warn('Unable to retrieve data.')
|
||||
|
||||
@cherrypy.expose
|
||||
def get_activity(self, **kwargs):
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue