fix for title and transcode sessions

This commit is contained in:
Blacktwin 2018-11-19 23:41:58 -05:00
parent 27db3a6eaa
commit 591c299af6

View File

@ -40,9 +40,11 @@ def kill_session(sess_key, message):
# Check for users stream # Check for users stream
username = session.usernames[0] username = session.usernames[0]
if session.sessionKey == sess_key: if session.sessionKey == sess_key:
title = (session.grandparentTitle + ' - ' if session.type == 'episode' else '') + session.title title = unicode(session.grandparentTitle + ' - ' if session.type == 'episode' else '') + session.title
print('{user} is watching {title} and they might be asleep.'.format(user=username, title=title)) title = unicodedata.normalize('NFKD', title).encode('ascii', 'ignore').translate(None, "'")
session.stop(reason=message) session.stop(reason=message)
print('Terminated {user}\'s stream of {title} to prioritize admin stream.'.format(user=username,
title=title))
def add_to_dictlist(d, key, val): def add_to_dictlist(d, key, val):
@ -56,30 +58,32 @@ def main():
user_dict = {} user_dict = {}
for session in plex.sessions(): for session in plex.sessions():
trans_dec = session.transcodeSessions[0].videoDecision if session.transcodeSessions:
username = session.usernames[0] trans_dec = session.transcodeSessions[0].videoDecision
if trans_dec == 'transcode' and username not in ADMIN_USER: username = session.usernames[0]
sess_key = session.sessionKey if trans_dec == 'transcode' and username not in ADMIN_USER:
percent_comp = int((float(session.viewOffset) / float(session.duration)) * 100) sess_key = session.sessionKey
time_to_comp = int(int(session.duration) - int(session.viewOffset)) / 1000 / 60 percent_comp = int((float(session.viewOffset) / float(session.duration)) * 100)
title = (session.grandparentTitle + ' - ' if session.type == 'episode' else '') + session.title time_to_comp = int(int(session.duration) - int(session.viewOffset)) / 1000 / 60
title = unicodedata.normalize('NFKD', title).encode('ascii', 'ignore').translate(None, "'") title = unicode(session.grandparentTitle + ' - ' if session.type == 'episode' else '') + session.title
add_to_dictlist(user_dict, username, [sess_key, percent_comp, title, username, time_to_comp]) title = unicodedata.normalize('NFKD', title).encode('ascii', 'ignore').translate(None, "'")
add_to_dictlist(user_dict, username, [sess_key, percent_comp, title, username, time_to_comp])
# Remove users with only 1 stream. Targeting users with multiple concurrent streams # Remove users with only 1 stream. Targeting users with multiple concurrent streams
filtered_dict = {key: value for key, value in user_dict.items() filtered_dict = {key: value for key, value in user_dict.items()
if len(value) is not 1} if len(value) is not 1}
# Find who to kill and who will be finishing first. # Find who to kill and who will be finishing first.
for users in filtered_dict.values(): if filtered_dict:
to_kill = min(users, key=itemgetter(1)) for users in filtered_dict.values():
to_finish = max(users, key=itemgetter(1)) to_kill = min(users, key=itemgetter(1))
to_finish = max(users, key=itemgetter(1))
MESSAGE = DEFAULT_REASON.format(user=to_finish[3], x=len(filtered_dict.values()[0]),
video=to_finish[2], time=to_finish[1], comp=to_finish[4]) MESSAGE = DEFAULT_REASON.format(user=to_finish[3], x=len(filtered_dict.values()[0]),
video=to_finish[2], time=to_finish[1], comp=to_finish[4])
print(MESSAGE)
kill_session(to_kill[0], MESSAGE) print(MESSAGE)
kill_session(to_kill[0], MESSAGE)
if __name__ == '__main__': if __name__ == '__main__':