Advertisement
lincthra

PlexApiController

Dec 30th, 2016
450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.28 KB | None | 0 0
  1. """
  2. Controller to interface with the Plex-app.
  3. """
  4. from . import BaseController
  5.  
  6. MESSAGE_TYPE = 'type'
  7.  
  8. TYPE_PLAY = "PLAY"
  9. TYPE_PAUSE = "PAUSE"
  10. TYPE_STOP = "STOP"
  11.  
  12. class PlexController(BaseController):
  13.     """ Controller to interact with Plex namespace. """
  14.  
  15.     def __init__(self):
  16.         super(PlexController, self).__init__(
  17.             "urn:x-cast:plex", "9AC194DC")
  18.         self.app_id="9AC194DC"
  19.         self.namespace="urn:x-cast:plex"
  20.         self.request_id = 0
  21.  
  22.     def stop(self):
  23.         """ Send stop command. """
  24.         self.namespace = "urn:x-cast:plex"
  25.         self.request_id += 1
  26.         self.send_message({MESSAGE_TYPE: TYPE_STOP})
  27.  
  28.     def pause(self):
  29.         """ Send pause command. """
  30.         self.namespace = "urn:x-cast:plex"
  31.         self.request_id += 1
  32.         self.send_message({MESSAGE_TYPE: TYPE_PAUSE})
  33.  
  34.     def play(self):
  35.         """ Send play command. """
  36.         self.namespace = "urn:x-cast:plex"
  37.         self.request_id += 1
  38.         self.send_message({MESSAGE_TYPE: TYPE_PLAY})
  39.  
  40.  
  41.     def play_media(self,item,server,medtype="LOAD"):
  42.         def app_launched_callback():
  43.                 self.set_load(item,server,medtype)
  44.  
  45.         receiver_ctrl = self._socket_client.receiver_controller
  46.         receiver_ctrl.launch_app(self.app_id,
  47.                                 callback_function=app_launched_callback)
  48.  
  49.     def set_load(self,item,server,medtype="LOAD"):
  50.         transient_token = server.query("/security/token?type=delegation&scope=all").attrib.get('token')
  51.         playqueue = server.createPlayQueue(item).playQueueID
  52.         self.request_id += 1
  53.         address = server.baseurl.split(":")[1][2:]
  54.         self.namespace="urn:x-cast:com.google.cast.media"
  55.         msg = {
  56.                 "type": medtype,
  57.                 "requestId": self.request_id,
  58.                 "sessionId": "python_player",
  59.                 "autoplay": True,
  60.                 "currentTime": 0,
  61.                 "media":{
  62.                         "contentId": item.key,
  63.                         "streamType": "BUFFERED",
  64.                         "contentType": "video",
  65.                         "customData": {
  66.                                 "offset": 0,
  67.                                 "server": {
  68.                                         "machineIdentifier": server.machineIdentifier,
  69.                                         "transcoderVideo": True,
  70.                                         "transcoderVideoRemuxOnly": False,
  71.                                         "transcoderAudio": True,
  72.                                         "version": "1.3.3.3148",
  73.                                         "myPlexSubscription": False,
  74.                                         "isVerifiedHostname": True,
  75.                                         "protocol": "https",
  76.                                         "address": address,
  77.                                         "port": "32400",
  78.                                         "accessToken": transient_token,
  79.                                 },
  80.                                 "user": {"username": server.myPlexUsername},
  81.                                 "containerKey": "/playQueues/{}?own=1&window=200".format(playqueue),
  82.                         },
  83.                 }
  84.         }
  85.         self.send_message(msg, inc_session_id=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement