Files
2024-11-03 18:03:49 +00:00

75 lines
2.2 KiB
Python

from plexapi.server import PlexServer
import requests
import os
# Replace with your Plex server's URL and API token
PLEX_SERVER_URL = "https://plex.membo.co.uk" # Replace with your Plex server URL
PLEX_API_TOKEN = "isxueinfDVgHjynYFExy" # Replace with your Plex token
LIBRARY_NAME = "Youtube"
# Get a list of all playlists
TUBEARCHIVIST_URL = "https://tube-archivist.membo.co.uk"
TUBEARCHISIT_API = "1f99470b6f9378dcf45af60ad4d228e49200eeff"
playlists = []
######
# Finds playlists that the particular youtube video is in
# ytid: the youtube video youtube_id
######
def findPlaylists(ytid):
videoIn = []
for video in playlists:
if video["yt_id"] == ytid:
videoIn.append(video["playlist_name"])
return videoIn
########
# Parses the Plex File for the ytid
###########
def plexVideoID(filePath):
return os.path.splitext(os.path.basename(filePath))[0]
###########
# Creates the list of Tube Archivist playlists and videos
###########
def populatePlaylists():
headers = {"Authorization": "Token " + TUBEARCHISIT_API}
response = requests.get(
TUBEARCHIVIST_URL + "/api/playlist/", headers=headers
).json()
for item in response["data"]:
entries = item["playlist_entries"]
for entry in entries:
playlists.append(
{"playlist_name": item["playlist_name"], "yt_id": entry["youtube_id"]}
)
populatePlaylists()
# Connect to Plex server
plex = PlexServer(PLEX_SERVER_URL, PLEX_API_TOKEN)
# Get the library
library = plex.library.section(LIBRARY_NAME)
# Fetch all videos in the library
all_shows = library.all()
# Iterate through the videos and print their file names
for show in all_shows:
episodes = show.episodes()
for episode in episodes:
print("-" * 40)
print(f'Episode Title: "{episode.title}"')
print(f'Episode ID: "{plexVideoID(episode.media[-1].parts[-1].file)}"')
print("Found in following playlists")
print(findPlaylists(plexVideoID(episode.media[-1].parts[-1].file)))
for collection in findPlaylists(plexVideoID(episode.media[-1].parts[-1].file)):
episode.addCollection(collection)
print(f'Added to the : "{collection}" Collection')
print("")