chazlarson

meta_generate

Aug 22nd, 2025 (edited)
562
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.83 KB | Software | 0 0
  1. import yaml
  2. from plexapi.server import PlexServer
  3. from plexapi.exceptions import NotFound
  4.  
  5. # --- CONFIGURATION ---
  6. # Replace with your Plex server URL and token
  7. PLEX_URL = 'http://YOUR_PLEX_IP:32400'
  8. PLEX_TOKEN = 'YOUR_PLEX_TOKEN'
  9.  
  10. # The name of the library you want to export (e.g., 'Movies', 'TV Shows')
  11. LIBRARY_NAME = 'Movies'
  12.  
  13. # The name for the output YAML file
  14. OUTPUT_FILE = 'plex_library_metadata.yml'
  15.  
  16. # --- SCRIPT ---
  17.  
  18. def get_imdb_id(plex_item):
  19.     """
  20.    Parses the Plex item's GUID to find and return the IMDb ID (ttXXXXXXX).
  21.    Returns None if no IMDb ID is found.
  22.    """
  23.     for guid in plex_item.guids:
  24.         if guid.id.startswith('imdb://'):
  25.             # The format is 'imdb://tt1234567'
  26.             return guid.id.split('//')[-1]
  27.     return None
  28.  
  29. def generate_yaml_from_plex():
  30.     """
  31.    Connects to a Plex server, retrieves library items, and
  32.    generates a YAML file in the specified format.
  33.    """
  34.     try:
  35.         # Connect to your Plex server
  36.         plex = PlexServer(PLEX_URL, PLEX_TOKEN)
  37.         print(f"Successfully connected to Plex server at {PLEX_URL}")
  38.  
  39.         # Get the specified library
  40.         library = plex.library.section(LIBRARY_NAME)
  41.         print(f"Found library: '{library.title}'")
  42.  
  43.         # The final dictionary to hold all the data
  44.         all_metadata = {}
  45.  
  46.         # Loop through each item in the library
  47.         for item in library.all():
  48.             imdb_id = get_imdb_id(item)
  49.  
  50.             # Skip items without an IMDb ID, as it's required for the format
  51.             if not imdb_id:
  52.                 print(f"Skipping '{item.title}' - no IMDb ID found.")
  53.                 continue
  54.  
  55.             # Get the list of genres
  56.             genre = item.genres[0].tag
  57.  
  58.             # Create the nested dictionary for this item
  59.             item_data = {
  60.                 'match': {
  61.                     'mapping_id': imdb_id
  62.                 },
  63.                 'genre.sync': genre
  64.             }
  65.  
  66.             # Add the item's data to the main dictionary, using the title as the key
  67.             all_metadata[item.title] = item_data
  68.  
  69.         # Wrap the entire dictionary in the 'metadata' key
  70.         final_yaml_data = {'metadata': all_metadata}
  71.  
  72.         # Write the data to a YAML file
  73.         with open(OUTPUT_FILE, 'w', encoding='utf-8') as file:
  74.             # Use yaml.dump to write to the file. We can add a custom flow_style for better readability.
  75.             yaml.dump(final_yaml_data, file, allow_unicode=True, sort_keys=False)
  76.  
  77.         print(f"Successfully generated '{OUTPUT_FILE}' with {len(all_metadata)} items!")
  78.  
  79.     except NotFound:
  80.         print(f"Library '{LIBRARY_NAME}' not found. Please check the library name and try again.")
  81.     except Exception as e:
  82.         print(f"An error occurred: {e}")
  83.  
  84. if __name__ == '__main__':
  85.     generate_yaml_from_plex()
Advertisement