Skip to content

Commit 78c5982

Browse files
authored
Merge pull request #9 from RileyXX/3-print-out-neat-colored-data-tables-when-the-script-runs
New feature added to PlexPreferNonForcedSubs.py
2 parents e294b31 + 1c0f06e commit 78c5982

File tree

1 file changed

+36
-24
lines changed

1 file changed

+36
-24
lines changed

PlexPreferNonForcedSubs.py

+36-24
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,74 @@
11
from plexapi.server import PlexServer
22
import requests
33

4-
# Connect to the Plex server
4+
# Connect to a Plex Media Server. Set your Plex token here. How to get token: https://www.plexopedia.com/plex-media-server/general/plex-token/
55
baseurl = 'http://localhost:32400'
66
token = 'xxxxxxxx'
77
plex = PlexServer(baseurl, token)
88

9-
# Set all movies to use English non-forced subtitles if available, otherwise print no subtitles found
9+
table_headers = ['Title', 'Year', 'Status', 'Changes']
10+
11+
title_width = 70
12+
year_width = 5
13+
status_width = 20
14+
changes_width = 8
15+
16+
print("\n" + "-" * 114 + "\nMovies\n" + "-" * 114)
17+
18+
print(f'\033[1m\033[96m{" | ".join([h.ljust(title_width if i == 0 else year_width if i == 1 else status_width if i == 2 else changes_width) for i, h in enumerate(table_headers)])}\033[0m')
19+
1020
for movie in plex.library.section('Movies').all():
1121
movie.reload()
12-
# Get all English subtitle streams for the current movie
1322
english_subs = [stream for stream in movie.subtitleStreams() if stream.languageCode == 'eng']
14-
# Filter out any English subtitle streams that are marked as forced
15-
# or have the word "forced" in their title (case insensitive)
1623
non_forced_english_subs = [stream for stream in english_subs if not stream.forced or (hasattr(stream, 'title') and stream.title is not None and 'forced' not in stream.title.lower())]
17-
# Get all English subtitle streams that are marked as forced
1824
forced_english_subs = [stream for stream in english_subs if stream.forced or (hasattr(stream, 'title') and stream.title is not None and 'forced' in stream.title.lower())]
1925
part = movie.media[0].parts[0]
2026
partsid = part.id
2127
if forced_english_subs and non_forced_english_subs:
22-
# If movies has forced english subs AND non forced english subs THEN set show to prefer english non forced subs
23-
# Send a request to the Plex client to set the subtitle stream
2428
url = f'{baseurl}/library/parts/{partsid}?subtitleStreamID={non_forced_english_subs[0].id}&allParts=1'
2529
headers = {'X-Plex-Token': token}
2630
requests.put(url, headers=headers)
27-
print(f'\033[92m{movie.title}: Setting non forced English subtitles.\033[0m')
31+
print(f'\033[92m{movie.title[:title_width].ljust(title_width)} | {str(movie.year).ljust(year_width)} | {"English (Non-Forced)".ljust(status_width)} | {"Y".ljust(changes_width)}\033[0m')
2832
elif non_forced_english_subs and not forced_english_subs:
29-
print(f'{movie.title}: Has English subtitles but no English forced subtitles. No subtitle changes.')
30-
elif not non_forced_english_subs and not forced_english_subs and not forced_english_subs:
31-
print(f'{movie.title}: No English subtitles found. No subtitle changes.')
33+
print(f'{movie.title[:title_width].ljust(title_width)} | {str(movie.year).ljust(year_width)} | {"English".ljust(status_width)} | {"N".ljust(changes_width)}')
34+
elif not non_forced_english_subs and not forced_english_subs:
35+
print(f'\033[91m{movie.title[:title_width].ljust(title_width)} | {str(movie.year).ljust(year_width)} | {"No Subtitles Found".ljust(status_width)} | {"N".ljust(changes_width)}\033[0m')
3236
else:
33-
print(f'{movie.title}: No subtitle changes.')
37+
print(f'\033[91m{movie.title[:title_width].ljust(title_width)} | {str(movie.year).ljust(year_width)} | {"English (Forced)".ljust(status_width)} | {"N (Error)".ljust(changes_width)}\033[0m')
38+
39+
table_headers = ['Title', 'Year', 'Season #', 'Episode #', 'Status', 'Changes']
40+
41+
title_width = 42
42+
year_width = 5
43+
season_width = 11
44+
episode_width = 11
45+
status_width = 20
46+
changes_width = 8
47+
season_row_width = 4
48+
episode_row_width = 3
49+
50+
print("\n" + "-" * 114 + "\nShows\n" + "-" * 114)
51+
52+
print(f'\033[1m\033[96m{" | ".join([h.ljust(title_width if i == 0 else year_width if i == 1 else season_width if i == 2 else episode_width if i == 3 else status_width if i == 4 else changes_width) for i, h in enumerate(table_headers)])}\033[0m')
3453

35-
# Set all TV shows to use English non-forced subtitles if available, otherwise print no subtitles found
3654
for show in plex.library.section('TV Shows').all():
3755
show.reload()
3856
for episode in show.episodes():
3957
show.reload()
4058
episode.reload()
41-
# Get all English subtitle streams for the current show
4259
english_subs = [stream for stream in episode.subtitleStreams() if stream.languageCode == 'eng']
43-
# Filter out any English subtitle streams that are marked as forced
44-
# or have the word "forced" in their title (case insensitive)
4560
non_forced_english_subs = [stream for stream in english_subs if not stream.forced or (hasattr(stream, 'title') and stream.title is not None and 'forced' not in stream.title.lower())]
46-
# Get all English subtitle streams that are marked as forced
4761
forced_english_subs = [stream for stream in english_subs if stream.forced or (hasattr(stream, 'title') and stream.title is not None and 'forced' in stream.title.lower())]
4862
part = episode.media[0].parts[0]
4963
partsid = part.id
5064
if forced_english_subs and non_forced_english_subs:
51-
# If show has forced english subs AND non forced english subs THEN set show to prefer english non forced subs
52-
# Send a request to the Plex client to set the subtitle stream
5365
url = f'{baseurl}/library/parts/{partsid}?subtitleStreamID={non_forced_english_subs[0].id}&allParts=1'
5466
headers = {'X-Plex-Token': token}
5567
requests.put(url, headers=headers)
56-
print(f'\033[92m{show.title} - {episode.title}: Setting non forced English subtitles.\033[0m')
68+
print(f'\033[92m{show.title[:title_width].ljust(title_width)} | {str(show.year).ljust(year_width)} | {"Season " + str(episode.seasonNumber).ljust(season_row_width)} | {"Episode " + str(episode.index).ljust(episode_row_width)} | {"English (Non-Forced)".ljust(status_width)} | {"Y".ljust(changes_width)}\033[0m')
5769
elif non_forced_english_subs and not forced_english_subs:
58-
print(f'{show.title} - {episode.title}: Has English subtitles but no english forced subtitles. No subtitle changes.')
70+
print(f'{show.title[:title_width].ljust(title_width)} | {str(show.year).ljust(year_width)} | {"Season " + str(episode.seasonNumber).ljust(season_row_width)} | {"Episode " + str(episode.index).ljust(episode_row_width)} | {"English".ljust(status_width)} | {"N".ljust(changes_width)}')
5971
elif not non_forced_english_subs and not forced_english_subs and not forced_english_subs:
60-
print(f'{show.title} - {episode.title}: No English subtitles found. No subtitle changes.')
72+
print(f'\033[91m{show.title[:title_width].ljust(title_width)} | {str(show.year).ljust(year_width)} | {"Season " + str(episode.seasonNumber).ljust(season_row_width)} | {"Episode " + str(episode.index).ljust(episode_row_width)} | {"No Subtitles Found".ljust(status_width)} | {"N".ljust(changes_width)}\033[0m')
6173
else:
62-
print(f'{show.title} - {episode.title}: No subtitle changes.')
74+
print(f'\033[91m{show.title[:title_width].ljust(title_width)} | {str(show.year).ljust(year_width)} | {"Season " + str(episode.seasonNumber).ljust(season_row_width)} | {"Episode " + str(episode.index).ljust(episode_row_width)} | {"English (Forced)".ljust(status_width)} | {"N (Error)".ljust(changes_width)}\033[0m')

0 commit comments

Comments
 (0)