From 253949c1feb161ee2aa2fbd615437959d57fe94e Mon Sep 17 00:00:00 2001 From: JonnyWong16 <9099342+JonnyWong16@users.noreply.github.com> Date: Thu, 2 Nov 2023 21:24:46 -0700 Subject: [PATCH] Add `MyPlexAccount.ping()` to refresh authentication token (#1271) * Add `MyPlexAccount.ping()` to refresh auth token * Test account ping --- plexapi/myplex.py | 10 ++++++++++ tests/test_myplex.py | 4 ++++ 2 files changed, 14 insertions(+) diff --git a/plexapi/myplex.py b/plexapi/myplex.py index ede2276d1..0c95aaba0 100644 --- a/plexapi/myplex.py +++ b/plexapi/myplex.py @@ -108,6 +108,7 @@ class MyPlexAccount(PlexObject): OPTOUTS = 'https://plex.tv/api/v2/user/{userUUID}/settings/opt_outs' # get LINK = 'https://plex.tv/api/v2/pins/link' # put VIEWSTATESYNC = 'https://plex.tv/api/v2/user/view_state_sync' # put + PING = 'https://plex.tv/api/v2/ping' # Hub sections VOD = 'https://vod.provider.plex.tv' # get MUSIC = 'https://music.provider.plex.tv' # get @@ -250,6 +251,15 @@ def query(self, url, method=None, headers=None, timeout=None, **kwargs): data = response.text.encode('utf8') return ElementTree.fromstring(data) if data.strip() else None + def ping(self): + """ Ping the Plex.tv API. + This will refresh the authentication token to prevent it from expiring. + """ + pong = self.query(self.PING) + if pong is not None: + return utils.cast(bool, pong.text) + return False + def device(self, name=None, clientId=None): """ Returns the :class:`~plexapi.myplex.MyPlexDevice` that matches the name specified. diff --git a/tests/test_myplex.py b/tests/test_myplex.py index f84939fc2..aac4e84b3 100644 --- a/tests/test_myplex.py +++ b/tests/test_myplex.py @@ -361,3 +361,7 @@ def test_myplex_pin(account, plex): def test_myplex_geoip(account): assert account.geoip(account.publicIP()) + + +def test_myplex_ping(account): + assert account.ping()