def _sendNotification(self, info, playerid): playqueue = self.playqueue.playqueues[playerid] xargs = getXArgsDeviceInfo() params = { 'containerKey': self.containerKey or "/library/metadata/900000", 'key': self.lastkey or "/library/metadata/900000", 'ratingKey': self.ratingkey or "900000", 'state': info['state'], 'time': info['time'], 'duration': info['duration'] } if state.PLEX_TRANSIENT_TOKEN: xargs['X-Plex-Token'] = state.PLEX_TRANSIENT_TOKEN elif playqueue.plex_transient_token: xargs['X-Plex-Token'] = playqueue.plex_transient_token if info.get('playQueueID'): params['containerKey'] = '/playQueues/%s' % info['playQueueID'] params['playQueueVersion'] = info['playQueueVersion'] params['playQueueItemID'] = info['playQueueItemID'] serv = self.getServerByHost(self.server) url = '%s://%s:%s/:/timeline' % (serv.get( 'protocol', 'http'), serv.get( 'server', 'localhost'), serv.get('port', '32400')) self.doUtils(url, parameters=params, headerOptions=xargs) log.debug("Sent server notification with parameters: %s to %s" % (params, url))
def startSession(self, reset=False): """ User should be authenticated when this method is called (via userclient) """ # Start session self.s = requests.Session() self.deviceId = client.getDeviceId() # Attach authenticated header to the session self.s.headers = client.getXArgsDeviceInfo() self.s.encoding = 'utf-8' # Set SSL settings self.setSSL() # Set other stuff self.setServer(window('pms_server')) self.setToken(window('pms_token')) self.setUserId(window('currUserId')) self.setUsername(window('plex_username')) # Counters to declare PMS dead or unauthorized # Use window variables because start of movies will be called with a # new plugin instance - it's impossible to share data otherwise if reset is True: window('countUnauthorized', value='0') window('countError', value='0') # Retry connections to the server self.s.mount("http://", requests.adapters.HTTPAdapter(max_retries=1)) self.s.mount("https://", requests.adapters.HTTPAdapter(max_retries=1)) log.info("Requests session started on: %s" % self.server)
def getHeader(self, options=None): header = client.getXArgsDeviceInfo() if options is not None: header.update(options) return header
def answer_request(self, send_data): self.serverlist = self.server.client.getServerList() sub_mgr = self.server.subscription_manager request_path = self.path[1:] request_path = sub(r"\?.*", "", request_path) url = urlparse(self.path) paramarrays = parse_qs(url.query) params = {} for key in paramarrays: params[key] = paramarrays[key][0] LOG.debug("remote request_path: %s", request_path) LOG.debug("params received from remote: %s", params) sub_mgr.update_command_id( self.headers.get('X-Plex-Client-Identifier', self.client_address[0]), params.get('commandID')) if request_path == "version": self.response( "PlexKodiConnect Plex Companion: Running\nVersion: %s" % v.ADDON_VERSION) elif request_path == "verify": self.response("XBMC JSON connection test:\n" + js.ping()) elif request_path == 'resources': self.response( RESOURCES_XML.format( title=v.DEVICENAME, machineIdentifier=v.PKC_MACHINE_IDENTIFIER), getXArgsDeviceInfo(include_token=False)) elif request_path == 'player/timeline/poll': # Plex web does polling if connected to PKC via Companion # Only reply if there is indeed something playing # Otherwise, all clients seem to keep connection open if params.get('wait') == '1': MONITOR.waitForAbort(0.95) if self.client_address[0] not in CLIENT_DICT: CLIENT_DICT[self.client_address[0]] = [] tracker = CLIENT_DICT[self.client_address[0]] tracker.append(self.client_address[1]) while (not PLAYER.isPlaying() and not MONITOR.abortRequested() and sub_mgr.stop_sent_to_web and not (len(tracker) >= 4 and tracker[0] == self.client_address[1])): # Keep at most 3 connections open, then drop the first one # Doesn't need to be thread-save # Silly stuff really MONITOR.waitForAbort(1) # Let PKC know that we're releasing this connection tracker.pop(0) msg = sub_mgr.msg( js.get_players()).format(command_id=params.get('commandID', 0)) if sub_mgr.isplaying: self.response( msg, { 'X-Plex-Client-Identifier': v.PKC_MACHINE_IDENTIFIER, 'X-Plex-Protocol': '1.0', 'Access-Control-Allow-Origin': '*', 'Access-Control-Max-Age': '1209600', 'Access-Control-Expose-Headers': 'X-Plex-Client-Identifier', 'Content-Type': 'text/xml;charset=utf-8' }) elif not sub_mgr.stop_sent_to_web: sub_mgr.stop_sent_to_web = True LOG.debug('Signaling STOP to Plex Web') self.response( msg, { 'X-Plex-Client-Identifier': v.PKC_MACHINE_IDENTIFIER, 'X-Plex-Protocol': '1.0', 'Access-Control-Allow-Origin': '*', 'Access-Control-Max-Age': '1209600', 'Access-Control-Expose-Headers': 'X-Plex-Client-Identifier', 'Content-Type': 'text/xml;charset=utf-8' }) else: # Fail connection with HTTP 500 error - has been open too long self.response( 'Need to close this connection on the PKC side', { 'X-Plex-Client-Identifier': v.PKC_MACHINE_IDENTIFIER, 'X-Plex-Protocol': '1.0', 'Access-Control-Allow-Origin': '*', 'Access-Control-Max-Age': '1209600', 'Access-Control-Expose-Headers': 'X-Plex-Client-Identifier', 'Content-Type': 'text/xml;charset=utf-8' }, code=500) elif "/subscribe" in request_path: self.response(v.COMPANION_OK_MESSAGE, getXArgsDeviceInfo(include_token=False)) protocol = params.get('protocol') host = self.client_address[0] port = params.get('port') uuid = self.headers.get('X-Plex-Client-Identifier') command_id = params.get('commandID', 0) sub_mgr.add_subscriber(protocol, host, port, uuid, command_id) elif "/unsubscribe" in request_path: self.response(v.COMPANION_OK_MESSAGE, getXArgsDeviceInfo(include_token=False)) uuid = self.headers.get('X-Plex-Client-Identifier') \ or self.client_address[0] sub_mgr.remove_subscriber(uuid) else: # Throw it to companion.py process_command(request_path, params) self.response('', getXArgsDeviceInfo(include_token=False))