Last active
September 2, 2020 22:57
-
-
Save nwithan8/512c4acadddc4a55f27bf7db09d8b19f to your computer and use it in GitHub Desktop.
Add all items from a Plex playlist to a dizqueTV channel
This file contains hidden or 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
| # RUN THIS FIRST: | |
| # pip install plexapi dizqueTV | |
| from typing import List, Union | |
| from plexapi import myplex, server, media, library, playlist | |
| from dizqueTV import API | |
| # COMPLETE THESE SETTINGS | |
| DIZQUETV_URL = "http://localhost:8000" | |
| DIZQUETV_CHANNEL_NUMBER = 1 | |
| PLEX_URL = "http://localhost:32400" | |
| PLEX_TOKEN = "thisisaplextoken" | |
| PLAYLIST_NAME = "mysmartplaylist" | |
| class Plex: | |
| def __init__(self, url, token): | |
| self.url = url | |
| self.token = token | |
| self.server = server.PlexServer(url, token) | |
| def get_users(self) -> List[myplex.MyPlexUser]: | |
| return self.server.myPlexAccount().users() | |
| def user_has_server_access(self, user) -> bool: | |
| for s in user.servers: | |
| if s.name == self.server.friendlyName: | |
| return True | |
| return False | |
| def get_playlists(self) -> List[playlist.Playlist]: | |
| return self.server.playlists() | |
| def get_playlist(self, playlist_name: str) -> Union[playlist.Playlist, None]: | |
| for playlist in self.get_playlists(): | |
| if playlist.title == playlist_name: | |
| return playlist | |
| return None | |
| def create_new_playlist(self, playlist_name, items: List[media.Media]): | |
| self.server.createPlaylist(title=playlist_name, items=items) | |
| def reset_playlist(self, playlist_name, items: List[media.Media]): | |
| playlist = self.get_playlist(playlist_name=playlist_name) | |
| if playlist: | |
| playlist.delete() | |
| self.create_new_playlist(playlist_name=playlist_name, items=items) | |
| def get_library_sections(self) -> List[library.LibrarySection]: | |
| return self.server.library.sections() | |
| def get_all_section_items(self, section) -> List[media.Media]: | |
| return section.all() | |
| def get_plex_item(self, item, section_name=None) -> Union[media.Media, None]: | |
| if section_name: | |
| results = self.server.library.section(section_name).search(title=item.title) | |
| else: | |
| results = self.server.library.search(title=item.title) | |
| for media in results: | |
| if int(media.ratingKey) == int(item.ratingKey): | |
| return media | |
| return None | |
| dtv = API(url=DIZQUETV_URL) | |
| plex = Plex(url=PLEX_URL, token=PLEX_TOKEN) | |
| plex_playlist = plex.get_playlist(playlist_name=PLAYLIST_NAME) | |
| channel = dtv.get_channel(channel_number=DIZQUETV_CHANNEL_NUMBER) | |
| to_add = [] | |
| if plex_playlist: | |
| for item in plex_playlist.items(): | |
| item = dtv.convert_plex_item_to_program(plex_item=item, plex_server=plex.server) | |
| if item: | |
| print(f"Adding {item.title}...") | |
| to_add.append(item) | |
| if channel.delete_all_programs(): | |
| dtv.add_programs_to_channels(programs=to_add, channels=[channel]) | |
| else: | |
| print(f"Could not find {PLAYLIST_NAME} playlist.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment