tubearchivist / tubearchivist-plex

Tube Archivist Plex Integration
GNU General Public License v3.0
106 stars 6 forks source link

[Feature Request] Automatically Add Playlists #38

Open LordFransie opened 7 months ago

LordFransie commented 7 months ago

I've read the documentation.

Your Feature Request

Is your feature request related to a problem? Please describe.

It is not.

Describe the solution you'd like

I’d like TA’s Plex Plugin to automatically create playlists

Additional context

I’m aware this is on the roadmap but is not currently in development. I am creating this ticket to act as a hub to discuss the issue. I’ve already gone through the plugin’s code and I’d like to start the development on this feature.

Your help is needed!

lamusmaser commented 7 months ago

From my initial brain dump on this issue: Playlists will probably become collections. Collections should be notated whether they are plug-in created vs other (this would hopefully correct #37). Collections that are plug-in created would then be imported/updated following the appropriate scan/ update logic. There should be a setting to enable/ disable automatic import of playlists.

Let me know if you have specific questions and I'll try to be responsive.

lamusmaser commented 4 months ago

Have you been able to work on this at all? If not, I will look into it further as potential feature with v0.2.0. Once I have more details, I can provide my notes here.

LordFransie commented 1 month ago

Okay, I have a rather janky script that just barely works

from plexapi.server import PlexServer
import requests
import os

# Replace with your Plex server's URL and API token
PLEX_SERVER_URL = ''  # Replace with your Plex server URL
PLEX_API_TOKEN = ''  # Replace with your Plex token
LIBRARY_NAME = 'TubeArchivist' # Replace with the name of your Plex TubeArchivist Library Name

# Get a list of all playlists
TUBEARCHIVIST_URL = ""
TUBEARCHISIT_API = ""

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("")
tehniemer commented 4 days ago

Another option for this may be to add a playlist as a "Series" in plex, with the additional option to allow channels to be hidden if they are not subscribed in TA.

For example, I subscribe to a bunch of playlists that have content from multiple channels. All of those channels show up as individual series in my plex library with one or two videos in them, I'd rather see the subscribed playlist show up as the series and the individual channels be hidden. I'm not sure if this is possible or should have a separate feature request.

lamusmaser commented 3 days ago

@tehniemer Unfortunately, this one isn't possible. Because of how the relationship has to be associated when generating the Plex reference ID, I cannot a) associate the video in two places nor b) associate it as a Series component. This is because I generate the details based on the filesystem structure as a fallback for when the metadata isn't provided, and those are only associative to Channel/Video details. YouTube Playlists by nature will have to come in as Collections since Plex Playlists are a per-account level interaction and cannot be created by other users. Collections are the only thing that I've found that perform as expected for this area, just haven't had the development time to actually generate the functionality, although @LordFransie's work does help in this arena to lower the threshold for time requirements.

tehniemer commented 3 days ago

Understood, I could live with collections of individually subscribed content, playlists or channels, sounds like that's possible?

tehniemer commented 3 days ago

Maybe some of the methodology in this project could help with collection building?

https://github.com/Kometa-Team/Kometa

LordFransie commented 3 days ago

Understood, I could live with collections of individually subscribed content, playlists or channels, sounds like that's possible?

So currently the script that I posted does this. It turns each playlist into their own collection, so they'll actually show up alongside the channels when you look at library. Then if you want an even easier time going through them you can just hit "collections" and it will just show you your playlists turned collection.

I have an updated version I need to post that handles pagination and also adds playlists as playlists since that works a bit easier for me for downloading purposes, though it does still have the shortfalls mentioned earlier where playlists don't show up for everyone else on the server.

Feel free to give it a rip, it's python3 and very easy to run.

tehniemer commented 3 days ago

Does it put each subscribed channel into its own collection also? I would personally like to be able to go to one location in plex to find only my subscribed content, both channels and playlists.

This is the example of what I'm struggling with, I don't want to see all the single video channels since I'm not actually subscribed to them, but the video is part of a playlist. image

LordFransie commented 3 days ago

No. Channels are still their own series.

The script that I wrote asks tube archivist for a list of all the playlists it has and then recreates those in Plex as collections.

So you can click on the “collections” button for the library and only view those collections.

I recommend creating a test collection to see how the collection experience works.

On Wed, Sep 11, 2024 at 7:29 AM tehniemer @.***> wrote:

Does it put each subscribed channel into its own collection also? I would personally like to be able to go to one location in plex to find only my subscribed content, both channels and playlists.

This is the example of what I'm struggling with, I don't want to see all the single video channels since I'm not actually subscribed to them, but the video is part of a playlist. image.png (view on web) https://github.com/user-attachments/assets/19785787-f331-427c-b672-56c0879c1355

— Reply to this email directly, view it on GitHub https://github.com/tubearchivist/tubearchivist-plex/issues/38#issuecomment-2343374554, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAEDZAGPCUDX4JR56Z4PHJTZWASSPAVCNFSM6AAAAABCNCF75KVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGNBTGM3TINJVGQ . You are receiving this because you were mentioned.Message ID: @.***>