Good day plex!

This commit is contained in:
John 2015-09-28 03:04:15 +02:00
parent f7bc208fd1
commit 37df262b24
5 changed files with 578 additions and 53 deletions

View file

@ -1,3 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This file is part of PlexPy.
#
# PlexPy is free software: you can redistribute it and/or modify
@ -14,7 +17,8 @@
# along with PlexPy. If not, see <http://www.gnu.org/licenses/>.
from plexpy import logger, helpers, users, http_handler, database
import xmltodict
import json
from xml.dom import minidom
import base64
@ -401,4 +405,21 @@ class PlexTV(object):
server_urls.append(server_details)
return server_urls
return server_urls
def discover(self):
""" Query plex for all servers online. Returns the ones you own in a selectize format """
result = self.get_plextv_resources(include_https=True, output_format='raw')
clean_servers = []
servers = xmltodict.parse(result, process_namespaces=True, attr_prefix='')
if servers:
for server in servers['MediaContainer']['Device']:
if server.get('presence', None) == '1' and server.get('owned', None) == '1':
for s in server['Connection']:
s.update(server)
s['value'] = s['address']
s['label'] = server['name']
del s['Connection']
clean_servers.append(s)
return json.dumps(clean_servers, indent=4)

View file

@ -1,4 +1,7 @@
# This file is part of PlexPy.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 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
@ -97,8 +100,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:
if plexpy.CONFIG.FIRST_RUN_COMPLETE:
plexpy.initialize_scheduler()
raise cherrypy.HTTPRedirect("home")
else:
@ -566,7 +568,7 @@ class WebInterface(object):
watched_percent = plexpy.CONFIG.NOTIFY_WATCHED_PERCENT
custom_where=[]
custom_where = []
if user_id:
custom_where = [['session_history.user_id', user_id]]
elif user:
@ -887,7 +889,7 @@ class WebInterface(object):
@cherrypy.expose
def get_user_ips(self, user_id=None, user=None, **kwargs):
custom_where=[]
custom_where = []
if user_id:
custom_where = [['user_id', user_id]]
elif user:
@ -1121,7 +1123,7 @@ class WebInterface(object):
pms_connect = pmsconnect.PmsConnect()
result = pms_connect.get_server_children()
if result:
cherrypy.response.headers['Content-type'] = 'application/json'
return json.dumps(result)
@ -1386,8 +1388,8 @@ class WebInterface(object):
new_key_list = pms_connect.get_rating_keys_list(rating_key=new_rating_key, media_type=media_type)
update_db = data_factory.update_rating_key(old_key_list=old_key_list,
new_key_list=new_key_list,
media_type=media_type)
new_key_list=new_key_list,
media_type=media_type)
if update_db:
cherrypy.response.headers['Content-type'] = 'application/json'
@ -1396,7 +1398,6 @@ class WebInterface(object):
cherrypy.response.headers['Content-type'] = 'application/json'
return json.dumps({'message': 'no data received'})
# test code
@cherrypy.expose
def get_new_rating_keys(self, rating_key='', media_type='', **kwargs):
@ -1433,11 +1434,27 @@ class WebInterface(object):
new_key_list = pms_connect.get_rating_keys_list(rating_key=new_rating_key, media_type=media_type)
result = data_factory.update_rating_key(old_key_list=old_key_list,
new_key_list=new_key_list,
media_type=media_type)
new_key_list=new_key_list,
media_type=media_type)
if result:
cherrypy.response.headers['Content-type'] = 'application/json'
return json.dumps(result)
else:
logger.warn('Unable to retrieve data.')
@cherrypy.expose
def discover(self, token=''):
"""
Returns the servers that you own as a
list of dicts (formatted for selectize)
"""
# Need to set token so result dont return http 401
plexpy.CONFIG.__setattr__('PMS_TOKEN', token)
plexpy.CONFIG.write()
result = plextv.PlexTV()
servers = result.discover()
if servers:
cherrypy.response.headers['Content-type'] = 'application/json'
return servers