class PlexController(): plex = None plexClient = None plexLibrary = None def __init__(self): self.plex = PlexServer(secrets.plexBaseUrl, secrets.token) self.plexLibrary = self.plex.library.section(secrets.librarySection) def initializeRoku(self): self.plexClient = self.plex.client(secrets.rokuClientName) def printClients(self): print("Available plex clients:") for client in self.plex.clients(): print(client.title) # Get a show by name def getShow(self, showName): shows = self.plexLibrary.search(title=showName) # We're just going to return the first if shows: return shows[0] return None def getEpisodeCount(self, showName): show = self.getShow(showName) return len(show.episodes()) if show else None def getUnwatchedEpisodeCount(self, showName): show = self.getShow(showName) return len(list(filter(lambda x: not x.isWatched, show.episodes()))) def getNextEpisode(self, showName): show = self.getShow(showName) for episode in show.episodes(): if episode.isWatched: continue return episode return None def playEpisode(self, episode): self.plexClient.playMedia(episode) def resetWatched(self, showName): show = self.getShow(showName) for episode in show.episodes(): episode.markUnwatched()
def plexlogin(): global plex global client baseurl = "http://serverip:port" from plexapi.server import PlexServer plex = PlexServer(baseurl) client = plex.client("clientnamegoeshere")
def playstatus(): sql = sqlite3.connect(MYDB) cur = sql.cursor() cur.execute('SELECT setting FROM settings WHERE item LIKE \'PLEXUN\'') PLEXUN = cur.fetchone() PLEXUN = PLEXUN[0] cur.execute('SELECT setting FROM settings WHERE item LIKE \'PLEXPW\'') PLEXPW = cur.fetchone() PLEXPW = PLEXPW[0] cur.execute('SELECT setting FROM settings WHERE item LIKE \'PLEXSVR\'') PLEXSVR = cur.fetchone() PLEXSVR = PLEXSVR[0] cur.execute('SELECT setting FROM settings WHERE item LIKE \'PLEXCLIENT\'') PLEXCLIENT = cur.fetchone() PLEXCLIENT = PLEXCLIENT[0] cur.execute( 'SELECT setting FROM settings WHERE item LIKE \'PLEXSERVERIP\'') PLEXSERVERIP = cur.fetchone() PLEXSERVERIP = PLEXSERVERIP[0] cur.execute( 'SELECT setting FROM settings WHERE item LIKE \'PLEXSERVERPORT\'') PLEXSERVERPORT = cur.fetchone() PLEXSERVERPORT = PLEXSERVERPORT[0] from plexapi.myplex import MyPlexAccount try: from plexapi.server import PlexServer baseurl = 'http://' + PLEXSERVERIP + ':' + PLEXSERVERPORT plex = PlexServer(baseurl, token) except Exception: user = MyPlexAccount.signin(PLEXUN, PLEXPW) print("Local Fail. Trying cloud access.") plex = user.resource(PLEXSVR).connect() client = plex.client(PLEXCLIENT) pstatus = client.isPlayingMedia() sql.close() #print (pstatus) if pstatus is True: return ("Playing") else: return ("Stopped")
def plexlogin(): global PLEXUN global PLEXSVR global PLEXCLIENT global plex global client global LOGGEDIN try: cur.execute('SELECT setting FROM settings WHERE item LIKE \'PLEXUN\'') PLEXUN = cur.fetchone() PLEXUN = PLEXUN[0] cur.execute('SELECT setting FROM settings WHERE item LIKE \'PLEXPW\'') PLEXPW = cur.fetchone() PLEXPW = PLEXPW[0] cur.execute('SELECT setting FROM settings WHERE item LIKE \'PLEXSVR\'') PLEXSVR = cur.fetchone() PLEXSVR = PLEXSVR[0] cur.execute( 'SELECT setting FROM settings WHERE item LIKE \'PLEXCLIENT\'') PLEXCLIENT = cur.fetchone() PLEXCLIENT = PLEXCLIENT[0] try: cur.execute( 'SELECT setting FROM settings WHERE item LIKE \'PLEXSERVERIP\'' ) PLEXSERVERIP = cur.fetchone() PLEXSERVERIP = PLEXSERVERIP[0] cur.execute( 'SELECT setting FROM settings WHERE item LIKE \'PLEXSERVERPORT\'' ) PLEXSERVERPORT = cur.fetchone() PLEXSERVERPORT = PLEXSERVERPORT[0] except Exception: print("Local Variables not set. Run setup to use local access.") from plexapi.myplex import MyPlexAccount try: LOGGEDIN except Exception: try: from plexapi.server import PlexServer baseurl = 'http://' + PLEXSERVERIP + ':' + PLEXSERVERPORT plex = PlexServer(baseurl) except Exception: print("Local Fail. Trying cloud access.") user = MyPlexAccount.signin(PLEXUN, PLEXPW) plex = user.resource(PLEXSVR).connect() client = plex.client(PLEXCLIENT) LOGGEDIN = "YES" except IndexError: print( "Error getting necessary plex api variables. Run system_setup.py.")
def sessionstatus(): sql = sqlite3.connect(MYDB) cur = sql.cursor() cur.execute('SELECT setting FROM settings WHERE item LIKE \'PLEXUN\'') PLEXUN = cur.fetchone() PLEXUN = str(PLEXUN[0]).strip() cur.execute('SELECT setting FROM settings WHERE item LIKE \'PLEXPW\'') PLEXPW = cur.fetchone() PLEXPW = str(PLEXPW[0]).strip() cur.execute('SELECT setting FROM settings WHERE item LIKE \'PLEXSVR\'') PLEXSVR = cur.fetchone() PLEXSVR = PLEXSVR[0] cur.execute('SELECT setting FROM settings WHERE item LIKE \'PLEXCLIENT\'') PLEXCLIENT = cur.fetchone() PLEXCLIENT = PLEXCLIENT[0] cur.execute( 'SELECT setting FROM settings WHERE item LIKE \'PLEXSERVERIP\'') PLEXSERVERIP = cur.fetchone() PLEXSERVERIP = PLEXSERVERIP[0] cur.execute( 'SELECT setting FROM settings WHERE item LIKE \'PLEXSERVERPORT\'') PLEXSERVERPORT = cur.fetchone() PLEXSERVERPORT = PLEXSERVERPORT[0] from plexapi.myplex import MyPlexAccount try: from plexapi.server import PlexServer baseurl = 'http://' + PLEXSERVERIP + ':' + PLEXSERVERPORT plex = PlexServer(baseurl) print("using local access.") except Exception: print("Local Fail. Trying cloud access.") user = MyPlexAccount.signin(PLEXUN, PLEXPW) plex = user.resource(PLEXSVR).connect() client = plex.client(PLEXCLIENT) psess = plex.sessions() if not psess: return ("Stopped") else: cur.execute( 'SELECT State FROM States WHERE Option LIKE \'Nowplaying\'') nowp = cur.fetchone() nowp = nowp[0] if "TV Show:" in nowp: nowp = nowp.split("Episode: ") nowp = nowp[1] elif "Movie:" in nowp: nowp = nowp.split("Movie: ") nowp = nowp[1] for sess in psess: sess = sess.title if nowp in sess: #print ("Go") return ("Playing") print("Fail") return ("Unknown")
else: return prop_type(input(f'Please enter the: {prop_key}:')) if __name__ == '__main__': try: with open('config.json') as config_file: config = json.loads(config_file.read()) except: config = {} client_type = get_prop(config, 'client_type') sync_url = get_prop(config, 'sync_url') if client_type == 'vlc': passwd = get_prop(config, 'passwd') vlc_url = get_prop(config, 'vlc_url') loop_vlc() elif client_type == 'plex': token = get_prop(config, 'token') plex_url = get_prop(config, 'plex_url') client_name = get_prop(config, 'client_name') plex = PlexServer(plex_url, token) client = plex.client(client_name) client.pause() loop_plex() else: print('Invalid client type!!!!!')
class PlexBackend(): def __init__(self, plexurl, token, libname, data_path, client_name): self.token = token self.plexurl = plexurl self.lib_name = libname self.data_path = data_path self.client_name = client_name self.plex = PlexServer(self.plexurl, self.token) self.music = self.plex.library.section(self.lib_name) def down_plex_lib(self): songs = {} try: playlists = self.plex.playlists() songs["playlist"] = {} for p in playlists: p_name = p.title songs["playlist"][p_name] = [] for track in p.items(): title = track.title album = track.album().title artist = track.artist().title file_key = self.get_file(track) file = self.get_tokenized_uri( file_key ) songs["playlist"][p_name].append([artist, album, title, file]) root = self.music.all() artists = defaultdict(list) albums = defaultdict(list) titles = defaultdict(list) count = 0 for artist in root: artist_title = artist.title songs[artist_title] = {} for album in artist.albums(): album_title = album.title songs[artist_title][album_title] = [] for track in album.tracks(): title = track.title key = track.key file_key = self.get_file(track) file = self.get_tokenized_uri( file_key ) try: print("""%d %s -- %s %s %s %s """ % (count, artist_title, album_title, title,file_key, key)) songs[artist_title][album_title].append([title, file, key]) count += 1 except Exception as ex: print(ex) self.json_save(songs, self.data_path) print("done loading library") except Exception as e: print(e) return None def json_save(self, data, fname): with open(fname, 'w') as fp: dump(data, fp) def json_load(self, fname): with open(fname, 'r') as fp: return load(fp) def get_tokenized_uri(self, uri): return self.plexurl+uri+"?X-Plex-Token="+self.token def get_file(self,track): for media in track.media: for p in media.parts: return p.key def play_media(self, key, media_type): client = self.plex.client(self.client_name) item = self.plex.library.fetchItem(key) if media_type == "album": item = self.plex.library.fetchItem(item.parentKey) client.playMedia(item) elif media_type == "artist": item = self.plex.library.fetchItem(item.grandparentKey) queue = self.plex.createPlayQueue(item, shuffle = 1) client.playMedia(queue) else: client.playMedia(item) def pause(self): client = self.plex.client(self.client_name) client.pause("music") def next(self): client = self.plex.client(self.client_name) client.skipNext("music") def previous(self): client = self.plex.client(self.client_name) client.skipPrevious("music") def resume(self): client = self.plex.client(self.client_name) client.play("music") def stop(self): client = self.plex.client(self.client_name) client.stop("music")
class Remote: def __init__(self, language, plex_url, client_name): self.lang = language self.formatHelper = FormatHelper(language) self.plex = PlexServer(plex_url) self.client = self.plex.client(client_name) # last results for context awareness self.found_media = [] self.found_actions = [] self.last_request = [] self.last_picked = None # filter configuration self.filterable_in_episode = ['director', 'writer', 'year', 'decade'] self.filterable_in_show = ['actor', 'genre', 'contentRating'] self.filter_config = {'actor': self.formatHelper.filter_person, 'director': self.formatHelper.filter_person, 'writer': self.formatHelper.filter_person, 'producer': self.formatHelper.filter_person, 'year': self.formatHelper.filter_year, 'decade': self.formatHelper.filter_year, 'genre': self.formatHelper.filter_case_insensitive, 'country': self.formatHelper.filter_case_insensitive, 'contentRating': self.formatHelper.filter_case_insensitive} for section in self.plex.library.sections(): if section.TYPE == 'movie': self.movies = section elif section.TYPE == 'show': self.shows = section def execute(self, text): print(text) commands = self.lang.match(text.lower()) self.search(commands) self.filter_highest_priority() return self.execute_actions() def search(self, commands): self.found_media = [] self.found_actions = [] threads = [] for priority, matched in commands: search_actions = [action for action in ['play', 'navigate', 'follow_up'] if action in matched] direct_actions = [action for action in ['another_one', 'play_it', 'main_menu', 'subtitle_on', 'subtitle_off', 'subtitle_toggle', 'language_toggle', 'osd', 'jump_forward', 'jump_backward', 'pause', 'play_after_pause'] if action in matched] if direct_actions: self.found_media.append((priority, direct_actions, None)) else: if 'movie' in matched: function = self.search_movies elif 'tv' in matched: function = self.search_episodes else: function = self.search_general thr = threading.Thread(target=function, args=[matched, priority, search_actions]) threads.append(thr) thr.start() for thr in threads: thr.join() def search_movies(self, matched, priority, actions): title = matched.get('title') if 'unseen' in matched: movie_filter = 'unwatched' else: movie_filter = 'all' multi_filters = self.create_filter(self.movies, matched) if multi_filters: results = self.movies.search(title, filter=movie_filter, **multi_filters.pop()) for filters in multi_filters: filter_results = self.movies.search(title, filter=movie_filter, **filters) results = [result for result in results if result in filter_results] for video in self.post_filter(matched, results): self.found_media.append((priority, actions, video)) def search_episodes(self, matched, priority, actions): title = matched.get('title') season = matched.get('season') if 'unseen' in matched: watched_filter = 'unwatched' matched['oldest'] = None else: watched_filter = 'all' show_multi_filters = self.create_filter(self.shows, filter_dict(matched, self.filterable_in_show)) episode_multi_filters = self.create_filter(self.shows, filter_dict(matched, self.filterable_in_episode)) if show_multi_filters is None or episode_multi_filters is None: return episode_set = [] used_episode_filter = False if episode_multi_filters[0]: used_episode_filter = True episode_set = self.shows.searchEpisodes(None, filter=watched_filter, **episode_multi_filters.pop()) for filters in episode_multi_filters: filter_episode_set = self.shows.searchEpisodes(title, filter=watched_filter, **filters) episode_set = [result for result in episode_set if result in filter_episode_set] results = [] used_show_filter = False if show_multi_filters[0] or season or not used_episode_filter or title or watched_filter == 'unwatched': used_show_filter = True show_set = self.shows.search(title, filter=watched_filter, **show_multi_filters.pop()) for filters in show_multi_filters: filter_show_set = self.shows.search(title, filter=watched_filter, **filters) show_set = [result for result in show_set if result in filter_show_set] for show in show_set: if season: show = show.season(self.formatHelper.season_format(season)) res = show.episodes(watched='unseen' not in matched) results += self.post_filter(matched, res) if used_episode_filter: if used_show_filter: results = [result for result in results if result in episode_set] else: results = episode_set results = self.post_filter(matched, results) for video in results: self.found_media.append((priority, actions, video)) def search_general(self, matched, priority, actions): title = matched.get('title') results = self.plex.search(title) results = self.post_filter(matched, results) for video in results: self.found_media.append((priority, actions, video)) def create_filter(self, library, matched): multi_filter = [{}] for key, filter_method in self.filter_config.iteritems(): entities = matched.get(key) if entities: server_entities = getattr(library, 'get_' + key)() for index, entity in enumerate(entities.split(self.lang.and_phrase())): res = filter_method(server_entities, entity) if res: if len(multi_filter) <= index: multi_filter.append(multi_filter[0].copy()) filters = multi_filter[index] filters[key] = res else: return None return multi_filter @staticmethod def post_filter(matched, results): if 'higher_rating' in matched and results: border = float(matched['higher_rating']) results = [video for video in results if hasattr(video, 'rating') and float(video.rating) > border] if 'lower_rating' in matched and results: border = float(matched['lower_rating']) results = [video for video in results if hasattr(video, 'rating') and float(video.rating) < border] if 'newest' in matched and len(results) > 1: newest = results[0] newest_date = datetime(1, 1, 1) for video in results: if hasattr(video, 'originallyAvailableAt') and video.originallyAvailableAt > newest_date: newest_date = video.originallyAvailableAt newest = video return [newest] if 'oldest' in matched and len(results) > 1: oldest = results[0] oldest_date = datetime(9999, 1, 1) for video in results: if hasattr(video, 'originallyAvailableAt') and video.originallyAvailableAt < oldest_date: oldest_date = video.originallyAvailableAt oldest = video return [oldest] return results def filter_highest_priority(self): highest_priority = -1 for priority, actions, media in self.found_media: highest_priority = max(priority, highest_priority) filtered = [] highest_priority_actions = [] for priority, actions, media in self.found_media: if priority == highest_priority: highest_priority_actions += actions filtered.append(media) self.found_actions = highest_priority_actions self.found_media = filtered def execute_actions(self): if self.found_actions: print(self.found_actions[0]) # direct actions if 'main_menu' in self.found_actions: self.client.stop() if 'subtitle_on' in self.found_actions: self.client.subtitle('on') if 'subtitle_off' in self.found_actions: self.client.subtitle('off') if 'subtitle_toggle' in self.found_actions: self.client.subtitle('next') if 'language_toggle' in self.found_actions: self.client.switch_language() if 'osd' in self.found_actions: self.client.toggleOSD() if 'jump_forward' in self.found_actions: self.client.stepForward() if 'jump_backward' in self.found_actions: self.client.stepBack() if 'pause' in self.found_actions: self.client.pause() if 'play_after_pause' in self.found_actions: self.client.play() if 'another_one' in self.found_actions: self.last_picked = self.pick_another_one() if self.last_picked: self.client.navigate(self.last_picked) if 'play_it' in self.found_actions: if self.last_picked: self.client.playMedia(self.last_picked) # search actions if 'follow_up' in self.found_actions or 'play' in self.found_actions or 'navigate' in self.found_actions: if 'follow_up' in self.found_actions: self.found_media = [f for f in self.found_media if f in self.last_request] self.last_picked = self.pick_one() self.last_request = self.found_media if self.last_picked: if 'play' in self.found_actions: print('play ' + str(self.last_picked)) self.client.playMedia(self.last_picked) elif 'navigate' in self.found_actions: print('go to ' + str(self.last_picked)) self.client.navigate(self.last_picked) return len(self.found_actions) > 0 def pick_one(self): if len(self.found_media) == 0: return None pos = Random().randint(0, len(self.found_media) - 1) return self.found_media[pos] def pick_another_one(self): if len(self.last_request) == 0: return None if len(self.last_request) == 1: return self.last_request[0] video = None while not video or video == self.last_picked: video = self.last_request[Random().randint(0, len(self.last_request) - 1)] return video
def plexlogin(): global PLEXUN global PLEXSVR global PLEXCLIENT global plex global client try: cur.execute('SELECT setting FROM settings WHERE item LIKE \'PLEXUN\'') PLEXUN = cur.fetchone() PLEXUN = PLEXUN[0] cur.execute('SELECT setting FROM settings WHERE item LIKE \'PLEXPW\'') PLEXPW = cur.fetchone() PLEXPW = PLEXPW[0] cur.execute('SELECT setting FROM settings WHERE item LIKE \'PLEXSVR\'') PLEXSVR = cur.fetchone() PLEXSVR = PLEXSVR[0] cur.execute( 'SELECT setting FROM settings WHERE item LIKE \'PLEXCLIENT\'') PLEXCLIENT = cur.fetchone() PLEXCLIENT = PLEXCLIENT[0] try: cur.execute( 'SELECT setting FROM settings WHERE item LIKE \'PLEXSERVERTOKEN\'' ) PLEXTOKEN = cur.fetchone() PLEXTOKEN = PLEXTOKEN[0] except TypeError: print( "Error getting server Token. Local Access will fail. Try running system_setup.py to correct." ) cur.execute( 'SELECT setting FROM settings WHERE item LIKE \'PLEXSERVERIP\'') PLEXSERVERIP = cur.fetchone() PLEXSERVERIP = PLEXSERVERIP[0] cur.execute( 'SELECT setting FROM settings WHERE item LIKE \'PLEXSERVERPORT\'') PLEXSERVERPORT = cur.fetchone() PLEXSERVERPORT = PLEXSERVERPORT[0] from plexapi.myplex import MyPlexAccount try: from plexapi.server import PlexServer baseurl = 'http://' + PLEXSERVERIP + ':' + PLEXSERVERPORT token = PLEXTOKEN plex = PlexServer(baseurl, token) #print ("using local access.") except Exception: print("Local Fail. Trying cloud access.") user = MyPlexAccount.signin(PLEXUN, PLEXPW) plex = user.resource(PLEXSVR).connect() client = plex.client(PLEXCLIENT) except TypeError: print( "Error getting necessary plex api variables. Run system_setup.py.")
def playstatus(): sql = sqlite3.connect(MYDB) cur = sql.cursor() cur.execute('SELECT setting FROM settings WHERE item LIKE \'PLEXSVR\'') PLEXSVR = cur.fetchone() PLEXSVR = PLEXSVR[0] cur.execute('SELECT setting FROM settings WHERE item LIKE \'PLEXCLIENT\'') PLEXCLIENT = cur.fetchone() PLEXCLIENT = PLEXCLIENT[0] cur.execute( 'SELECT setting FROM settings WHERE item LIKE \'PLEXSERVERIP\'') PLEXSERVERIP = cur.fetchone() PLEXSERVERIP = PLEXSERVERIP[0] cur.execute( 'SELECT setting FROM settings WHERE item LIKE \'PLEXSERVERPORT\'') PLEXSERVERPORT = cur.fetchone() PLEXSERVERPORT = PLEXSERVERPORT[0] #cur.execute('SELECT setting FROM settings WHERE item LIKE \'PLEXSERVERTOKEN\'') #PLEXSERVERTOKEN = cur.fetchone() #PLEXSERVERTOKEN = PLEXSERVERTOKEN[0] from plexapi.myplex import MyPlexAccount try: from plexapi.server import PlexServer baseurl = 'http://' + PLEXSERVERIP + ':' + PLEXSERVERPORT #token = PLEXSERVERTOKEN #plex = PlexServer(baseurl, token) plex = PlexServer(baseurl) except Exception: cur.execute('SELECT setting FROM settings WHERE item LIKE \'PLEXUN\'') PLEXUN = cur.fetchone() PLEXUN = PLEXUN[0] try: cur.execute( 'SELECT setting FROM settings WHERE item LIKE \'PLEXPW\'') PLEXPW = cur.fetchone() PLEXPW = PLEXPW[0] import base64 PLEXPW = str(base64.b64decode(PLEXPW)) except Exception: print("Your Plex Password is temporarly needed to proceed:\n") PLEXPW = str(getpass.getpass("Password: "******"Local Fail. Trying cloud access.") plex = user.resource(PLEXSVR).connect() client = plex.client(PLEXCLIENT) pstatus = client.isPlayingMedia() sql.close() #print (pstatus) if pstatus is True: return ("Playing") else: return ("Stopped")