Created
August 3, 2023 00:03
-
-
Save JonnyWong16/769e4d3854c7dfde2d4853fb4434386c to your computer and use it in GitHub Desktop.
Dump all Plex TV library guids to json file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import requests | |
from plexapi.server import PlexServer | |
PLEX_URL = 'http://localhost:32400' | |
PLEX_TOKEN = 'XXXXXXXXXXXXXXXXXXXX' | |
TVSHOW_LIBRARY = 'TV Shows' | |
TMDB_TOKEN = '' | |
def get_tmdb_ids(series_id, season_number=None, episode_number=None): | |
headers = { | |
'Authorization': f'Bearer {TMDB_TOKEN}', | |
'Accept': 'application/json' | |
} | |
if season_number is not None and episode_number is not None: | |
url = f'https://api.themoviedb.org/3/tv/{series_id}/season/{season_number}/episode/{episode_number}?append_to_response=external_ids' | |
r = requests.get(url, headers=headers).json() | |
return r['id'], r['external_ids']['tvdb_id'], r['external_ids']['imdb_id'] | |
elif season_number is not None: | |
url = f'https://api.themoviedb.org/3/tv/{series_id}/season/{season_number}?append_to_response=external_ids' | |
r = requests.get(url, headers=headers).json() | |
return r['id'], r['external_ids']['tvdb_id'] | |
def main(): | |
plex = PlexServer(PLEX_URL, token=PLEX_TOKEN) | |
tvshows = plex.library.section(TVSHOW_LIBRARY) | |
all_guids = [] | |
for show in tvshows.all(): | |
print(show) | |
sh = { | |
'title': show.title, | |
'year': show.year, | |
'guids': { | |
'plex': show.guid, | |
'tmdb': '', | |
'tvdb': '', | |
'imdb': '', | |
}, | |
'seasons': [], | |
'episodes': [], | |
} | |
for guid in show.guids: | |
sh['guids'][guid.id[:4]] = guid.id | |
for season in tvshows.searchSeasons(filters={'show.id': show.ratingKey}): | |
se = { | |
'seasonNumber': season.seasonNumber, | |
'title': season.title, | |
'guids': { | |
'plex': season.guid, | |
'tmdb': '', | |
'tvdb': '', | |
'imdb': '', | |
}, | |
'tmdb_guids': { | |
'tmdb': '', | |
'tvdb': '', | |
'imdb': '', | |
}, | |
} | |
for guid in season.guids: | |
se['guids'][guid.id[:4]] = guid.id | |
# Retrieve external IDs from TMDB if the TMDB id is missing | |
# Note: Keep in mind shows using TheTVDB episode ordering in Plex may not have matching TMDB IDs | |
if not se['guids']['tmdb']: | |
try: | |
tmdb_id, tvdb_id = get_tmdb_ids(sh['guids']['tmdb'][7:], se['seasonNumber']) | |
se['tmdb_guids']['tmdb'] = tmdb_id | |
se['tmdb_guids']['tvdb'] = tvdb_id | |
except Exception: | |
pass | |
sh['seasons'].append(se) | |
for episode in tvshows.searchEpisodes(filters={'show.id': show.ratingKey}): | |
ep = { | |
's00e00': episode.seasonEpisode, | |
'seasonNumber': episode.seasonNumber, | |
'episodeNumber': episode.episodeNumber, | |
'title': episode.title, | |
'guids': { | |
'plex': episode.guid, | |
'tmdb': '', | |
'tvdb': '', | |
'imdb': '', | |
}, | |
'tmdb_guids': { | |
'tmdb': '', | |
'tvdb': '', | |
'imdb': '', | |
}, | |
} | |
for guid in episode.guids: | |
ep['guids'][guid.id[:4]] = guid.id | |
# Retrieve external IDs from TMDB if the TMDB id is missing | |
# Note: Keep in mind shows using TheTVDB episode ordering in Plex may not have matching TMDB IDs | |
if not ep['guids']['tmdb']: | |
try: | |
tmdb_id, tvdb_id, imdb_id = get_tmdb_ids(sh['guids']['tmdb'][7:], ep['seasonNumber'], ep['episodeNumber']) | |
ep['tmdb_guids']['tmdb'] = tmdb_id | |
ep['tmdb_guids']['tvdb'] = tvdb_id | |
ep['tmdb_guids']['imdb'] = imdb_id | |
except Exception: | |
pass | |
sh['episodes'].append(ep) | |
all_guids.append(sh) | |
with open('all_guids.json', 'w', encoding='utf-8') as f: | |
json.dump(all_guids, f) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment