|
1 | 1 | from plexapi.server import PlexServer
|
2 | 2 | import requests
|
3 | 3 |
|
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/ |
5 | 5 | baseurl = 'http://localhost:32400'
|
6 | 6 | token = 'xxxxxxxx'
|
7 | 7 | plex = PlexServer(baseurl, token)
|
8 | 8 |
|
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 | + |
10 | 20 | for movie in plex.library.section('Movies').all():
|
11 | 21 | movie.reload()
|
12 |
| - # Get all English subtitle streams for the current movie |
13 | 22 | 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) |
16 | 23 | 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 |
18 | 24 | 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())]
|
19 | 25 | part = movie.media[0].parts[0]
|
20 | 26 | partsid = part.id
|
21 | 27 | 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 |
24 | 28 | url = f'{baseurl}/library/parts/{partsid}?subtitleStreamID={non_forced_english_subs[0].id}&allParts=1'
|
25 | 29 | headers = {'X-Plex-Token': token}
|
26 | 30 | 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') |
28 | 32 | 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') |
32 | 36 | 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') |
34 | 53 |
|
35 |
| -# Set all TV shows to use English non-forced subtitles if available, otherwise print no subtitles found |
36 | 54 | for show in plex.library.section('TV Shows').all():
|
37 | 55 | show.reload()
|
38 | 56 | for episode in show.episodes():
|
39 | 57 | show.reload()
|
40 | 58 | episode.reload()
|
41 |
| - # Get all English subtitle streams for the current show |
42 | 59 | 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) |
45 | 60 | 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 |
47 | 61 | 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())]
|
48 | 62 | part = episode.media[0].parts[0]
|
49 | 63 | partsid = part.id
|
50 | 64 | 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 |
53 | 65 | url = f'{baseurl}/library/parts/{partsid}?subtitleStreamID={non_forced_english_subs[0].id}&allParts=1'
|
54 | 66 | headers = {'X-Plex-Token': token}
|
55 | 67 | 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') |
57 | 69 | 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)}') |
59 | 71 | 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') |
61 | 73 | 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