Skip to content

Commit 49ce2f9

Browse files
committed
Refactor getWebURL
1 parent 8a0b03c commit 49ce2f9

File tree

6 files changed

+45
-80
lines changed

6 files changed

+45
-80
lines changed

plexapi/audio.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -416,11 +416,6 @@ def _defaultSyncTitle(self):
416416
""" Returns str, default title for a new syncItem. """
417417
return '%s - %s - %s' % (self.grandparentTitle, self.parentTitle, self.title)
418418

419-
def getWebURL(self, base=None):
420-
""" Returns the Plex Web URL for the track's album.
421-
422-
Parameters:
423-
base (str): The base URL before the fragment (``#!``).
424-
Default is https://app.plex.tv/desktop.
425-
"""
426-
return self._buildWebURL(base=base, key=self.parentKey)
419+
def _getWebURL(self, base=None):
420+
""" Get the Plex Web URL with the correct parameters. """
421+
return self._server._buildWebURL(base=base, endpoint='details', key=self.parentKey)

plexapi/base.py

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -582,37 +582,20 @@ def history(self, maxresults=9999999, mindate=None):
582582
"""
583583
return self._server.history(maxresults=maxresults, mindate=mindate, ratingKey=self.ratingKey)
584584

585-
def _buildWebURL(self, base=None, endpoint='details', key='', legacy=False):
586-
""" Build the Plex Web URL for the item.
587-
588-
Parameters:
589-
base (str): The base URL before the fragment (``#!``).
590-
Default is https://app.plex.tv/desktop.
591-
endpoint (str): The Plex Web URL endpoint.
592-
'playlist' for playlists, 'details' for all other media types.
593-
key (str): The Plex API URL for the item (/library/metadata/<ratingKey>).
594-
legacy (bool): True or False to use the legacy URL.
595-
Photoalbum and Photo use the legacy URL.
585+
def _getWebURL(self, base=None):
586+
""" Get the Plex Web URL with the correct parameters.
587+
Private method to allow overriding parameters from subclasses.
596588
"""
597-
if base is None:
598-
base = 'https://app.plex.tv/desktop/'
599-
600-
params = {'key': key or self.key}
601-
if legacy:
602-
params['legacy'] = 1
603-
604-
return '%s#!/server/%s/%s%s' % (
605-
base, self._server.machineIdentifier, endpoint, utils.joinArgs(params)
606-
)
589+
return self._server._buildWebURL(base=base, endpoint='details', key=self.key)
607590

608591
def getWebURL(self, base=None):
609-
""" Returns the Plex Web URL for the item.
592+
""" Returns the Plex Web URL for a media item.
610593
611594
Parameters:
612595
base (str): The base URL before the fragment (``#!``).
613596
Default is https://app.plex.tv/desktop.
614597
"""
615-
return self._buildWebURL(base=base)
598+
return self._getWebURL(base=base)
616599

617600

618601
class Playable(object):

plexapi/library.py

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,19 +1465,13 @@ def getWebURL(self, base=None, tab=None, key=None):
14651465
tab (str): The library tab (recommended, library, collections, playlists, timeline).
14661466
key (str): A hub key.
14671467
"""
1468-
if base is None:
1469-
base = 'https://app.plex.tv/desktop/'
1470-
14711468
params = {'source': self.key}
14721469
if tab is not None:
14731470
params['pivot'] = tab
14741471
if key is not None:
14751472
params['key'] = key
14761473
params['pageType'] = 'list'
1477-
1478-
return '%s#!/media/%s/com.plexapp.plugins.library%s' % (
1479-
base, self._server.machineIdentifier, utils.joinArgs(params)
1480-
)
1474+
return self._server._buildWebURL(base=base, **params)
14811475

14821476

14831477
class MovieSection(LibrarySection):
@@ -1899,15 +1893,6 @@ def section(self):
18991893
self._section = self._server.library.sectionByID(self.librarySectionID)
19001894
return self._section
19011895

1902-
def getWebURL(self, base=None):
1903-
""" Returns the Plex Web URL for the library.
1904-
1905-
Parameters:
1906-
base (str): The base URL before the fragment (``#!``).
1907-
Default is https://app.plex.tv/desktop.
1908-
"""
1909-
return self.section().getWebURL(base=base, key=self.key)
1910-
19111896

19121897
class HubMediaTag(PlexObject):
19131898
""" Base class of hub media tag search results.

plexapi/photo.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,9 @@ def download(self, savepath=None, keep_original_name=False, showstatus=False):
137137
filepaths.append(filepath)
138138
return filepaths
139139

140-
def getWebURL(self, base=None):
141-
""" Returns the Plex Web URL for the photoalbum.
142-
143-
Parameters:
144-
base (str): The base URL before the fragment (``#!``).
145-
Default is https://app.plex.tv/desktop.
146-
"""
147-
return self._buildWebURL(base=base, legacy=True)
140+
def _getWebURL(self, base=None):
141+
""" Get the Plex Web URL with the correct parameters. """
142+
return self._server._buildWebURL(base=base, endpoint='details', key=self.key, legacy=1)
148143

149144

150145
@utils.registerPlexObject
@@ -311,11 +306,6 @@ def download(self, savepath=None, keep_original_name=False, showstatus=False):
311306
filepaths.append(filepath)
312307
return filepaths
313308

314-
def getWebURL(self, base=None):
315-
""" Returns the Plex Web URL for the photo's photoalbum.
316-
317-
Parameters:
318-
base (str): The base URL before the fragment (``#!``).
319-
Default is https://app.plex.tv/desktop.
320-
"""
321-
return self._buildWebURL(base=base, key=self.parentKey, legacy=True)
309+
def _getWebURL(self, base=None):
310+
""" Get the Plex Web URL with the correct parameters. """
311+
return self._server._buildWebURL(base=base, endpoint='details', key=self.parentKey, legacy=1)

plexapi/playlist.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -460,11 +460,6 @@ def sync(self, videoQuality=None, photoResolution=None, audioBitrate=None, clien
460460

461461
return myplex.sync(sync_item, client=client, clientId=clientId)
462462

463-
def getWebURL(self, base=None):
464-
""" Returns the Plex Web URL for the playlist.
465-
466-
Parameters:
467-
base (str): The base URL before the fragment (``#!``).
468-
Default is https://app.plex.tv/desktop.
469-
"""
470-
return self._buildWebURL(base=base, endpoint='playlist')
463+
def _getWebURL(self, base=None):
464+
""" Get the Plex Web URL with the correct parameters. """
465+
return self._server._buildWebURL(base=base, endpoint='playlist', key=self.key)

plexapi/server.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -890,24 +890,41 @@ def resources(self):
890890
key = '/statistics/resources?timespan=6'
891891
return self.fetchItems(key, StatisticsResources)
892892

893-
def getPlaylistsWebURL(self, base=None, tab=None):
894-
""" Returns the Plex Web URL for the server playlists page.
893+
def _buildWebURL(self, base=None, endpoint=None, **kwargs):
894+
""" Build the Plex Web URL for the object.
895895
896896
Parameters:
897897
base (str): The base URL before the fragment (``#!``).
898898
Default is https://app.plex.tv/desktop.
899-
tab (str): The playlist tab (audio, video, photo).
899+
endpoint (str): The Plex Web URL endpoint.
900+
None for server, 'playlist' for playlists, 'details' for all other media types.
901+
**kwargs (dict): Dictionary of URL parameters.
900902
"""
901903
if base is None:
902904
base = 'https://app.plex.tv/desktop/'
903905

904-
params = {'source': 'playlists'}
905-
if tab is not None:
906-
params['pivot'] = 'playlists.%s' % tab
906+
if endpoint:
907+
return '%s#!/server/%s/%s%s' % (
908+
base, self.machineIdentifier, endpoint, utils.joinArgs(kwargs)
909+
)
910+
else:
911+
return '%s#!/media/%s/com.plexapp.plugins.library%s' % (
912+
base, self.machineIdentifier, utils.joinArgs(kwargs)
913+
)
914+
915+
def getWebURL(self, base=None, playlistTab=None):
916+
""" Returns the Plex Web URL for the server.
907917
908-
return '%s#!/media/%s/com.plexapp.plugins.library%s' % (
909-
base, self._server.machineIdentifier, utils.joinArgs(params)
910-
)
918+
Parameters:
919+
base (str): The base URL before the fragment (``#!``).
920+
Default is https://app.plex.tv/desktop.
921+
playlistTab (str): The playlist tab (audio, video, photo). Only used for the playlist URL.
922+
"""
923+
if playlistTab is not None:
924+
params = {'source': 'playlists', 'pivot': 'playlists.%s' % playlistTab}
925+
else:
926+
params = {'key': '/hubs', 'pageType': 'hub'}
927+
return self._buildWebURL(base=base, **params)
911928

912929

913930
class Account(PlexObject):

0 commit comments

Comments
 (0)