Created
August 4, 2021 04:27
-
-
Save tobiasglen/1612b2fe760a89dd6f70b4e24741b445 to your computer and use it in GitHub Desktop.
script to quickly show whats being played in Emby using a rich table
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
| import sys | |
| import requests | |
| from rich.table import Table | |
| from rich.console import Console | |
| emby_base_url = 'https://emby.example.com' | |
| emby_api_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' | |
| # use rich to print with style & color | |
| console = Console() | |
| active_emby_sessions_request = requests.get(url=f'{emby_base_url}/emby/Sessions?api_key={emby_api_key}') | |
| if not active_emby_sessions_request.ok: | |
| sys.exit(f'Request to /emby/Sessions resulted in the status code: {active_emby_sessions_request.status_code}') | |
| # Set up the table we will add each session into | |
| active_sessions_table = Table(show_header=True, header_style="bold cyan") | |
| active_sessions_table.add_column("Username", justify="left") | |
| active_sessions_table.add_column("Currently Playing", justify="left") | |
| active_sessions_table.add_column("Type", justify="left") | |
| active_sessions_table.add_column("External ID", justify="left") | |
| active_sessions_table.add_column("Play Method", justify="left") | |
| active_sessions_table.add_column("% Done", justify="left") | |
| active_sessions_table.add_column("Device", justify="left") | |
| # now load the json response & parse it next | |
| active_sessions = active_emby_sessions_request.json() | |
| current_playback_count = 0 | |
| # Loop through the json & pick out only active streams | |
| for sess in active_sessions: | |
| if "NowPlayingItem" in sess: | |
| current_playback_count += 1 | |
| # Format the title of whats being played (combine title & year) | |
| item_title = f'{sess["NowPlayingItem"]["Name"]} ({sess["NowPlayingItem"]["ProductionYear"]})' | |
| # Content Type | |
| content_type = 'tv' if sess["NowPlayingItem"]["Type"] == 'Episode' else 'movie' | |
| # Get the IMDB (if exists, if not then tmdb or tvdb) | |
| external_id = None | |
| for external_id_source in ['Imdb', 'Tmdb', 'Tvdb']: | |
| if external_id_source in sess["NowPlayingItem"]["ProviderIds"]: | |
| external_id = f'{external_id_source.lower()}:{sess["NowPlayingItem"]["ProviderIds"][external_id_source]}' | |
| break | |
| # Play method info | |
| if "TranscodingInfo" not in sess: | |
| stream_type = 'DirectPlay' | |
| else: # If the "TranscodingInfo" group does exist then we can use the "IsVideoDirect" value to identify the play method | |
| stream_type = 'DirectStream' if bool(sess["TranscodingInfo"]["IsVideoDirect"]) else 'Transcode' | |
| stream_color = 'yellow' if stream_type == 'Transcode' else 'green' | |
| # Stream progress | |
| cur_pos_ticks = sess["PlayState"]["PositionTicks"] | |
| total_ticks = sess["NowPlayingItem"]["RunTimeTicks"] | |
| percentage_complete = f'{round(cur_pos_ticks / total_ticks * 100)}%' | |
| # Client (Watch for chromecast) | |
| client = sess["Client"].replace('Chromecast', '[bold red]Chromecast[/bold red]') | |
| # Add all the sess data to a new row | |
| active_sessions_table.add_row( | |
| sess["UserName"], item_title, content_type, external_id, f'[{stream_color}]{stream_type}[/{stream_color}]', | |
| percentage_complete, client | |
| ) | |
| console.print(active_sessions_table, highlight=False) | |
| console.print(f'Number of Streams: [chartreuse1]{current_playback_count}[/chartreuse1]\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment