diff --git a/plexapi/myplex.py b/plexapi/myplex.py index 84970dafd..517df1986 100644 --- a/plexapi/myplex.py +++ b/plexapi/myplex.py @@ -2,6 +2,7 @@ import copy import threading import time +from typing import List from xml.etree import ElementTree import requests @@ -14,6 +15,7 @@ from plexapi.server import PlexServer from plexapi.sonos import PlexSonosClient from plexapi.sync import SyncItem, SyncList +from plexapi.together import Together from plexapi.utils import joinArgs from requests.status_codes import _codes as codes @@ -83,6 +85,7 @@ class MyPlexAccount(PlexObject): NEWS = 'https://news.provider.plex.tv/' # get PODCASTS = 'https://podcasts.provider.plex.tv/' # get MUSIC = 'https://music.provider.plex.tv/' # get + TOGETHER = 'https://together.plex.tv/' # get # Key may someday switch to the following url. For now the current value works. # https://plex.tv/api/v2/user?X-Plex-Token={token}&X-Plex-Client-Identifier={clientId} key = 'https://plex.tv/users/account' @@ -689,12 +692,16 @@ def podcasts(self): return self.findItems(elem) def tidal(self): - """ Returns a list of tidal Hub items :class:`~plexapi.library.Hub` + """ Returns a list of Tidal Hub items :class:`~plexapi.library.Hub` """ req = requests.get(self.MUSIC + 'hubs/', headers={'X-Plex-Token': self._token}) elem = ElementTree.fromstring(req.text) return self.findItems(elem) + @property + def watch_together(self) -> Together: + return Together(endpoint=self.TOGETHER, token=self._token) # returns JSON, not XML + def link(self, pin): """ Link a device to the account using a pin code. diff --git a/plexapi/together.py b/plexapi/together.py new file mode 100644 index 000000000..899556a56 --- /dev/null +++ b/plexapi/together.py @@ -0,0 +1,49 @@ +# -*- coding: utf-8 -*- +import requests + +from plexapi import utils + + +class RoomUser: + """ Represents a single RoomUser.""" + + def __init__(self, data): + self._data = data + self.id = data.get('id') + self.subscription = data.get('subscription') + self.thumbUri = data.get('thumb') + self.username = data.get('title') + self.uuid = data.get('uuid') + + +class Room: + """ Represents a single Room.""" + + def __init__(self, data): + self._data = data + self.endsAt = utils.toDatetime(data.get('endsAt')) + self.id = data.get('id') + self.sourceUri = data.get('sourceUri') + self.startsAt = utils.toDatetime(data.get('startsAt')) + self.syncplayHost = data.get('syncplayHost') + self.syncplayPort = data.get('syncplayPort') + self.title = data.get('title') + self.users = [RoomUser(user) for user in data.get('users', [])] + self.updatedAt = utils.toDatetime(data.get('updatedAt')) + self.source = data.get('source') + + +class Together: + def __init__(self, endpoint, token): + self.endpoint = endpoint + self._token = token + + @property + def rooms(self): + rooms = [] + res = requests.get(self.endpoint + 'rooms', headers={'X-Plex-Token': self._token}) + if res: + data = res.json() + for room in data.get('rooms', []): + rooms.append(Room(room)) + return rooms